ISDACompliantCurveCalibrator { public ISDACompliantDateCreditCurve calibrateHazardCurve(final LocalDate today, final LocalDate stepinDate, final LocalDate valueDate, final LocalDate startDate, final LocalDate[] endDates, final double[] couponRates, final boolean payAccOnDefault, final Period tenor, final StubType stubType, final boolean protectStart, final ISDACompliantDateYieldCurve yieldCurve, final double recoveryRate) { ArgumentChecker.notNull(today, "null today"); ArgumentChecker.notNull(stepinDate, "null stepinDate"); ArgumentChecker.notNull(valueDate, "null valueDate"); ArgumentChecker.notNull(startDate, "null startDate"); ArgumentChecker.noNulls(endDates, "null endDates"); ArgumentChecker.notEmpty(couponRates, "no or null couponRates"); ArgumentChecker.notNull(tenor, "null tenor"); ArgumentChecker.notNull(stubType, "null stubType"); ArgumentChecker.notNull(yieldCurve, "null yieldCurve"); ArgumentChecker.isInRangeExcludingHigh(0, 1.0, recoveryRate); ArgumentChecker.isFalse(valueDate.isBefore(today), "Require valueDate >= today"); ArgumentChecker.isFalse(stepinDate.isBefore(today), "Require stepin >= today"); final int n = endDates.length; ArgumentChecker.isTrue(n == couponRates.length, "length of couponRates does not match endDates"); final double[] guess = new double[n]; final double[] t = new double[n]; final double lgd = 1 - recoveryRate; for (int i = 0; i < n; i++) { guess[i] = couponRates[i] / lgd; t[i] = ACT_365.getDayCountFraction(today, endDates[i]); } ISDACompliantDateCreditCurve hazardCurve = new ISDACompliantDateCreditCurve(today, endDates, guess); for (int i = 0; i < n; i++) { final CDSPricer func = new CDSPricer(i, today, stepinDate, valueDate, startDate, endDates[i], couponRates[i], protectStart, payAccOnDefault, tenor, stubType, recoveryRate, yieldCurve, hazardCurve); final double[] bracket = BRACKER.getBracketedPoints(func, 0.9 * guess[i], 1.1 * guess[i], 0.0, Double.POSITIVE_INFINITY); final double zeroRate = ROOTFINDER.getRoot(func, bracket[0], bracket[1]); hazardCurve = hazardCurve.withRate(zeroRate, i); } return hazardCurve; }  ISDACompliantDateCreditCurve calibrateHazardCurve(final LocalDate today, final LocalDate stepinDate, final LocalDate valueDate, final LocalDate startDate, final LocalDate[] endDates,
      final double[] couponRates, final boolean payAccOnDefault, final Period tenor, final StubType stubType, final boolean protectStart, final ISDACompliantDateYieldCurve yieldCurve,
      final double recoveryRate);  }
@SuppressWarnings("unused") @Test public void test() { final LocalDate today = LocalDate.of(2013, 2, 2); final LocalDate stepinDate = today.plusDays(1); final LocalDate valueDate = today; final LocalDate startDate = LocalDate.of(2012, 7, 29); final LocalDate[] endDates = new LocalDate[] {LocalDate.of(2013, 6, 20), LocalDate.of(2013, 9, 20), LocalDate.of(2014, 3, 20), LocalDate.of(2015, 3, 20), LocalDate.of(2016, 3, 20), LocalDate.of(2018, 3, 20), LocalDate.of(2023, 3, 20) }; final double[] coupons = new double[] {50, 70, 100, 150, 200, 400, 1000 }; final int n = coupons.length; for (int i = 0; i < n; i++) { coupons[i] /= 10000; } final Period tenor = Period.ofMonths(3); final StubType stubType = StubType.FRONTSHORT; final boolean payAccOndefault = true; final boolean protectionStart = true; final double recovery = 0.4; final ISDACompliantCurveCalibrator calibrator = new ISDACompliantCurveCalibrator(); final ISDACompliantDateCreditCurve hc = calibrator.calibrateHazardCurve(today, stepinDate, valueDate, startDate, endDates, coupons, payAccOndefault, tenor, stubType, protectionStart, YIELD_CURVE, recovery); for (int i = 0; i < n; i++) { final double rpv01 = PRICER.pvPremiumLegPerUnitSpread(today, stepinDate, valueDate, startDate, endDates[i], payAccOndefault, tenor, stubType, YIELD_CURVE, hc, protectionStart, PriceType.CLEAN); final double proLeg = PRICER.calculateProtectionLeg(today, stepinDate, valueDate, startDate, endDates[i], YIELD_CURVE, hc, recovery, protectionStart); final double pv = 1e7 * (proLeg - coupons[i] * rpv01); assertEquals(0.0, pv, 1e-8); } final int warmup = 1; final int benchmark = 0; for (int k = 0; k < warmup; k++) { final ISDACompliantDateCreditCurve hc2 = calibrator.calibrateHazardCurve(today, stepinDate, valueDate, startDate, endDates, coupons, payAccOndefault, tenor, stubType, protectionStart, YIELD_CURVE, recovery); } if (benchmark > 0) { final long t0 = System.nanoTime(); for (int k = 0; k < benchmark; k++) { final ISDACompliantDateCreditCurve hc2 = calibrator.calibrateHazardCurve(today, stepinDate, valueDate, startDate, endDates, coupons, payAccOndefault, tenor, stubType, protectionStart, YIELD_CURVE, recovery); } final long time = System.nanoTime() - t0; final double timePerCalibration = (time) / 1e6 / benchmark; System.out.println("time per calibration: " + timePerCalibration + "ms"); } }