/** * Returns a new mutable builder for building instances of {@code ParameterizedDataPointShifts}. * * @param shiftType the type of shift to apply to the rates * @return a new mutable builder for building instances of {@code ParameterizedDataPointShifts} */ public static PointShiftsBuilder builder(ShiftType shiftType) { return new PointShiftsBuilder(shiftType); }
/** * 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); }
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); }
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)); } }