Esempio n. 1
0
        public virtual void test_bondDiscountingProvider()
        {
            LocalDate valDate = LocalDate.of(2015, 6, 30);
            Curve     ccAUsd  = ConstantNodalCurve.of(Curves.zeroRates(CC_A_USD.CurveName, ACT_365F), 0.5d, 1.5d);
            Curve     ccBGbp  = ConstantNodalCurve.of(Curves.zeroRates(CC_B_GBP.CurveName, ACT_365F), 0.5d, 2d);
            Curve     ccAGbp  = ConstantNodalCurve.of(Curves.zeroRates(CC_A_GBP.CurveName, ACT_365F), 0.5d, 3d);
            Curve     dcGbp   = ConstantNodalCurve.of(Curves.zeroRates(DC_GBP.CurveName, ACT_365F), 0.5d, 0.1d);
            Curve     dcUsd   = ConstantNodalCurve.of(Curves.zeroRates(DC_USD.CurveName, ACT_365F), 0.5d, 0.05d);
            Curve     rcA     = ConstantCurve.of(Curves.recoveryRates(RC_A.CurveName, ACT_365F), 0.5d);
            Curve     rcB     = ConstantCurve.of(Curves.recoveryRates(RC_B.CurveName, ACT_365F), 0.4234d);
            IDictionary <CurveId, Curve> curveMap = new Dictionary <CurveId, Curve>();

            curveMap[CC_A_USD] = ccAUsd;
            curveMap[CC_B_GBP] = ccBGbp;
            curveMap[CC_A_GBP] = ccAGbp;
            curveMap[DC_USD]   = dcUsd;
            curveMap[DC_GBP]   = dcGbp;
            curveMap[RC_A]     = rcA;
            curveMap[RC_B]     = rcB;
            MarketData          md       = ImmutableMarketData.of(valDate, ImmutableMap.copyOf(curveMap));
            CreditRatesProvider provider = LOOKUP_WITH_SOURCE.creditRatesProvider(md);

            assertEquals(provider.ValuationDate, valDate);
            assertEquals(provider.findData(CC_A_USD.CurveName), ccAUsd);
            assertEquals(provider.findData(DC_USD.CurveName), dcUsd);
            assertEquals(provider.findData(RC_B.CurveName), rcB);
            assertEquals(provider.findData(CurveName.of("Rubbish")), null);
            // check credit curve
            LegalEntitySurvivalProbabilities cc      = provider.survivalProbabilities(ISSUER_A, GBP);
            IsdaCreditDiscountFactors        ccUnder = (IsdaCreditDiscountFactors)cc.SurvivalProbabilities;

            assertEquals(ccUnder.Curve.Name, ccAGbp.Name);
            assertThrowsRuntime(() => provider.survivalProbabilities(ISSUER_B, USD));
            assertThrowsRuntime(() => provider.survivalProbabilities(ISSUER_C, USD));
            // check discount curve
            IsdaCreditDiscountFactors dc = (IsdaCreditDiscountFactors)provider.discountFactors(USD);

            assertEquals(dc.Curve.Name, dcUsd.Name);
            assertThrowsRuntime(() => provider.discountFactors(EUR));
            // check recovery rate curve
            ConstantRecoveryRates rc = (ConstantRecoveryRates)provider.recoveryRates(ISSUER_B);

            assertEquals(rc.RecoveryRate, rcB.getParameter(0));
            assertThrowsRuntime(() => provider.recoveryRates(ISSUER_C));
        }
