@Override public double rate( InflationMonthlyRateComputation computation, LocalDate startDate, LocalDate endDate, RatesProvider provider) { PriceIndex index = computation.getIndex(); PriceIndexValues values = provider.priceIndexValues(index); double indexStart = values.value(computation.getStartObservation()); double indexEnd = values.value(computation.getEndObservation()); return indexEnd / indexStart - 1d; }
@Override public InflationMonthlyRateComputation build() { return new InflationMonthlyRateComputation( startObservation, endObservation); }
@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case -1098347926: // startObservation return ((InflationMonthlyRateComputation) bean).getStartObservation(); case 82210897: // endObservation return ((InflationMonthlyRateComputation) bean).getEndObservation(); } return super.propertyGet(bean, propertyName, quiet); }
public void test_serialization() { InflationMonthlyRateComputation test = InflationMonthlyRateComputation.of(GB_HICP, START_MONTH, END_MONTH); assertSerialization(test); }
public void test_collectIndices() { InflationMonthlyRateComputation test = InflationMonthlyRateComputation.of(GB_HICP, START_MONTH, END_MONTH); ImmutableSet.Builder<Index> builder = ImmutableSet.builder(); test.collectIndices(builder); assertEquals(builder.build(), ImmutableSet.of(GB_HICP)); }
@Override public void collectIndices(ImmutableSet.Builder<Index> builder) { builder.add(getIndex()); }
private LocalDate calculateLastFixingDate(LocalDate valuationDate, ReferenceData refData) { SwapTrade trade = template.createTrade(valuationDate, BuySell.BUY, 1, 1, refData); SwapLeg inflationLeg = trade.getProduct().getLegs(SwapLegType.INFLATION).get(0); ResolvedSwapLeg inflationLegExpanded = inflationLeg.resolve(refData); List<SwapPaymentPeriod> periods = inflationLegExpanded.getPaymentPeriods(); int nbPeriods = periods.size(); RatePaymentPeriod lastPeriod = (RatePaymentPeriod) periods.get(nbPeriods - 1); List<RateAccrualPeriod> accruals = lastPeriod.getAccrualPeriods(); int nbAccruals = accruals.size(); RateAccrualPeriod lastAccrual = accruals.get(nbAccruals - 1); if (lastAccrual.getRateComputation() instanceof InflationMonthlyRateComputation) { return ((InflationMonthlyRateComputation) lastAccrual.getRateComputation()) .getEndObservation().getFixingMonth().atEndOfMonth(); } if (lastAccrual.getRateComputation() instanceof InflationInterpolatedRateComputation) { return ((InflationInterpolatedRateComputation) lastAccrual.getRateComputation()) .getEndSecondObservation().getFixingMonth().atEndOfMonth(); } if (lastAccrual.getRateComputation() instanceof InflationEndMonthRateComputation) { return ((InflationEndMonthRateComputation) lastAccrual.getRateComputation()) .getEndObservation().getFixingMonth().atEndOfMonth(); } if (lastAccrual.getRateComputation() instanceof InflationEndInterpolatedRateComputation) { return ((InflationEndInterpolatedRateComputation) lastAccrual.getRateComputation()) .getEndSecondObservation().getFixingMonth().atEndOfMonth(); } throw new IllegalArgumentException("Rate computation type not supported for last fixing date of an inflation swap."); }
private InflationMonthlyRateComputation( PriceIndexObservation startObservation, PriceIndexObservation endObservation) { JodaBeanUtils.notNull(startObservation, "startObservation"); JodaBeanUtils.notNull(endObservation, "endObservation"); this.startObservation = startObservation; this.endObservation = endObservation; validate(); }
@Override public PointSensitivityBuilder rateSensitivity( InflationMonthlyRateComputation computation, LocalDate startDate, LocalDate endDate, RatesProvider provider) { PriceIndex index = computation.getIndex(); PriceIndexValues values = provider.priceIndexValues(index); double indexStart = values.value(computation.getStartObservation()); double indexEnd = values.value(computation.getEndObservation()); double indexStartInv = 1d / indexStart; PointSensitivityBuilder sensi1 = values.valuePointSensitivity(computation.getStartObservation()) .multipliedBy(-indexEnd * indexStartInv * indexStartInv); PointSensitivityBuilder sensi2 = values.valuePointSensitivity(computation.getEndObservation()) .multipliedBy(indexStartInv); return sensi1.combinedWith(sensi2); }
public void coverage() { InflationMonthlyRateComputation test1 = InflationMonthlyRateComputation.of(GB_HICP, START_MONTH, END_MONTH); coverImmutableBean(test1); InflationMonthlyRateComputation test2 = InflationMonthlyRateComputation.of(CH_CPI, YearMonth.of(2014, 4), YearMonth.of(2015, 4)); coverBeanEquals(test1, test2); }
/** * Creates an instance from an index, reference start month and reference end month. * * @param index the index * @param referenceStartMonth the reference start month * @param referenceEndMonth the reference end month * @return the inflation rate computation */ public static InflationMonthlyRateComputation of( PriceIndex index, YearMonth referenceStartMonth, YearMonth referenceEndMonth) { return new InflationMonthlyRateComputation( PriceIndexObservation.of(index, referenceStartMonth), PriceIndexObservation.of(index, referenceEndMonth)); }
@Override public double explainRate( InflationMonthlyRateComputation computation, LocalDate startDate, LocalDate endDate, RatesProvider provider, ExplainMapBuilder builder) { PriceIndex index = computation.getIndex(); PriceIndexValues values = provider.priceIndexValues(index); double indexStart = values.value(computation.getStartObservation()); double indexEnd = values.value(computation.getEndObservation()); builder.addListEntry(ExplainKey.OBSERVATIONS, child -> child .put(ExplainKey.ENTRY_TYPE, "InflationObservation") .put(ExplainKey.FIXING_DATE, computation.getStartObservation().getFixingMonth().atEndOfMonth()) .put(ExplainKey.INDEX, index) .put(ExplainKey.INDEX_VALUE, indexStart)); builder.addListEntry(ExplainKey.OBSERVATIONS, child -> child .put(ExplainKey.ENTRY_TYPE, "InflationObservation") .put(ExplainKey.FIXING_DATE, computation.getEndObservation().getFixingMonth().atEndOfMonth()) .put(ExplainKey.INDEX, index) .put(ExplainKey.INDEX_VALUE, indexEnd)); double rate = rate(computation, startDate, endDate, provider); builder.put(ExplainKey.COMBINED_RATE, rate); return rate; }
public void test_rate_InflationMonthlyRateComputation() { double mockRate = 223.0d; RateComputationFn<InflationMonthlyRateComputation> mockInfMon = mock(RateComputationFn.class); InflationMonthlyRateComputation ro = InflationMonthlyRateComputation.of(US_CPI_U, ACCRUAL_START_MONTH, ACCRUAL_END_MONTH); when(mockInfMon.rate(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV)) .thenReturn(mockRate); DispatchingRateComputationFn test = new DispatchingRateComputationFn( MOCK_IBOR_EMPTY, MOCK_IBOR_INT_EMPTY, MOCK_IBOR_AVE_EMPTY, MOCK_ON_CPD_EMPTY, MOCK_BRL_ON_CPD_EMPTY, MOCK_ON_AVE_EMPTY, MOCK_ON_AVE_DLY_EMPTY, mockInfMon, MOCK_INF_INT_EMPTY, MOCK_INF_BOND_MON_EMPTY, MOCK_INF_BOND_INT_EMPTY); assertEquals(test.rate(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV), mockRate, TOLERANCE_RATE); }
public 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).isPresent(), true); assertEquals(built.get(ExplainKey.OBSERVATIONS).get().size(), 2); ExplainMap explain0 = built.get(ExplainKey.OBSERVATIONS).get().get(0); assertEquals(explain0.get(ExplainKey.FIXING_DATE), Optional.of(REFERENCE_START_MONTH.atEndOfMonth())); assertEquals(explain0.get(ExplainKey.INDEX), Optional.of(GB_RPIX)); assertEquals(explain0.get(ExplainKey.INDEX_VALUE), Optional.of(RATE_START)); ExplainMap explain1 = built.get(ExplainKey.OBSERVATIONS).get().get(1); assertEquals(explain1.get(ExplainKey.FIXING_DATE), Optional.of(REFERENCE_END_MONTH.atEndOfMonth())); assertEquals(explain1.get(ExplainKey.INDEX), Optional.of(GB_RPIX)); assertEquals(explain1.get(ExplainKey.INDEX_VALUE), Optional.of(RATE_END)); assertEquals(built.get(ExplainKey.COMBINED_RATE).get().doubleValue(), rateExpected, EPS); }
private RateComputation createRateComputation(SchedulePeriod period, int scheduleIndex) { // handle where index value at start date is known LocalDate endDate = period.getEndDate(); if (firstIndexValue != null && scheduleIndex == 0) { return createRateComputation(endDate); } YearMonth referenceStartMonth = YearMonth.from(period.getStartDate().minus(lag)); YearMonth referenceEndMonth = YearMonth.from(endDate.minus(lag)); if (indexCalculationMethod.equals(PriceIndexCalculationMethod.INTERPOLATED)) { // interpolate between data from two different months double weight = 1d - (endDate.getDayOfMonth() - 1d) / endDate.lengthOfMonth(); return InflationInterpolatedRateComputation.of(index, referenceStartMonth, referenceEndMonth, weight); } else if (indexCalculationMethod.equals(PriceIndexCalculationMethod.MONTHLY)) { // no interpolation return InflationMonthlyRateComputation.of(index, referenceStartMonth, referenceEndMonth); } else { throw new IllegalArgumentException( "PriceIndexCalculationMethod " + indexCalculationMethod.toString() + " is not supported"); } }
.yearFraction(1.0) .rateComputation( InflationMonthlyRateComputation.of( GB_HICP, YearMonth.from(DATE_2015_01_05).minusMonths(3), .yearFraction(1.0) .rateComputation( InflationMonthlyRateComputation.of( GB_HICP, YearMonth.from(DATE_2015_02_05).minusMonths(3), .yearFraction(1.0) .rateComputation( InflationMonthlyRateComputation.of( GB_HICP, YearMonth.from(DATE_2015_03_07).minusMonths(3),
public void test_rateSensitivity() { ImmutableRatesProvider prov = createProvider(RATE_START, RATE_END); ImmutableRatesProvider provStartUp = createProvider(RATE_START + EPS_FD, RATE_END); ImmutableRatesProvider provStartDw = createProvider(RATE_START - EPS_FD, RATE_END); ImmutableRatesProvider provEndUp = createProvider(RATE_START, RATE_END + EPS_FD); ImmutableRatesProvider provEndDw = createProvider(RATE_START, RATE_END - EPS_FD); InflationMonthlyRateComputation ro = InflationMonthlyRateComputation.of(GB_RPIX, REFERENCE_START_MONTH, REFERENCE_END_MONTH); ForwardInflationMonthlyRateComputationFn obsFn = ForwardInflationMonthlyRateComputationFn.DEFAULT; double rateSrtUp = obsFn.rate(ro, DUMMY_ACCRUAL_START_DATE, DUMMY_ACCRUAL_END_DATE, provStartUp); double rateSrtDw = obsFn.rate(ro, DUMMY_ACCRUAL_START_DATE, DUMMY_ACCRUAL_END_DATE, provStartDw); double rateEndUp = obsFn.rate(ro, DUMMY_ACCRUAL_START_DATE, DUMMY_ACCRUAL_END_DATE, provEndUp); double rateEndDw = obsFn.rate(ro, DUMMY_ACCRUAL_START_DATE, DUMMY_ACCRUAL_END_DATE, provEndDw); PointSensitivityBuilder sensiStr = InflationRateSensitivity.of( PriceIndexObservation.of(GB_RPIX, REFERENCE_START_MONTH), 0.5 * (rateSrtUp - rateSrtDw) / EPS_FD); PointSensitivityBuilder sensiEnd = InflationRateSensitivity.of( PriceIndexObservation.of(GB_RPIX, REFERENCE_END_MONTH), 0.5 * (rateEndUp - rateEndDw) / EPS_FD); PointSensitivityBuilder sensiExpected = sensiStr.combinedWith(sensiEnd); PointSensitivityBuilder sensiComputed = obsFn.rateSensitivity(ro, DUMMY_ACCRUAL_START_DATE, DUMMY_ACCRUAL_END_DATE, prov); assertTrue(sensiComputed.build().normalized().equalWithTolerance(sensiExpected.build().normalized(), EPS_FD)); }