for (int loopcf = 0; loopcf < nbFixed; loopcf++) {
alphaFixed[loopcf] = MODEL.alpha(parameters, 0.0, expiryTime, expiryTime, swap.getFixedLeg().getNthPayment(loopcf).getPaymentTime());
dfFixed[loopcf] = multicurves.getDiscountFactor(ccy, swap.getFixedLeg().getNthPayment(loopcf).getPaymentTime());
discountedCashFlowFixed[loopcf] = dfFixed[loopcf] * swap.getFixedLeg().getNthPayment(loopcf).getPaymentYearFraction() * swap.getFixedLeg().getNthPayment(loopcf).getNotional();
}
final AnnuityPaymentFixed cfeIbor = swap.getSecondLeg().accept(CFEC, multicurves);
final int nbIbor = cfeIbor.getNumberOfPayments();
final double[] alphaIbor = new double[cfeIbor.getNumberOfPayments()];
final double[] dfIbor = new double[cfeIbor.getNumberOfPayments()];
final double[] discountedCashFlowIbor = new double[cfeIbor.getNumberOfPayments()];
for (int loopcf = 0; loopcf < cfeIbor.getNumberOfPayments(); loopcf++) {
alphaIbor[loopcf] = MODEL.alpha(parameters, 0.0, expiryTime, expiryTime, cfeIbor.getNthPayment(loopcf).getPaymentTime());
dfIbor[loopcf] = multicurves.getDiscountFactor(ccy, cfeIbor.getNthPayment(loopcf).getPaymentTime());
discountedCashFlowIbor[loopcf] = dfIbor[loopcf] * cfeIbor.getNthPayment(loopcf).getAmount();
}
final double alphaPayment = MODEL.alpha(parameters, 0.0, expiryTime, expiryTime, cms.getPaymentTime());
final double x0 = -alphaPayment;
final double a0 = MODEL.swapRate(x0, discountedCashFlowFixed, alphaFixed, discountedCashFlowIbor, alphaIbor);
final double a2 = MODEL.swapRateDx2(x0, discountedCashFlowFixed, alphaFixed, discountedCashFlowIbor, alphaIbor);
// final double pv = (a0 + a2 / 2) * dfPayment * cms.getNotional() * cms.getPaymentYearFraction();
// Backward sweep
final double pvBar = 1.0;
final double a2Bar = 0.5 * dfPayment * cms.getNotional() * cms.getPaymentYearFraction() * pvBar;
final double a0Bar = dfPayment * cms.getNotional() * cms.getPaymentYearFraction() * pvBar;
final double[] discountedCashFlowAdjIborBar0 = MODEL.swapRateDdcfi1(x0, discountedCashFlowFixed, alphaFixed, discountedCashFlowIbor, alphaIbor);
final double[] discountedCashFlowAdjFixedBar0 = MODEL.swapRateDdcff1(x0, discountedCashFlowFixed, alphaFixed, discountedCashFlowIbor, alphaIbor);
final Pair<double[], double[]> discountedCashFlowAdjXBar2 = MODEL.swapRateDx2Ddcf1(x0, discountedCashFlowFixed, alphaFixed, discountedCashFlowIbor, alphaIbor);
final double[] discountedCashFlowAdjFixedBar2 = discountedCashFlowAdjXBar2.getFirst();
final double[] discountedCashFlowAdjIborBar2 = discountedCashFlowAdjXBar2.getSecond();
final double[] discountedCashFlowIborBar = new double[nbIbor];
for (int loopcf = 0; loopcf < nbIbor; loopcf++) {
discountedCashFlowIborBar[loopcf] = discountedCashFlowAdjIborBar0[loopcf] * a0Bar + discountedCashFlowAdjIborBar2[loopcf] * a2Bar;
}
final double[] dfIborBar = new double[nbIbor];
for (int loopcf = 0; loopcf < nbIbor; loopcf++) {
dfIborBar[loopcf] = cfeIbor.getNthPayment(loopcf).getAmount() * discountedCashFlowIborBar[loopcf];
}
final double[] cfeIborAmountBar = new double[nbIbor];
for (int loopcf = 0; loopcf < nbIbor; loopcf++) {
cfeIborAmountBar[loopcf] = dfIbor[loopcf] * discountedCashFlowIborBar[loopcf]; // OK
}
final double[] discountedCashFlowFixedBar = new double[nbFixed];
for (int loopcf = 0; loopcf < nbFixed; loopcf++) {
discountedCashFlowFixedBar[loopcf] = discountedCashFlowAdjFixedBar0[loopcf] * a0Bar + discountedCashFlowAdjFixedBar2[loopcf] * a2Bar;
}
final double[] dfFixedBar = new double[nbFixed];
for (int loopcf = 0; loopcf < nbFixed; loopcf++) {
dfFixedBar[loopcf] = swap.getFixedLeg().getNthPayment(loopcf).getPaymentYearFraction() * swap.getFixedLeg().getNthPayment(loopcf).getNotional() *
discountedCashFlowFixedBar[loopcf];
}
final List<DoublesPair> listDfSensi = new ArrayList<>();
for (int loopcf = 0; loopcf < nbIbor; loopcf++) {
final DoublesPair dfSensi = new DoublesPair(cfeIbor.getNthPayment(loopcf).getPaymentTime(), -cfeIbor.getNthPayment(loopcf).getPaymentTime() * dfIbor[loopcf] * dfIborBar[loopcf]);
listDfSensi.add(dfSensi);
}
for (int loopcf = 0; loopcf < nbFixed; loopcf++) {
final DoublesPair dfSensi = new DoublesPair(swap.getFixedLeg().getNthPayment(loopcf).getPaymentTime(), -swap.getFixedLeg().getNthPayment(loopcf).getPaymentTime() * dfFixed[loopcf] *
dfFixedBar[loopcf]);
listDfSensi.add(dfSensi);
}
final Map<String, List<DoublesPair>> pvsDF = new HashMap<>();
pvsDF.put(multicurvesHW.getMulticurveProvider().getName(ccy), listDfSensi);
final double dfPaymentBar = (a0 + a2 / 2) * cms.getNotional() * cms.getPaymentYearFraction() * pvBar;
final DoublesPair dfPaymentSensi = new DoublesPair(payTimeCMS, -payTimeCMS * dfPayment * dfPaymentBar); // Sensi to dfPayment
listDfSensi.add(dfPaymentSensi);
MulticurveSensitivity sensitivity = MulticurveSensitivity.ofYieldDiscounting(pvsDF);
sensitivity = sensitivity.cleaned();
// Sensitivity from the CFE
final Map<Double, MulticurveSensitivity> cfeCurveSensi = swap.accept(CFECSC, multicurvesHW.getMulticurveProvider());
for (int loopcf = 0; loopcf < nbIbor; loopcf++) {
final MulticurveSensitivity sensiCfe = cfeCurveSensi.get(cfeIbor.getNthPayment(loopcf).getPaymentTime());
if (!(sensiCfe == null)) { // There is some sensitivity to that cfe. No sensi of the last cf.
sensitivity = sensitivity.plus(sensiCfe.multipliedBy(cfeIborAmountBar[loopcf]));
}
}
sensitivity = sensitivity.cleaned();