MarkovChainApprox { public double price(final double forward, final double df, final double strike) { final EuropeanVanillaOption option = new EuropeanVanillaOption(strike, _t, true); final BlackPriceFunction func = new BlackPriceFunction(); final Function1D<BlackFunctionData, Double> priceFunc = func.getPriceFunction(option); double sum = 0; for (int i = 0; i < _weights.length; i++) { final BlackFunctionData data = new BlackFunctionData(forward, df, _vols[i]); sum += _weights[i] * priceFunc.evaluate(data); } return sum; } MarkovChainApprox(final double vol1, final double vol2, final double lambda12, final double lambda21, final double probState1, final double expiry); double price(final double forward, final double df, final double strike); double priceCEV(final double forward, final double df, final double strike, final double beta); double[] getMoments(final double expiry);  }
@Test public void priceTest() { final double forward = 0.04; final double df = 0.9; double strike; double impVol; double mcImpVol; final BlackFunctionData data = new BlackFunctionData(forward, df, 0.0); for (int i = 0; i < 101; i++) { strike = 0.01 + 0.1 * i / 100; final EuropeanVanillaOption option = new EuropeanVanillaOption(strike, T, true); final double price = CHAIN_APPROX.price(forward, df, strike); final double mcPrice = CHAIN.price(forward, df, strike, T, SIMS); try { impVol = BLACK_IMPLIED_VOL.getImpliedVolatility(data, option, price); } catch (final Exception e) { impVol = 0; } try { mcImpVol = BLACK_IMPLIED_VOL.getImpliedVolatility(data, option, mcPrice); } catch (final Exception e) { mcImpVol = 0; } if (strike > 0.01 && strike < 0.2) { assertEquals(impVol, mcImpVol, 1e-2); } } }