public virtual void test_rateSensitivity() { IborIndexRates mockIbor = mock(typeof(IborIndexRates)); SimpleRatesProvider prov = new SimpleRatesProvider(); prov.IborRates = mockIbor; IList <IborAveragedFixing> fixings = new List <IborAveragedFixing>(); double totalWeight = 0.0d; for (int i = 0; i < OBSERVATIONS.Length; i++) { IborIndexObservation obs = OBSERVATIONS[i]; IborAveragedFixing fixing = IborAveragedFixing.builder().observation(obs).weight(WEIGHTS[i]).build(); fixings.Add(fixing); totalWeight += WEIGHTS[i]; when(mockIbor.ratePointSensitivity(obs)).thenReturn(SENSITIVITIES[i]); } PointSensitivities expected = PointSensitivities.of(ImmutableList.of(IborRateSensitivity.of(OBSERVATIONS[0], WEIGHTS[0] / totalWeight), IborRateSensitivity.of(OBSERVATIONS[1], WEIGHTS[1] / totalWeight), IborRateSensitivity.of(OBSERVATIONS[2], WEIGHTS[2] / totalWeight), IborRateSensitivity.of(OBSERVATIONS[3], WEIGHTS[3] / totalWeight))); IborAveragedRateComputation ro = IborAveragedRateComputation.of(fixings); ForwardIborAveragedRateComputationFn obsFn = ForwardIborAveragedRateComputationFn.DEFAULT; PointSensitivityBuilder test = obsFn.rateSensitivity(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, prov); assertEquals(test.build(), expected); }
public virtual void test_rateSensitivity_finiteDifference() { IborIndexRates mockIbor = mock(typeof(IborIndexRates)); SimpleRatesProvider prov = new SimpleRatesProvider(); prov.IborRates = mockIbor; double eps = 1.0e-7; int nDates = OBSERVATIONS.Length; IList <IborAveragedFixing> fixings = new List <IborAveragedFixing>(); for (int i = 0; i < nDates; i++) { IborIndexObservation obs = OBSERVATIONS[i]; IborAveragedFixing fixing = IborAveragedFixing.builder().observation(obs).weight(WEIGHTS[i]).build(); fixings.Add(fixing); when(mockIbor.ratePointSensitivity(obs)).thenReturn(SENSITIVITIES[i]); } IborAveragedRateComputation ro = IborAveragedRateComputation.of(fixings); ForwardIborAveragedRateComputationFn obsFn = ForwardIborAveragedRateComputationFn.DEFAULT; PointSensitivityBuilder test = obsFn.rateSensitivity(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, prov); for (int i = 0; i < nDates; ++i) { IborIndexRates mockIborUp = mock(typeof(IborIndexRates)); SimpleRatesProvider provUp = new SimpleRatesProvider(); provUp.IborRates = mockIborUp; IborIndexRates mockIborDw = mock(typeof(IborIndexRates)); SimpleRatesProvider provDw = new SimpleRatesProvider(); provDw.IborRates = mockIborDw; for (int j = 0; j < nDates; ++j) { if (i == j) { when(mockIborUp.rate(OBSERVATIONS[j])).thenReturn(FIXING_VALUES[j] + eps); when(mockIborDw.rate(OBSERVATIONS[j])).thenReturn(FIXING_VALUES[j] - eps); } else { when(mockIborUp.rate(OBSERVATIONS[j])).thenReturn(FIXING_VALUES[j]); when(mockIborDw.rate(OBSERVATIONS[j])).thenReturn(FIXING_VALUES[j]); } } double rateUp = obsFn.rate(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, provUp); double rateDw = obsFn.rate(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, provDw); double resExpected = 0.5 * (rateUp - rateDw) / eps; assertEquals(test.build().Sensitivities.get(i).Sensitivity, resExpected, eps); } }