public virtual void test_before() { LocalDate startDate = date(2018, 2, 1); LocalDate endDate = date(2018, 2, 28); OvernightAveragedDailyRateComputation cmp = OvernightAveragedDailyRateComputation.of(USD_FED_FUND, startDate, endDate, REF_DATA); ImmutableRatesProvider rates = getRatesProvider(date(2018, 1, 24)); double computedRate = FUNCTION.rate(cmp, DUMMY_ACCRUAL_START_DATE, DUMMY_ACCRUAL_END_DATE, rates); PointSensitivityBuilder sensiComputed = FUNCTION.rateSensitivity(cmp, startDate, endDate, rates); ExplainMapBuilder builder = ExplainMap.builder(); double explainRate = FUNCTION.explainRate(cmp, startDate, endDate, rates, builder); double expectedRate = 0d; PointSensitivityBuilder sensiExpected = PointSensitivityBuilder.none(); LocalDate date = startDate; while (!date.isAfter(endDate)) { OvernightIndexObservation obs = OvernightIndexObservation.of(USD_FED_FUND, date, REF_DATA); double rate = rates.overnightIndexRates(USD_FED_FUND).rate(obs); PointSensitivityBuilder rateSensi = rates.overnightIndexRates(USD_FED_FUND).ratePointSensitivity(obs); LocalDate nextDate = cmp.FixingCalendar.next(date); long days = DAYS.between(date, nextDate); expectedRate += rate * days; sensiExpected = sensiComputed.combinedWith(rateSensi.multipliedBy(days)); date = nextDate; } double nDays = 28d; expectedRate /= nDays; sensiExpected = sensiExpected.multipliedBy(1d / nDays); assertEquals(computedRate, expectedRate, TOL); assertTrue(sensiComputed.build().equalWithTolerance(sensiExpected.build(), TOL)); assertEquals(explainRate, computedRate, TOL); assertEquals(builder.build().get(ExplainKey.COMBINED_RATE).Value, expectedRate, TOL); }
public virtual void test_rate() { SimpleRatesProvider prov = new SimpleRatesProvider(); LocalDateDoubleTimeSeries timeSeries = LocalDateDoubleTimeSeries.of(FIXING_DATE, RATE); IborIndexRates mockIbor = new TestingIborIndexRates(GBP_LIBOR_3M, FIXING_DATE, LocalDateDoubleTimeSeries.empty(), timeSeries); prov.IborRates = mockIbor; ForwardIborRateComputationFn obsFn = ForwardIborRateComputationFn.DEFAULT; assertEquals(obsFn.rate(GBP_LIBOR_3M_COMP, ACCRUAL_START_DATE, ACCRUAL_END_DATE, prov), RATE); // explain ExplainMapBuilder builder = ExplainMap.builder(); assertEquals(obsFn.explainRate(GBP_LIBOR_3M_COMP, ACCRUAL_START_DATE, ACCRUAL_END_DATE, prov, builder), RATE); ExplainMap built = builder.build(); assertEquals(built.get(ExplainKey.OBSERVATIONS).Present, true); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().size(), 1); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.FIXING_DATE), FIXING_DATE); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.INDEX), GBP_LIBOR_3M); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.INDEX_VALUE), RATE); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.FROM_FIXING_SERIES), true); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.FORWARD_RATE_START_DATE), FORWARD_START_DATE); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.FORWARD_RATE_END_DATE), FORWARD_END_DATE); assertEquals(built.get(ExplainKey.COMBINED_RATE), RATE); }
//------------------------------------------------------------------------- public virtual void test_rate() { ImmutableRatesProvider prov = createProvider(RATE_END, RATE_END_INTERP); InflationEndInterpolatedRateComputation ro = InflationEndInterpolatedRateComputation.of(GB_RPIX, START_INDEX_VALUE, REF_END_MONTH, WEIGHT); ForwardInflationEndInterpolatedRateComputationFn obsFn = ForwardInflationEndInterpolatedRateComputationFn.DEFAULT; // rate double rateExpected = (WEIGHT * RATE_END + (1.0 - WEIGHT) * RATE_END_INTERP) / START_INDEX_VALUE - 1; assertEquals(obsFn.rate(ro, DUMMY_ACCRUAL_START_DATE, DUMMY_ACCRUAL_END_DATE, prov), rateExpected, EPS); // explain ExplainMapBuilder builder = ExplainMap.builder(); assertEquals(obsFn.explainRate(ro, DUMMY_ACCRUAL_START_DATE, DUMMY_ACCRUAL_END_DATE, prov, builder), rateExpected, EPS); ExplainMap built = builder.build(); assertEquals(built.get(ExplainKey.OBSERVATIONS).Present, true); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().size(), 2); ExplainMap explain0 = built.get(ExplainKey.OBSERVATIONS).get().get(0); assertEquals(explain0.get(ExplainKey.FIXING_DATE), REF_END_MONTH.atEndOfMonth()); assertEquals(explain0.get(ExplainKey.INDEX), GB_RPIX); assertEquals(explain0.get(ExplainKey.INDEX_VALUE), RATE_END); assertEquals(explain0.get(ExplainKey.WEIGHT), WEIGHT); ExplainMap explain1 = built.get(ExplainKey.OBSERVATIONS).get().get(1); assertEquals(explain1.get(ExplainKey.FIXING_DATE), REF_END_MONTH_INTERP.atEndOfMonth()); assertEquals(explain1.get(ExplainKey.INDEX), GB_RPIX); assertEquals(explain1.get(ExplainKey.INDEX_VALUE), RATE_END_INTERP); assertEquals(explain1.get(ExplainKey.WEIGHT), (1d - WEIGHT)); assertEquals(built.get(ExplainKey.COMBINED_RATE).Value.doubleValue(), rateExpected, EPS); }
//------------------------------------------------------------------------- public virtual void test_rate() { ImmutableRatesProvider prov = createProvider(RATE_START, RATE_END); InflationMonthlyRateComputation ro = InflationMonthlyRateComputation.of(GB_RPIX, REFERENCE_START_MONTH, REFERENCE_END_MONTH); ForwardInflationMonthlyRateComputationFn obsFn = ForwardInflationMonthlyRateComputationFn.DEFAULT; double rateExpected = RATE_END / RATE_START - 1.0; assertEquals(obsFn.rate(ro, DUMMY_ACCRUAL_START_DATE, DUMMY_ACCRUAL_END_DATE, prov), rateExpected, EPS); // explain ExplainMapBuilder builder = ExplainMap.builder(); assertEquals(obsFn.explainRate(ro, DUMMY_ACCRUAL_START_DATE, DUMMY_ACCRUAL_END_DATE, prov, builder), rateExpected, EPS); ExplainMap built = builder.build(); assertEquals(built.get(ExplainKey.OBSERVATIONS).Present, true); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().size(), 2); ExplainMap explain0 = built.get(ExplainKey.OBSERVATIONS).get().get(0); assertEquals(explain0.get(ExplainKey.FIXING_DATE), REFERENCE_START_MONTH.atEndOfMonth()); assertEquals(explain0.get(ExplainKey.INDEX), GB_RPIX); assertEquals(explain0.get(ExplainKey.INDEX_VALUE), RATE_START); ExplainMap explain1 = built.get(ExplainKey.OBSERVATIONS).get().get(1); assertEquals(explain1.get(ExplainKey.FIXING_DATE), REFERENCE_END_MONTH.atEndOfMonth()); assertEquals(explain1.get(ExplainKey.INDEX), GB_RPIX); assertEquals(explain1.get(ExplainKey.INDEX_VALUE), RATE_END); assertEquals(built.get(ExplainKey.COMBINED_RATE).Value.doubleValue(), rateExpected, EPS); }
//------------------------------------------------------------------------- /// <summary> /// Explains the present value of the FRA product. /// <para> /// This returns explanatory information about the calculation. /// /// </para> /// </summary> /// <param name="fra"> the FRA product for which present value should be computed </param> /// <param name="provider"> the rates provider </param> /// <returns> the explanatory information </returns> public virtual ExplainMap explainPresentValue(ResolvedFra fra, RatesProvider provider) { ExplainMapBuilder builder = ExplainMap.builder(); Currency currency = fra.Currency; builder.put(ExplainKey.ENTRY_TYPE, "FRA"); builder.put(ExplainKey.PAYMENT_DATE, fra.PaymentDate); builder.put(ExplainKey.START_DATE, fra.StartDate); builder.put(ExplainKey.END_DATE, fra.EndDate); builder.put(ExplainKey.ACCRUAL_YEAR_FRACTION, fra.YearFraction); builder.put(ExplainKey.DAYS, (int)DAYS.between(fra.StartDate, fra.EndDate)); builder.put(ExplainKey.PAYMENT_CURRENCY, currency); builder.put(ExplainKey.NOTIONAL, CurrencyAmount.of(currency, fra.Notional)); builder.put(ExplainKey.TRADE_NOTIONAL, CurrencyAmount.of(currency, fra.Notional)); if (fra.PaymentDate.isBefore(provider.ValuationDate)) { builder.put(ExplainKey.COMPLETED, true); builder.put(ExplainKey.FORECAST_VALUE, CurrencyAmount.zero(currency)); builder.put(ExplainKey.PRESENT_VALUE, CurrencyAmount.zero(currency)); } else { double rate = rateComputationFn.explainRate(fra.FloatingRate, fra.StartDate, fra.EndDate, provider, builder); builder.put(ExplainKey.FIXED_RATE, fra.FixedRate); builder.put(ExplainKey.DISCOUNT_FACTOR, provider.discountFactor(currency, fra.PaymentDate)); builder.put(ExplainKey.PAY_OFF_RATE, rate); builder.put(ExplainKey.UNIT_AMOUNT, unitAmount(fra, provider)); builder.put(ExplainKey.FORECAST_VALUE, forecastValue(fra, provider)); builder.put(ExplainKey.PRESENT_VALUE, presentValue(fra, provider)); } return(builder.build()); }
public virtual void test_explainPresentValue_inPast() { RatesProvider prov = createProvider(VAL_DATE); ExplainMapBuilder builder = ExplainMap.builder(); PRICER.explainPresentValue(PERIOD_PAST, prov, builder); ExplainMap explain = builder.build(); assertEquals(explain.get(ExplainKey.ENTRY_TYPE).get(), "KnownAmountPaymentPeriod"); assertEquals(explain.get(ExplainKey.PAYMENT_DATE).get(), PERIOD_PAST.PaymentDate); assertEquals(explain.get(ExplainKey.PAYMENT_CURRENCY).get(), PERIOD_PAST.Currency); int daysBetween = (int)DAYS.between(DATE_1, DATE_2); assertEquals(explain.get(ExplainKey.START_DATE).get(), PERIOD_PAST.StartDate); assertEquals(explain.get(ExplainKey.UNADJUSTED_START_DATE).get(), PERIOD_PAST.UnadjustedStartDate); assertEquals(explain.get(ExplainKey.END_DATE).get(), PERIOD_PAST.EndDate); assertEquals(explain.get(ExplainKey.UNADJUSTED_END_DATE).get(), PERIOD_PAST.UnadjustedEndDate); assertEquals(explain.get(ExplainKey.DAYS).Value, (int?)daysBetween); assertEquals(explain.get(ExplainKey.FORECAST_VALUE).get().Currency, PERIOD_PAST.Currency); assertEquals(explain.get(ExplainKey.FORECAST_VALUE).get().Amount, 0, TOLERANCE_PV); assertEquals(explain.get(ExplainKey.PRESENT_VALUE).get().Currency, PERIOD_PAST.Currency); assertEquals(explain.get(ExplainKey.PRESENT_VALUE).get().Amount, 0 * DISCOUNT_FACTOR, TOLERANCE_PV); }
//------------------------------------------------------------------------- /// <summary> /// Explains the present value of a CMS leg. /// <para> /// This returns explanatory information about the calculation. /// /// </para> /// </summary> /// <param name="cmsLeg"> the CMS leg </param> /// <param name="provider"> the rates provider </param> /// <param name="volatilities"> the swaption volatilities </param> /// <returns> the explanatory information </returns> public virtual ExplainMap explainPresentValue(ResolvedCmsLeg cmsLeg, RatesProvider provider, SabrSwaptionVolatilities volatilities) { ExplainMapBuilder builder = ExplainMap.builder(); builder.put(ExplainKey.ENTRY_TYPE, "CmsLeg"); builder.put(ExplainKey.PAY_RECEIVE, cmsLeg.PayReceive); builder.put(ExplainKey.PAYMENT_CURRENCY, cmsLeg.Currency); builder.put(ExplainKey.START_DATE, cmsLeg.StartDate); builder.put(ExplainKey.END_DATE, cmsLeg.EndDate); builder.put(ExplainKey.INDEX, cmsLeg.Index); foreach (CmsPeriod period in cmsLeg.CmsPeriods) { builder.addListEntry(ExplainKey.PAYMENT_PERIODS, child => cmsPeriodPricer.explainPresentValue(period, provider, volatilities, child)); } builder.put(ExplainKey.PRESENT_VALUE, presentValue(cmsLeg, provider, volatilities)); return(builder.build()); }
public virtual void test_rate() { RatesProvider mockProv = mock(typeof(RatesProvider)); LocalDateDoubleTimeSeries timeSeries = LocalDateDoubleTimeSeries.of(FIXING_DATE, RATE3TS); IborIndexRates mockRates3M = new TestingIborIndexRates(GBP_LIBOR_3M, FIXING_DATE, LocalDateDoubleTimeSeries.empty(), timeSeries); IborIndexRates mockRates6M = new TestingIborIndexRates(GBP_LIBOR_6M, FIXING_DATE, LocalDateDoubleTimeSeries.of(FIXING_DATE, RATE6), LocalDateDoubleTimeSeries.empty()); when(mockProv.iborIndexRates(GBP_LIBOR_3M)).thenReturn(mockRates3M); when(mockProv.iborIndexRates(GBP_LIBOR_6M)).thenReturn(mockRates6M); IborInterpolatedRateComputation ro = IborInterpolatedRateComputation.of(GBP_LIBOR_3M, GBP_LIBOR_6M, FIXING_DATE, REF_DATA); ForwardIborInterpolatedRateComputationFn obs = ForwardIborInterpolatedRateComputationFn.DEFAULT; LocalDate fixingEndDate3M = GBP_LIBOR_3M_OBS.MaturityDate; LocalDate fixingEndDate6M = GBP_LIBOR_6M_OBS.MaturityDate; double days3M = fixingEndDate3M.toEpochDay() - FIXING_DATE.toEpochDay(); //nb days in 3M fixing period double days6M = fixingEndDate6M.toEpochDay() - FIXING_DATE.toEpochDay(); //nb days in 6M fixing period double daysCpn = ACCRUAL_END_DATE.toEpochDay() - FIXING_DATE.toEpochDay(); double weight3M = (days6M - daysCpn) / (days6M - days3M); double weight6M = (daysCpn - days3M) / (days6M - days3M); double rateExpected = (weight3M * RATE3TS + weight6M * RATE6); double rateComputed = obs.rate(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, mockProv); assertEquals(rateComputed, rateExpected, TOLERANCE_RATE); // explain ExplainMapBuilder builder = ExplainMap.builder(); assertEquals(obs.explainRate(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, mockProv, builder), rateExpected, TOLERANCE_RATE); ExplainMap built = builder.build(); assertEquals(built.get(ExplainKey.OBSERVATIONS).Present, true); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().size(), 2); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.FIXING_DATE), FIXING_DATE); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.INDEX), GBP_LIBOR_3M); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.INDEX_VALUE), RATE3TS); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.WEIGHT), weight3M); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(0).get(ExplainKey.FROM_FIXING_SERIES), true); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(1).get(ExplainKey.FIXING_DATE), FIXING_DATE); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(1).get(ExplainKey.INDEX), GBP_LIBOR_6M); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(1).get(ExplainKey.INDEX_VALUE), RATE6); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(1).get(ExplainKey.WEIGHT), weight6M); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().get(1).get(ExplainKey.FROM_FIXING_SERIES), null); assertEquals(built.get(ExplainKey.COMBINED_RATE), rateExpected); }
public virtual void test_explainPresentValueWithZSpread_past() { ExplainMapBuilder builder = ExplainMap.builder(); PRICER.explainPresentValueWithZSpread(PERIOD_INTERP, IRP_AFTER_PAY, ICDF_AFTER_PAY, builder, Z_SPREAD, PERIODIC, PERIOD_PER_YEAR); ExplainMap explain = builder.build(); assertEquals(explain.get(ExplainKey.ENTRY_TYPE).get(), "CapitalIndexedBondPaymentPeriod"); assertEquals(explain.get(ExplainKey.PAYMENT_DATE).get(), PERIOD_INTERP.PaymentDate); assertEquals(explain.get(ExplainKey.PAYMENT_CURRENCY).get(), PERIOD_INTERP.Currency); assertEquals(explain.get(ExplainKey.START_DATE).get(), START); assertEquals(explain.get(ExplainKey.UNADJUSTED_START_DATE).get(), START_UNADJ); assertEquals(explain.get(ExplainKey.END_DATE).get(), END); assertEquals(explain.get(ExplainKey.UNADJUSTED_END_DATE).get(), END_UNADJ); assertEquals(explain.get(ExplainKey.DAYS).Value.intValue(), (int)DAYS.between(START_UNADJ, END_UNADJ)); assertEquals(explain.get(ExplainKey.FORECAST_VALUE).get().Amount, 0d, NOTIONAL * TOL); assertEquals(explain.get(ExplainKey.PRESENT_VALUE).get().Amount, 0d, NOTIONAL * TOL); }
//------------------------------------------------------------------------- public virtual void test_explainPresentValue() { ExplainMapBuilder builder = ExplainMap.builder(); PRICER.explainPresentValue(PERIOD_INTERP, IRP_BEFORE_START, ICDF_BEFORE_START, builder); ExplainMap explain = builder.build(); assertEquals(explain.get(ExplainKey.ENTRY_TYPE).get(), "CapitalIndexedBondPaymentPeriod"); assertEquals(explain.get(ExplainKey.PAYMENT_DATE).get(), PERIOD_INTERP.PaymentDate); assertEquals(explain.get(ExplainKey.PAYMENT_CURRENCY).get(), PERIOD_INTERP.Currency); assertEquals(explain.get(ExplainKey.START_DATE).get(), START); assertEquals(explain.get(ExplainKey.UNADJUSTED_START_DATE).get(), START_UNADJ); assertEquals(explain.get(ExplainKey.END_DATE).get(), END); assertEquals(explain.get(ExplainKey.UNADJUSTED_END_DATE).get(), END_UNADJ); assertEquals(explain.get(ExplainKey.DAYS).Value.intValue(), (int)DAYS.between(START_UNADJ, END_UNADJ)); assertEquals(explain.get(ExplainKey.DISCOUNT_FACTOR).Value, ICDF_BEFORE_START.discountFactor(END)); assertEquals(explain.get(ExplainKey.FORECAST_VALUE).get().Amount, PRICER.forecastValue(PERIOD_INTERP, IRP_BEFORE_START), NOTIONAL * TOL); assertEquals(explain.get(ExplainKey.PRESENT_VALUE).get().Amount, PRICER.presentValue(PERIOD_INTERP, IRP_BEFORE_START, ICDF_BEFORE_START), NOTIONAL * TOL); }
/// <summary> /// Explains the present value of the payment. /// <para> /// This returns explanatory information about the calculation. /// /// </para> /// </summary> /// <param name="payment"> the payment </param> /// <param name="provider"> the provider </param> /// <returns> the explanatory information </returns> public virtual ExplainMap explainPresentValue(Payment payment, BaseProvider provider) { Currency currency = payment.Currency; LocalDate paymentDate = payment.Date; ExplainMapBuilder builder = ExplainMap.builder(); builder.put(ExplainKey.ENTRY_TYPE, "Payment"); builder.put(ExplainKey.PAYMENT_DATE, paymentDate); builder.put(ExplainKey.PAYMENT_CURRENCY, currency); if (paymentDate.isBefore(provider.ValuationDate)) { builder.put(ExplainKey.COMPLETED, true); builder.put(ExplainKey.FORECAST_VALUE, CurrencyAmount.zero(currency)); builder.put(ExplainKey.PRESENT_VALUE, CurrencyAmount.zero(currency)); } else { builder.put(ExplainKey.DISCOUNT_FACTOR, provider.discountFactor(currency, paymentDate)); builder.put(ExplainKey.FORECAST_VALUE, forecastValue(payment, provider)); builder.put(ExplainKey.PRESENT_VALUE, presentValue(payment, provider)); } return(builder.build()); }