예제 #1
0
        public virtual void test_explainPresentValue_paymentDateInPast()
        {
            SimpleRatesProvider prov = createProvider(FX_RESET_NOTIONAL_EXCHANGE_REC_USD);

            prov.ValuationDate = VAL_DATE.plusYears(1);

            DiscountingFxResetNotionalExchangePricer test = new DiscountingFxResetNotionalExchangePricer();
            ExplainMapBuilder builder = ExplainMap.builder();

            test.explainPresentValue(FX_RESET_NOTIONAL_EXCHANGE_REC_USD, prov, builder);
            ExplainMap explain = builder.build();

            Currency paymentCurrency  = FX_RESET_NOTIONAL_EXCHANGE_REC_USD.Currency;
            Currency notionalCurrency = FX_RESET_NOTIONAL_EXCHANGE_REC_USD.ReferenceCurrency;
            double   notional         = FX_RESET_NOTIONAL_EXCHANGE_REC_USD.Notional;

            assertEquals(explain.get(ExplainKey.ENTRY_TYPE).get(), "FxResetNotionalExchange");
            assertEquals(explain.get(ExplainKey.PAYMENT_DATE).get(), FX_RESET_NOTIONAL_EXCHANGE_REC_USD.PaymentDate);
            assertEquals(explain.get(ExplainKey.PAYMENT_CURRENCY).get(), paymentCurrency);
            assertEquals(explain.get(ExplainKey.TRADE_NOTIONAL).get().Currency, notionalCurrency);
            assertEquals(explain.get(ExplainKey.TRADE_NOTIONAL).get().Amount, notional, TOLERANCE);
            assertEquals(explain.get(ExplainKey.FORECAST_VALUE).get().Currency, paymentCurrency);
            assertEquals(explain.get(ExplainKey.FORECAST_VALUE).get().Amount, 0d, TOLERANCE);
            assertEquals(explain.get(ExplainKey.PRESENT_VALUE).get().Currency, paymentCurrency);
            assertEquals(explain.get(ExplainKey.PRESENT_VALUE).get().Amount, 0d * DISCOUNT_FACTOR, TOLERANCE);
        }
예제 #2
0
        /// <summary>
        /// Explain present value for a swap leg.
        /// </summary>
        /// <param name="leg">  the swap log </param>
        /// <param name="provider">  the rates provider </param>
        /// <returns> the explain PV map </returns>
        public virtual ExplainMap explainPresentValue(ResolvedSwapLeg leg, RatesProvider provider)
        {
            ExplainMapBuilder builder = ExplainMap.builder();

            explainPresentValueInternal(leg, provider, builder);
            return(builder.build());
        }
예제 #3
0
        public virtual void test_explainPresentValue_paymentDateInPast()
        {
            SimpleRatesProvider prov = createProvider(NOTIONAL_EXCHANGE_REC_GBP);

            prov.ValuationDate = VAL_DATE.plusYears(1);

            DiscountingNotionalExchangePricer test = DiscountingNotionalExchangePricer.DEFAULT;
            ExplainMapBuilder builder = ExplainMap.builder();

            test.explainPresentValue(NOTIONAL_EXCHANGE_REC_GBP, prov, builder);
            ExplainMap explain = builder.build();

            Currency       currency = NOTIONAL_EXCHANGE_REC_GBP.Currency;
            CurrencyAmount notional = NOTIONAL_EXCHANGE_REC_GBP.PaymentAmount;

            assertEquals(explain.get(ExplainKey.ENTRY_TYPE).get(), "NotionalExchange");
            assertEquals(explain.get(ExplainKey.PAYMENT_DATE).get(), NOTIONAL_EXCHANGE_REC_GBP.PaymentDate);
            assertEquals(explain.get(ExplainKey.PAYMENT_CURRENCY).get(), currency);
            assertEquals(explain.get(ExplainKey.TRADE_NOTIONAL).get().Currency, currency);
            assertEquals(explain.get(ExplainKey.TRADE_NOTIONAL).get().Amount, notional.Amount, TOLERANCE);
            assertEquals(explain.get(ExplainKey.FORECAST_VALUE).get().Currency, currency);
            assertEquals(explain.get(ExplainKey.FORECAST_VALUE).get().Amount, 0d, TOLERANCE);
            assertEquals(explain.get(ExplainKey.PRESENT_VALUE).get().Currency, currency);
            assertEquals(explain.get(ExplainKey.PRESENT_VALUE).get().Amount, 0d * DISCOUNT_FACTOR, TOLERANCE);
        }
