//------------------------------------------------------------------------- public override NodalCurve calibrate(IList <ResolvedCdsTrade> calibrationCDSs, DoubleArray premiums, DoubleArray pointsUpfront, CurveName name, LocalDate valuationDate, CreditDiscountFactors discountFactors, RecoveryRates recoveryRates, ReferenceData refData) { int n = calibrationCDSs.Count; double[] guess = new double[n]; double[] t = new double[n]; double[] lgd = new double[n]; for (int i = 0; i < n; i++) { LocalDate endDate = calibrationCDSs[i].Product.ProtectionEndDate; t[i] = discountFactors.relativeYearFraction(endDate); lgd[i] = 1d - recoveryRates.recoveryRate(endDate); guess[i] = (premiums.get(i) + pointsUpfront.get(i) / t[i]) / lgd[i]; } DoubleArray times = DoubleArray.ofUnsafe(t); CurveMetadata baseMetadata = DefaultCurveMetadata.builder().xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).curveName(name).dayCount(discountFactors.DayCount).build(); NodalCurve creditCurve = n == 1 ? ConstantNodalCurve.of(baseMetadata, t[0], guess[0]) : InterpolatedNodalCurve.of(baseMetadata, times, DoubleArray.ofUnsafe(guess), CurveInterpolators.PRODUCT_LINEAR, CurveExtrapolators.FLAT, CurveExtrapolators.PRODUCT_LINEAR); for (int i = 0; i < n; i++) { System.Func <double, double> func = getPriceFunction(i, calibrationCDSs[i], premiums.get(i), pointsUpfront.get(i), valuationDate, creditCurve, discountFactors, recoveryRates, refData); double[] bracket = BRACKER.getBracketedPoints(func, 0.8 * guess[i], 1.25 * guess[i], 0.0, double.PositiveInfinity); double zeroRate = bracket[0] > bracket[1] ? ROOTFINDER.getRoot(func, bracket[1], bracket[0]) : ROOTFINDER.getRoot(func, bracket[0], bracket[1]); //Negative guess handled creditCurve = creditCurve.withParameter(i, zeroRate); } return(creditCurve); }
static CalibrationDiscountingSimple1Test() { IBOR_INDICES.Add(USD_LIBOR_3M); DSC_NAMES[ALL_CURVE_NAME] = USD; IDX_NAMES[ALL_CURVE_NAME] = IBOR_INDICES; ALL_NODES[0] = IborFixingDepositCurveNode.of(IborFixingDepositTemplate.of(USD_LIBOR_3M), QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[0]))); for (int i = 0; i < FWD3_NB_FRA_NODES; i++) { ALL_NODES[i + 1] = FraCurveNode.of(FraTemplate.of(FWD3_FRA_TENORS[i], USD_LIBOR_3M), QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[1]))); } for (int i = 0; i < FWD3_NB_IRS_NODES; i++) { ALL_NODES[i + 1 + FWD3_NB_FRA_NODES] = FixedIborSwapCurveNode.of(FixedIborSwapTemplate.of(Period.ZERO, Tenor.of(FWD3_IRS_TENORS[i]), USD_FIXED_6M_LIBOR_3M), QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[i]))); } ImmutableMarketDataBuilder builder = ImmutableMarketData.builder(VAL_DATE); for (int i = 0; i < FWD3_NB_NODES; i++) { builder.addValue(QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[i])), FWD3_MARKET_QUOTES[i]); } ALL_QUOTES = builder.build(); IList <CurveNode[]> groupNodes = new List <CurveNode[]>(); groupNodes.Add(ALL_NODES); CURVES_NODES.Add(groupNodes); IList <CurveMetadata> groupMetadata = new List <CurveMetadata>(); groupMetadata.Add(DefaultCurveMetadata.builder().curveName(ALL_CURVE_NAME).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).dayCount(CURVE_DC).build()); CURVES_METADATA.Add(groupMetadata); }
//------------------------------------------------------------------------- // constructs an interpolated nodal curve internal InterpolatedNodalCurve createCurve(LocalDate date, IList <LoadedCurveNode> curveNodes) { // copy and sort IList <LoadedCurveNode> nodes = new List <LoadedCurveNode>(curveNodes); nodes.sort(System.Collections.IComparer.naturalOrder()); // build each node double[] xValues = new double[nodes.Count]; double[] yValues = new double[nodes.Count]; IList <ParameterMetadata> pointsMetadata = new List <ParameterMetadata>(nodes.Count); for (int i = 0; i < nodes.Count; i++) { LoadedCurveNode point = nodes[i]; double yearFraction = dayCount.yearFraction(date, point.Date); xValues[i] = yearFraction; yValues[i] = point.Value; ParameterMetadata pointMetadata = LabelDateParameterMetadata.of(point.Date, point.Label); pointsMetadata.Add(pointMetadata); } // create metadata CurveMetadata curveMetadata = DefaultCurveMetadata.builder().curveName(curveName).xValueType(xValueType).yValueType(yValueType).dayCount(dayCount).parameterMetadata(pointsMetadata).build(); return(InterpolatedNodalCurve.builder().metadata(curveMetadata).xValues(DoubleArray.copyOf(xValues)).yValues(DoubleArray.copyOf(yValues)).interpolator(interpolator).extrapolatorLeft(extrapolatorLeft).extrapolatorRight(extrapolatorRight).build()); }
static CalibrationDiscountingSmithWilsonTest() { IBOR_INDICES.Add(GBP_LIBOR_6M); DSC_NAMES[CURVE_NAME] = GBP; IDX_NAMES[CURVE_NAME] = IBOR_INDICES; for (int i = 0; i < FWD6_NB_NODES; i++) { ALL_NODES[i] = FixedIborSwapCurveNode.of(FixedIborSwapTemplate.of(Period.ZERO, Tenor.of(FWD6_IRS_TENORS[i]), GBP_FIXED_6M_LIBOR_6M), QuoteId.of(StandardId.of(SCHEME, FWD6_ID_VALUE[i]))); NODE_TIMES[i] = CURVE_DC.relativeYearFraction(VAL_DATE, ALL_NODES[i].date(VAL_DATE, REF_DATA)); } ImmutableMarketDataBuilder builder = ImmutableMarketData.builder(VAL_DATE); for (int i = 0; i < FWD6_NB_NODES; i++) { builder.addValue(QuoteId.of(StandardId.of(SCHEME, FWD6_ID_VALUE[i])), FWD6_MARKET_QUOTES[i]); } ALL_QUOTES = builder.build(); IList <CurveNode[]> groupNodes = new List <CurveNode[]>(); groupNodes.Add(ALL_NODES); CURVES_NODES.Add(groupNodes); IList <CurveMetadata> groupMetadata = new List <CurveMetadata>(); groupMetadata.Add(DefaultCurveMetadata.builder().curveName(CURVE_NAME).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.DISCOUNT_FACTOR).dayCount(CURVE_DC).build()); CURVES_METADATA.Add(groupMetadata); }
static CalibrationZeroRateAndDiscountFactorUsd2OisIrsTest() { DSC_NAMES[DSCON_CURVE_NAME] = USD; ISet <Index> usdFedFundSet = new HashSet <Index>(); usdFedFundSet.Add(USD_FED_FUND); IDX_NAMES[DSCON_CURVE_NAME] = usdFedFundSet; ISet <Index> usdLibor3Set = new HashSet <Index>(); usdLibor3Set.Add(USD_LIBOR_3M); IDX_NAMES[FWD3_CURVE_NAME] = usdLibor3Set; double fixingValue = 0.002345; LocalDateDoubleTimeSeries tsBdUsdLibor3M = LocalDateDoubleTimeSeries.builder().put(VAL_DATE_BD, fixingValue).build(); LocalDate fixingDateHo = LocalDate.of(2015, 12, 24); LocalDateDoubleTimeSeries tsHoUsdLibor3M = LocalDateDoubleTimeSeries.builder().put(fixingDateHo, fixingValue).build(); TS_BD_LIBOR3M = ImmutableMarketData.builder(VAL_DATE_BD).addTimeSeries(IndexQuoteId.of(USD_LIBOR_3M), tsBdUsdLibor3M).build(); TS_HO_LIBOR3M = ImmutableMarketData.builder(VAL_DATE_HO).addTimeSeries(IndexQuoteId.of(USD_LIBOR_3M), tsHoUsdLibor3M).build(); for (int i = 0; i < DSC_NB_OIS_NODES; i++) { DSC_NODES[i] = FixedOvernightSwapCurveNode.of(FixedOvernightSwapTemplate.of(Period.ZERO, Tenor.of(DSC_OIS_TENORS[i]), USD_FIXED_1Y_FED_FUND_OIS), QuoteId.of(StandardId.of(SCHEME, DSC_ID_VALUE[i]))); } FWD3_NODES[0] = IborFixingDepositCurveNode.of(IborFixingDepositTemplate.of(USD_LIBOR_3M), QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[0]))); for (int i = 0; i < FWD3_NB_FRA_NODES; i++) { FWD3_NODES[i + 1] = FraCurveNode.of(FraTemplate.of(FWD3_FRA_TENORS[i], USD_LIBOR_3M), QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[i + 1]))); } for (int i = 0; i < FWD3_NB_IRS_NODES; i++) { FWD3_NODES[i + 1 + FWD3_NB_FRA_NODES] = FixedIborSwapCurveNode.of(FixedIborSwapTemplate.of(Period.ZERO, Tenor.of(FWD3_IRS_TENORS[i]), USD_FIXED_6M_LIBOR_3M), QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[i + 1 + FWD3_NB_FRA_NODES]))); } ImmutableMarketDataBuilder builder = ImmutableMarketData.builder(VAL_DATE_BD); for (int i = 0; i < FWD3_NB_NODES; i++) { builder.addValue(QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[i])), FWD3_MARKET_QUOTES[i]); } for (int i = 0; i < DSC_NB_NODES; i++) { builder.addValue(QuoteId.of(StandardId.of(SCHEME, DSC_ID_VALUE[i])), DSC_MARKET_QUOTES[i]); } ALL_QUOTES_BD = builder.build(); IList <CurveNode[]> groupDsc = new List <CurveNode[]>(); groupDsc.Add(DSC_NODES); CURVES_NODES.Add(groupDsc); IList <CurveNode[]> groupFwd3 = new List <CurveNode[]>(); groupFwd3.Add(FWD3_NODES); CURVES_NODES.Add(groupFwd3); IList <CurveMetadata> groupDsc = new List <CurveMetadata>(); groupDsc.Add(DefaultCurveMetadata.builder().curveName(DSCON_CURVE_NAME).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).dayCount(CURVE_DC).build()); CURVES_METADATA.Add(groupDsc); IList <CurveMetadata> groupFwd3 = new List <CurveMetadata>(); groupFwd3.Add(DefaultCurveMetadata.builder().curveName(FWD3_CURVE_NAME).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).dayCount(CURVE_DC).build()); CURVES_METADATA.Add(groupFwd3); }
/// <summary> /// Creates an instance from year fraction and zero rate values. /// </summary> /// <param name="currency"> the currency </param> /// <param name="valuationDate"> the valuation date </param> /// <param name="curveName"> the curve name </param> /// <param name="yearFractions"> the year fractions </param> /// <param name="zeroRates"> the zero rates </param> /// <param name="dayCount"> the day count </param> /// <returns> the instance </returns> public static IsdaCreditDiscountFactors of(Currency currency, LocalDate valuationDate, CurveName curveName, DoubleArray yearFractions, DoubleArray zeroRates, DayCount dayCount) { ArgChecker.notNull(yearFractions, "yearFractions"); ArgChecker.notNull(zeroRates, "zeroRates"); DefaultCurveMetadata metadata = DefaultCurveMetadata.builder().xValueType(YEAR_FRACTION).yValueType(ZERO_RATE).curveName(curveName).dayCount(dayCount).build(); NodalCurve curve = (yearFractions.size() == 1 && zeroRates.size() == 1) ? ConstantNodalCurve.of(metadata, yearFractions.get(0), zeroRates.get(0)) : InterpolatedNodalCurve.of(metadata, yearFractions, zeroRates, CurveInterpolators.PRODUCT_LINEAR, CurveExtrapolators.FLAT, CurveExtrapolators.PRODUCT_LINEAR); return(new IsdaCreditDiscountFactors(currency, valuationDate, curve)); }
public virtual void test_of_badCurve() { InterpolatedNodalCurve notYearFraction = InterpolatedNodalCurve.of(Curves.prices(NAME), DoubleArray.of(0, 10), DoubleArray.of(1, 2), INTERPOLATOR); InterpolatedNodalCurve notDiscountFactor = InterpolatedNodalCurve.of(Curves.zeroRates(NAME, ACT_365F), DoubleArray.of(0, 10), DoubleArray.of(1, 2), INTERPOLATOR); CurveMetadata noDayCountMetadata = DefaultCurveMetadata.builder().curveName(NAME).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.DISCOUNT_FACTOR).build(); InterpolatedNodalCurve notDayCount = InterpolatedNodalCurve.of(noDayCountMetadata, DoubleArray.of(0, 10), DoubleArray.of(1, 2), INTERPOLATOR); assertThrowsIllegalArg(() => SimpleDiscountFactors.of(GBP, DATE_VAL, notYearFraction)); assertThrowsIllegalArg(() => SimpleDiscountFactors.of(GBP, DATE_VAL, notDiscountFactor)); assertThrowsIllegalArg(() => SimpleDiscountFactors.of(GBP, DATE_VAL, notDayCount)); }
public virtual void test_of_fail() { DefaultCurveMetadata metadata = DefaultCurveMetadata.builder().xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).curveName("yieldUsd").build(); InterpolatedNodalCurve curveNoDcc = InterpolatedNodalCurve.of(metadata, TIME, RATE, CurveInterpolators.PRODUCT_LINEAR, CurveExtrapolators.FLAT, CurveExtrapolators.PRODUCT_LINEAR); assertThrowsIllegalArg(() => IsdaCreditDiscountFactors.of(USD, VALUATION, curveNoDcc)); InterpolatedNodalCurve curveWrongLeft = InterpolatedNodalCurve.of(METADATA, TIME, RATE, CurveInterpolators.PRODUCT_LINEAR, CurveExtrapolators.PRODUCT_LINEAR, CurveExtrapolators.PRODUCT_LINEAR); assertThrowsIllegalArg(() => IsdaCreditDiscountFactors.of(USD, VALUATION, curveWrongLeft)); InterpolatedNodalCurve curveWrongInterp = InterpolatedNodalCurve.of(METADATA, TIME, RATE, CurveInterpolators.NATURAL_SPLINE, CurveExtrapolators.FLAT, CurveExtrapolators.PRODUCT_LINEAR); assertThrowsIllegalArg(() => IsdaCreditDiscountFactors.of(USD, VALUATION, curveWrongInterp)); InterpolatedNodalCurve curveWrongRight = InterpolatedNodalCurve.of(METADATA, TIME, RATE, CurveInterpolators.PRODUCT_LINEAR, CurveExtrapolators.FLAT, CurveExtrapolators.FLAT); assertThrowsIllegalArg(() => IsdaCreditDiscountFactors.of(USD, VALUATION, curveWrongRight)); }
public virtual void test_of_badCurve() { InterpolatedNodalCurve notYearFraction = InterpolatedNodalCurve.of(Curves.prices(NAME), DoubleArray.of(0, 10), DoubleArray.of(1, 2), INTERPOLATOR); InterpolatedNodalCurve notZeroRate = InterpolatedNodalCurve.of(Curves.discountFactors(NAME, ACT_365F), DoubleArray.of(0, 10), DoubleArray.of(1, 2), INTERPOLATOR); CurveMetadata noDayCountMetadata = DefaultCurveMetadata.builder().curveName(NAME).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).addInfo(CurveInfoType.COMPOUNDING_PER_YEAR, 4).build(); InterpolatedNodalCurve notDayCount = InterpolatedNodalCurve.of(noDayCountMetadata, DoubleArray.of(0, 10), DoubleArray.of(1, 2), INTERPOLATOR); CurveMetadata metaNoCompoundPerYear = DefaultCurveMetadata.builder().curveName(NAME).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).dayCount(ACT_365F).build(); InterpolatedNodalCurve notCompoundPerYear = InterpolatedNodalCurve.of(metaNoCompoundPerYear, DoubleArray.of(0, 10), DoubleArray.of(1, 2), INTERPOLATOR); CurveMetadata metaNegativeNb = DefaultCurveMetadata.builder().curveName(NAME).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).dayCount(ACT_365F).addInfo(CurveInfoType.COMPOUNDING_PER_YEAR, -1).build(); InterpolatedNodalCurve curveNegativeNb = InterpolatedNodalCurve.of(metaNegativeNb, DoubleArray.of(0, 10), DoubleArray.of(1, 2), INTERPOLATOR); assertThrowsIllegalArg(() => ZeroRatePeriodicDiscountFactors.of(GBP, DATE_VAL, notYearFraction)); assertThrowsIllegalArg(() => ZeroRatePeriodicDiscountFactors.of(GBP, DATE_VAL, notZeroRate)); assertThrowsIllegalArg(() => ZeroRatePeriodicDiscountFactors.of(GBP, DATE_VAL, notDayCount)); assertThrowsIllegalArg(() => ZeroRatePeriodicDiscountFactors.of(GBP, DATE_VAL, notCompoundPerYear)); assertThrowsIllegalArg(() => ZeroRatePeriodicDiscountFactors.of(GBP, DATE_VAL, curveNegativeNb)); }
static CurveSensitivityUtilsJacobianTest() { Tenor[] tenors = new Tenor[] { Tenor.TENOR_1D, Tenor.TENOR_1M, Tenor.TENOR_3M, Tenor.TENOR_6M, Tenor.TENOR_1Y, Tenor.TENOR_2Y, Tenor.TENOR_3Y, Tenor.TENOR_4Y, Tenor.TENOR_5Y, Tenor.TENOR_7Y, Tenor.TENOR_10Y, Tenor.TENOR_15Y, Tenor.TENOR_20Y, Tenor.TENOR_30Y }; IList <TenorParameterMetadata> metadataList = new List <TenorParameterMetadata>(); for (int looptenor = 0; looptenor < tenors.Length; looptenor++) { metadataList.Add(TenorParameterMetadata.of(tenors[looptenor])); } DoubleArray rate_eur = DoubleArray.of(0.0160, 0.0165, 0.0155, 0.0155, 0.0155, 0.0150, 0.0150, 0.0160, 0.0165, 0.0155, 0.0155, 0.0155, 0.0150, 0.0140); InterpolatedNodalCurve curve_single_eur = InterpolatedNodalCurve.builder().metadata(DefaultCurveMetadata.builder().curveName(EUR_SINGLE_NAME).parameterMetadata(metadataList).dayCount(ACT_365F).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).build()).xValues(TIME_EUR).yValues(rate_eur).extrapolatorLeft(CurveExtrapolators.FLAT).extrapolatorRight(CurveExtrapolators.FLAT).interpolator(CurveInterpolators.LINEAR).build(); MULTICURVE_EUR_SINGLE_INPUT = ImmutableRatesProvider.builder(VALUATION_DATE).discountCurve(EUR, curve_single_eur).iborIndexCurve(EUR_EURIBOR_6M, curve_single_eur).build(); LIST_CURVE_NAMES_1.Add(CurveParameterSize.of(EUR_SINGLE_NAME, TIME_EUR.size())); }
static CalibrationDiscountFactorUsd2FomcDatesOisIrsTest() { DSC_NAMES[DSCON_CURVE_NAME] = USD; ISet <Index> usdFedFundSet = new HashSet <Index>(); usdFedFundSet.Add(USD_FED_FUND); IDX_NAMES[DSCON_CURVE_NAME] = usdFedFundSet; ISet <Index> usdLibor3Set = new HashSet <Index>(); usdLibor3Set.Add(USD_LIBOR_3M); IDX_NAMES[FWD3_CURVE_NAME] = usdLibor3Set; double fixingValue = 0.002345; LocalDateDoubleTimeSeries tsBdUsdLibor3M = LocalDateDoubleTimeSeries.builder().put(VAL_DATE_BD, fixingValue).build(); TS_BD_LIBOR3M = ImmutableMarketData.builder(VAL_DATE_BD).addTimeSeries(IndexQuoteId.of(USD_LIBOR_3M), tsBdUsdLibor3M).build(); for (int i = 0; i < DSC_NB_DEPO_NODES; i++) { BusinessDayAdjustment bda = BusinessDayAdjustment.of(FOLLOWING, USNY); TermDepositConvention convention = ImmutableTermDepositConvention.of("USD-Dep", USD, bda, ACT_360, DaysAdjustment.ofBusinessDays(DSC_DEPO_OFFSET[i], USNY)); LocalDate nodeDate = FOMC_NODES[i]; if (nodeDate != null) { DSC_NODES[i] = TermDepositCurveNode.builder().template(TermDepositTemplate.of(Period.ofDays(1), convention)).rateId(QuoteId.of(StandardId.of(SCHEME, DSC_ID_VALUE[i]))).date(CurveNodeDate.of(nodeDate)).build(); } else { DSC_NODES[i] = TermDepositCurveNode.of(TermDepositTemplate.of(Period.ofDays(1), convention), QuoteId.of(StandardId.of(SCHEME, DSC_ID_VALUE[i]))); } } for (int i = 0; i < DSC_NB_OIS_NODES; i++) { LocalDate nodeDate = FOMC_NODES[DSC_NB_DEPO_NODES + i]; if (nodeDate != null) { DSC_NODES[DSC_NB_DEPO_NODES + i] = FixedOvernightSwapCurveNode.builder().template(FixedOvernightSwapTemplate.of(Period.ZERO, Tenor.of(DSC_OIS_TENORS[i]), USD_FIXED_1Y_FED_FUND_OIS)).rateId(QuoteId.of(StandardId.of(SCHEME, DSC_ID_VALUE[DSC_NB_DEPO_NODES + i]))).date(CurveNodeDate.of(nodeDate)).build(); } else { DSC_NODES[DSC_NB_DEPO_NODES + i] = FixedOvernightSwapCurveNode.of(FixedOvernightSwapTemplate.of(Period.ZERO, Tenor.of(DSC_OIS_TENORS[i]), USD_FIXED_1Y_FED_FUND_OIS), QuoteId.of(StandardId.of(SCHEME, DSC_ID_VALUE[DSC_NB_DEPO_NODES + i]))); } } FWD3_NODES[0] = IborFixingDepositCurveNode.of(IborFixingDepositTemplate.of(USD_LIBOR_3M), QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[0]))); for (int i = 0; i < FWD3_NB_FRA_NODES; i++) { FWD3_NODES[i + 1] = FraCurveNode.of(FraTemplate.of(FWD3_FRA_TENORS[i], USD_LIBOR_3M), QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[i + 1]))); } for (int i = 0; i < FWD3_NB_IRS_NODES; i++) { FWD3_NODES[i + 1 + FWD3_NB_FRA_NODES] = FixedIborSwapCurveNode.of(FixedIborSwapTemplate.of(Period.ZERO, Tenor.of(FWD3_IRS_TENORS[i]), USD_FIXED_6M_LIBOR_3M), QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[i + 1 + FWD3_NB_FRA_NODES]))); } ImmutableMarketDataBuilder builder = ImmutableMarketData.builder(VAL_DATE_BD); for (int i = 0; i < FWD3_NB_NODES; i++) { builder.addValue(QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[i])), FWD3_MARKET_QUOTES[i]); } for (int i = 0; i < DSC_NB_NODES; i++) { builder.addValue(QuoteId.of(StandardId.of(SCHEME, DSC_ID_VALUE[i])), DSC_MARKET_QUOTES[i]); } ALL_QUOTES_BD = builder.build(); IList <CurveNode[]> groupDsc = new List <CurveNode[]>(); groupDsc.Add(DSC_NODES); CURVES_NODES.Add(groupDsc); IList <CurveNode[]> groupFwd3 = new List <CurveNode[]>(); groupFwd3.Add(FWD3_NODES); CURVES_NODES.Add(groupFwd3); IList <CurveMetadata> groupDsc = new List <CurveMetadata>(); groupDsc.Add(DefaultCurveMetadata.builder().curveName(DSCON_CURVE_NAME).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).dayCount(CURVE_DC).build()); CURVES_METADATA.Add(groupDsc); IList <CurveMetadata> groupFwd3 = new List <CurveMetadata>(); groupFwd3.Add(DefaultCurveMetadata.builder().curveName(FWD3_CURVE_NAME).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).dayCount(CURVE_DC).build()); CURVES_METADATA.Add(groupFwd3); }
//------------------------------------------------------------------------- public override NodalCurve calibrate(IList <ResolvedCdsTrade> calibrationCDSs, DoubleArray flactionalSpreads, DoubleArray pointsUpfront, CurveName name, LocalDate valuationDate, CreditDiscountFactors discountFactors, RecoveryRates recoveryRates, ReferenceData refData) { int n = calibrationCDSs.Count; double[] guess = new double[n]; double[] t = new double[n]; double[] lgd = new double[n]; for (int i = 0; i < n; i++) { LocalDate endDate = calibrationCDSs[i].Product.ProtectionEndDate; t[i] = discountFactors.relativeYearFraction(endDate); lgd[i] = 1d - recoveryRates.recoveryRate(endDate); guess[i] = (flactionalSpreads.get(i) + pointsUpfront.get(i) / t[i]) / lgd[i]; } DoubleArray times = DoubleArray.ofUnsafe(t); CurveMetadata baseMetadata = DefaultCurveMetadata.builder().xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).curveName(name).dayCount(discountFactors.DayCount).build(); NodalCurve creditCurve = n == 1 ? ConstantNodalCurve.of(baseMetadata, t[0], guess[0]) : InterpolatedNodalCurve.of(baseMetadata, times, DoubleArray.ofUnsafe(guess), CurveInterpolators.PRODUCT_LINEAR, CurveExtrapolators.FLAT, CurveExtrapolators.PRODUCT_LINEAR); for (int i = 0; i < n; i++) { ResolvedCds cds = calibrationCDSs[i].Product; LocalDate stepinDate = cds.StepinDateOffset.adjust(valuationDate, refData); LocalDate effectiveStartDate = cds.calculateEffectiveStartDate(stepinDate); LocalDate settlementDate = calibrationCDSs[i].Info.SettlementDate.orElse(cds.SettlementDateOffset.adjust(valuationDate, refData)); double accrued = cds.accruedYearFraction(stepinDate); Pricer pricer = new Pricer(this, cds, discountFactors, times, flactionalSpreads.get(i), pointsUpfront.get(i), lgd[i], stepinDate, effectiveStartDate, settlementDate, accrued); System.Func <double, double> func = pricer.getPointFunction(i, creditCurve); switch (ArbitrageHandling) { case IGNORE: { try { double[] bracket = BRACKETER.getBracketedPoints(func, 0.8 * guess[i], 1.25 * guess[i], double.NegativeInfinity, double.PositiveInfinity); double zeroRate = bracket[0] > bracket[1] ? ROOTFINDER.getRoot(func, bracket[1], bracket[0]) : ROOTFINDER.getRoot(func, bracket[0], bracket[1]); //Negative guess handled creditCurve = creditCurve.withParameter(i, zeroRate); } //JAVA TO C# CONVERTER WARNING: 'final' catch parameters are not available in C#: //ORIGINAL LINE: catch (final com.opengamma.strata.math.MathException e) catch (MathException e) { //handling bracketing failure due to small survival probability if (Math.Abs(func(creditCurve.YValues.get(i - 1))) < 1.e-12) { creditCurve = creditCurve.withParameter(i, creditCurve.YValues.get(i - 1)); } else { throw new MathException(e); } } break; } case FAIL: { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final double minValue = i == 0 ? 0d : creditCurve.getYValues().get(i - 1) * creditCurve.getXValues().get(i - 1) / creditCurve.getXValues().get(i); double minValue = i == 0 ? 0d : creditCurve.YValues.get(i - 1) * creditCurve.XValues.get(i - 1) / creditCurve.XValues.get(i); if (i > 0 && func(minValue) > 0.0) { //can never fail on the first spread //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder(); if (pointsUpfront.get(i) == 0.0) { msg.Append("The par spread of " + flactionalSpreads.get(i) + " at index " + i); } else { msg.Append("The premium of " + flactionalSpreads.get(i) + "and points up-front of " + pointsUpfront.get(i) + " at index " + i); } msg.Append(" is an arbitrage; cannot fit a curve with positive forward hazard rate. "); throw new System.ArgumentException(msg.ToString()); } guess[i] = Math.Max(minValue, guess[i]); double[] bracket = BRACKETER.getBracketedPoints(func, guess[i], 1.2 * guess[i], minValue, double.PositiveInfinity); double zeroRate = ROOTFINDER.getRoot(func, bracket[0], bracket[1]).Value; creditCurve = creditCurve.withParameter(i, zeroRate); break; } case ZERO_HAZARD_RATE: { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final double minValue = i == 0 ? 0.0 : creditCurve.getYValues().get(i - 1) * creditCurve.getXValues().get(i - 1) / creditCurve.getXValues().get(i); double minValue = i == 0 ? 0.0 : creditCurve.YValues.get(i - 1) * creditCurve.XValues.get(i - 1) / creditCurve.XValues.get(i); if (i > 0 && func(minValue) > 0.0) { //can never fail on the first spread creditCurve = creditCurve.withParameter(i, minValue); } else { guess[i] = Math.Max(minValue, guess[i]); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final double[] bracket = BRACKETER.getBracketedPoints(func, guess[i], 1.2 * guess[i], minValue, Double.POSITIVE_INFINITY); double[] bracket = BRACKETER.getBracketedPoints(func, guess[i], 1.2 * guess[i], minValue, double.PositiveInfinity); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final double zeroRate = ROOTFINDER.getRoot(func, bracket[0], bracket[1]); double zeroRate = ROOTFINDER.getRoot(func, bracket[0], bracket[1]).Value; creditCurve = creditCurve.withParameter(i, zeroRate); } break; } default: throw new System.ArgumentException("unknown case " + ArbitrageHandling); } } return(creditCurve); }
static CalibrationZeroRateUsd3OisIrsBsTest() { DSC_NAMES[DSCON_CURVE_NAME] = USD; ISet <Index> usdFedFundSet = new HashSet <Index>(); usdFedFundSet.Add(USD_FED_FUND); IDX_NAMES[DSCON_CURVE_NAME] = usdFedFundSet; ISet <Index> usdLibor3Set = new HashSet <Index>(); usdLibor3Set.Add(USD_LIBOR_3M); IDX_NAMES[FWD3_CURVE_NAME] = usdLibor3Set; ISet <Index> usdLibor6Set = new HashSet <Index>(); usdLibor6Set.Add(USD_LIBOR_6M); IDX_NAMES[FWD6_CURVE_NAME] = usdLibor6Set; for (int i = 0; i < DSC_NB_DEPO_NODES; i++) { BusinessDayAdjustment bda = BusinessDayAdjustment.of(FOLLOWING, USNY); TermDepositConvention convention = ImmutableTermDepositConvention.of("USD-Dep", USD, bda, ACT_360, DaysAdjustment.ofBusinessDays(DSC_DEPO_OFFSET[i], USNY)); DSC_NODES[i] = TermDepositCurveNode.of(TermDepositTemplate.of(Period.ofDays(1), convention), QuoteId.of(StandardId.of(SCHEME, DSC_ID_VALUE[i]))); } for (int i = 0; i < DSC_NB_OIS_NODES; i++) { DSC_NODES[DSC_NB_DEPO_NODES + i] = FixedOvernightSwapCurveNode.of(FixedOvernightSwapTemplate.of(Period.ZERO, Tenor.of(DSC_OIS_TENORS[i]), USD_FIXED_1Y_FED_FUND_OIS), QuoteId.of(StandardId.of(SCHEME, DSC_ID_VALUE[DSC_NB_DEPO_NODES + i]))); } FWD3_NODES[0] = IborFixingDepositCurveNode.of(IborFixingDepositTemplate.of(USD_LIBOR_3M), QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[0]))); for (int i = 0; i < FWD3_NB_FRA_NODES; i++) { FWD3_NODES[i + 1] = FraCurveNode.of(FraTemplate.of(FWD3_FRA_TENORS[i], USD_LIBOR_3M), QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[i + 1]))); } for (int i = 0; i < FWD3_NB_IRS_NODES; i++) { FWD3_NODES[i + 1 + FWD3_NB_FRA_NODES] = FixedIborSwapCurveNode.of(FixedIborSwapTemplate.of(Period.ZERO, Tenor.of(FWD3_IRS_TENORS[i]), USD_FIXED_6M_LIBOR_3M), QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[i + 1 + FWD3_NB_FRA_NODES]))); } FWD6_NODES[0] = IborFixingDepositCurveNode.of(IborFixingDepositTemplate.of(USD_LIBOR_6M), QuoteId.of(StandardId.of(SCHEME, FWD6_ID_VALUE[0]))); for (int i = 0; i < FWD6_NB_FRA_NODES; i++) { FWD6_NODES[i + 1] = FraCurveNode.of(FraTemplate.of(FWD6_FRA_TENORS[i], USD_LIBOR_6M), QuoteId.of(StandardId.of(SCHEME, FWD6_ID_VALUE[i + 1]))); } for (int i = 0; i < FWD6_NB_IRS_NODES; i++) { FWD6_NODES[i + 1 + FWD6_NB_FRA_NODES] = IborIborSwapCurveNode.of(IborIborSwapTemplate.of(Period.ZERO, Tenor.of(FWD6_IRS_TENORS[i]), USD_LIBOR_3M_LIBOR_6M), QuoteId.of(StandardId.of(SCHEME, FWD6_ID_VALUE[i + 1 + FWD6_NB_FRA_NODES]))); } ImmutableMarketDataBuilder builder = ImmutableMarketData.builder(VAL_DATE); for (int i = 0; i < DSC_NB_NODES; i++) { builder.addValue(QuoteId.of(StandardId.of(SCHEME, DSC_ID_VALUE[i])), DSC_MARKET_QUOTES[i]); } for (int i = 0; i < FWD3_NB_NODES; i++) { builder.addValue(QuoteId.of(StandardId.of(SCHEME, FWD3_ID_VALUE[i])), FWD3_MARKET_QUOTES[i]); } for (int i = 0; i < FWD6_NB_NODES; i++) { builder.addValue(QuoteId.of(StandardId.of(SCHEME, FWD6_ID_VALUE[i])), FWD6_MARKET_QUOTES[i]); } ALL_QUOTES = builder.build(); IList <CurveNode[]> groupDsc = new List <CurveNode[]>(); groupDsc.Add(DSC_NODES); CURVES_NODES.Add(groupDsc); IList <CurveNode[]> groupFwd3 = new List <CurveNode[]>(); groupFwd3.Add(FWD3_NODES); CURVES_NODES.Add(groupFwd3); IList <CurveNode[]> groupFwd6 = new List <CurveNode[]>(); groupFwd6.Add(FWD6_NODES); CURVES_NODES.Add(groupFwd6); IList <CurveMetadata> groupDsc = new List <CurveMetadata>(); groupDsc.Add(DefaultCurveMetadata.builder().curveName(DSCON_CURVE_NAME).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).dayCount(CURVE_DC).build()); CURVES_METADATA.Add(groupDsc); IList <CurveMetadata> groupFwd3 = new List <CurveMetadata>(); groupFwd3.Add(DefaultCurveMetadata.builder().curveName(FWD3_CURVE_NAME).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).dayCount(CURVE_DC).build()); CURVES_METADATA.Add(groupFwd3); IList <CurveMetadata> groupFwd6 = new List <CurveMetadata>(); groupFwd6.Add(DefaultCurveMetadata.builder().curveName(FWD6_CURVE_NAME).xValueType(ValueType.YEAR_FRACTION).yValueType(ValueType.ZERO_RATE).dayCount(CURVE_DC).build()); CURVES_METADATA.Add(groupFwd6); }