AnalyticBondPricer { public double bondPriceForHazardRate(final BondAnalytic bond, final ISDACompliantYieldCurve yieldCurve, final double hazardRate, final PriceType cleanOrDirty) { return getBondPriceForHazardRateFunction(bond, yieldCurve, cleanOrDirty).evaluate(hazardRate); }  double getEquivalentCDSSpread(final BondAnalytic bond, final ISDACompliantYieldCurve yieldCurve, final double bondPrice, final PriceType cleanOrDirty, final CDSAnalytic cds); double getHazardRate(final BondAnalytic bond, final ISDACompliantYieldCurve yieldCurve, final double bondPrice, final PriceType cleanOrDirty); double bondPriceForHazardRate(final BondAnalytic bond, final ISDACompliantYieldCurve yieldCurve, final double hazardRate, final PriceType cleanOrDirty); Function1D<Double, Double> getBondPriceForHazardRateFunction(final BondAnalytic bond, final ISDACompliantYieldCurve yieldCurve, final PriceType cleanOrDirty);  }
@Test public void bondPriceTest2() { final ISDACompliantYieldCurve yieldCurve = YieldCurveProvider.ISDA_USD_20140205; final double recoveryRate = 0.27; final Period couponPeriod = Period.ofMonths(6); final StubType stub = StubType.FRONTSHORT; final BusinessDayConvention bd = FOLLOWING; final Calendar cal = DEFAULT_CALENDAR; final CDSAnalyticFactory factory = new CDSAnalyticFactory(0.0).with(couponPeriod).withPayAccOnDefault(false); final LocalDate startDate = LocalDate.of(2013, 9, 20); final LocalDate endDate = LocalDate.of(2019, 3, 20); final LocalDate tradeDate = LocalDate.of(2014, 2, 5); final double exp = ACT365F.getDayCountFraction(tradeDate, endDate); final CDSAnalytic protectionLegCDS = factory.makeCDS(tradeDate, startDate, endDate); final CDSAnalytic annuityCDS = factory.withProtectionStart(false).makeCDS(tradeDate, startDate, endDate); final double bondCoupon = 0.07; final ISDAPremiumLegSchedule sch = new ISDAPremiumLegSchedule(startDate, endDate, couponPeriod, stub, bd, cal, false); final BondAnalytic bond = new BondAnalytic(tradeDate, bondCoupon, sch, recoveryRate, ACT360); final AnalyticCDSPricer cdsPricer = new AnalyticCDSPricer(); final AnalyticBondPricer bondPricer = new AnalyticBondPricer(); for (int i = 0; i < 10; i++) { final double lambda = 0.0 + 0.3 * i / 9.; final ISDACompliantCreditCurve cc = new ISDACompliantCreditCurve(10.0, lambda); final double bondPrice = bondPricer.bondPriceForHazardRate(bond, yieldCurve, lambda, PriceType.DIRTY); final double cdsProtLeg = cdsPricer.protectionLeg(protectionLegCDS, yieldCurve, cc, 0.0); final double cdsAnnuity = cdsPricer.annuity(annuityCDS, yieldCurve, cc, PriceType.DIRTY, 0.0); final double q = cc.getSurvivalProbability(exp); final double p = yieldCurve.getDiscountFactor(exp); final double bondPriceAsCDS = cdsAnnuity * bondCoupon + q * p + recoveryRate * cdsProtLeg; assertEquals(bondPriceAsCDS, bondPrice, 1e-15); } }