Example #1
0
        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);
        }
Example #2
0
        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);
            }
        }
Example #3
0
        public virtual void test_rate()
        {
            LocalDate fixingDate = OBSERVATIONS[0].FixingDate;
            LocalDateDoubleTimeSeries timeSeries = LocalDateDoubleTimeSeries.of(fixingDate, FIXING_VALUES[0]);
            LocalDateDoubleTimeSeries rates      = LocalDateDoubleTimeSeries.builder().put(OBSERVATIONS[1].FixingDate, FIXING_VALUES[1]).put(OBSERVATIONS[2].FixingDate, FIXING_VALUES[2]).put(OBSERVATIONS[3].FixingDate, FIXING_VALUES[3]).build();
            IborIndexRates            mockIbor   = new TestingIborIndexRates(GBP_LIBOR_3M, fixingDate, rates, timeSeries);
            SimpleRatesProvider       prov       = new SimpleRatesProvider(fixingDate);

            prov.IborRates = mockIbor;

            IList <IborAveragedFixing> fixings = new List <IborAveragedFixing>();
            double totalWeightedRate           = 0.0d;
            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);
                totalWeightedRate += FIXING_VALUES[i] * WEIGHTS[i];
                totalWeight       += WEIGHTS[i];
            }

            double rateExpected = totalWeightedRate / totalWeight;
            IborAveragedRateComputation          ro    = IborAveragedRateComputation.of(fixings);
            ForwardIborAveragedRateComputationFn obsFn = ForwardIborAveragedRateComputationFn.DEFAULT;
            double rateComputed = obsFn.rate(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, prov);

            assertEquals(rateComputed, rateExpected, TOLERANCE_RATE);

            // explain
            ExplainMapBuilder builder = ExplainMap.builder();

            assertEquals(obsFn.explainRate(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, prov, builder), rateExpected, TOLERANCE_RATE);

            ExplainMap built = builder.build();

            assertEquals(built.get(ExplainKey.OBSERVATIONS).Present, true);
            assertEquals(built.get(ExplainKey.OBSERVATIONS).get().size(), OBSERVATIONS.Length);
            for (int i = 0; i < 4; i++)
            {
                ExplainMap childMap = built.get(ExplainKey.OBSERVATIONS).get().get(i);
                assertEquals(childMap.get(ExplainKey.FIXING_DATE), (OBSERVATIONS[i].FixingDate));
                assertEquals(childMap.get(ExplainKey.INDEX), GBP_LIBOR_3M);
                assertEquals(childMap.get(ExplainKey.INDEX_VALUE), FIXING_VALUES[i]);
                assertEquals(childMap.get(ExplainKey.WEIGHT), WEIGHTS[i]);
                assertEquals(childMap.get(ExplainKey.FROM_FIXING_SERIES), i == 0 ? true : null);
            }
            assertEquals(built.get(ExplainKey.COMBINED_RATE), rateExpected);
        }