sensi = sensi.cleaned();
final double pv = SWAPTION_LONG_PAYER.accept(PVC, sabrBundle);
// 1. Forward curve sensitivity
final String bumpedCurveName = "Bumped Curve";
final String[] bumpedCurvesForwardName = {FUNDING_CURVE_NAME, bumpedCurveName};
final SwaptionPhysicalFixedIbor swaptionBumpedForward = SWAPTION_DEFINITION_LONG_PAYER.toDerivative(REFERENCE_DATE, bumpedCurvesForwardName);
final YieldAndDiscountCurve curveForward = curves.getCurve(FORWARD_CURVE_NAME);
final Set<Double> timeForwardSet = new TreeSet<>();
for (final Payment pay : SWAPTION_LONG_PAYER.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 = sensi.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 = swaptionBumpedForward.accept(PVC, 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 SwaptionPhysicalFixedIbor swaptionBumpedFunding = SWAPTION_DEFINITION_LONG_PAYER.toDerivative(REFERENCE_DATE, bumpedCurvesFundingName);
final SwapDefinition underlyingSwap = SWAPTION_DEFINITION_LONG_PAYER.getUnderlyingSwap();
AnnuityDefinition<? extends PaymentDefinition> floatLeg;
if (underlyingSwap.getFirstLeg() instanceof AnnuityCouponFixedDefinition) {
floatLeg = underlyingSwap.getSecondLeg();
} else {
floatLeg = underlyingSwap.getFirstLeg();
}
final int nbPayDate = floatLeg.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] = SWAPTION_LONG_PAYER.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 = sensi.getSensitivities().get(FUNDING_CURVE_NAME);
for (int i = 0; i < nbPayDate; i++) {
final YieldAndDiscountCurve bumpedCurve = tempCurveFunding.withSingleShift(nodeTimesFunding[i + 1], deltaShift);
final YieldCurveBundle curvesBumped = new YieldCurveBundle();
curvesBumped.addAll(curves);
curvesBumped.setCurve("Bumped Curve", bumpedCurve);
final SABRInterestRateDataBundle sabrBundleBumped = new SABRInterestRateDataBundle(sabrParameter, curvesBumped);
final double bumpedpv = swaptionBumpedFunding.accept(PVC, sabrBundleBumped);
final double res = (bumpedpv - pv) / deltaShift;
final DoublesPair pair = tempFunding.get(i);
assertEquals("Node " + i, nodeTimesFunding[i + 1], pair.getFirst(), 1E-8);
assertEquals("Node " + i, res, pair.getSecond(), deltaTolerance);
}