@Override public PointShifts build() { return new PointShifts( shiftType, shifts, nodeIndices); }
/** * Creates a new set of point shifts. * * @param shiftType the type of the shift, absolute or relative * @param shifts the shifts, with one row per scenario and one column per parameter * @param nodeIdentifiers the node identifiers corresponding to the columns in the matrix of shifts */ PointShifts( ShiftType shiftType, DoubleMatrix shifts, List<Object> nodeIdentifiers) { this(shiftType, shifts, buildNodeMap(nodeIdentifiers)); }
@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case 893345500: // shiftType return ((PointShifts) bean).getShiftType(); case -903338959: // shifts return ((PointShifts) bean).getShifts(); case -1547874491: // nodeIndices return ((PointShifts) bean).getNodeIndices(); } return super.propertyGet(bean, propertyName, quiet); }
public void test_pointShifts() { SabrParametersSwaptionVolatilities base = SabrParametersSwaptionVolatilities.of(NAME, CONV, DATE_TIME, PARAM); PointShiftsBuilder builder = PointShifts.builder(ShiftType.ABSOLUTE); for (int i = 0; i < base.getParameterCount(); ++i) { builder.addShift(0, base.getParameterMetadata(i).getIdentifier(), 0.1d * (i + 1d)); builder.addShift(1, base.getParameterMetadata(i).getIdentifier(), 10d * (i + 1d)); } PointShifts shifts = builder.build(); MarketDataBox<ParameterizedData> resBox = shifts.applyTo(MarketDataBox.ofSingleValue(base), REF_DATA); SabrParametersSwaptionVolatilities computed0 = (SabrParametersSwaptionVolatilities) resBox.getValue(0); SabrParametersSwaptionVolatilities computed1 = (SabrParametersSwaptionVolatilities) resBox.getValue(1); for (int i = 0; i < base.getParameterCount(); ++i) { assertEquals(computed0.getParameter(i), base.getParameter(i) + 0.1d * (i + 1d)); assertEquals(computed1.getParameter(i), base.getParameter(i) + 10d * (i + 1d)); } }
public void coverage() { PointShifts test = PointShifts.builder(ShiftType.RELATIVE) .addShift(0, Tenor.TENOR_1W, 0.1) .addShift(0, Tenor.TENOR_1M, 0.2) .addShift(0, Tenor.TENOR_3M, 0.3) .build(); coverImmutableBean(test); PointShifts test2 = PointShifts.builder(ShiftType.ABSOLUTE) .addShift(0, Tenor.TENOR_1M, 0.2) .addShift(0, Tenor.TENOR_3M, 0.3) .build(); coverBeanEquals(test, test2); }
@Override public MarketDataBox<ParameterizedData> applyTo( MarketDataBox<ParameterizedData> marketData, ReferenceData refData) { log.debug("Applying {} point shift to ParameterizedData '{}'", shiftType, marketData.getValue(0).toString()); return marketData.mapWithIndex( shifts.rowCount(), (prams, scenarioIndex) -> applyShifts(scenarioIndex, prams)); }
private ParameterizedData applyShifts(int scenarioIndex, ParameterizedData prams) { return prams.withPerturbation((index, value, meta) -> { double shiftAmount = shiftForNode(scenarioIndex, meta); return shiftType.applyShift(value, shiftAmount); }); }
/** * Returns an instance of {@link PointShifts} built from the data in this builder. * * @return an instance of {@link PointShifts} built from the data in this builder */ public PointShifts build() { // This finds the scenario count by finding the maximum index and adding 1. // If OptionalInt had map() it could be written more sensibly as: ...max().map(i -> i + 1).orElse(0) // but it doesn't, hence using -1 and adding 1 to it for the case of zero scenarios int scenarioCount = shifts.keySet().stream() .mapToInt(Pair::getFirst) .max() .orElse(-1) + 1; List<Object> nodeIdentifiers = shifts.keySet().stream() .map(Pair::getSecond) .distinct() // Use distinct to preserve order. Collecting to a set wouldn't preserve it .collect(toImmutableList()); DoubleMatrix shiftMatrix = DoubleMatrix.of(scenarioCount, nodeIdentifiers.size(), (r, c) -> shiftValue(r, nodeIdentifiers.get(c))); return new PointShifts(shiftType, shiftMatrix, nodeIdentifiers); }