PDEResults1D res;
if (isAmerican) {
if (option.isCall()) {
lower = new NeumannBoundaryCondition(0.0, sMin, true);
upper = new NeumannBoundaryCondition(1.0, sMax, false);
} else {
lower = new NeumannBoundaryCondition(-1.0, sMin, true);
upper = new NeumannBoundaryCondition(0.0, sMax, false);
}
final Function<Double, Double> func = new Function<Double, Double>() {
@Override
public Double evaluate(final Double... tx) {
final double x = tx[1];
return payoff.evaluate(x);
}
};
final FunctionalDoublesSurface free = new FunctionalDoublesSurface(func);
PDE1DDataBundle<ConvectionDiffusionPDE1DCoefficients> data = new PDE1DDataBundle<ConvectionDiffusionPDE1DCoefficients>(coef, payoff, lower, upper, free, grid[0]);
ThetaMethodFiniteDifference solver = new ThetaMethodFiniteDifference(theta[0], false);
res = solver.solve(data);
for (int ii = 1; ii < n; ii++) {
data = new PDE1DDataBundle<ConvectionDiffusionPDE1DCoefficients>(coef, res.getTerminalResults(), lower, upper, free, grid[ii]);
solver = new ThetaMethodFiniteDifference(theta[ii], false);
res = solver.solve(data);
}
// European
} else {
if (option.isCall()) {
lower = new NeumannBoundaryCondition(0.0, sMin, true);
final Function1D<Double, Double> upFunc = new Function1D<Double, Double>() {
@Override
public Double evaluate(final Double tau) {
return Math.exp((costOfCarry.getYValue(tau) - riskFreeRate.getYValue(tau)) * tau);
}
};
upper = new NeumannBoundaryCondition(upFunc, sMax, false);
} else {
final Function1D<Double, Double> downFunc = new Function1D<Double, Double>() {
@Override
public Double evaluate(final Double tau) {
return -Math.exp((costOfCarry.getYValue(tau) - riskFreeRate.getYValue(tau)) * tau);
}
};
lower = new NeumannBoundaryCondition(downFunc, sMin, true);
upper = new NeumannBoundaryCondition(0.0, sMax, false);
}
PDE1DDataBundle<ConvectionDiffusionPDE1DCoefficients> data = new PDE1DDataBundle<ConvectionDiffusionPDE1DCoefficients>(coef, payoff, lower, upper, grid[0]);
ThetaMethodFiniteDifference solver = new ThetaMethodFiniteDifference(theta[0], false);
res = solver.solve(data);
for (int ii = 1; ii < n; ii++) {