public void test_withers() { PortfolioItemInfo test = PortfolioItemInfo.empty() .withId(ID) .withAttribute(AttributeType.DESCRIPTION, "A"); assertEquals(test.getId(), Optional.of(ID)); assertEquals(test.getAttributeTypes(), ImmutableSet.of(AttributeType.DESCRIPTION)); assertEquals(test.getAttribute(AttributeType.DESCRIPTION), "A"); assertEquals(test.findAttribute(AttributeType.DESCRIPTION), Optional.of("A")); assertThrows(IllegalArgumentException.class, () -> test.getAttribute(AttributeType.NAME)); }
/** * Combines this set of sensitivities with another set. * <p> * This returns a new curve sensitivities with a combined map of typed sensitivities. * Any sensitivities of the same type will be combined as though using * {@link CurrencyParameterSensitivities#mergedWith(CurrencyParameterSensitivities)}. * The identifier and attributes of this instance will take precedence. * * @param other the other parameter sensitivities * @return an instance based on this one, with the other instance added */ @SuppressWarnings({"rawtypes", "unchecked"}) public CurveSensitivities mergedWith(CurveSensitivities other) { PortfolioItemInfo combinedInfo = info; if (!info.getId().isPresent() && other.info.getId().isPresent()) { combinedInfo = combinedInfo.withId(other.info.getId().get()); } for (AttributeType attrType : other.info.getAttributeTypes()) { if (!combinedInfo.getAttributeTypes().contains(attrType)) { combinedInfo = combinedInfo.withAttribute(attrType, other.info.getAttribute(attrType)); } } return new CurveSensitivities(combinedInfo, mergedWith(other.typedSensitivities).getTypedSensitivities()); }
private PortfolioItemInfo parseInfo(CsvRow row) { PortfolioItemInfo info = PortfolioItemInfo.empty(); String scheme = row.findValue(ID_SCHEME_HEADER).orElse(DEFAULT_SCHEME); StandardId id = row.findValue(ID_HEADER).map(str -> StandardId.of(scheme, str)).orElse(null); if (id != null) { info = info.withId(id); } return resolver.parseSensitivityInfo(row, info); }
public void test_mergedWith_sens_mergeAndAdd() { CurveSensitivities base1 = sut(); CurveSensitivities base2 = sut2(); CurveSensitivities test = base1.mergedWith(base2); assertEquals(test.getInfo(), PortfolioItemInfo.empty() .withId(ID2) .withAttribute(NAME, "2") .withAttribute(DESCRIPTION, "1")); assertEquals(test.getTypedSensitivities().keySet(), ImmutableSet.of(ZERO_RATE_DELTA, ZERO_RATE_GAMMA)); assertEquals(test.getTypedSensitivities().get(ZERO_RATE_DELTA), SENSI1.multipliedBy(2)); assertEquals(test.getTypedSensitivities().get(ZERO_RATE_GAMMA), SENSI2); }
public void test_write_standard() { CurveName curve1 = CurveName.of("GBDSC"); CurveName curve2 = CurveName.of("GBFWD"); // listed in reverse order to check ordering CurveSensitivities sens = CurveSensitivities.builder(PortfolioItemInfo.empty().withAttribute(CCP_ATTR, "LCH")) .add(ZERO_RATE_GAMMA, curve2, Currency.GBP, TenorParameterMetadata.of(Tenor.TENOR_3M), 1) .add(ZERO_RATE_GAMMA, curve2, Currency.GBP, TenorParameterMetadata.of(Tenor.TENOR_6M), 2) .add(ZERO_RATE_DELTA, curve2, Currency.GBP, TenorParameterMetadata.of(Tenor.TENOR_3M), 3) .add(ZERO_RATE_DELTA, curve2, Currency.GBP, TenorParameterMetadata.of(Tenor.TENOR_6M), 5) .add(ZERO_RATE_DELTA, curve1, Currency.GBP, TenorParameterMetadata.of(Tenor.TENOR_3M), 2) .add(ZERO_RATE_DELTA, curve1, Currency.GBP, TenorParameterMetadata.of(Tenor.TENOR_6M), 4) .build(); StringBuffer buf = new StringBuffer(); WRITER_CCP.write(sens, buf); String content = buf.toString(); String expected = "" + "Reference,Sensitivity Type,Sensitivity Tenor,Currency,Value,CCP\n" + "GBDSC,ZeroRateDelta,3M,GBP,2.0,LCH\n" + "GBDSC,ZeroRateDelta,6M,GBP,4.0,LCH\n" + "GBFWD,ZeroRateDelta,3M,GBP,3.0,LCH\n" + "GBFWD,ZeroRateDelta,6M,GBP,5.0,LCH\n" + "GBFWD,ZeroRateGamma,3M,GBP,1.0,LCH\n" + "GBFWD,ZeroRateGamma,6M,GBP,2.0,LCH\n"; assertEquals(content, expected); }
/** * Obtains an empty instance. * * @return the empty sensitivities instance */ public static CurveSensitivities empty() { return new CurveSensitivities(PortfolioItemInfo.empty(), ImmutableMap.of()); }
/** * Gets the primary identifier for the portfolio item, optional. * <p> * The identifier is used to identify the portfolio item. * It will typically be an identifier in an external data system. * <p> * A portfolio item may have multiple active identifiers. Any identifier may be chosen here. * Certain uses of the identifier, such as storage in a database, require that the * identifier does not change over time, and this should be considered best practice. * * @return the identifier, optional */ public default Optional<StandardId> getId() { return getInfo().getId(); }
public void test_parse_standard_full() { CharSource source = ResourceLocator.ofClasspath("com/opengamma/strata/loader/csv/sensitivity-standard-full.csv").getCharSource(); assertEquals(LOADER_CCP.isKnownFormat(source), true); ValueWithFailures<ListMultimap<String, CurveSensitivities>> test = LOADER_CCP.parse(ImmutableList.of(source)); assertEquals(test.getFailures().size(), 0, test.getFailures().toString()); assertEquals(test.getValue().size(), 2); List<CurveSensitivities> list0 = test.getValue().get("SCHEME~TR1"); assertEquals(list0.size(), 1); CurveSensitivities csens0 = list0.get(0); assertEquals(csens0.getInfo().getAttribute(CCP_ATTR), "LCH"); assertEquals(csens0.getTypedSensitivities().size(), 1); assertSens(csens0, ZERO_RATE_DELTA, "GBCURVE", GBP, "1M, 3M, 6M", 1, 2, 3); List<CurveSensitivities> list1 = test.getValue().get("OG-Sensitivity~TR2"); assertEquals(list1.size(), 1); CurveSensitivities csens1 = list1.get(0); assertEquals(csens1.getInfo().getAttribute(CCP_ATTR), "CME"); assertEquals(csens1.getTypedSensitivities().size(), 1); assertSens(csens1, ZERO_RATE_GAMMA, "GBCURVE", GBP, "1M, 3M, 6M", 4, 5, 6); }
@Override public PortfolioItemInfo parseSensitivityInfo(CsvRow row, PortfolioItemInfo info) { return info.withAttribute(CCP_ATTR, row.getValue("CCP")); }
@Override public List<String> values( List<String> additionalHeaders, CurveSensitivities curveSens, CurrencyParameterSensitivity paramSens) { return ImmutableList.of(curveSens.getInfo().findAttribute(CCP_ATTR).orElse("")); } };
public void test_write_standard_withDate() { CurveName curve1 = CurveName.of("GBDSC"); CurveName curve2 = CurveName.of("GBFWD"); // listed in reverse order to check ordering CurveSensitivities sens = CurveSensitivities.builder(PortfolioItemInfo.empty().withAttribute(CCP_ATTR, "LCH")) .add(ZERO_RATE_GAMMA, curve2, Currency.GBP, TenorParameterMetadata.of(Tenor.TENOR_3M), 1) .add(ZERO_RATE_GAMMA, curve2, Currency.GBP, TenorParameterMetadata.of(Tenor.TENOR_6M), 2) .add(ZERO_RATE_DELTA, curve2, Currency.GBP, TenorParameterMetadata.of(Tenor.TENOR_3M), 3) .add(ZERO_RATE_DELTA, curve2, Currency.GBP, TenorParameterMetadata.of(Tenor.TENOR_6M), 5) .add(ZERO_RATE_DELTA, curve1, Currency.GBP, TenorDateParameterMetadata.of(date(2018, 6, 30), Tenor.TENOR_3M), 2) .add(ZERO_RATE_DELTA, curve1, Currency.GBP, TenorParameterMetadata.of(Tenor.TENOR_6M), 4) .build(); StringBuffer buf = new StringBuffer(); WRITER_CCP.write(sens, buf); String content = buf.toString(); String expected = "" + "Reference,Sensitivity Type,Sensitivity Tenor,Sensitivity Date,Currency,Value,CCP\n" + "GBDSC,ZeroRateDelta,3M,2018-06-30,GBP,2.0,LCH\n" + "GBDSC,ZeroRateDelta,6M,,GBP,4.0,LCH\n" + "GBFWD,ZeroRateDelta,3M,,GBP,3.0,LCH\n" + "GBFWD,ZeroRateDelta,6M,,GBP,5.0,LCH\n" + "GBFWD,ZeroRateGamma,3M,,GBP,1.0,LCH\n" + "GBFWD,ZeroRateGamma,6M,,GBP,2.0,LCH\n"; assertEquals(content, expected); }
CsvRow peekedRow = csv.peek(); PortfolioItemInfo info = parseInfo(peekedRow); String id = info.getId().map(StandardId::toString).orElse("");
public void test_parse_list_full() { CharSource source = ResourceLocator.ofClasspath("com/opengamma/strata/loader/csv/sensitivity-list-full.csv").getCharSource(); assertEquals(LOADER_CCP.isKnownFormat(source), true); ValueWithFailures<ListMultimap<String, CurveSensitivities>> test = LOADER_CCP.parse(ImmutableList.of(source)); assertEquals(test.getFailures().size(), 0, test.getFailures().toString()); assertEquals(test.getValue().size(), 2); List<CurveSensitivities> list0 = test.getValue().get("SCHEME~TR1"); assertEquals(list0.size(), 1); CurveSensitivities csens0 = list0.get(0); assertEquals(csens0.getInfo().getAttribute(CCP_ATTR), "LCH"); assertEquals(csens0.getTypedSensitivities().size(), 1); assertSens(csens0, ZERO_RATE_DELTA, "GBCURVE", GBP, "1M, 3M, 6M", 1, 2, 3); List<CurveSensitivities> list1 = test.getValue().get("OG-Sensitivity~TR2"); assertEquals(list1.size(), 1); CurveSensitivities csens1 = list1.get(0); assertEquals(csens1.getInfo().getAttribute(CCP_ATTR), "CME"); assertEquals(csens1.getTypedSensitivities().size(), 1); assertSens(csens1, ZERO_RATE_GAMMA, "GBCURVE", GBP, "1M, 3M, 6M", 4, 5, 6); }
public void test_empty() { CurveSensitivities test = CurveSensitivities.empty(); assertEquals(test.getInfo(), PortfolioItemInfo.empty()); assertEquals(test.getTypedSensitivities(), ImmutableMap.of()); }
CsvRow peekedRow = csv.peek(); PortfolioItemInfo info = parseInfo(peekedRow); String id = info.getId().map(StandardId::toString).orElse("");
public void test_parse_grid_full() { CharSource source = ResourceLocator.ofClasspath("com/opengamma/strata/loader/csv/sensitivity-grid-full.csv").getCharSource(); assertEquals(LOADER_CCP.isKnownFormat(source), true); ValueWithFailures<ListMultimap<String, CurveSensitivities>> test = LOADER_CCP.parse(ImmutableList.of(source)); assertEquals(test.getFailures().size(), 0, test.getFailures().toString()); List<CurveSensitivities> list0 = test.getValue().get("SCHEME~TR1"); assertEquals(list0.size(), 1); CurveSensitivities csens0 = list0.get(0); assertEquals(csens0.getId(), Optional.of(StandardId.of("SCHEME", "TR1"))); assertEquals(csens0.getInfo().getAttribute(CCP_ATTR), "LCH"); assertEquals(csens0.getTypedSensitivities().size(), 2); assertSens(csens0, ZERO_RATE_DELTA, "GBCURVE", GBP, "1M, 3M, 6M", 1, 2, 3); assertSens(csens0, ZERO_RATE_GAMMA, "GBCURVE", GBP, "1M, 3M, 6M", 4, 5, 6); List<CurveSensitivities> list1 = test.getValue().get("OG-Sensitivity~TR2"); assertEquals(list1.size(), 1); CurveSensitivities csens1 = list1.get(0); assertEquals(csens1.getId(), Optional.of(StandardId.of("OG-Sensitivity", "TR2"))); assertEquals(csens1.getInfo().getAttribute(CCP_ATTR), "CME"); assertEquals(csens1.getTypedSensitivities().size(), 1); assertSens(csens1, ZERO_RATE_DELTA, "GBCURVE", GBP, "1M, 3M, 6M", 7, 8, 9); }
public void test_builder_mixCurrency() { CurveName curveName = CurveName.of("WEIRD"); CurveSensitivities test = CurveSensitivities.builder(PortfolioItemInfo.empty()) .add(ZERO_RATE_DELTA, curveName, GBP, TENOR_MD_1Y, 1) .add(ZERO_RATE_DELTA, curveName, USD, TENOR_MD_1Y, 2) .build(); assertEquals(test.getInfo(), PortfolioItemInfo.empty()); assertEquals(test.getTypedSensitivities().size(), 1); CurrencyParameterSensitivities sens = test.getTypedSensitivity(ZERO_RATE_DELTA); assertEquals(sens.getSensitivities().size(), 2); CurrencyParameterSensitivity sensGbp = sens.getSensitivity(curveName, GBP); assertEquals(sensGbp.getSensitivity(), DoubleArray.of(1)); assertEquals(sensGbp.getParameterMetadata(0), TENOR_MD_1Y); CurrencyParameterSensitivity sensUsd = sens.getSensitivity(curveName, USD); assertEquals(sensUsd.getSensitivity(), DoubleArray.of(2)); assertEquals(sensUsd.getParameterMetadata(0), TENOR_MD_1Y); }
CsvRow peekedRow = csv.peek(); PortfolioItemInfo info = parseInfo(peekedRow); String id = info.getId().map(StandardId::toString).orElse("");
public void test_builder_tenors() { CurveName curveName = CurveName.of("GBP"); CurrencyParameterSensitivity sens1Y = CurrencyParameterSensitivity.of( curveName, ImmutableList.of(TENOR_MD_1Y), GBP, DoubleArray.of(3)); CurveSensitivities test = CurveSensitivities.builder(PortfolioItemInfo.empty()) .add(ZERO_RATE_DELTA, curveName, GBP, TENOR_MD_1M, 4) .add(ZERO_RATE_DELTA, curveName, GBP, TENOR_MD_1W, 1) .add(ZERO_RATE_DELTA, curveName, GBP, TENOR_MD_1Y, 2) .add(ZERO_RATE_DELTA, curveName, GBP, TENOR_MD_1W, 2) .add(ZERO_RATE_DELTA, sens1Y) .build(); assertEquals(test.getInfo(), PortfolioItemInfo.empty()); assertEquals(test.getTypedSensitivities().size(), 1); CurrencyParameterSensitivities sens = test.getTypedSensitivity(ZERO_RATE_DELTA); assertEquals(sens.getSensitivities().size(), 1); CurrencyParameterSensitivity singleSens = sens.getSensitivity(curveName, GBP); assertEquals(singleSens.getSensitivity(), DoubleArray.of(3, 4, 5)); assertEquals(singleSens.getParameterMetadata(0), TENOR_MD_1W); assertEquals(singleSens.getParameterMetadata(1), TENOR_MD_1M); assertEquals(singleSens.getParameterMetadata(2), TENOR_MD_1Y); }