public void presentValueSABRReplicationCurveSensitivity() {
final YieldCurveBundle curves = TestsDataSetsSABR.createCurves1();
final SABRInterestRateParameters sabrParameter = TestsDataSetsSABR.createSABR1();
final SABRInterestRateDataBundle sabrBundle = new SABRInterestRateDataBundle(sabrParameter, curves);
// Swaption sensitivity
InterestRateCurveSensitivity pvsReceiver = METHOD.presentValueCurveSensitivity(CMS_COUPON_RECEIVER, sabrBundle);
// Present value sensitivity comparison with finite difference.
final double deltaTolerance = 1E+2; //Sensitivity is for a movement of 1. 1E+2 = 1 cent for a 1 bp move.
final double deltaShift = 1e-9;
pvsReceiver = pvsReceiver.cleaned();
final double pv = METHOD.presentValue(CMS_COUPON_RECEIVER, sabrBundle).getAmount();
// 1. Forward curve sensitivity
final String bumpedCurveName = "Bumped Curve";
final String[] bumpedCurvesForwardName = {FUNDING_CURVE_NAME, bumpedCurveName };
final CouponCMS cmsBumpedForward = (CouponCMS) CMS_COUPON_RECEIVER_DEFINITION.toDerivative(REFERENCE_DATE, bumpedCurvesForwardName);
final YieldAndDiscountCurve curveForward = curves.getCurve(FORWARD_CURVE_NAME);
final Set<Double> timeForwardSet = new TreeSet<>();
for (final Payment pay : CMS_COUPON_RECEIVER.getUnderlyingSwap().getSecondLeg().getPayments()) {
final CouponIbor coupon = (CouponIbor) pay;
timeForwardSet.add(coupon.getFixingPeriodStartTime());
timeForwardSet.add(coupon.getFixingPeriodEndTime());
}
final int nbForwardDate = timeForwardSet.size();
final List<Double> timeForwardList = new ArrayList<>(timeForwardSet);
Double[] timeForwardArray = new Double[nbForwardDate];
timeForwardArray = timeForwardList.toArray(timeForwardArray);
final double[] yieldsForward = new double[nbForwardDate + 1];
final double[] nodeTimesForward = new double[nbForwardDate + 1];
yieldsForward[0] = curveForward.getInterestRate(0.0);
for (int i = 0; i < nbForwardDate; i++) {
nodeTimesForward[i + 1] = timeForwardArray[i];
yieldsForward[i + 1] = curveForward.getInterestRate(nodeTimesForward[i + 1]);
}
final YieldAndDiscountCurve tempCurveForward = YieldCurve.from(InterpolatedDoublesCurve.fromSorted(nodeTimesForward, yieldsForward, new LinearInterpolator1D()));
final List<DoublesPair> tempForward = pvsReceiver.getSensitivities().get(FORWARD_CURVE_NAME);
for (int i = 0; i < nbForwardDate; i++) {
final YieldAndDiscountCurve bumpedCurveForward = tempCurveForward.withSingleShift(nodeTimesForward[i + 1], deltaShift);
final YieldCurveBundle curvesBumpedForward = new YieldCurveBundle();
curvesBumpedForward.addAll(curves);
curvesBumpedForward.setCurve("Bumped Curve", bumpedCurveForward);
final SABRInterestRateDataBundle sabrBundleBumped = new SABRInterestRateDataBundle(sabrParameter, curvesBumpedForward);
final double bumpedpv = cmsBumpedForward.accept(PVC_SABR, sabrBundleBumped);
final double res = (bumpedpv - pv) / deltaShift;
final DoublesPair pair = tempForward.get(i);
assertEquals("Node " + i, nodeTimesForward[i + 1], pair.getFirst(), 1E-8);
assertEquals("Node " + i, res, pair.getSecond(), deltaTolerance);
}
// 2. Funding curve sensitivity
final String[] bumpedCurvesFundingName = {bumpedCurveName, FORWARD_CURVE_NAME };
final CouponCMS cmsBumpedFunding = (CouponCMS) CMS_COUPON_RECEIVER_DEFINITION.toDerivative(REFERENCE_DATE, bumpedCurvesFundingName);
final int nbPayDate = CMS_COUPON_RECEIVER_DEFINITION.getUnderlyingSwap().getIborLeg().getPayments().length;
final YieldAndDiscountCurve curveFunding = curves.getCurve(FUNDING_CURVE_NAME);
final double[] yieldsFunding = new double[nbPayDate + 1];
final double[] nodeTimesFunding = new double[nbPayDate + 1];
yieldsFunding[0] = curveFunding.getInterestRate(0.0);
for (int i = 0; i < nbPayDate; i++) {
nodeTimesFunding[i + 1] = CMS_COUPON_RECEIVER.getUnderlyingSwap().getSecondLeg().getNthPayment(i).getPaymentTime();
yieldsFunding[i + 1] = curveFunding.getInterestRate(nodeTimesFunding[i + 1]);
}
final YieldAndDiscountCurve tempCurveFunding = YieldCurve.from(InterpolatedDoublesCurve.fromSorted(nodeTimesFunding, yieldsFunding, new LinearInterpolator1D()));
final List<DoublesPair> tempFunding = pvsReceiver.getSensitivities().get(FUNDING_CURVE_NAME);
final double[] res = new double[nbPayDate];
for (int i = 0; i < nbPayDate; i++) {
final YieldAndDiscountCurve bumpedCurve = tempCurveFunding.withSingleShift(nodeTimesFunding[i + 1], deltaShift);
final YieldCurveBundle curvesBumped = new YieldCurveBundle();
curvesBumped.addAll(curves);