Esempio n. 2
0
        private Pair <CreditDiscountFactors, LegalEntitySurvivalProbabilities> reduceDiscountFactors(ResolvedCds cds, CreditRatesProvider ratesProvider)
        {
            Currency currency = cds.Currency;
            CreditDiscountFactors discountFactors = ratesProvider.discountFactors(currency);

            ArgChecker.isTrue(discountFactors.IsdaCompliant, "discount factors must be IsdaCompliantZeroRateDiscountFactors");
            LegalEntitySurvivalProbabilities survivalProbabilities = ratesProvider.survivalProbabilities(cds.LegalEntityId, currency);

            ArgChecker.isTrue(survivalProbabilities.SurvivalProbabilities.IsdaCompliant, "survival probabilities must be IsdaCompliantZeroRateDiscountFactors");
            ArgChecker.isTrue(discountFactors.DayCount.Equals(survivalProbabilities.SurvivalProbabilities.DayCount), "day count conventions of discounting curve and credit curve must be the same");
            return(Pair.of(discountFactors, survivalProbabilities));
        }
        //-------------------------------------------------------------------------
        /// <summary>
        /// Calculates the present value of the trade.
        /// <para>
        /// The present value of the product is based on the valuation date.
        /// </para>
        /// <para>
        /// This method can calculate the clean or dirty present value, see <seealso cref="PriceType"/>.
        /// If calculating the clean value, the accrued interest is calculated based on the step-in date.
        ///
        /// </para>
        /// </summary>
        /// <param name="trade">  the trade </param>
        /// <param name="ratesProvider">  the rates provider </param>
        /// <param name="priceType">  the price type </param>
        /// <param name="refData">  the reference data </param>
        /// <returns> the price </returns>
        public virtual CurrencyAmount presentValue(ResolvedCdsTrade trade, CreditRatesProvider ratesProvider, PriceType priceType, ReferenceData refData)
        {
            CurrencyAmount pvProduct = productPricer.presentValue(trade.Product, ratesProvider, ratesProvider.ValuationDate, priceType, refData);

            if (!trade.UpfrontFee.Present)
            {
                return(pvProduct);
            }
            Payment        upfront   = trade.UpfrontFee.get();
            CurrencyAmount pvUpfront = upfrontPricer.presentValue(upfront, ratesProvider.discountFactors(upfront.Currency).toDiscountFactors());

            return(pvProduct.plus(pvUpfront));
        }
        internal virtual Triple <CreditDiscountFactors, LegalEntitySurvivalProbabilities, double> reduceDiscountFactors(ResolvedCds cds, CreditRatesProvider ratesProvider)
        {
            Currency currency = cds.Currency;
            CreditDiscountFactors discountFactors = ratesProvider.discountFactors(currency);

            ArgChecker.isTrue(discountFactors.IsdaCompliant, "discount factors must be IsdaCompliantZeroRateDiscountFactors");
            LegalEntitySurvivalProbabilities survivalProbabilities = ratesProvider.survivalProbabilities(cds.LegalEntityId, currency);

            ArgChecker.isTrue(survivalProbabilities.SurvivalProbabilities.IsdaCompliant, "survival probabilities must be IsdaCompliantZeroRateDiscountFactors");
            ArgChecker.isTrue(discountFactors.DayCount.Equals(survivalProbabilities.SurvivalProbabilities.DayCount), "day count conventions of discounting curve and credit curve must be the same");
            double indexFactor = ((IsdaCreditDiscountFactors)survivalProbabilities.SurvivalProbabilities).Curve.Metadata.getInfo(CurveInfoType.CDS_INDEX_FACTOR);

            return(Triple.of(discountFactors, survivalProbabilities, indexFactor));
        }
