protected DoubleMatrix1D finiteDiffNodeSensitivitiesSpread(final InstrumentDerivative ird, final InstrumentDerivativeVisitor<YieldCurveBundle, Double> valueCalculator,
final YieldCurveBundle fixedCurves, final YieldCurveBundle spreadCurves) {
int nNodes = 0;
for (final String curveName : spreadCurves.getAllNames()) {
final YieldCurve yieldCurve = (YieldCurve) ((YieldAndDiscountAddZeroSpreadCurve) spreadCurves.getCurve(curveName)).getCurves()[0];
final Interpolator1DDataBundle dataBundle = ((InterpolatedDoublesCurve) yieldCurve.getCurve()).getDataBundle();
nNodes += dataBundle.size() + 1; // +1 for spread
}
final double[] param = new double[nNodes];
int index = 0;
for (final String curveName : spreadCurves.getAllNames()) {
final YieldCurve yieldCurve = (YieldCurve) ((YieldAndDiscountAddZeroSpreadCurve) spreadCurves.getCurve(curveName)).getCurves()[0];
final Interpolator1DDataBundle dataBundle = ((InterpolatedDoublesCurve) yieldCurve.getCurve()).getDataBundle();
for (final double y : dataBundle.getValues()) {
param[index++] = y;
}
final YieldCurve spreadCurve = (YieldCurve) ((YieldAndDiscountAddZeroSpreadCurve) spreadCurves.getCurve(curveName)).getCurves()[1];
param[index++] = ((ConstantDoublesCurve) (spreadCurve.getCurve())).getYData()[0];
}
final Function1D<DoubleMatrix1D, Double> f = new Function1D<DoubleMatrix1D, Double>() {
@Override
public Double evaluate(final DoubleMatrix1D x) {
final YieldCurveBundle curves = spreadCurves.copy();
int index2 = 0;
for (final String name : spreadCurves.getAllNames()) {
final YieldCurve yieldCurve = (YieldCurve) ((YieldAndDiscountAddZeroSpreadCurve) spreadCurves.getCurve(name)).getCurves()[0];
final Interpolator1DDataBundle dataBundle = ((InterpolatedDoublesCurve) yieldCurve.getCurve()).getDataBundle();
final int numberOfNodes = dataBundle.size();
final double[] yields1 = Arrays.copyOfRange(x.getData(), index2, index2 + numberOfNodes);
final double spread1 = x.getData()[index2 + numberOfNodes];
index2 += numberOfNodes + 1;
final YieldCurve newYieldCurve = YieldCurve.from(InterpolatedDoublesCurve.from(dataBundle.getKeys(), yields1, ((InterpolatedDoublesCurve) yieldCurve.getCurve()).getInterpolator()));
final YieldCurve newSpreadCurve = YieldCurve.from(new ConstantDoublesCurve(spread1));
final YieldAndDiscountCurve newCurve = new YieldAndDiscountAddZeroSpreadCurve("NewYield+Spread", false, newYieldCurve, newSpreadCurve);
curves.replaceCurve(name, newCurve);
}
if (fixedCurves != null) {