@Override public <T> Optional<T> findValue(MarketDataId<T> id) { Optional<MarketDataBox<T>> optionalBox = marketData.findValue(id); return optionalBox.map(box -> box.getValue(scenarioIndex)); }
@Override public LocalDate getValuationDate() { return marketData.getValuationDate().getValue(scenarioIndex); }
@Override public <T> T getValue(MarketDataId<T> id) { return marketData.getValue(id).getValue(scenarioIndex); }
private <U, R> MarketDataBox<R> combineWithMultiple(MarketDataBox<U> other, BiFunction<T, U, R> fn) { ScenarioArray<U> otherValue = other.getScenarioValue(); int scenarioCount = otherValue.getScenarioCount(); List<R> values = IntStream.range(0, scenarioCount) .mapToObj(i -> fn.apply(value, other.getValue(i))) .collect(toImmutableList()); return MarketDataBox.ofScenarioValues(values); }
@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)); }
@Override public QuoteScenarioArray createScenarioValue(MarketDataBox<Double> marketDataBox, int scenarioCount) { return QuoteScenarioArray.of(DoubleArray.of(scenarioCount, i -> marketDataBox.getValue(i))); }
public void getValue() { MarketDataBox<Integer> box = MarketDataBox.ofScenarioValues(27, 28, 29); assertThat(box.getValue(0)).isEqualTo(27); assertThat(box.getValue(1)).isEqualTo(28); assertThat(box.getValue(2)).isEqualTo(29); assertThrows(() -> box.getValue(-1), IllegalArgumentException.class, "Expected 0 <= 'scenarioIndex' < 3, but found -1"); assertThrows(() -> box.getValue(3), IllegalArgumentException.class, "Expected 0 <= 'scenarioIndex' < 3, but found 3"); }
/** * Test that the box always returns the same value for any non-negative scenario index. */ public void getValue() { MarketDataBox<Integer> box = MarketDataBox.ofSingleValue(27); assertThat(box.getValue(0)).isEqualTo(27); assertThat(box.getValue(Integer.MAX_VALUE)).isEqualTo(27); assertThrows(() -> box.getValue(-1), IllegalArgumentException.class); }
/** * Tests that applying a function multiple times to the value creates a box of scenario values. */ public void mapWithIndex() { MarketDataBox<Integer> box = MarketDataBox.ofScenarioValues(27, 28, 29); MarketDataBox<Integer> scenarioBox = box.mapWithIndex(3, (v, idx) -> v + idx); assertThat(scenarioBox.isScenarioValue()).isTrue(); assertThat(scenarioBox.getScenarioCount()).isEqualTo(3); assertThat(scenarioBox.getValue(0)).isEqualTo(27); assertThat(scenarioBox.getValue(1)).isEqualTo(29); assertThat(scenarioBox.getValue(2)).isEqualTo(31); }
/** * Tests that applying a function multiple times to the value creates a box of scenario values. */ public void mapWithIndex() { MarketDataBox<Integer> box = MarketDataBox.ofSingleValue(27); MarketDataBox<Integer> scenarioBox = box.mapWithIndex(3, (v, idx) -> v + idx); assertThat(scenarioBox.isScenarioValue()).isTrue(); assertThat(scenarioBox.getScenarioCount()).isEqualTo(3); assertThat(scenarioBox.getValue(0)).isEqualTo(27); assertThat(scenarioBox.getValue(1)).isEqualTo(28); assertThat(scenarioBox.getValue(2)).isEqualTo(29); }
public void combineWithSingleBox() { MarketDataBox<Integer> box = MarketDataBox.ofScenarioValues(27, 28, 29); MarketDataBox<Integer> otherBox = MarketDataBox.ofSingleValue(15); MarketDataBox<Integer> resultBox = box.combineWith(otherBox, (v1, v2) -> v1 + v2); assertThat(resultBox.isScenarioValue()).isTrue(); assertThat(resultBox.getScenarioCount()).isEqualTo(3); assertThat(resultBox.getValue(0)).isEqualTo(42); assertThat(resultBox.getValue(1)).isEqualTo(43); assertThat(resultBox.getValue(2)).isEqualTo(44); }
public void combineWithScenarioBox() { MarketDataBox<Integer> box = MarketDataBox.ofSingleValue(27); MarketDataBox<Integer> otherBox = MarketDataBox.ofScenarioValues(15, 16, 17); MarketDataBox<Integer> resultBox = box.combineWith(otherBox, (v1, v2) -> v1 + v2); assertThat(resultBox.isScenarioValue()).isTrue(); assertThat(resultBox.getScenarioCount()).isEqualTo(3); assertThat(resultBox.getValue(0)).isEqualTo(42); assertThat(resultBox.getValue(1)).isEqualTo(43); assertThat(resultBox.getValue(2)).isEqualTo(44); }
public void combineWithScenarioBox() { MarketDataBox<Integer> box = MarketDataBox.ofScenarioValues(27, 28, 29); MarketDataBox<Integer> otherBox = MarketDataBox.ofScenarioValues(15, 16, 17); MarketDataBox<Integer> resultBox = box.combineWith(otherBox, (v1, v2) -> v1 + v2); assertThat(resultBox.isScenarioValue()).isTrue(); assertThat(resultBox.getScenarioCount()).isEqualTo(3); assertThat(resultBox.getValue(0)).isEqualTo(42); assertThat(resultBox.getValue(1)).isEqualTo(44); assertThat(resultBox.getValue(2)).isEqualTo(46); }
static void checkType(MarketDataId<?> id, MarketDataBox<?> box, int scenarioCount) { if (box == null) { throw new IllegalArgumentException(Messages.format( "Value for identifier '{}' must not be null", id)); } if (box.isScenarioValue() && box.getScenarioCount() != scenarioCount) { throw new IllegalArgumentException(Messages.format( "Value for identifier '{}' should have had {} scenarios but had {}", id, scenarioCount, box.getScenarioCount())); } if (box.getScenarioCount() > 0 && !id.getMarketDataType().isInstance(box.getValue(0))) { throw new ClassCastException(Messages.format( "Value for identifier '{}' does not implement expected type '{}': '{}'", id, id.getMarketDataType().getSimpleName(), box)); } }
public void test_scenarios() { Map<MarketDataId<?>, MarketDataBox<?>> dataMap = ImmutableMap.of(ID1, BOX1); Map<ObservableId, LocalDateDoubleTimeSeries> tsMap = ImmutableMap.of(ID1, TIME_SERIES); ScenarioMarketData test = ScenarioMarketData.of(2, VAL_DATE, dataMap, tsMap); MarketData scenario0 = test.scenario(0); MarketData scenario1 = test.scenario(1); assertThat(scenario0.getValue(ID1)).isEqualTo(BOX1.getValue(0)); assertThat(scenario1.getValue(ID1)).isEqualTo(BOX1.getValue(1)); List<Double> list = test.scenarios().map(s -> s.getValue(ID1)).collect(toImmutableList()); assertThat(list.get(0)).isEqualTo(BOX1.getValue(0)); assertThat(list.get(1)).isEqualTo(BOX1.getValue(1)); }
public void combineWithSingleBox() { MarketDataBox<Integer> box = MarketDataBox.ofSingleValue(27); MarketDataBox<Integer> otherBox = MarketDataBox.ofSingleValue(15); MarketDataBox<Integer> resultBox = box.combineWith(otherBox, (v1, v2) -> v1 + v2); assertThat(resultBox.isSingleValue()).isTrue(); assertThat(resultBox.getValue(0)).isEqualTo(42); }
@Override public TestDoubleArray createScenarioValue(MarketDataBox<Double> marketDataBox, int scenarioCount) { return new TestDoubleArray(DoubleArray.of(scenarioCount, i -> marketDataBox.getValue(i))); } }
@Override public MarketDataBox<FxRate> applyTo(MarketDataBox<FxRate> marketData, ReferenceData refData) { log.debug("Applying {} shift to FX rate '{}'", shiftType, marketData.getValue(0).getPair().toString()); return marketData.mapWithIndex( getScenarioCount(), (fxRate, scenarioIndex) -> FxRate.of( currencyPair, shiftType.applyShift(fxRate.fxRate(currencyPair), shiftAmount.get(scenarioIndex)))); }
public void test_absolute() { CurveParallelShifts test = CurveParallelShifts.absolute(1d, 2d, 4d); Curve baseCurve = InterpolatedNodalCurve.of( Curves.zeroRates("curve", DayCounts.ACT_365F), DoubleArray.of(1, 2, 3), DoubleArray.of(5, 6, 7), CurveInterpolators.LOG_LINEAR); MarketDataBox<Curve> shiftedCurveBox = test.applyTo(MarketDataBox.ofSingleValue(baseCurve), REF_DATA); assertEquals(shiftedCurveBox.getValue(0), ParallelShiftedCurve.absolute(baseCurve, 1d)); assertEquals(shiftedCurveBox.getValue(1), ParallelShiftedCurve.absolute(baseCurve, 2d)); assertEquals(shiftedCurveBox.getValue(2), ParallelShiftedCurve.absolute(baseCurve, 4d)); }
public void test_relative() { CurveParallelShifts test = CurveParallelShifts.relative(0.1d, 0.2d, 0.4d); Curve baseCurve = InterpolatedNodalCurve.of( Curves.zeroRates("curve", DayCounts.ACT_365F), DoubleArray.of(1, 2, 3), DoubleArray.of(5, 6, 7), CurveInterpolators.LOG_LINEAR); MarketDataBox<Curve> shiftedCurveBox = test.applyTo(MarketDataBox.ofSingleValue(baseCurve), REF_DATA); assertEquals(shiftedCurveBox.getValue(0), ParallelShiftedCurve.relative(baseCurve, 0.1d)); assertEquals(shiftedCurveBox.getValue(1), ParallelShiftedCurve.relative(baseCurve, 0.2d)); assertEquals(shiftedCurveBox.getValue(2), ParallelShiftedCurve.relative(baseCurve, 0.4d)); }