예제 #4
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// Explains the present value of the swap product.
        /// <para>
        /// This returns explanatory information about the calculation.
        ///
        /// </para>
        /// </summary>
        /// <param name="swap">  the product </param>
        /// <param name="provider">  the rates provider </param>
        /// <returns> the explanatory information </returns>
        public virtual ExplainMap explainPresentValue(ResolvedSwap swap, RatesProvider provider)
        {
            ExplainMapBuilder builder = ExplainMap.builder();

            builder.put(ExplainKey.ENTRY_TYPE, "Swap");
            foreach (ResolvedSwapLeg leg in swap.Legs)
            {
                builder.addListEntryWithIndex(ExplainKey.LEGS, child => legPricer.explainPresentValueInternal(leg, provider, child));
            }
            return(builder.build());
        }
예제 #5
0
        //-------------------------------------------------------------------------
        public virtual void test_explainRate_FixedRateComputation()
        {
            FixedRateComputation         ro      = FixedRateComputation.of(0.0123d);
            DispatchingRateComputationFn test    = DispatchingRateComputationFn.DEFAULT;
            ExplainMapBuilder            builder = ExplainMap.builder();

            assertEquals(test.explainRate(ro, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV, builder), 0.0123d, 0d);
            ExplainMap built = builder.build();

            assertEquals(built.get(ExplainKey.FIXED_RATE), 0.0123d);
            assertEquals(built.get(ExplainKey.COMBINED_RATE), 0.0123d);
        }
예제 #6
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);
        }
예제 #7
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// Explains the present value of the CMS product.
        /// <para>
        /// This returns explanatory information about the calculation.
        ///
        /// </para>
        /// </summary>
        /// <param name="cms">  the CMS product </param>
        /// <param name="ratesProvider">  the rates provider </param>
        /// <param name="swaptionVolatilities">  the swaption volatilities </param>
        /// <returns> the explain PV map </returns>
        public virtual ExplainMap explainPresentValue(ResolvedCms cms, RatesProvider ratesProvider, SabrSwaptionVolatilities swaptionVolatilities)
        {
            ExplainMapBuilder builder = ExplainMap.builder();

            builder.put(ExplainKey.ENTRY_TYPE, "CmsSwap");
            IList <ExplainMap> legsExplain = new List <ExplainMap>();

            legsExplain.Add(cmsLegPricer.explainPresentValue(cms.CmsLeg, ratesProvider, swaptionVolatilities));
            if (cms.PayLeg.Present)
            {
                legsExplain.Add(payLegPricer.explainPresentValue(cms.PayLeg.get(), ratesProvider));
            }
            builder.put(ExplainKey.LEGS, legsExplain);
            return(builder.build());
        }
