public void test_cashFlowEquivalentAndSensitivity_compounding() { RatePaymentPeriod iborCmp = RatePaymentPeriod.builder() .paymentDate(PAYMENT2) .accrualPeriods(IBOR1, IBOR2) .dayCount(ACT_365F) .currency(GBP) .notional(-NOTIONAL) .build(); ExpandedSwapLeg iborLegCmp = ExpandedSwapLeg.builder().type(IBOR).payReceive(PAY).paymentPeriods(iborCmp).build(); Swap swap1 = Swap.builder().legs(iborLegCmp, FIXED_LEG).build(); assertThrowsIllegalArg( () -> CashFlowEquivalentCalculator.cashFlowEquivalentAndSensitivitySwap( swap1.expand(), PROVIDER)); RatePaymentPeriod fixedCmp = RatePaymentPeriod.builder() .paymentDate(PAYMENT2) .accrualPeriods(FIXED1, FIXED2) .dayCount(ACT_365F) .currency(GBP) .notional(NOTIONAL) .build(); ExpandedSwapLeg fixedLegCmp = ExpandedSwapLeg.builder().type(FIXED).payReceive(RECEIVE).paymentPeriods(fixedCmp).build(); Swap swap2 = Swap.builder().legs(IBOR_LEG, fixedLegCmp).build(); assertThrowsIllegalArg( () -> CashFlowEquivalentCalculator.cashFlowEquivalentAndSensitivitySwap( swap2.expand(), PROVIDER)); }
public void test_cashFlowEquivalentAndSensitivity_wrongSwap() { Swap swap1 = Swap.builder().legs(IBOR_LEG, FIXED_LEG, IBOR_LEG).build(); assertThrowsIllegalArg( () -> CashFlowEquivalentCalculator.cashFlowEquivalentAndSensitivitySwap( swap1.expand(), PROVIDER)); Swap swap2 = Swap.builder().legs(FIXED_LEG, FIXED_LEG).build(); assertThrowsIllegalArg( () -> CashFlowEquivalentCalculator.cashFlowEquivalentAndSensitivitySwap( swap2.expand(), PROVIDER)); Swap swap3 = Swap.builder() .legs( FIXED_LEG, CashFlowEquivalentCalculator.cashFlowEquivalentIborLeg(IBOR_LEG, PROVIDER)) .build(); assertThrowsIllegalArg( () -> CashFlowEquivalentCalculator.cashFlowEquivalentAndSensitivitySwap( swap3.expand(), PROVIDER)); }
// ------------------------------------------------------------------------- public void test_cashFlowEquivalentAndSensitivity() { Swap swap = Swap.builder().legs(IBOR_LEG, FIXED_LEG).build(); ImmutableMap<NotionalExchange, PointSensitivityBuilder> computedFull = CashFlowEquivalentCalculator.cashFlowEquivalentAndSensitivitySwap(swap.expand(), PROVIDER); ImmutableList<NotionalExchange> keyComputedFull = computedFull.keySet().asList(); ImmutableList<PointSensitivityBuilder> valueComputedFull = computedFull.values().asList(); ImmutableMap<NotionalExchange, PointSensitivityBuilder> computedIborLeg = CashFlowEquivalentCalculator.cashFlowEquivalentAndSensitivityIborLeg( IBOR_LEG.expand(), PROVIDER); ImmutableMap<NotionalExchange, PointSensitivityBuilder> computedFixedLeg = CashFlowEquivalentCalculator.cashFlowEquivalentAndSensitivityFixedLeg( FIXED_LEG.expand(), PROVIDER); assertEquals(computedFixedLeg.keySet().asList(), keyComputedFull.subList(0, 2)); assertEquals(computedIborLeg.keySet().asList(), keyComputedFull.subList(2, 6)); assertEquals(computedFixedLeg.values().asList(), valueComputedFull.subList(0, 2)); assertEquals(computedIborLeg.values().asList(), valueComputedFull.subList(2, 6)); double eps = 1.0e-7; RatesFiniteDifferenceSensitivityCalculator calc = new RatesFiniteDifferenceSensitivityCalculator(eps); int size = keyComputedFull.size(); for (int i = 0; i < size; ++i) { final int index = i; CurveCurrencyParameterSensitivities expected = calc.sensitivity( PROVIDER, p -> ((NotionalExchange) CashFlowEquivalentCalculator.cashFlowEquivalentSwap(swap.expand(), p) .getPaymentEvents() .get(index)) .getPaymentAmount()); PointSensitivityBuilder point = computedFull.get( CashFlowEquivalentCalculator.cashFlowEquivalentSwap(swap.expand(), PROVIDER) .getPaymentEvents() .get(index)); CurveCurrencyParameterSensitivities computed = PROVIDER.curveParameterSensitivity(point.build()); assertTrue(computed.equalWithTolerance(expected, eps * NOTIONAL)); } }