Esempio n. 5
0
        /// <summary>
        /// The par spread quotes are converted to points upfronts or quoted spreads.
        /// <para>
        /// The relevant discount curve and recovery rate curve must be stored in {@code ratesProvider}.
        /// The credit curve is internally calibrated to par spread values.
        /// </para>
        /// <para>
        /// {@code trades} must be sorted in ascending order in maturity and coherent to {@code quotes}.
        /// </para>
        /// <para>
        /// The resultant quote is specified by {@code targetConvention}.
        ///
        /// </para>
        /// </summary>
        /// <param name="trades">  the trades </param>
        /// <param name="quotes">  the quotes </param>
        /// <param name="ratesProvider">  the rates provider </param>
        /// <param name="targetConvention">  the target convention </param>
        /// <param name="refData">  the reference data </param>
        /// <returns> the quotes </returns>
        public virtual IList <CdsQuote> quotesFromParSpread(IList <ResolvedCdsTrade> trades, IList <CdsQuote> quotes, CreditRatesProvider ratesProvider, CdsQuoteConvention targetConvention, ReferenceData refData)
        {
            ArgChecker.noNulls(trades, "trades");
            ArgChecker.noNulls(quotes, "quotes");
            ArgChecker.notNull(ratesProvider, "ratesProvider");
            ArgChecker.notNull(targetConvention, "targetConvention");
            ArgChecker.notNull(refData, "refData");

            int nNodes = trades.Count;

            ArgChecker.isTrue(quotes.Count == nNodes, "trades and quotes must be the same size");
            quotes.ForEach(q => ArgChecker.isTrue(q.QuoteConvention.Equals(CdsQuoteConvention.PAR_SPREAD), "quote must be par spread"));
//JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter:
            IEnumerator <StandardId> legalEntities = trades.Select(t => t.Product.LegalEntityId).collect(Collectors.toSet()).GetEnumerator();
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
            StandardId legalEntityId = legalEntities.next();

//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
            ArgChecker.isFalse(legalEntities.hasNext(), "legal entity must be common to trades");
//JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter:
            IEnumerator <Currency> currencies = trades.Select(t => t.Product.Currency).collect(Collectors.toSet()).GetEnumerator();
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
            Currency currency = currencies.next();

//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
            ArgChecker.isFalse(currencies.hasNext(), "currency must be common to trades");

            LocalDate             valuationDate    = ratesProvider.ValuationDate;
            CreditDiscountFactors discountFactors  = ratesProvider.discountFactors(currency);
            RecoveryRates         recoveryRates    = ratesProvider.recoveryRates(legalEntityId);
            NodalCurve            creditCurve      = calibrator.calibrate(trades, DoubleArray.of(nNodes, q => quotes[q].QuotedValue), DoubleArray.filled(nNodes), CurveName.of("temp"), valuationDate, discountFactors, recoveryRates, refData);
            CreditRatesProvider   ratesProviderNew = ratesProvider.toImmutableCreditRatesProvider().toBuilder().creditCurves(ImmutableMap.of(Pair.of(legalEntityId, currency), LegalEntitySurvivalProbabilities.of(legalEntityId, IsdaCreditDiscountFactors.of(currency, valuationDate, creditCurve)))).build();

            System.Func <ResolvedCdsTrade, CdsQuote> quoteValueFunction = createQuoteValueFunction(ratesProviderNew, targetConvention, refData);
//JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter:
            ImmutableList <CdsQuote> result = trades.Select(c => quoteValueFunction(c)).collect(Collectors.collectingAndThen(Collectors.toList(), ImmutableList.copyOf));

            return(result);
        }
Esempio n. 6
0
        /// <summary>
        /// Converts points upfront to quoted spread.
        /// <para>
        /// Thus {@code quote} must be {@code CdsQuoteConvention.POINTS_UPFRONT}.
        /// </para>
        /// <para>
        /// The relevant discount curve and recovery rate curve must be stored in {@code ratesProvider}.
        /// The credit curve is internally calibrated to convert one quote type to the other quote type.
        ///
        /// </para>
        /// </summary>
        /// <param name="trade">  the trade </param>
        /// <param name="quote">  the quote </param>
        /// <param name="ratesProvider">  the rates provider </param>
        /// <param name="refData">  the reference data </param>
        /// <returns> the quote </returns>
        public virtual CdsQuote quotedSpreadFromPointsUpfront(ResolvedCdsTrade trade, CdsQuote quote, CreditRatesProvider ratesProvider, ReferenceData refData)
        {
            ArgChecker.notNull(trade, "trade");
            ArgChecker.notNull(quote, "quote");
            ArgChecker.notNull(ratesProvider, "ratesProvider");
            ArgChecker.notNull(refData, "refData");
            ArgChecker.isTrue(quote.QuoteConvention.Equals(CdsQuoteConvention.POINTS_UPFRONT), "quote must be points upfront");

            ResolvedCds         product          = trade.Product;
            Currency            currency         = product.Currency;
            StandardId          legalEntityId    = product.LegalEntityId;
            LocalDate           valuationDate    = ratesProvider.ValuationDate;
            NodalCurve          creditCurve      = calibrator.calibrate(ImmutableList.of(trade), DoubleArray.of(product.FixedRate), DoubleArray.of(quote.QuotedValue), CurveName.of("temp"), valuationDate, ratesProvider.discountFactors(currency), ratesProvider.recoveryRates(legalEntityId), refData);
            CreditRatesProvider ratesProviderNew = ratesProvider.toImmutableCreditRatesProvider().toBuilder().creditCurves(ImmutableMap.of(Pair.of(legalEntityId, currency), LegalEntitySurvivalProbabilities.of(legalEntityId, IsdaCreditDiscountFactors.of(currency, valuationDate, creditCurve)))).build();
            double sp = pricer.parSpread(trade, ratesProviderNew, refData);

            return(CdsQuote.of(CdsQuoteConvention.QUOTED_SPREAD, sp));
        }
