HedgeRatioCalculator { public DoubleMatrix1D getHedgeRatios(final CDSAnalytic cds, final double coupon, final CDSAnalytic[] hedgeCDSs, final double[] hedgeCDSCoupons, final double[] hegdeCDSPUF, final ISDACompliantYieldCurve yieldCurve) { final ISDACompliantCreditCurve cc = _builder.calibrateCreditCurve(hedgeCDSs, hedgeCDSCoupons, yieldCurve, hegdeCDSPUF); return getHedgeRatios(cds, coupon, hedgeCDSs, hedgeCDSCoupons, cc, yieldCurve); } HedgeRatioCalculator();  HedgeRatioCalculator(AccrualOnDefaultFormulae formula); DoubleMatrix1D getCurveSensitivities(final CDSAnalytic cds, final double coupon, final ISDACompliantCreditCurve creditCurve, final ISDACompliantYieldCurve yieldCurve); DoubleMatrix2D getCurveSensitivities(final CDSAnalytic[] cds, final double[] coupons, final ISDACompliantCreditCurve creditCurve, final ISDACompliantYieldCurve yieldCurve); DoubleMatrix1D getHedgeRatios(final CDSAnalytic cds, final double coupon, final CDSAnalytic[] hedgeCDSs, final double[] hedgeCDSCoupons, final double[] hegdeCDSPUF,
      final ISDACompliantYieldCurve yieldCurve); DoubleMatrix1D getHedgeRatios(final CDSAnalytic cds, final double coupon, final CDSAnalytic[] hedgeCDSs, final double[] hedgeCDSCoupons, final ISDACompliantCreditCurve creditCurve,
      final ISDACompliantYieldCurve yieldCurve); DoubleMatrix1D getHedgeRatios(final DoubleMatrix1D cdsSensitivities, final DoubleMatrix2D hedgeCDSSensitivities); DoubleMatrix1D getHedgeRatios(final DoubleMatrix1D cdsSensitivities, final LUDecompositionResult luRes);  }
@Test public void test() { final LocalDate accStart = FOLLOWING.adjustDate(DEFAULT_CALENDAR, getPrevIMMDate(TRADE_DATE)); final CDSAnalytic cds = CDS_FACTORY.makeCDS(TRADE_DATE, accStart, MATURITY); final double cdsCoupon = 0.01; final int n = HEDGE_CDS.length; final double[] hedgeCoupons = new double[n]; Arrays.fill(hedgeCoupons, cdsCoupon); final DoubleMatrix1D w = HEDGE_CAL.getHedgeRatios(cds, cdsCoupon, HEDGE_CDS, hedgeCoupons, CREDIT_CURVE, YIELD_CURVE); final double[] expected = new double[] {-1.1842173839714448E-6, 0.36244465818986815, 0.6376106050590048, 0.0, 0.0, 0.0 }; for (int i = 0; i < n; i++) { assertEquals("", expected[i], w.getEntry(i), 1e-15); } double pv = PRICER.pv(cds, YIELD_CURVE, CREDIT_CURVE, cdsCoupon); for (int i = 0; i < n; i++) { pv -= w.getEntry(i) * PRICER.pv(HEDGE_CDS[i], YIELD_CURVE, CREDIT_CURVE, hedgeCoupons[i]); } final double[] t = CREDIT_CURVE.getKnotTimes(); final double[] h = CREDIT_CURVE.getKnotZeroRates(); final double sigma = 0.01; for (int k = 0; k < 200; k++) { final double[] bumpedH = new double[n]; System.arraycopy(h, 0, bumpedH, 0, n); for (int i = 0; i < n; i++) { bumpedH[i] *= Math.exp(sigma * NORMAL.nextRandom() - sigma * sigma / 2); } final ISDACompliantCreditCurve cc = new ISDACompliantCreditCurve(t, bumpedH); double pvBumped = PRICER.pv(cds, YIELD_CURVE, cc, cdsCoupon); for (int i = 0; i < n; i++) { pvBumped -= w.getEntry(i) * PRICER.pv(HEDGE_CDS[i], YIELD_CURVE, cc, hedgeCoupons[i]); } final double change = pvBumped - pv; assertTrue(change > 0 && change < 3e-7); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void constHazardRateTest() { final ISDACompliantCreditCurve flatCC = new ISDACompliantCreditCurve(5.0, 0.02); final LocalDate accStart = FOLLOWING.adjustDate(DEFAULT_CALENDAR, getPrevIMMDate(TRADE_DATE)); final CDSAnalytic cds = CDS_FACTORY.makeCDS(TRADE_DATE, accStart, MATURITY); final double cdsCoupon = 0.01; final int n = HEDGE_CDS.length; final double[] hedgeCoupons = new double[n]; Arrays.fill(hedgeCoupons, cdsCoupon); final DoubleMatrix1D w = HEDGE_CAL.getHedgeRatios(cds, cdsCoupon, HEDGE_CDS, hedgeCoupons, flatCC, YIELD_CURVE); System.out.println(w); }
@Test public void lessCDStest() { final LocalDate accStart = FOLLOWING.adjustDate(DEFAULT_CALENDAR, getPrevIMMDate(TRADE_DATE)); final CDSAnalytic cds = CDS_FACTORY.makeCDS(TRADE_DATE, accStart, MATURITY); final double cdsCoupon = 0.01; final CDSAnalytic[] hedgeCDS = CDS_FACTORY.makeIMMCDS(TRADE_DATE, new Period[] {Period.ofYears(1), Period.ofYears(5) }); final int n = hedgeCDS.length; final double[] hedgeCoupons = new double[n]; Arrays.fill(hedgeCoupons, cdsCoupon); final DoubleMatrix1D w = HEDGE_CAL.getHedgeRatios(cds, cdsCoupon, hedgeCDS, hedgeCoupons, CREDIT_CURVE, YIELD_CURVE); final double[] expected = new double[] {0.3877847710928422, 0.026594401620818442 }; for (int i = 0; i < n; i++) { assertEquals("", expected[i], w.getEntry(i), 1e-15); } }