GapOptionModel extends AnalyticOptionModel<GapOptionDefinition, StandardOptionDataBundle> { @Override public Function1D<StandardOptionDataBundle, Double> getPricingFunction(final GapOptionDefinition definition) { Validate.notNull(definition, "definition"); return new Function1D<StandardOptionDataBundle, Double>() { @SuppressWarnings("synthetic-access") @Override public Double evaluate(final StandardOptionDataBundle data) { Validate.notNull(data, "data"); final double s = data.getSpot(); final double k = definition.getStrike(); final double t = definition.getTimeToExpiry(data.getDate()); final double r = data.getInterestRate(t); final double sigma = data.getVolatility(t, k); final double b = data.getCostOfCarry(); final double payoffStrike = definition.getPayoffStrike(); final int sign = definition.isCall() ? 1 : -1; final double d1 = getD1(s, k, t, sigma, b); final double d2 = getD2(d1, sigma, t); return sign * (s * Math.exp(t * (b - r)) * NORMAL.getCDF(sign * d1) - payoffStrike * Math.exp(-r * t) * NORMAL.getCDF(sign * d2)); } }; }  @Override Function1D<StandardOptionDataBundle, Double> getPricingFunction(final GapOptionDefinition definition);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullDefinition() { MODEL.getPricingFunction(null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullData() { MODEL.getPricingFunction(new GapOptionDefinition(STRIKE, EXPIRY, true, PAYOFF_STRIKE)).evaluate((StandardOptionDataBundle) null); }
@Test public void testAgainstBSM() { final StandardOptionDataBundle data = DATA; final Function1D<StandardOptionDataBundle, Double> call = MODEL.getPricingFunction(new GapOptionDefinition(STRIKE, EXPIRY, true, STRIKE)); final Function1D<StandardOptionDataBundle, Double> put = MODEL.getPricingFunction(new GapOptionDefinition(STRIKE, EXPIRY, false, STRIKE)); final Function1D<StandardOptionDataBundle, Double> bsmCall = BSM.getPricingFunction(new EuropeanVanillaOptionDefinition(STRIKE, EXPIRY, true)); final Function1D<StandardOptionDataBundle, Double> bsmPut = BSM.getPricingFunction(new EuropeanVanillaOptionDefinition(STRIKE, EXPIRY, false)); assertEquals(call.evaluate(data), bsmCall.evaluate(data), EPS); assertEquals(put.evaluate(data), bsmPut.evaluate(data), EPS); }
@Test public void test() { assertEquals(MODEL.getPricingFunction(new GapOptionDefinition(STRIKE, EXPIRY, true, PAYOFF_STRIKE)).evaluate(DATA), -0.0053, 1e-4); }