Esempio n. 7
0
        internal override DoubleArray computedBucketedCs01(ResolvedCdsTrade trade, IList <ResolvedCdsTrade> bucketCds, CreditRatesProvider ratesProvider, ReferenceData refData)
        {
            checkCdsBucket(trade, bucketCds);
            ResolvedCds product       = trade.Product;
            Currency    currency      = product.Currency;
            StandardId  legalEntityId = product.LegalEntityId;
            LocalDate   valuationDate = ratesProvider.ValuationDate;
            ImmutableCreditRatesProvider immutableRatesProvider = ratesProvider.toImmutableCreditRatesProvider();

            int nBucket = bucketCds.Count;

            double[]    res                    = new double[nBucket];
            DoubleArray impSp                  = impliedSpread(bucketCds, ratesProvider, refData);
            NodalCurve  creditCurveBase        = Calibrator.calibrate(bucketCds, impSp, DoubleArray.filled(nBucket), CurveName.of("baseImpliedCreditCurve"), valuationDate, ratesProvider.discountFactors(currency), ratesProvider.recoveryRates(legalEntityId), refData);
            Pair <StandardId, Currency> lePair = Pair.of(legalEntityId, currency);

            IsdaCreditDiscountFactors df = IsdaCreditDiscountFactors.of(currency, valuationDate, creditCurveBase);
            CreditRatesProvider       ratesProviderBase = immutableRatesProvider.toBuilder().creditCurves(ImmutableMap.of(lePair, LegalEntitySurvivalProbabilities.of(legalEntityId, df))).build();
            double pvBase = Pricer.presentValueOnSettle(trade, ratesProviderBase, PriceType.DIRTY, refData).Amount;

            for (int i = 0; i < nBucket; ++i)
            {
                double[] bumpedSp = impSp.toArray();
                bumpedSp[i] += bumpAmount;
                NodalCurve creditCurveBump                  = Calibrator.calibrate(bucketCds, DoubleArray.ofUnsafe(bumpedSp), DoubleArray.filled(nBucket), CurveName.of("bumpedImpliedCreditCurve"), valuationDate, ratesProvider.discountFactors(currency), ratesProvider.recoveryRates(legalEntityId), refData);
                IsdaCreditDiscountFactors dfBump            = IsdaCreditDiscountFactors.of(currency, valuationDate, creditCurveBump);
                CreditRatesProvider       ratesProviderBump = immutableRatesProvider.toBuilder().creditCurves(ImmutableMap.of(lePair, LegalEntitySurvivalProbabilities.of(legalEntityId, dfBump))).build();
                double pvBumped = Pricer.presentValueOnSettle(trade, ratesProviderBump, PriceType.DIRTY, refData).Amount;
                res[i] = (pvBumped - pvBase) / bumpAmount;
            }
            return(DoubleArray.ofUnsafe(res));
        }
