Exemplo n.º 1
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));
        }
Exemplo n.º 2
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));
        }
        public virtual void test_singleDiscountCurveParameterSensitivity()
        {
            ZeroRateSensitivity            zeroPt   = ZeroRateSensitivity.of(USD, 10d, 5d);
            CreditCurveZeroRateSensitivity creditPt = CreditCurveZeroRateSensitivity.of(LEGAL_ENTITY_ABC, JPY, 2d, 3d);
            FxForwardSensitivity           fxPt     = FxForwardSensitivity.of(CurrencyPair.of(JPY, USD), USD, LocalDate.of(2017, 2, 14), 15d);
            CreditRatesProvider            test     = ImmutableCreditRatesProvider.builder().creditCurves(ImmutableMap.of(Pair.of(LEGAL_ENTITY_ABC, USD), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_ABC, CRD_ABC_USD), Pair.of(LEGAL_ENTITY_ABC, JPY), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_ABC, CRD_ABC_JPY), Pair.of(LEGAL_ENTITY_DEF, JPY), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_DEF, CRD_DEF))).discountCurves(ImmutableMap.of(USD, DSC_USD, JPY, DSC_JPY)).recoveryRateCurves(ImmutableMap.of(LEGAL_ENTITY_ABC, RR_ABC, LEGAL_ENTITY_DEF, RR_DEF)).valuationDate(VALUATION).build();
            CurrencyParameterSensitivities computed = CurrencyParameterSensitivities.of(test.singleDiscountCurveParameterSensitivity(zeroPt.combinedWith(creditPt).combinedWith(fxPt).build(), USD));
            CurrencyParameterSensitivities expected = DSC_USD.parameterSensitivity(zeroPt);

            assertTrue(computed.equalWithTolerance(expected, 1.0e-14));
        }
        public virtual void test_valuationDateMismatch()
        {
            ConstantRecoveryRates rr_wrong = ConstantRecoveryRates.of(LEGAL_ENTITY_ABC, VALUATION.plusWeeks(1), RECOVERY_RATE_ABC);

            assertThrowsIllegalArg(() => ImmutableCreditRatesProvider.builder().valuationDate(VALUATION).creditCurves(ImmutableMap.of(Pair.of(LEGAL_ENTITY_ABC, USD), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_ABC, CRD_ABC_USD), Pair.of(LEGAL_ENTITY_ABC, JPY), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_ABC, CRD_ABC_JPY), Pair.of(LEGAL_ENTITY_DEF, JPY), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_DEF, CRD_DEF))).discountCurves(ImmutableMap.of(USD, DSC_USD, JPY, DSC_JPY)).recoveryRateCurves(ImmutableMap.of(LEGAL_ENTITY_ABC, rr_wrong, LEGAL_ENTITY_DEF, RR_DEF)).build());
            IsdaCreditDiscountFactors crd_wrong = IsdaCreditDiscountFactors.of(JPY, VALUATION.plusWeeks(1), NAME_CRD_DEF, TIME_CRD_DEF, RATE_CRD_DEF, ACT_365F);

            assertThrowsIllegalArg(() => ImmutableCreditRatesProvider.builder().valuationDate(VALUATION).creditCurves(ImmutableMap.of(Pair.of(LEGAL_ENTITY_ABC, USD), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_ABC, CRD_ABC_USD), Pair.of(LEGAL_ENTITY_ABC, JPY), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_ABC, CRD_ABC_JPY), Pair.of(LEGAL_ENTITY_DEF, JPY), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_DEF, crd_wrong))).discountCurves(ImmutableMap.of(USD, DSC_USD, JPY, DSC_JPY)).recoveryRateCurves(ImmutableMap.of(LEGAL_ENTITY_ABC, RR_ABC, LEGAL_ENTITY_DEF, RR_DEF)).build());
            IsdaCreditDiscountFactors dsc_wrong = IsdaCreditDiscountFactors.of(USD, VALUATION.plusWeeks(1), NAME_DSC_USD, TIME_DSC_USD, RATE_DSC_USD, ACT_365F);

            assertThrowsIllegalArg(() => ImmutableCreditRatesProvider.builder().valuationDate(VALUATION).creditCurves(ImmutableMap.of(Pair.of(LEGAL_ENTITY_ABC, USD), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_ABC, CRD_ABC_USD), Pair.of(LEGAL_ENTITY_ABC, JPY), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_ABC, CRD_ABC_JPY), Pair.of(LEGAL_ENTITY_DEF, JPY), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_DEF, CRD_DEF))).discountCurves(ImmutableMap.of(USD, dsc_wrong, JPY, DSC_JPY)).recoveryRateCurves(ImmutableMap.of(LEGAL_ENTITY_ABC, RR_ABC, LEGAL_ENTITY_DEF, RR_DEF)).build());
        }
        //-------------------------------------------------------------------------
        public virtual void coverage()
        {
            ImmutableCreditRatesProvider test1 = ImmutableCreditRatesProvider.builder().creditCurves(ImmutableMap.of(Pair.of(LEGAL_ENTITY_ABC, USD), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_ABC, CRD_ABC_USD))).discountCurves(ImmutableMap.of(USD, DSC_USD)).recoveryRateCurves(ImmutableMap.of(LEGAL_ENTITY_ABC, RR_ABC)).valuationDate(VALUATION).build();

            coverImmutableBean(test1);
            IsdaCreditDiscountFactors    dsc   = IsdaCreditDiscountFactors.of(JPY, VALUATION.plusDays(1), NAME_DSC_JPY, TIME_DSC_JPY, RATE_DSC_JPY, ACT_365F);
            IsdaCreditDiscountFactors    hzd   = IsdaCreditDiscountFactors.of(JPY, VALUATION.plusDays(1), NAME_CRD_DEF, TIME_CRD_DEF, RATE_CRD_DEF, ACT_365F);
            ConstantRecoveryRates        rr    = ConstantRecoveryRates.of(LEGAL_ENTITY_DEF, VALUATION.plusDays(1), RECOVERY_RATE_DEF);
            ImmutableCreditRatesProvider test2 = ImmutableCreditRatesProvider.builder().creditCurves(ImmutableMap.of(Pair.of(LEGAL_ENTITY_DEF, JPY), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_DEF, hzd))).discountCurves(ImmutableMap.of(JPY, dsc)).recoveryRateCurves(ImmutableMap.of(LEGAL_ENTITY_DEF, rr)).valuationDate(VALUATION.plusDays(1)).build();

            coverBeanEquals(test1, test2);
        }
        //-------------------------------------------------------------------------
        private static ImmutableCreditRatesProvider createRatesProvider(LocalDate tradeDate, LocalDate snapDate, double rateScale, double recoveryRate)
        {
            ImmutableMarketDataBuilder builder = ImmutableMarketData.builder(snapDate);

            for (int j = 0; j < NUM_INSTRUMENTS; j++)
            {
                builder.addValue(QuoteId.of(StandardId.of("OG", ID_VALUES[j])), RATES[j] * rateScale);
            }
            ImmutableMarketData       quotes          = builder.build();
            IsdaCreditCurveDefinition curveDefinition = IsdaCreditCurveDefinition.of(CurveName.of("yield"), EUR, tradeDate, ACT_365F, DSC_NODES, false, false);
            IsdaCreditDiscountFactors yc = IsdaCompliantDiscountCurveCalibrator.standard().calibrate(curveDefinition, quotes, REF_DATA);

            return(ImmutableCreditRatesProvider.builder().valuationDate(tradeDate).discountCurves(ImmutableMap.of(EUR, yc)).recoveryRateCurves(ImmutableMap.of(LEGAL_ENTITY, ConstantRecoveryRates.of(LEGAL_ENTITY, tradeDate, recoveryRate))).creditCurves(ImmutableMap.of()).build());
        }
        public virtual void test_getter()
        {
            ImmutableCreditRatesProvider test = ImmutableCreditRatesProvider.builder().valuationDate(VALUATION).creditCurves(ImmutableMap.of(Pair.of(LEGAL_ENTITY_ABC, USD), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_ABC, CRD_ABC_USD), Pair.of(LEGAL_ENTITY_ABC, JPY), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_ABC, CRD_ABC_JPY), Pair.of(LEGAL_ENTITY_DEF, JPY), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_DEF, CRD_DEF))).discountCurves(ImmutableMap.of(USD, DSC_USD, JPY, DSC_JPY)).recoveryRateCurves(ImmutableMap.of(LEGAL_ENTITY_ABC, RR_ABC, LEGAL_ENTITY_DEF, RR_DEF)).build();

            assertEquals(test.discountFactors(USD), DSC_USD);
            assertEquals(test.discountFactors(JPY), DSC_JPY);
            assertEquals(test.survivalProbabilities(LEGAL_ENTITY_ABC, USD), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_ABC, CRD_ABC_USD));
            assertEquals(test.survivalProbabilities(LEGAL_ENTITY_ABC, JPY), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_ABC, CRD_ABC_JPY));
            assertEquals(test.survivalProbabilities(LEGAL_ENTITY_DEF, JPY), LegalEntitySurvivalProbabilities.of(LEGAL_ENTITY_DEF, CRD_DEF));
            assertEquals(test.recoveryRates(LEGAL_ENTITY_ABC), RR_ABC);
            assertEquals(test.recoveryRates(LEGAL_ENTITY_DEF), RR_DEF);
            StandardId entity = StandardId.of("OG", "NONE");

            assertThrowsIllegalArg(() => test.discountFactors(EUR));
            assertThrowsIllegalArg(() => test.survivalProbabilities(LEGAL_ENTITY_DEF, USD));
            assertThrowsIllegalArg(() => test.survivalProbabilities(entity, USD));
            assertThrowsIllegalArg(() => test.recoveryRates(entity));
        }
        //-------------------------------------------------------------------------
        protected internal virtual void testCalibrationAgainstISDA(IsdaCompliantCreditCurveCalibrator builder, DayCount dayCount, Currency currency, double tol)
        {
            IsdaCdsProductPricer pricer = new IsdaCdsProductPricer(builder.AccrualOnDefaultFormula);

            for (int i = 0; i < NUM_TESTS; i++)
            {
                LegalEntitySurvivalProbabilities creditCurve = builder.calibrate(ImmutableList.copyOf(NODE_CDS[i]), CurveName.of("credit"), CDS_MARKET_DATA[i], YIELD_CURVES[i], dayCount, currency, false, false, REF_DATA);
                ResolvedCdsTrade[]           expectedCds     = EXP_NODE_CDS[i];
                ImmutableCreditRatesProvider provider        = YIELD_CURVES[i].toBuilder().creditCurves(ImmutableMap.of(Pair.of(LEGAL_ENTITY, EUR), creditCurve)).build();
                double[] expected = builder.AccrualOnDefaultFormula == AccrualOnDefaultFormula.MARKIT_FIX ? EXP_PROB_MARKIT_FIX[i] : EXP_PROB_ISDA[i];
                for (int k = 0; k < N_OBS; k++)
                {
                    assertEquals(creditCurve.SurvivalProbabilities.discountFactor(OBS_TIMES[k]), expected[k], tol);
                }
                int m = expectedCds.Length;
                for (int j = 0; j < m; j++)
                {
                    ResolvedCdsTrade cdsFromNode = NODE_CDS[i][j].trade(1d, CDS_MARKET_DATA[i], REF_DATA).UnderlyingTrade.resolve(REF_DATA);
                    assertEquals(cdsFromNode.Product, expectedCds[j].Product);
                    double price1 = pricer.price(cdsFromNode.Product, provider, SPREADS[i][j], cdsFromNode.Info.SettlementDate.get(), PriceType.CLEAN, REF_DATA);
                    assertEquals(price1, 0.0, 5e-16);
                }
            }
        }
 private double[] getStandardQuoteForm(ResolvedCdsTrade calibrationCds, CdsQuote marketQuote, LocalDate valuationDate, CreditDiscountFactors discountFactors, RecoveryRates recoveryRates, bool computeJacobian, ReferenceData refData)
 {
     double[] res = new double[3];
     res[2] = 1d;
     if (marketQuote.QuoteConvention.Equals(CdsQuoteConvention.PAR_SPREAD))
     {
         res[0] = marketQuote.QuotedValue;
     }
     else if (marketQuote.QuoteConvention.Equals(CdsQuoteConvention.QUOTED_SPREAD))
     {
         double     qSpread                 = marketQuote.QuotedValue;
         CurveName  curveName               = CurveName.of("quoteConvertCurve");
         NodalCurve tempCreditCurve         = calibrate(ImmutableList.of(calibrationCds), DoubleArray.of(qSpread), DoubleArray.of(0d), curveName, valuationDate, discountFactors, recoveryRates, refData);
         Currency   currency                = calibrationCds.Product.Currency;
         StandardId legalEntityId           = calibrationCds.Product.LegalEntityId;
         ImmutableCreditRatesProvider rates = ImmutableCreditRatesProvider.builder().valuationDate(valuationDate).discountCurves(ImmutableMap.of(currency, discountFactors)).recoveryRateCurves(ImmutableMap.of(legalEntityId, recoveryRates)).creditCurves(ImmutableMap.of(Pair.of(legalEntityId, currency), LegalEntitySurvivalProbabilities.of(legalEntityId, IsdaCreditDiscountFactors.of(currency, valuationDate, tempCreditCurve)))).build();
         res[0] = calibrationCds.Product.FixedRate;
         res[1] = tradePricer.price(calibrationCds, rates, PriceType.CLEAN, refData);
         if (computeJacobian)
         {
             CurrencyParameterSensitivities pufSensi = rates.parameterSensitivity(tradePricer.priceSensitivity(calibrationCds, rates, refData));
             CurrencyParameterSensitivities spSensi  = rates.parameterSensitivity(tradePricer.parSpreadSensitivity(calibrationCds, rates, refData));
             res[2] = spSensi.getSensitivity(curveName, currency).Sensitivity.get(0) / pufSensi.getSensitivity(curveName, currency).Sensitivity.get(0);
         }
     }
     else if (marketQuote.QuoteConvention.Equals(CdsQuoteConvention.POINTS_UPFRONT))
     {
         res[0] = calibrationCds.Product.FixedRate;
         res[1] = marketQuote.QuotedValue;
     }
     else
     {
         throw new System.ArgumentException("Unknown CDSQuoteConvention type " + marketQuote.GetType());
     }
     return(res);
 }
