IssuerProviderIssuerDecoratedSpreadPeriodic implements IssuerProviderInterface { @Override public double[] parameterSensitivity(final String name, final List<DoublesPair> pointSensitivity) { if (name.equals(getName(_issuer))) { int nPoints = pointSensitivity.size(); List<DoublesPair> pointSensitivityNew = new ArrayList<>(nPoints); for (int i = 0; i < nPoints; ++i) { double time = pointSensitivity.get(i).getFirst(); double eRovN = Math.pow(_issuerProvider.getDiscountFactor(_issuer, time), -1.0 / _nPeriodsPerYear / time); double factor = eRovN / (eRovN + _spread / _nPeriodsPerYear); pointSensitivityNew.add(DoublesPair.of(time, factor * pointSensitivity.get(i).getSecond())); } return _issuerProvider.parameterSensitivity(name, pointSensitivityNew); } return _issuerProvider.parameterSensitivity(name, pointSensitivity); } IssuerProviderIssuerDecoratedSpreadPeriodic(IssuerProviderInterface issuerProvider, LegalEntity issuer,
      double spread, int nPeriodsPerYear); @Override MulticurveProviderInterface getMulticurveProvider(); @Override IssuerProviderInterface getIssuerProvider(); @Override IssuerProviderInterface copy(); @Override double getDiscountFactor(final LegalEntity issuer, final Double time); @Override String getName(final Pair<Object, LegalEntityFilter<LegalEntity>> issuerCcy); @Override String getName(final LegalEntity issuerCcy); @Override Set<String> getAllNames(); @Override double[] parameterSensitivity(final String name, final List<DoublesPair> pointSensitivity); @Override double[] parameterForwardSensitivity(final String name, final List<ForwardSensitivity> pointSensitivity); @Override Integer getNumberOfParameters(final String name); @Override List<String> getUnderlyingCurvesNames(final String name); @Override Set<Pair<Object, LegalEntityFilter<LegalEntity>>> getIssuers(); @Override Set<String> getAllCurveNames(); @Override int hashCode(); @Override boolean equals(final Object obj);  }
