Validate.isTrue(CompareUtils.closeEquals(options[i].getTimeToExpiry(), maturity),
"All options must have the same maturity " + maturity + "; have one with maturity " + options[i].getTimeToExpiry());
strikes[i] = options[i].getStrike();
blackVols[i] = data[i].getBlackVolatility();
}
final UncoupledParameterTransforms transforms = new UncoupledParameterTransforms(new DoubleMatrix1D(initialFitParameters), TRANSFORMS, fixed);
final EuropeanVanillaOption atmOption = new EuropeanVanillaOption(forward, maturity, true);
final ParameterizedFunction<Double, DoubleMatrix1D, Double> function = new ParameterizedFunction<Double, DoubleMatrix1D, Double>() {
@SuppressWarnings("synthetic-access")
@Override
public Double evaluate(final Double strike, final DoubleMatrix1D fp) {
final DoubleMatrix1D mp = transforms.inverseTransform(fp);
double alpha = mp.getEntry(0);
final double beta = mp.getEntry(1);
final double rho = mp.getEntry(2);
final double nu = mp.getEntry(3);
final SABRFormulaData sabrFormulaData;
if (recoverATMVol) {
alpha = _atmCalculator.calculate(new SABRFormulaData(alpha, beta, rho, nu), atmOption, forward, atmVol);
sabrFormulaData = new SABRFormulaData(alpha, beta, rho, nu);
} else {
sabrFormulaData = new SABRFormulaData(alpha, beta, rho, nu);
}
final EuropeanVanillaOption option = new EuropeanVanillaOption(strike, maturity, true);
return _formula.getVolatilityFunction(option, forward).evaluate(sabrFormulaData);
}
};
final DoubleMatrix1D fp = transforms.transform(new DoubleMatrix1D(initialFitParameters));
LeastSquareResults lsRes = errors == null ? SOLVER.solve(new DoubleMatrix1D(strikes), new DoubleMatrix1D(blackVols), function, fp)
: SOLVER.solve(new DoubleMatrix1D(strikes), new DoubleMatrix1D(blackVols), new DoubleMatrix1D(errors), function, fp);
final double[] mp = transforms.inverseTransform(lsRes.getFitParameters()).toArray();
if (recoverATMVol) {
final double beta = mp[1];
final double nu = mp[2];
final double rho = mp[3];
final EuropeanVanillaOption option = new EuropeanVanillaOption(forward, maturity, true);