Exemplo n.º 10
0
        //-------------------------------------------------------------------------
        protected internal virtual void testJacobian(LegalEntitySurvivalProbabilities curve, ImmutableCreditRatesProvider ratesProvider, IList <CdsIndexIsdaCreditCurveNode> nodes, double[] quotes)
        {
            int nNode = nodes.Count;
            IsdaCreditDiscountFactors df = (IsdaCreditDiscountFactors)curve.SurvivalProbabilities;
            int nCurveNode = df.ParameterCount;

            for (int i = 0; i < nCurveNode; ++i)
            {
                double[] quotesUp = Arrays.copyOf(quotes, nNode);
                double[] quotesDw = Arrays.copyOf(quotes, nNode);
                quotesUp[i] += EPS;
                quotesDw[i] -= EPS;
                ImmutableMarketDataBuilder builderCreditUp = MARKET_DATA.toBuilder();
                ImmutableMarketDataBuilder builderCreditDw = MARKET_DATA.toBuilder();
                for (int j = 0; j < nNode; ++j)
                {
                    builderCreditUp.addValue(nodes[j].ObservableId, quotesUp[j]);
                    builderCreditDw.addValue(nodes[j].ObservableId, quotesDw[j]);
                }
                ImmutableMarketData       marketDataUp = builderCreditUp.build();
                ImmutableMarketData       marketDataDw = builderCreditDw.build();
                IsdaCreditCurveDefinition definition   = IsdaCreditCurveDefinition.of(df.Curve.Name, df.Currency, df.ValuationDate, df.DayCount, nodes, false, false);
                IsdaCreditDiscountFactors ccUp         = (IsdaCreditDiscountFactors)CALIBRATOR.calibrate(definition, marketDataUp, ratesProvider, REF_DATA).SurvivalProbabilities;
                IsdaCreditDiscountFactors ccDw         = (IsdaCreditDiscountFactors)CALIBRATOR.calibrate(definition, marketDataDw, ratesProvider, REF_DATA).SurvivalProbabilities;
                for (int j = 0; j < nNode; ++j)
                {
                    double computed = df.Curve.Metadata.findInfo(CurveInfoType.JACOBIAN).get().JacobianMatrix.get(j, i);
                    double expected = 0.5 * (ccUp.Curve.YValues.get(j) - ccDw.Curve.YValues.get(j)) / EPS;
                    assertEquals(computed, expected, EPS * 10d);
                }
            }
        }
        protected internal virtual void testJacobian(IsdaCompliantCreditCurveCalibrator builder, LegalEntitySurvivalProbabilities curve, ImmutableCreditRatesProvider ratesProvider, IList <CdsIsdaCreditCurveNode> nodes, double[] quotes, double quoteScale, double eps)
        {
            LocalDate valuationDate      = curve.ValuationDate;
            int       nNode              = nodes.Count;
            IsdaCreditDiscountFactors df = (IsdaCreditDiscountFactors)curve.SurvivalProbabilities;
            CurveName name       = df.Curve.Name;
            int       nCurveNode = df.ParameterCount;

            for (int i = 0; i < nCurveNode; ++i)
            {
                double[] quotesUp = Arrays.copyOf(quotes, nNode);
                double[] quotesDw = Arrays.copyOf(quotes, nNode);
                quotesUp[i] += eps / quoteScale;
                quotesDw[i] -= eps / quoteScale;
                ImmutableMarketDataBuilder builderCreditUp = ImmutableMarketData.builder(valuationDate);
                ImmutableMarketDataBuilder builderCreditDw = ImmutableMarketData.builder(valuationDate);
                for (int j = 0; j < nNode; ++j)
                {
                    builderCreditUp.addValue(nodes[j].ObservableId, quotesUp[j] * quoteScale);
                    builderCreditDw.addValue(nodes[j].ObservableId, quotesDw[j] * quoteScale);
                }
                ImmutableMarketData       marketDataUp = builderCreditUp.build();
                ImmutableMarketData       marketDataDw = builderCreditDw.build();
                IsdaCreditDiscountFactors ccUp         = (IsdaCreditDiscountFactors)builder.calibrate(nodes, name, marketDataUp, ratesProvider, curve.SurvivalProbabilities.DayCount, curve.Currency, false, false, REF_DATA).SurvivalProbabilities;
                IsdaCreditDiscountFactors ccDw         = (IsdaCreditDiscountFactors)builder.calibrate(nodes, name, marketDataDw, ratesProvider, curve.SurvivalProbabilities.DayCount, curve.Currency, false, false, REF_DATA).SurvivalProbabilities;
                for (int j = 0; j < nNode; ++j)
                {
                    double computed = df.Curve.Metadata.findInfo(CurveInfoType.JACOBIAN).get().JacobianMatrix.get(j, i);
                    double expected = 0.5 * (ccUp.Curve.YValues.get(j) - ccDw.Curve.YValues.get(j)) / eps;
                    assertEquals(computed, expected, eps * 10d);
                }
            }
        }
        internal virtual LegalEntitySurvivalProbabilities calibrate(IList <CdsIsdaCreditCurveNode> curveNodes, CurveName name, MarketData marketData, ImmutableCreditRatesProvider ratesProvider, DayCount definitionDayCount, Currency definitionCurrency, bool computeJacobian, bool storeTrade, ReferenceData refData)
        {
//JAVA TO C# CONVERTER TODO TASK: Method reference arbitrary object instance method syntax is not converted by Java to C# Converter:
//JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter:
            IEnumerator <StandardId> legalEntities = curveNodes.Select(CdsIsdaCreditCurveNode::getLegalEntityId).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 curve nodes");
//JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter:
            IEnumerator <Currency> currencies = curveNodes.Select(n => n.Template.Convention.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 curve nodes");
            ArgChecker.isTrue(definitionCurrency.Equals(currency), "curve definition currency must be the same as the currency of CDS");
//JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter:
            IEnumerator <CdsQuoteConvention> quoteConventions = curveNodes.Select(n => n.QuoteConvention).collect(Collectors.toSet()).GetEnumerator();
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
            CdsQuoteConvention quoteConvention = quoteConventions.next();

//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
            ArgChecker.isFalse(quoteConventions.hasNext(), "quote convention must be common to curve nodes");
            LocalDate valuationDate = marketData.ValuationDate;

            ArgChecker.isTrue(valuationDate.Equals(marketData.ValuationDate), "ratesProvider and marketDate must be based on the same valuation date");
            CreditDiscountFactors discountFactors = ratesProvider.discountFactors(currency);

            ArgChecker.isTrue(definitionDayCount.Equals(discountFactors.DayCount), "credit curve and discount curve must be based on the same day count convention");
            RecoveryRates recoveryRates = ratesProvider.recoveryRates(legalEntityId);

            int nNodes = curveNodes.Count;

            double[] coupons = new double[nNodes];
            double[] pufs    = new double[nNodes];
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] diag = new double[nNodes][nNodes];
            double[][] diag = RectangularArrays.ReturnRectangularDoubleArray(nNodes, nNodes);
            ImmutableList.Builder <ResolvedCdsTrade> tradesBuilder = ImmutableList.builder();
            for (int i = 0; i < nNodes; i++)
            {
                CdsCalibrationTrade tradeCalibration = curveNodes[i].trade(1d, marketData, refData);
                ResolvedCdsTrade    trade            = tradeCalibration.UnderlyingTrade.resolve(refData);
                tradesBuilder.add(trade);
                double[] temp = getStandardQuoteForm(trade, tradeCalibration.Quote, valuationDate, discountFactors, recoveryRates, computeJacobian, refData);
                coupons[i] = temp[0];
                pufs[i]    = temp[1];
                diag[i][i] = temp[2];
            }
            ImmutableList <ResolvedCdsTrade> trades = tradesBuilder.build();
            NodalCurve nodalCurve = calibrate(trades, DoubleArray.ofUnsafe(coupons), DoubleArray.ofUnsafe(pufs), name, valuationDate, discountFactors, recoveryRates, refData);

            if (computeJacobian)
            {
                LegalEntitySurvivalProbabilities            creditCurve      = LegalEntitySurvivalProbabilities.of(legalEntityId, IsdaCreditDiscountFactors.of(currency, valuationDate, nodalCurve));
                ImmutableCreditRatesProvider                ratesProviderNew = ratesProvider.toBuilder().creditCurves(ImmutableMap.of(Pair.of(legalEntityId, currency), creditCurve)).build();
                System.Func <ResolvedCdsTrade, DoubleArray> sensiFunc        = quoteConvention.Equals(CdsQuoteConvention.PAR_SPREAD) ? getParSpreadSensitivityFunction(ratesProviderNew, name, currency, refData) : getPointsUpfrontSensitivityFunction(ratesProviderNew, name, currency, refData);
                DoubleMatrix sensi = DoubleMatrix.ofArrayObjects(nNodes, nNodes, i => sensiFunc(trades.get(i)));
                sensi = (DoubleMatrix)MATRIX_ALGEBRA.multiply(DoubleMatrix.ofUnsafe(diag), sensi);
                JacobianCalibrationMatrix jacobian = JacobianCalibrationMatrix.of(ImmutableList.of(CurveParameterSize.of(name, nNodes)), MATRIX_ALGEBRA.getInverse(sensi));
                nodalCurve = nodalCurve.withMetadata(nodalCurve.Metadata.withInfo(CurveInfoType.JACOBIAN, jacobian));
            }

            ImmutableList <ParameterMetadata> parameterMetadata;

            if (storeTrade)
            {
                parameterMetadata = IntStream.range(0, nNodes).mapToObj(n => ResolvedTradeParameterMetadata.of(trades.get(n), curveNodes[n].Label)).collect(Guavate.toImmutableList());
            }
            else
            {
                parameterMetadata = IntStream.range(0, nNodes).mapToObj(n => curveNodes[n].metadata(trades.get(n).Product.ProtectionEndDate)).collect(Guavate.toImmutableList());
            }
            nodalCurve = nodalCurve.withMetadata(nodalCurve.Metadata.withParameterMetadata(parameterMetadata));

            return(LegalEntitySurvivalProbabilities.of(legalEntityId, IsdaCreditDiscountFactors.of(currency, valuationDate, nodalCurve)));
        }
        //-------------------------------------------------------------------------
        /// <summary>
        /// Calibrates the ISDA compliant credit curve to the market data.
        /// <para>
        /// This creates the single credit curve for a legal entity.
        /// The curve nodes in {@code IsdaCreditCurveDefinition} should be single-name credit default swaps on this legal entity.
        /// </para>
        /// <para>
        /// The relevant discount curve and recovery rate curve must be stored in {@code ratesProvider}.
        /// The day count convention for the resulting credit curve is the same as that of the discount curve.
        ///
        /// </para>
        /// </summary>
        /// <param name="curveDefinition">  the curve definition </param>
        /// <param name="marketData">  the market data </param>
        /// <param name="ratesProvider">  the rates provider </param>
        /// <param name="refData">  the reference data </param>
        /// <returns> the ISDA compliant credit curve </returns>
        public virtual LegalEntitySurvivalProbabilities calibrate(IsdaCreditCurveDefinition curveDefinition, MarketData marketData, ImmutableCreditRatesProvider ratesProvider, ReferenceData refData)
        {
            ArgChecker.isTrue(curveDefinition.CurveValuationDate.Equals(ratesProvider.ValuationDate), "ratesProvider and curveDefinition must be based on the same valuation date");
//JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter:
            ImmutableList <CdsIsdaCreditCurveNode> curveNodes = curveDefinition.CurveNodes.Where(n => n is CdsIsdaCreditCurveNode).Select(n => (CdsIsdaCreditCurveNode)n).collect(Guavate.toImmutableList());

            return(calibrate(curveNodes, curveDefinition.Name, marketData, ratesProvider, curveDefinition.DayCount, curveDefinition.Currency, curveDefinition.ComputeJacobian, curveDefinition.StoreNodeTrade, refData));
        }