예제 #8
0
        //-------------------------------------------------------------------------
        public virtual void coverage()
        {
            DispatchingRateComputationFn            test               = new DispatchingRateComputationFn(MOCK_IBOR_EMPTY, MOCK_IBOR_INT_EMPTY, MOCK_IBOR_AVE_EMPTY, MOCK_ON_CPD_EMPTY, MOCK_ON_AVE_EMPTY, MOCK_ON_AVE_DLY_EMPTY, MOCK_INF_MON_EMPTY, MOCK_INF_INT_EMPTY, MOCK_INF_BOND_MON_EMPTY, MOCK_INF_BOND_INT_EMPTY);
            FixedRateComputation                    @fixed             = FixedRateComputation.of(0.0123d);
            IborRateComputation                     ibor               = IborRateComputation.of(GBP_LIBOR_3M, FIXING_DATE, REF_DATA);
            IborInterpolatedRateComputation         iborInt            = IborInterpolatedRateComputation.of(GBP_LIBOR_3M, GBP_LIBOR_6M, FIXING_DATE, REF_DATA);
            IborAveragedRateComputation             iborAvg            = IborAveragedRateComputation.of(ImmutableList.of(IborAveragedFixing.of(ibor.Observation)));
            OvernightCompoundedRateComputation      onCpd              = OvernightCompoundedRateComputation.of(USD_FED_FUND, ACCRUAL_START_DATE, ACCRUAL_END_DATE, 0, REF_DATA);
            OvernightAveragedRateComputation        onAvg              = OvernightAveragedRateComputation.of(USD_FED_FUND, ACCRUAL_START_DATE, ACCRUAL_END_DATE, 0, REF_DATA);
            OvernightAveragedDailyRateComputation   onAvgDly           = OvernightAveragedDailyRateComputation.of(USD_FED_FUND, ACCRUAL_START_DATE, ACCRUAL_END_DATE, REF_DATA);
            InflationMonthlyRateComputation         inflationMonthly   = InflationMonthlyRateComputation.of(US_CPI_U, ACCRUAL_START_MONTH, ACCRUAL_END_MONTH);
            InflationInterpolatedRateComputation    inflationInterp    = InflationInterpolatedRateComputation.of(US_CPI_U, ACCRUAL_START_MONTH, ACCRUAL_END_MONTH, 0.3);
            InflationEndMonthRateComputation        inflationEndMonth  = InflationEndMonthRateComputation.of(US_CPI_U, 234d, ACCRUAL_END_MONTH);
            InflationEndInterpolatedRateComputation inflationEndInterp = InflationEndInterpolatedRateComputation.of(US_CPI_U, 1234d, ACCRUAL_END_MONTH, 0.3);

            RateComputation mock = mock(typeof(RateComputation));

            ignoreThrows(() => test.rateSensitivity(@fixed, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV));
            ignoreThrows(() => test.rateSensitivity(ibor, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV));
            ignoreThrows(() => test.rateSensitivity(iborInt, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV));
            ignoreThrows(() => test.rateSensitivity(iborAvg, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV));
            ignoreThrows(() => test.rateSensitivity(onCpd, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV));
            ignoreThrows(() => test.rateSensitivity(onAvg, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV));
            ignoreThrows(() => test.rateSensitivity(onAvgDly, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV));
            ignoreThrows(() => test.rateSensitivity(inflationMonthly, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV));
            ignoreThrows(() => test.rateSensitivity(inflationInterp, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV));
            ignoreThrows(() => test.rateSensitivity(inflationEndMonth, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV));
            ignoreThrows(() => test.rateSensitivity(inflationEndInterp, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV));
            ignoreThrows(() => test.rateSensitivity(mock, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV));

            ExplainMapBuilder explain = ExplainMap.builder();

            ignoreThrows(() => test.explainRate(@fixed, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV, explain));
            ignoreThrows(() => test.explainRate(ibor, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV, explain));
            ignoreThrows(() => test.explainRate(iborInt, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV, explain));
            ignoreThrows(() => test.explainRate(iborAvg, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV, explain));
            ignoreThrows(() => test.explainRate(onCpd, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV, explain));
            ignoreThrows(() => test.explainRate(onAvg, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV, explain));
            ignoreThrows(() => test.explainRate(onAvgDly, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV, explain));
            ignoreThrows(() => test.explainRate(inflationMonthly, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV, explain));
            ignoreThrows(() => test.explainRate(inflationInterp, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV, explain));
            ignoreThrows(() => test.explainRate(inflationEndMonth, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV, explain));
            ignoreThrows(() => test.explainRate(inflationEndInterp, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV, explain));
            ignoreThrows(() => test.explainRate(mock, ACCRUAL_START_DATE, ACCRUAL_END_DATE, MOCK_PROV, explain));
        }
예제 #9
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// Test for the case where publication lag=1, effective offset=0 (USD conventions) and no cutoff period. </summary>
        public virtual void rateFedFundNoCutOff()
        {
            OvernightIndexRates mockRates = mock(typeof(OvernightIndexRates));

            when(mockRates.Index).thenReturn(USD_FED_FUND);
            SimpleRatesProvider simpleProv = new SimpleRatesProvider(mockRates);

            for (int i = 0; i < USD_OBS.Length; i++)
            {
                when(mockRates.rate(USD_OBS[i])).thenReturn(FIXING_RATES[i]);
            }
            OvernightAveragedRateComputation ro = OvernightAveragedRateComputation.of(USD_FED_FUND, START_DATE, END_DATE, 0, REF_DATA);
            // Accrual dates = fixing dates
            ForwardOvernightAveragedRateComputationFn obsFn = ForwardOvernightAveragedRateComputationFn.DEFAULT;
            double accrualFactorTotal = 0.0d;
            double accruedRate        = 0.0d;
            int    indexLast          = 5; // Fixing in the observation period are from 1 to 5 (inclusive)

            for (int i = 1; i <= indexLast; i++)
            {
                LocalDate endDate = USD_OBS[i].MaturityDate;
                double    af      = USD_FED_FUND.DayCount.yearFraction(FIXING_DATES[i], endDate);
                accrualFactorTotal += af;
                accruedRate        += FIXING_RATES[i] * af;
            }
            double rateExpected = accruedRate / accrualFactorTotal;
            double rateComputed = obsFn.rate(ro, DUMMY_ACCRUAL_START_DATE, DUMMY_ACCRUAL_END_DATE, simpleProv);

            assertEquals(rateExpected, rateComputed, TOLERANCE_RATE);

            // explain
            ExplainMapBuilder builder       = ExplainMap.builder();
            double            explainedRate = obsFn.explainRate(ro, DUMMY_ACCRUAL_START_DATE, DUMMY_ACCRUAL_END_DATE, simpleProv, builder);

            assertEquals(explainedRate, rateExpected, TOLERANCE_RATE);

            ExplainMap built = builder.build();

            assertEquals(built.get(ExplainKey.OBSERVATIONS).Present, false);
            assertEquals(built.get(ExplainKey.COMBINED_RATE).Value.doubleValue(), rateExpected, TOLERANCE_RATE);
        }
        public virtual double explainRate(OvernightAveragedRateComputation computation, LocalDate startDate, LocalDate endDate, RatesProvider provider, ExplainMapBuilder builder)
        {
            double rate = this.rate(computation, startDate, endDate, provider);

            builder.put(ExplainKey.COMBINED_RATE, rate);
            return(rate);
        }