@Test public void sensitivityFiniteDifferenceTest() { double eps = 1.0e-7; double spreadSemiannual = -0.02; int nPeriodS = 2; double spreadQuarterly = 0.06; int nPeriodQ = 4; IssuerProviderIssuerDecoratedSpreadPeriodic providerWithSpread = new IssuerProviderIssuerDecoratedSpreadPeriodic( ISSUER_PROVIDER, ISSUER_DSC, spreadSemiannual, nPeriodS); IssuerProviderIssuerDecoratedSpreadPeriodic providerWithSpreadDouble = new IssuerProviderIssuerDecoratedSpreadPeriodic( providerWithSpread, ISSUER_YLD, spreadQuarterly, nPeriodQ); { double[] senseComputed = providerWithSpreadDouble.parameterSensitivity(CURVE_NAME_DSC, POINT_SENSITIVITY_LIST); int nParams = FACTOR_DSC.length; double[] senseExpected = new double[nParams]; for (int i = 0; i < nParams; ++i) { LegalEntityFilter<LegalEntity> filter = new LegalEntityShortName(); IssuerProviderDiscount issuerDiscountUp = new IssuerProviderDiscount(); IssuerProviderDiscount issuerDiscountDw = new IssuerProviderDiscount(); Double[] pramsUp = Arrays.copyOf(FACTOR_DSC, nParams); Double[] pramsDw = Arrays.copyOf(FACTOR_DSC, nParams); pramsUp[i] += eps; pramsDw[i] -= eps; InterpolatedDoublesCurve interpolatedCurveUp = InterpolatedDoublesCurve.fromSorted(TIME_DSC, pramsUp, INTERPOLATOR, CURVE_NAME_DSC); DiscountCurve discountCurveUp = new DiscountCurve(CURVE_NAME_DSC, interpolatedCurveUp); issuerDiscountUp.setCurve(Pairs.of((Object) ISSUER_NAME_DSC, filter), discountCurveUp); double up = issuerDiscountUp.getIssuerCurve(CURVE_NAME_DSC).getInterestRate( POINT_SENSITIVITY_LIST.get(0).getFirst()); up = addShiftToPrdCmpRates(up, spreadSemiannual, nPeriodS); InterpolatedDoublesCurve interpolatedCurveDw = InterpolatedDoublesCurve.fromSorted(TIME_DSC, pramsDw, INTERPOLATOR, CURVE_NAME_DSC); DiscountCurve discountCurveDw = new DiscountCurve(CURVE_NAME_DSC, interpolatedCurveDw); issuerDiscountDw.setCurve(Pairs.of((Object) ISSUER_NAME_DSC, filter), discountCurveDw); double dw = issuerDiscountDw.getIssuerCurve(CURVE_NAME_DSC).getInterestRate( POINT_SENSITIVITY_LIST.get(0).getFirst()); dw = addShiftToPrdCmpRates(dw, spreadSemiannual, nPeriodS); senseExpected[i] = 0.5 * (up - dw) * POINT_SENSITIVITY_LIST.get(0).getSecond() / eps; } assertArrayRelative("sensitivityFiniteDifferenceDiscountTest", senseExpected, senseComputed, eps); } { double[] senseComputed = providerWithSpreadDouble.parameterSensitivity(CURVE_NAME_YLD, POINT_SENSITIVITY_LIST); int nParams = RATE_YLD.length; double[] senseExpected = new double[nParams]; for (int i = 0; i < nParams; ++i) { LegalEntityFilter<LegalEntity> filter = new LegalEntityShortName(); IssuerProviderDiscount issuerDiscountUp = new IssuerProviderDiscount(); IssuerProviderDiscount issuerDiscountDw = new IssuerProviderDiscount(); Double[] pramsUp = Arrays.copyOf(RATE_YLD, nParams); Double[] pramsDw = Arrays.copyOf(RATE_YLD, nParams); pramsUp[i] += eps; pramsDw[i] -= eps; InterpolatedDoublesCurve interpolatedCurveUp = InterpolatedDoublesCurve.fromSorted(TIME_YLD, pramsUp, INTERPOLATOR, CURVE_NAME_YLD); YieldCurve discountCurveUp = new YieldCurve(CURVE_NAME_YLD, interpolatedCurveUp); issuerDiscountUp.setCurve(Pairs.of((Object) ISSUER_NAME_YLD, filter), discountCurveUp); double up = issuerDiscountUp.getIssuerCurve(CURVE_NAME_YLD).getInterestRate( POINT_SENSITIVITY_LIST.get(0).getFirst()); up = addShiftToPrdCmpRates(up, spreadQuarterly, nPeriodQ); InterpolatedDoublesCurve interpolatedCurveDw = InterpolatedDoublesCurve.fromSorted(TIME_YLD, pramsDw, INTERPOLATOR, CURVE_NAME_YLD); YieldCurve discountCurveDw = new YieldCurve(CURVE_NAME_YLD, interpolatedCurveDw); issuerDiscountDw.setCurve(Pairs.of((Object) ISSUER_NAME_YLD, filter), discountCurveDw); double dw = issuerDiscountDw.getIssuerCurve(CURVE_NAME_YLD).getInterestRate( POINT_SENSITIVITY_LIST.get(0).getFirst()); dw = addShiftToPrdCmpRates(dw, spreadQuarterly, nPeriodQ); senseExpected[i] = 0.5 * (up - dw) * POINT_SENSITIVITY_LIST.get(0).getSecond() / eps; } assertArrayRelative("sensitivityFiniteDifferenceDiscountTest", senseExpected, senseComputed, eps); } assertArrayRelative("sensitivityFiniteDifferenceDiscountTest", ISSUER_PROVIDER.parameterSensitivity(CURVE_NAME_PRD, POINT_SENSITIVITY_LIST), providerWithSpreadDouble.parameterSensitivity(CURVE_NAME_PRD, POINT_SENSITIVITY_LIST), TOL); }