for (int i = 0; i < nWeights; i++) {
weights[i] = TRANSFORM.inverseTransform(x.getEntry(i));
}
final LocalVolatilitySurfaceMoneyness localVolSurface = getLocalVol(bSplines, fwdCurve, weights);
final BlackVolatilitySurfaceMoneyness impVol = solveForwardPDE(fwdCurve, lower, upper, pdeGrid, initialCond, localVolSurface);
final double[] vols = new double[totalStrikes];
@SuppressWarnings("unused")
double chi2 = 0;
int index = 0;
for (int i = 0; i < nExp; i++) {
final double t = EXPIRIES[i];
final int n = STRIKES[i].length;
for (int j = 0; j < n; j++) {
final double k = STRIKES[i][j];
vols[index] = impVol.getVolatility(t, k);
chi2 += FunctionUtils.square(vols[index] - IMP_VOL);
index++;
}
}
final DoubleMatrix1D debug = new DoubleMatrix1D(vols);
// System.out.println(chi2);
return debug;
}
};
final double[] start = new double[nWeights];
// Arrays.fill(start, 0.4);
for (int i = 0; i < nWeights; i++) {
start[i] = TRANSFORM.transform(IMP_VOL + 0.05 * (random.nextDouble() - 0.5));
}
// DoubleMatrix1D res = MINIMIZER.minimize(objective, new DoubleMatrix1D(start));
final DoubleMatrix1D observed = new DoubleMatrix1D(totalStrikes, IMP_VOL);
final LeastSquareResults res = NLLS.solve(observed, volFunc, new DoubleMatrix1D(start), penalty);
System.out.println(res);
final double[] weights = new double[nWeights];
for (int i = 0; i < nWeights; i++) {
weights[i] = TRANSFORM.inverseTransform(res.getFitParameters().getEntry(i));
}
final LocalVolatilitySurfaceMoneyness lv = getLocalVol(bSplines, fwdCurve, weights);
PDEUtilityTools.printSurface("lv", lv.getSurface(), 0.01, 2.0, 0.3, 3.0);
final BlackVolatilitySurfaceMoneyness iv = solveForwardPDE(fwdCurve, lower, upper, pdeGrid, initialCond, lv);
PDEUtilityTools.printSurface("imp vol", iv.getSurface(), 0.01, 2.0, 0.3, 3.0);
}