BaroneAdesiWhaleyModel { public double price(final double s0, final double k, final double r, final double b, final double t, final double sigma, final boolean isCall) { ArgumentChecker.isTrue(s0 > 0.0, "spot must be greater than zero"); ArgumentChecker.isTrue(k > 0.0, "strike must be greater than zero"); ArgumentChecker.isTrue(t > 0.0, "t must be greater than zero"); ArgumentChecker.isTrue(sigma > 0.0, "sigma must be greater than zero"); if (isCall) { final CallSolver solver = new CallSolver(s0, k, r, b, t, sigma); return solver.getPrice(); } final PutSolver solver = new PutSolver(s0, k, r, b, t, sigma); return solver.getPrice(); }  double price(final double s0, final double k, final double r, final double b, final double t, final double sigma, final boolean isCall); double[] getPriceAdjoint(final double s0, final double k, final double r, final double b, final double t, final double sigma, final boolean isCall); double[] getPriceDeltaGamma(final double s0, final double k, final double r, final double b, final double t, final double sigma, final boolean isCall); double[] getPriceAndVega(final double s0, final double k, final double r, final double b, final double t, final double sigma, final boolean isCall); Function1D<Double, double[]> getPriceAndVegaFunction(final double s0, final double k, final double r, final double b, final double t, final boolean isCall); double impliedVolatility(final double price, final double s0, final double k, final double r, final double b, final double t, final boolean isCall);  }
@Test public void knownValuesTest() { final BaroneAdesiWhaleyModel baw = new BaroneAdesiWhaleyModel(); for (int i = 0; i < EXPIRIES.length; i++) { for (int j = 0; j < VOLS.length; j++) { for (int k = 0; k < COM_PRICES.length; k++) { final double call = baw.price(COM_PRICES[k], STRIKE, R, B, EXPIRIES[i], VOLS[j], true); final double put = baw.price(COM_PRICES[k], STRIKE, R, B, EXPIRIES[i], VOLS[j], false); assertEquals("call", CALL_PRICES[i][j][k], call, 3e-3); assertEquals("put", PUT_PRICES[i][j][k], put, 1e-3); } } } }
@Test public void putCallTransformTest() { final double s0 = 110; final double t = 0.5; final double r = 0.1; final double sigma = 0.35; final double[] kSet = new double[] {60, 80, 100, 110, 120, 160 }; final double[] bSet = new double[] {-0.04, 0.0, 0.04, 0.11 }; final BaroneAdesiWhaleyModel baw = new BaroneAdesiWhaleyModel(); for (final double k : kSet) { for (final double b : bSet) { final double cprice = baw.price(s0, k, r, b, t, sigma, true); final double ccprice = baw.price(k, s0, r - b, -b, t, sigma, false); assertEquals("strike " + k, cprice, ccprice, 2e-3 * cprice); } } }
@Test(enabled = false) public void priceSigmaTest() { final double s0 = 110; final double t = 0.5; final double r = 0.1; final double b = -0.04; final double k = 80; final BaroneAdesiWhaleyModel baw = new BaroneAdesiWhaleyModel(); for (int i = 0; i < 101; i++) { final double sigma = 0.05 + 0.7 * i / 100.; final double p = baw.price(s0, k, r, b, t, sigma, true); System.out.println(sigma + "\t" + p); } }