@Override public double forecastValue(RatePaymentPeriod period, RatesProvider provider) { // notional * fxRate // fxRate is 1 if no FX conversion double notional = period.getNotional() * fxRate(period, provider); return accrualWithNotional(period, notional, provider); }
public void test_builder_oneAccrualPeriod() { RatePaymentPeriod test = RatePaymentPeriod.builder() .paymentDate(DATE_2014_10_01) .accrualPeriods(RAP2) .dayCount(ACT_365F) .currency(GBP) .notional(1000d) .compoundingMethod(CompoundingMethod.STRAIGHT) .build(); assertEquals(test.getStartDate(), DATE_2014_06_30); assertEquals(test.getEndDate(), DATE_2014_09_30); assertEquals(test.getPaymentDate(), DATE_2014_10_01); assertEquals(test.getAccrualPeriods(), ImmutableList.of(RAP2)); assertEquals(test.getCurrency(), GBP); assertEquals(test.getFxReset(), Optional.empty()); assertEquals(test.getNotional(), 1000d, 0d); assertEquals(test.getNotionalAmount(), CurrencyAmount.of(GBP, 1000d)); assertEquals(test.getCompoundingMethod(), CompoundingMethod.STRAIGHT); assertEquals(test.isCompoundingApplicable(), false); }
/** * Computes the conventional cash annuity from a swap leg. * <p> * The computation is relevant only for standard swaps with constant notional and regular payments. * The swap leg must be a fixed leg. However, this is not checked internally. * * @param fixedLeg the fixed leg of the swap * @param yield the yield * @return the cash annuity */ public double annuityCash(ResolvedSwapLeg fixedLeg, double yield) { int nbFixedPeriod = fixedLeg.getPaymentPeriods().size(); SwapPaymentPeriod paymentPeriod = fixedLeg.getPaymentPeriods().get(0); ArgChecker.isTrue(paymentPeriod instanceof RatePaymentPeriod, "payment period should be RatePaymentPeriod"); RatePaymentPeriod ratePaymentPeriod = (RatePaymentPeriod) paymentPeriod; int nbFixedPaymentYear = (int) Math.round(1d / ratePaymentPeriod.getDayCount().yearFraction(ratePaymentPeriod.getStartDate(), ratePaymentPeriod.getEndDate())); double notional = Math.abs(ratePaymentPeriod.getNotional()); double annuityCash = notional * annuityCash(nbFixedPaymentYear, nbFixedPeriod, yield); return annuityCash; }
public void test_builder_twoAccrualPeriods_compoundingDefaultedToNone_fxReset() { RatePaymentPeriod test = RatePaymentPeriod.builder() .paymentDate(DATE_2014_10_01) .accrualPeriods(RAP1, RAP2) .dayCount(ACT_365F) .currency(GBP) .fxReset(FX_RESET_USD) .notional(1000d) .compoundingMethod(CompoundingMethod.NONE) .build(); assertEquals(test.getStartDate(), DATE_2014_03_30); assertEquals(test.getEndDate(), DATE_2014_09_30); assertEquals(test.getPaymentDate(), DATE_2014_10_01); assertEquals(test.getAccrualPeriods(), ImmutableList.of(RAP1, RAP2)); assertEquals(test.getCurrency(), GBP); assertEquals(test.getFxReset(), Optional.of(FX_RESET_USD)); assertEquals(test.getNotional(), 1000d, 0d); assertEquals(test.getNotionalAmount(), CurrencyAmount.of(USD, 1000d)); assertEquals(test.isCompoundingApplicable(), false); }
private double pvbpCompoundedFlat(RatePaymentPeriod paymentPeriod, RatesProvider provider) { int nbCmp = paymentPeriod.getAccrualPeriods().size(); double[] rate = paymentPeriod.getAccrualPeriods().stream() .mapToDouble(ap -> rawRate(ap, provider)) .toArray(); double df = provider.discountFactor(paymentPeriod.getCurrency(), paymentPeriod.getPaymentDate()); double rBar = 1.0; double[] cpaAccumulatedBar = new double[nbCmp + 1]; cpaAccumulatedBar[nbCmp] = paymentPeriod.getNotional() * df * rBar; double spreadBar = 0.0d; for (int j = nbCmp - 1; j >= 0; j--) { cpaAccumulatedBar[j] = (1.0d + paymentPeriod.getAccrualPeriods().get(j).getYearFraction() * rate[j] * paymentPeriod.getAccrualPeriods().get(j).getGearing()) * cpaAccumulatedBar[j + 1]; spreadBar += paymentPeriod.getAccrualPeriods().get(j).getYearFraction() * cpaAccumulatedBar[j + 1]; } return spreadBar; }
/** * Restricted copy constructor. * @param beanToCopy the bean to copy from, not null */ private Builder(RatePaymentPeriod beanToCopy) { this.paymentDate = beanToCopy.getPaymentDate(); this.accrualPeriods = beanToCopy.getAccrualPeriods(); this.dayCount = beanToCopy.getDayCount(); this.currency = beanToCopy.getCurrency(); this.fxReset = beanToCopy.fxReset; this.notional = beanToCopy.getNotional(); this.compoundingMethod = beanToCopy.getCompoundingMethod(); }
public void test_pvbp_onePeriod() { RatesProvider mockProv = mock(RatesProvider.class); double df = 0.99d; when(mockProv.discountFactor(USD, FIXED_RATE_PAYMENT_PERIOD_PAY_USD.getPaymentDate())) .thenReturn(df); double expected = df * FIXED_RATE_PAYMENT_PERIOD_PAY_USD.getNotional() * FIXED_RATE_PAYMENT_PERIOD_PAY_USD.getAccrualPeriods().get(0).getYearFraction(); DiscountingSwapLegPricer test = DiscountingSwapLegPricer.DEFAULT; assertEquals(test.pvbp(FIXED_SWAP_LEG_PAY_USD, mockProv), expected, TOLERANCE); }
@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case -1540873516: // paymentDate return ((RatePaymentPeriod) bean).getPaymentDate(); case -92208605: // accrualPeriods return ((RatePaymentPeriod) bean).getAccrualPeriods(); case 1905311443: // dayCount return ((RatePaymentPeriod) bean).getDayCount(); case 575402001: // currency return ((RatePaymentPeriod) bean).getCurrency(); case -449555555: // fxReset return ((RatePaymentPeriod) bean).fxReset; case 1585636160: // notional return ((RatePaymentPeriod) bean).getNotional(); case -1376171496: // compoundingMethod return ((RatePaymentPeriod) bean).getCompoundingMethod(); } return super.propertyGet(bean, propertyName, quiet); }
/** * Computes the derivative of the conventional cash annuity with respect to the yield from a swap leg. * <p> * The computation is relevant only for standard swaps with constant notional and regular payments. * The swap leg must be a fixed leg. However, this is not checked internally. * * @param fixedLeg the fixed leg of the swap * @param yield the yield * @return the cash annuity */ public ValueDerivatives annuityCashDerivative(ResolvedSwapLeg fixedLeg, double yield) { int nbFixedPeriod = fixedLeg.getPaymentPeriods().size(); SwapPaymentPeriod paymentPeriod = fixedLeg.getPaymentPeriods().get(0); ArgChecker.isTrue(paymentPeriod instanceof RatePaymentPeriod, "payment period should be RatePaymentPeriod"); RatePaymentPeriod ratePaymentPeriod = (RatePaymentPeriod) paymentPeriod; int nbFixedPaymentYear = (int) Math.round(1d / ratePaymentPeriod.getDayCount().yearFraction(ratePaymentPeriod.getStartDate(), ratePaymentPeriod.getEndDate())); double notional = Math.abs(ratePaymentPeriod.getNotional()); ValueDerivatives annuityUnit = annuityCash1(nbFixedPaymentYear, nbFixedPeriod, yield); return ValueDerivatives.of(annuityUnit.getValue() * notional, annuityUnit.getDerivatives().multipliedBy(notional)); }
@Override public double pvbp(RatePaymentPeriod paymentPeriod, RatesProvider provider) { ArgChecker.isTrue(!paymentPeriod.getFxReset().isPresent(), "FX reset is not supported"); int accPeriodCount = paymentPeriod.getAccrualPeriods().size(); ArgChecker.isTrue(accPeriodCount == 1 || paymentPeriod.getCompoundingMethod().equals(CompoundingMethod.FLAT), "Only one accrued period or Flat compounding supported"); // no compounding if (accPeriodCount == 1) { RateAccrualPeriod accrualPeriod = paymentPeriod.getAccrualPeriods().get(0); double df = provider.discountFactor(paymentPeriod.getCurrency(), paymentPeriod.getPaymentDate()); return df * accrualPeriod.getYearFraction() * paymentPeriod.getNotional(); } else { // Flat compounding switch (paymentPeriod.getCompoundingMethod()) { case FLAT: return pvbpCompoundedFlat(paymentPeriod, provider); default: throw new UnsupportedOperationException("PVBP not implemented yet for non FLAT compounding"); } } }
private void assertFixedPaymentPeriod( ResolvedSwapLeg expandedPayLeg, int index, String paymentDateStr, String startDateStr, String endDateStr, double notional, double rate) { RatePaymentPeriod pp = (RatePaymentPeriod) expandedPayLeg.getPaymentPeriods().get(index); assertEquals(pp.getPaymentDate().toString(), paymentDateStr); assertEquals(Math.abs(pp.getNotional()), notional); assertEquals(pp.getAccrualPeriods().size(), 1); RateAccrualPeriod ap = pp.getAccrualPeriods().get(0); assertEquals(ap.getStartDate().toString(), startDateStr); assertEquals(ap.getEndDate().toString(), endDateStr); if (ap.getRateComputation() instanceof FixedRateComputation) { assertEquals(((FixedRateComputation) ap.getRateComputation()).getRate(), rate); } else { fail(); } }
@Test(dataProvider = "compoundingRatePaymentPeriod") public void test_forecastValueSensitivity_ibor_compounding(RatePaymentPeriod period) { RatesProvider mockProv = mock(RatesProvider.class); RateComputationFn<RateComputation> obsFunc = mock(RateComputationFn.class); when(mockProv.getValuationDate()).thenReturn(VAL_DATE); DiscountingRatePaymentPeriodPricer pricer = new DiscountingRatePaymentPeriodPricer(obsFunc); LocalDate[] dates = new LocalDate[] {CPN_DATE_1, CPN_DATE_2, CPN_DATE_3, CPN_DATE_4}; double[] rates = new double[] {RATE_1, RATE_2, RATE_3}; for (int i = 0; i < 3; ++i) { IborRateComputation rateObs = (IborRateComputation) period.getAccrualPeriods().get(i).getRateComputation(); IborRateSensitivity iborSense = IborRateSensitivity.of(rateObs.getObservation(), 1.0d); when(obsFunc.rateSensitivity(rateObs, dates[i], dates[i + 1], mockProv)).thenReturn(iborSense); when(obsFunc.rate(rateObs, dates[i], dates[i + 1], mockProv)).thenReturn(rates[i]); } PointSensitivities senseComputed = pricer.forecastValueSensitivity(period, mockProv).build(); List<IborRateSensitivity> senseExpectedList = futureFwdSensitivityFD(mockProv, period, obsFunc, EPS_FD); PointSensitivities senseExpected = PointSensitivities.of(senseExpectedList); assertTrue(senseComputed.equalWithTolerance(senseExpected, EPS_FD * period.getNotional())); }
@Override public PointSensitivityBuilder forecastValueSensitivity(RatePaymentPeriod period, RatesProvider provider) { // historic payments have zero sensi if (period.getPaymentDate().isBefore(provider.getValuationDate())) { return PointSensitivityBuilder.none(); } PointSensitivityBuilder sensiFx = fxRateSensitivity(period, provider); double accrual = accrualWithNotional(period, period.getNotional(), provider); sensiFx = sensiFx.multipliedBy(accrual); PointSensitivityBuilder sensiAccrual = PointSensitivityBuilder.none(); if (period.isCompoundingApplicable()) { sensiAccrual = accrueCompoundedSensitivity(period, provider); } else { sensiAccrual = unitNotionalSensitivityNoCompounding(period, provider); } double notional = period.getNotional() * fxRate(period, provider); sensiAccrual = sensiAccrual.multipliedBy(notional); return sensiFx.combinedWith(sensiAccrual); }
public void test_pvbp_twoPeriods() { ResolvedSwapLeg leg = ResolvedSwapLeg.builder() .type(FIXED) .payReceive(PAY) .paymentPeriods(FIXED_RATE_PAYMENT_PERIOD_PAY_USD, FIXED_RATE_PAYMENT_PERIOD_PAY_USD_2) .build(); RatesProvider mockProv = mock(RatesProvider.class); double df1 = 0.99d; when(mockProv.discountFactor(USD, FIXED_RATE_PAYMENT_PERIOD_PAY_USD.getPaymentDate())) .thenReturn(df1); double df2 = 0.98d; when(mockProv.discountFactor(USD, FIXED_RATE_PAYMENT_PERIOD_PAY_USD_2.getPaymentDate())) .thenReturn(df2); double expected = df1 * FIXED_RATE_PAYMENT_PERIOD_PAY_USD.getNotional() * FIXED_RATE_PAYMENT_PERIOD_PAY_USD.getAccrualPeriods().get(0).getYearFraction(); expected += df2 * FIXED_RATE_PAYMENT_PERIOD_PAY_USD_2.getNotional() * FIXED_RATE_PAYMENT_PERIOD_PAY_USD_2.getAccrualPeriods().get(0).getYearFraction(); DiscountingSwapLegPricer test = DiscountingSwapLegPricer.DEFAULT; assertEquals(test.pvbp(leg, mockProv), expected, TOLERANCE); }
public void test_builder_twoAccrualPeriods() { RatePaymentPeriod test = RatePaymentPeriod.builder() .paymentDate(DATE_2014_10_01) .accrualPeriods(RAP1, RAP2) .dayCount(ACT_365F) .currency(GBP) .notional(1000d) .compoundingMethod(CompoundingMethod.STRAIGHT) .build(); assertEquals(test.getStartDate(), DATE_2014_03_30); assertEquals(test.getEndDate(), DATE_2014_09_30); assertEquals(test.getPaymentDate(), DATE_2014_10_01); assertEquals(test.getAccrualPeriods(), ImmutableList.of(RAP1, RAP2)); assertEquals(test.getCurrency(), GBP); assertEquals(test.getFxReset(), Optional.empty()); assertEquals(test.getNotional(), 1000d, 0d); assertEquals(test.getCompoundingMethod(), CompoundingMethod.STRAIGHT); assertEquals(test.isCompoundingApplicable(), true); }
@Override public MultiCurrencyAmount currencyExposure(RatePaymentPeriod period, RatesProvider provider) { double df = provider.discountFactor(period.getCurrency(), period.getPaymentDate()); if (period.getFxReset().isPresent()) { FxReset fxReset = period.getFxReset().get(); LocalDate fixingDate = fxReset.getObservation().getFixingDate(); FxIndexRates rates = provider.fxIndexRates(fxReset.getObservation().getIndex()); if (!fixingDate.isAfter(provider.getValuationDate()) && rates.getFixings().get(fixingDate).isPresent()) { double fxRate = rates.rate(fxReset.getObservation(), fxReset.getReferenceCurrency()); return MultiCurrencyAmount.of(period.getCurrency(), accrualWithNotional(period, period.getNotional() * fxRate * df, provider)); } double fxRateSpotSensitivity = rates.getFxForwardRates() .rateFxSpotSensitivity(fxReset.getReferenceCurrency(), fxReset.getObservation().getMaturityDate()); return MultiCurrencyAmount.of(fxReset.getReferenceCurrency(), accrualWithNotional(period, period.getNotional() * fxRateSpotSensitivity * df, provider)); } return MultiCurrencyAmount.of(period.getCurrency(), accrualWithNotional(period, period.getNotional() * df, provider)); }
public void test_presentValueSensitivity_compoundNone_fx() { DiscountingRatePaymentPeriodPricer pricer = DiscountingRatePaymentPeriodPricer.DEFAULT; ImmutableRatesProvider provider = MULTI_GBP_USD; PointSensitivityBuilder pointSensiComputedUSD = pricer.presentValueSensitivity(PAYMENT_PERIOD_FULL_GS_FX_USD, provider); CurrencyParameterSensitivities sensiComputedUSD = provider.parameterSensitivity(pointSensiComputedUSD.build().normalized()); CurrencyParameterSensitivities sensiExpectedUSD = CAL_FD.sensitivity( provider, (p) -> CurrencyAmount.of(USD, pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_USD, (p)))); assertTrue(sensiComputedUSD.equalWithTolerance( sensiExpectedUSD, EPS_FD * PAYMENT_PERIOD_FULL_GS_FX_USD.getNotional())); PointSensitivityBuilder pointSensiComputedGBP = pricer.presentValueSensitivity(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider); CurrencyParameterSensitivities sensiComputedGBP = provider.parameterSensitivity(pointSensiComputedGBP.build().normalized()); CurrencyParameterSensitivities sensiExpectedGBP = CAL_FD.sensitivity( provider, (p) -> CurrencyAmount.of(GBP, pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_GBP, (p)))); assertTrue(sensiComputedGBP.equalWithTolerance( sensiExpectedGBP, EPS_FD * PAYMENT_PERIOD_FULL_GS_FX_GBP.getNotional())); }
public void test_forecastValueSensitivity_compoundNone_fx_dfCurve() { DiscountingRatePaymentPeriodPricer pricer = DiscountingRatePaymentPeriodPricer.DEFAULT; ImmutableRatesProvider provider = MULTI_GBP_USD_SIMPLE; PointSensitivityBuilder pointSensiComputedUSD = pricer.forecastValueSensitivity(PAYMENT_PERIOD_FULL_GS_FX_USD, provider); CurrencyParameterSensitivities sensiComputedUSD = provider.parameterSensitivity(pointSensiComputedUSD.build().normalized()); CurrencyParameterSensitivities sensiExpectedUSD = CAL_FD.sensitivity( provider, (p) -> CurrencyAmount.of(USD, pricer.forecastValue(PAYMENT_PERIOD_FULL_GS_FX_USD, (p)))); assertTrue(sensiComputedUSD.equalWithTolerance( sensiExpectedUSD, EPS_FD * PAYMENT_PERIOD_FULL_GS_FX_USD.getNotional())); PointSensitivityBuilder pointSensiComputedGBP = pricer.forecastValueSensitivity(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider); CurrencyParameterSensitivities sensiComputedGBP = provider.parameterSensitivity(pointSensiComputedGBP.build().normalized()); CurrencyParameterSensitivities sensiExpectedGBP = CAL_FD.sensitivity( provider, (p) -> CurrencyAmount.of(GBP, pricer.forecastValue(PAYMENT_PERIOD_FULL_GS_FX_GBP, (p)))); assertTrue(sensiComputedGBP.equalWithTolerance( sensiExpectedGBP, EPS_FD * PAYMENT_PERIOD_FULL_GS_FX_GBP.getNotional())); }
public void test_presentValueSensitivity_compoundNone_fx_dfCurve() { DiscountingRatePaymentPeriodPricer pricer = DiscountingRatePaymentPeriodPricer.DEFAULT; ImmutableRatesProvider provider = MULTI_GBP_USD_SIMPLE; PointSensitivityBuilder pointSensiComputedUSD = pricer.presentValueSensitivity(PAYMENT_PERIOD_FULL_GS_FX_USD, provider); CurrencyParameterSensitivities sensiComputedUSD = provider.parameterSensitivity(pointSensiComputedUSD.build().normalized()); CurrencyParameterSensitivities sensiExpectedUSD = CAL_FD.sensitivity( provider, (p) -> CurrencyAmount.of(USD, pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_USD, (p)))); assertTrue(sensiComputedUSD.equalWithTolerance( sensiExpectedUSD, EPS_FD * PAYMENT_PERIOD_FULL_GS_FX_USD.getNotional())); PointSensitivityBuilder pointSensiComputedGBP = pricer.presentValueSensitivity(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider); CurrencyParameterSensitivities sensiComputedGBP = provider.parameterSensitivity(pointSensiComputedGBP.build().normalized()); CurrencyParameterSensitivities sensiExpectedGBP = CAL_FD.sensitivity( provider, (p) -> CurrencyAmount.of(GBP, pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_GBP, (p)))); assertTrue(sensiComputedGBP.equalWithTolerance( sensiExpectedGBP, EPS_FD * PAYMENT_PERIOD_FULL_GS_FX_GBP.getNotional())); }
public void test_forecastValueSensitivity_compoundNone_fx() { DiscountingRatePaymentPeriodPricer pricer = DiscountingRatePaymentPeriodPricer.DEFAULT; ImmutableRatesProvider provider = MULTI_GBP_USD; PointSensitivityBuilder pointSensiComputedUSD = pricer.forecastValueSensitivity(PAYMENT_PERIOD_FULL_GS_FX_USD, provider); CurrencyParameterSensitivities sensiComputedUSD = provider.parameterSensitivity(pointSensiComputedUSD.build().normalized()); CurrencyParameterSensitivities sensiExpectedUSD = CAL_FD.sensitivity( provider, (p) -> CurrencyAmount.of(USD, pricer.forecastValue(PAYMENT_PERIOD_FULL_GS_FX_USD, (p)))); assertTrue(sensiComputedUSD.equalWithTolerance( sensiExpectedUSD, EPS_FD * PAYMENT_PERIOD_FULL_GS_FX_USD.getNotional())); PointSensitivityBuilder pointSensiComputedGBP = pricer.forecastValueSensitivity(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider); CurrencyParameterSensitivities sensiComputedGBP = provider.parameterSensitivity(pointSensiComputedGBP.build().normalized()); CurrencyParameterSensitivities sensiExpectedGBP = CAL_FD.sensitivity( provider, (p) -> CurrencyAmount.of(GBP, pricer.forecastValue(PAYMENT_PERIOD_FULL_GS_FX_GBP, (p)))); assertTrue(sensiComputedGBP.equalWithTolerance( sensiExpectedGBP, EPS_FD * PAYMENT_PERIOD_FULL_GS_FX_GBP.getNotional())); }