@Test
 public void presentValueCurveSensitivity() {
   final InterestRateCurveSensitivity pvcsComputed =
       METHOD_SECURITY.presentValueCurveSensitivity(BILL_IAM_SEC, CURVE_BUNDLE);
   assertEquals(
       "Bill Security: present value curve sensitivity",
       1,
       pvcsComputed.getSensitivities().size());
   assertEquals(
       "Bill Security: present value curve sensitivity",
       1,
       pvcsComputed.getSensitivities().get(NAME_CURVES[1]).size());
   final double deltaTolerancePrice = 1.0E+2;
   // Testing note: Sensitivity is for a movement of 1. 1E+2 = 0.01 unit for a 1 bp move.
   final double deltaShift = 1.0E-6;
   // Credit curve sensitivity
   final String bumpedCurveName = "Bumped Curve";
   final BillSecurity billBumped =
       BILL_IAM_SEC_DEFINITION.toDerivative(REFERENCE_DATE, NAME_CURVES[0], bumpedCurveName);
   final double[] nodeTimes = new double[] {billBumped.getEndTime()};
   final double[] sensi =
       SensitivityFiniteDifference.curveSensitivity(
           billBumped,
           CURVE_BUNDLE,
           NAME_CURVES[1],
           bumpedCurveName,
           nodeTimes,
           deltaShift,
           METHOD_SECURITY);
   final List<DoublesPair> sensiPv = pvcsComputed.getSensitivities().get(NAME_CURVES[1]);
   for (int loopnode = 0; loopnode < sensi.length; loopnode++) {
     final DoublesPair pairPv = sensiPv.get(loopnode);
     assertEquals(
         "Bill Security: curve sensitivity - Node " + loopnode,
         nodeTimes[loopnode],
         pairPv.getFirst(),
         1E-8);
     AssertJUnit.assertEquals(
         "Bill Security: curve sensitivity", pairPv.second, sensi[loopnode], deltaTolerancePrice);
   }
 }
 @Test
 /** Tests the curve sensitivity. */
 public void presentValueCurveSensitivity() {
   InterestRateCurveSensitivity pvsSwaption =
       METHOD_HW_APPROXIMATION.presentValueCurveSensitivity(SWAPTION_PAYER_LONG, BUNDLE_HW);
   pvsSwaption = pvsSwaption.cleaned();
   final double deltaTolerancePrice = 1.0E+4;
   // Testing note: Sensitivity is for a movement of 1. 1E+2 = 1 cent for a 1 bp move. Tolerance
   // increased to cope with numerical imprecision of finite difference.
   final double deltaShift = 1.0E-6;
   // 1. Forward curve sensitivity
   final String bumpedCurveName = "Bumped Curve";
   final SwaptionCashFixedIbor swptBumpedForward =
       SWAPTION_PAYER_LONG_DEFINITION.toDerivative(
           REFERENCE_DATE, new String[] {CURVES_NAME[0], bumpedCurveName});
   DoubleAVLTreeSet forwardTime = new DoubleAVLTreeSet();
   for (int loopcpn = 0;
       loopcpn < SWAPTION_PAYER_LONG.getUnderlyingSwap().getSecondLeg().getNumberOfPayments();
       loopcpn++) {
     CouponIbor cpn =
         (CouponIbor)
             SWAPTION_PAYER_LONG.getUnderlyingSwap().getSecondLeg().getNthPayment(loopcpn);
     forwardTime.add(cpn.getFixingPeriodStartTime());
     forwardTime.add(cpn.getFixingPeriodEndTime());
   }
   double[] nodeTimesForward = forwardTime.toDoubleArray();
   final double[] sensiForwardMethod =
       SensitivityFiniteDifference.curveSensitivity(
           swptBumpedForward,
           BUNDLE_HW,
           CURVES_NAME[1],
           bumpedCurveName,
           nodeTimesForward,
           deltaShift,
           METHOD_HW_APPROXIMATION);
   final List<DoublesPair> sensiPvForward = pvsSwaption.getSensitivities().get(CURVES_NAME[1]);
   for (int loopnode = 0; loopnode < sensiForwardMethod.length; loopnode++) {
     final DoublesPair pairPv = sensiPvForward.get(loopnode);
     assertEquals(
         "Sensitivity swaption pv to forward curve: Node " + loopnode,
         nodeTimesForward[loopnode],
         pairPv.getFirst(),
         1E-8);
     assertEquals(
         "Sensitivity finite difference method: node sensitivity " + loopnode,
         sensiForwardMethod[loopnode],
         pairPv.second,
         deltaTolerancePrice);
   }
   // 2. Discounting curve sensitivity
   final SwaptionCashFixedIbor swptBumpedDisc =
       SWAPTION_PAYER_LONG_DEFINITION.toDerivative(
           REFERENCE_DATE, new String[] {bumpedCurveName, CURVES_NAME[1]});
   DoubleAVLTreeSet discTime = new DoubleAVLTreeSet();
   discTime.add(SWAPTION_PAYER_LONG.getSettlementTime());
   for (int loopcpn = 0;
       loopcpn < SWAPTION_PAYER_LONG.getUnderlyingSwap().getSecondLeg().getNumberOfPayments();
       loopcpn++) {
     CouponIbor cpn =
         (CouponIbor)
             SWAPTION_PAYER_LONG.getUnderlyingSwap().getSecondLeg().getNthPayment(loopcpn);
     discTime.add(cpn.getPaymentTime());
   }
   double[] nodeTimesDisc = discTime.toDoubleArray();
   final double[] sensiDiscMethod =
       SensitivityFiniteDifference.curveSensitivity(
           swptBumpedDisc,
           BUNDLE_HW,
           CURVES_NAME[0],
           bumpedCurveName,
           nodeTimesDisc,
           deltaShift,
           METHOD_HW_APPROXIMATION);
   assertEquals(
       "Sensitivity finite difference method: number of node", 11, sensiDiscMethod.length);
   final List<DoublesPair> sensiPvDisc = pvsSwaption.getSensitivities().get(CURVES_NAME[0]);
   for (int loopnode = 0; loopnode < sensiDiscMethod.length; loopnode++) {
     final DoublesPair pairPv = sensiPvDisc.get(loopnode);
     assertEquals(
         "Sensitivity swaption pv to forward curve: Node " + loopnode,
         nodeTimesDisc[loopnode],
         pairPv.getFirst(),
         1E-8);
     assertEquals(
         "Sensitivity finite difference method: node sensitivity",
         sensiDiscMethod[loopnode],
         pairPv.second,
         deltaTolerancePrice);
   }
 }
 @Test
 /**
  * Test the present value rate sensitivity against a finite difference computation; strike above
  * the cut-off strike. Test sensitivity long/short parity.
  */
 public void testPresentValueSensitivityAboveCutOff() {
   final YieldCurveBundle curves = TestsDataSetsSABR.createCurves1();
   final SABRInterestRateParameters sabrParameter = TestsDataSetsSABR.createSABR1();
   final SABRInterestRateDataBundle sabrBundle =
       new SABRInterestRateDataBundle(sabrParameter, curves);
   InterestRateCurveSensitivity pvsCapLong =
       METHOD.presentValueSensitivity(CAP_HIGH_LONG, sabrBundle);
   final InterestRateCurveSensitivity pvsCapShort =
       METHOD.presentValueSensitivity(CAP_HIGH_SHORT, sabrBundle);
   // Long/short parity
   final InterestRateCurveSensitivity pvsCapShort_1 = pvsCapShort.multipliedBy(-1);
   assertEquals(pvsCapLong.getSensitivities(), pvsCapShort_1.getSensitivities());
   // Present value sensitivity comparison with finite difference.
   final double deltaTolerancePrice = 1.0E-1;
   // Testing note: Sensitivity is for a movement of 1. 1E+2 = 1 cent for a 1 bp move.
   final double deltaShift = 1.0E-7;
   pvsCapLong = pvsCapLong.cleaned();
   final String bumpedCurveName = "Bumped Curve";
   // 1. Forward curve sensitivity
   final String[] CurveNameBumpedForward = {FUNDING_CURVE_NAME, bumpedCurveName};
   final CapFloorIbor capBumpedForward =
       (CapFloorIbor)
           CAP_HIGH_LONG_DEFINITION.toDerivative(REFERENCE_DATE, CurveNameBumpedForward);
   final double[] nodeTimesForward =
       new double[] {
         capBumpedForward.getFixingPeriodStartTime(), capBumpedForward.getFixingPeriodEndTime()
       };
   final double[] sensiForwardMethod =
       SensitivityFiniteDifference.curveSensitivity(
           capBumpedForward,
           SABR_BUNDLE,
           FORWARD_CURVE_NAME,
           bumpedCurveName,
           nodeTimesForward,
           deltaShift,
           METHOD);
   assertEquals(
       "Sensitivity finite difference method: number of node", 2, sensiForwardMethod.length);
   final List<DoublesPair> sensiPvForward = pvsCapLong.getSensitivities().get(FORWARD_CURVE_NAME);
   for (int loopnode = 0; loopnode < sensiForwardMethod.length; loopnode++) {
     final DoublesPair pairPv = sensiPvForward.get(loopnode);
     assertEquals(
         "Sensitivity cap/floor pv to forward curve: Node " + loopnode,
         nodeTimesForward[loopnode],
         pairPv.getFirst(),
         1E-8);
     //      assertEquals("Sensitivity finite difference method: node sensitivity: Node " +
     // loopnode, pairPv.second, sensiForwardMethod[loopnode], deltaTolerancePrice);
   }
   // 2. Discounting curve sensitivity
   final String[] CurveNameBumpedDisc = {bumpedCurveName, FORWARD_CURVE_NAME};
   final CapFloorIbor capBumpedDisc =
       (CapFloorIbor) CAP_HIGH_LONG_DEFINITION.toDerivative(REFERENCE_DATE, CurveNameBumpedDisc);
   final double[] nodeTimesDisc = new double[] {capBumpedDisc.getPaymentTime()};
   final double[] sensiDiscMethod =
       SensitivityFiniteDifference.curveSensitivity(
           capBumpedDisc,
           SABR_BUNDLE,
           FUNDING_CURVE_NAME,
           bumpedCurveName,
           nodeTimesDisc,
           deltaShift,
           METHOD);
   assertEquals("Sensitivity finite difference method: number of node", 1, sensiDiscMethod.length);
   final List<DoublesPair> sensiPvDisc = pvsCapLong.getSensitivities().get(FUNDING_CURVE_NAME);
   for (int loopnode = 0; loopnode < sensiDiscMethod.length; loopnode++) {
     final DoublesPair pairPv = sensiPvDisc.get(loopnode);
     assertEquals(
         "Sensitivity cap/floor pv to forward curve: Node " + loopnode,
         nodeTimesDisc[loopnode],
         pairPv.getFirst(),
         1E-8);
     assertEquals(
         "Sensitivity finite difference method: node sensitivity",
         pairPv.second,
         sensiDiscMethod[loopnode],
         deltaTolerancePrice);
   }
 }