예제 #11
0
        /// <summary>
        /// Explains the present value of the CMS period.
        /// <para>
        /// This returns explanatory information about the calculation.
        ///
        /// </para>
        /// </summary>
        /// <param name="period">  the product </param>
        /// <param name="ratesProvider">  the rates provider </param>
        /// <param name="swaptionVolatilities">  the volatilities </param>
        /// <param name="builder">  the builder to populate </param>
        public void explainPresentValue(CmsPeriod period, RatesProvider ratesProvider, SabrSwaptionVolatilities swaptionVolatilities, ExplainMapBuilder builder)
        {
            string    type        = period.CmsPeriodType.ToString();
            Currency  ccy         = period.Currency;
            LocalDate paymentDate = period.PaymentDate;

            builder.put(ExplainKey.ENTRY_TYPE, "Cms" + type + "Period");
            builder.put(ExplainKey.STRIKE_VALUE, period.Strike);
            builder.put(ExplainKey.NOTIONAL, CurrencyAmount.of(ccy, period.Notional));
            builder.put(ExplainKey.PAYMENT_DATE, period.PaymentDate);
            builder.put(ExplainKey.DISCOUNT_FACTOR, ratesProvider.discountFactor(ccy, paymentDate));
            builder.put(ExplainKey.START_DATE, period.StartDate);
            builder.put(ExplainKey.END_DATE, period.EndDate);
            builder.put(ExplainKey.FIXING_DATE, period.FixingDate);
            builder.put(ExplainKey.ACCRUAL_YEAR_FRACTION, period.YearFraction);
            builder.put(ExplainKey.PRESENT_VALUE, presentValue(period, ratesProvider, swaptionVolatilities));
            builder.put(ExplainKey.FORWARD_RATE, swapPricer.parRate(period.UnderlyingSwap, ratesProvider));
            builder.put(ExplainKey.CONVEXITY_ADJUSTED_RATE, adjustedForwardRate(period, ratesProvider, swaptionVolatilities));
        }
예제 #12
0
 //-------------------------------------------------------------------------
 /// <summary>
 /// Explain present value builder used to build large explain map from the individual legs.
 /// </summary>
 /// <param name="leg">  the swap log </param>
 /// <param name="provider">  the rates provider </param>
 /// <param name="builder">  the explain map builder which will be populated but the leg  </param>
 internal virtual void explainPresentValueInternal(ResolvedSwapLeg leg, RatesProvider provider, ExplainMapBuilder builder)
 {
     builder.put(ExplainKey.ENTRY_TYPE, "Leg");
     builder.put(ExplainKey.PAY_RECEIVE, leg.PayReceive);
     builder.put(ExplainKey.LEG_TYPE, leg.Type.ToString());
     foreach (SwapPaymentPeriod period in leg.PaymentPeriods)
     {
         builder.addListEntry(ExplainKey.PAYMENT_PERIODS, child => paymentPeriodPricer.explainPresentValue(period, provider, child));
     }
     foreach (SwapPaymentEvent @event in leg.PaymentEvents)
     {
         builder.addListEntry(ExplainKey.PAYMENT_EVENTS, child => paymentEventPricer.explainPresentValue(@event, provider, child));
     }
     builder.put(ExplainKey.FORECAST_VALUE, forecastValue(leg, provider));
     builder.put(ExplainKey.PRESENT_VALUE, presentValue(leg, provider));
 }