Esempio n. 8
0
        //-------------------------------------------------------------------------
        public override CurrencyAmount parallelCs01(ResolvedCdsTrade trade, IList <ResolvedCdsTrade> bucketCds, CreditRatesProvider ratesProvider, ReferenceData refData)
        {
            checkCdsBucket(trade, bucketCds);
            ResolvedCds product       = trade.Product;
            Currency    currency      = product.Currency;
            StandardId  legalEntityId = product.LegalEntityId;
            LocalDate   valuationDate = ratesProvider.ValuationDate;
            ImmutableCreditRatesProvider immutableRatesProvider = ratesProvider.toImmutableCreditRatesProvider();

            int         nBucket                = bucketCds.Count;
            DoubleArray impSp                  = impliedSpread(bucketCds, ratesProvider, refData);
            NodalCurve  creditCurveBase        = Calibrator.calibrate(bucketCds, impSp, DoubleArray.filled(nBucket), CurveName.of("baseImpliedCreditCurve"), valuationDate, ratesProvider.discountFactors(currency), ratesProvider.recoveryRates(legalEntityId), refData);
            Pair <StandardId, Currency> lePair = Pair.of(legalEntityId, currency);

            IsdaCreditDiscountFactors df = IsdaCreditDiscountFactors.of(currency, valuationDate, creditCurveBase);
            CreditRatesProvider       ratesProviderBase = immutableRatesProvider.toBuilder().creditCurves(ImmutableMap.of(lePair, LegalEntitySurvivalProbabilities.of(legalEntityId, df))).build();
            CurrencyAmount            pvBase            = Pricer.presentValueOnSettle(trade, ratesProviderBase, PriceType.DIRTY, refData);

            DoubleArray bumpedSp                        = DoubleArray.of(nBucket, i => impSp.get(i) + bumpAmount);
            NodalCurve  creditCurveBump                 = Calibrator.calibrate(bucketCds, bumpedSp, DoubleArray.filled(nBucket), CurveName.of("bumpedImpliedCreditCurve"), valuationDate, ratesProvider.discountFactors(currency), ratesProvider.recoveryRates(legalEntityId), refData);
            IsdaCreditDiscountFactors dfBump            = IsdaCreditDiscountFactors.of(currency, valuationDate, creditCurveBump);
            CreditRatesProvider       ratesProviderBump = immutableRatesProvider.toBuilder().creditCurves(ImmutableMap.of(lePair, LegalEntitySurvivalProbabilities.of(legalEntityId, dfBump))).build();
            CurrencyAmount            pvBumped          = Pricer.presentValueOnSettle(trade, ratesProviderBump, PriceType.DIRTY, refData);

            return(CurrencyAmount.of(currency, (pvBumped.Amount - pvBase.Amount) / bumpAmount));
        }
        /// <summary>
        /// Calculates the present value sensitivity of the trade.
        /// <para>
        /// The present value sensitivity of the trade is the sensitivity of present value to the underlying curves.
        ///
        /// </para>
        /// </summary>
        /// <param name="trade">  the trade </param>
        /// <param name="ratesProvider">  the rates provider </param>
        /// <param name="refData">  the reference data </param>
        /// <returns> the present value sensitivity </returns>
        public virtual PointSensitivities presentValueSensitivity(ResolvedCdsTrade trade, CreditRatesProvider ratesProvider, ReferenceData refData)
        {
            PointSensitivityBuilder pvSensiProduct = productPricer.presentValueSensitivity(trade.Product, ratesProvider, ratesProvider.ValuationDate, refData);

            if (!trade.UpfrontFee.Present)
            {
                return(pvSensiProduct.build());
            }
            Payment upfront = trade.UpfrontFee.get();
            PointSensitivityBuilder pvUpfront = upfrontPricer.presentValueSensitivity(upfront, ratesProvider.discountFactors(upfront.Currency).toDiscountFactors());

            return(pvSensiProduct.combinedWith(pvUpfront).build());
        }
        internal override DoubleArray computedBucketedCs01(ResolvedCdsTrade trade, IList <ResolvedCdsTrade> bucketCds, CreditRatesProvider ratesProvider, ReferenceData refData)
        {
            checkCdsBucket(trade, bucketCds);
            ResolvedCds product       = trade.Product;
            Currency    currency      = product.Currency;
            StandardId  legalEntityId = product.LegalEntityId;
            LocalDate   valuationDate = ratesProvider.ValuationDate;

            int         nBucket          = bucketCds.Count;
            DoubleArray impSp            = impliedSpread(bucketCds, ratesProvider, refData);
            NodalCurve  creditCurveBase  = Calibrator.calibrate(bucketCds, impSp, DoubleArray.filled(nBucket), CurveName.of("baseImpliedCreditCurve"), valuationDate, ratesProvider.discountFactors(currency), ratesProvider.recoveryRates(legalEntityId), refData);
            IsdaCreditDiscountFactors df = IsdaCreditDiscountFactors.of(currency, valuationDate, creditCurveBase);
            CreditRatesProvider       ratesProviderBase = ratesProvider.toImmutableCreditRatesProvider().toBuilder().creditCurves(ImmutableMap.of(Pair.of(legalEntityId, currency), LegalEntitySurvivalProbabilities.of(legalEntityId, df))).build();

            double[][]         res     = new double[nBucket][];
            PointSensitivities pointPv = Pricer.presentValueOnSettleSensitivity(trade, ratesProviderBase, refData);
            DoubleArray        vLambda = ratesProviderBase.singleCreditCurveParameterSensitivity(pointPv, legalEntityId, currency).Sensitivity;

            for (int i = 0; i < nBucket; i++)
            {
                PointSensitivities pointSp = Pricer.parSpreadSensitivity(bucketCds[i], ratesProviderBase, refData);
                res[i] = ratesProviderBase.singleCreditCurveParameterSensitivity(pointSp, legalEntityId, currency).Sensitivity.toArray();
            }
            DoubleMatrix          jacT  = MATRIX_ALGEBRA.getTranspose(DoubleMatrix.ofUnsafe(res));
            LUDecompositionResult luRes = DECOMPOSITION.apply(jacT);
            DoubleArray           vS    = luRes.solve(vLambda);

            return(vS);
        }