CashOrNothingOptionModel extends AnalyticOptionModel<CashOrNothingOptionDefinition, StandardOptionDataBundle> { @Override public Function1D<StandardOptionDataBundle, Double> getPricingFunction(final CashOrNothingOptionDefinition definition) { Validate.notNull(definition, "definition"); return new Function1D<StandardOptionDataBundle, Double>() { @SuppressWarnings("synthetic-access") @Override public Double evaluate(final StandardOptionDataBundle data) { Validate.notNull(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 d = getD2(getD1(s, k, t, sigma, b), sigma, t); final double payment = definition.getPayment(); return payment * Math.exp(-r * t) * NORMAL.getCDF(definition.isCall() ? d : -d); } }; }  @Override Function1D<StandardOptionDataBundle, Double> getPricingFunction(final CashOrNothingOptionDefinition definition);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullDefinition() { MODEL.getPricingFunction(null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullData() { MODEL.getPricingFunction(PUT).evaluate((StandardOptionDataBundle) null); }
@Test public void testZeroVol() { final double delta = 20; final StandardOptionDataBundle data = DATA.withVolatilitySurface(new VolatilitySurface(ConstantDoublesSurface.from(0))); Function1D<StandardOptionDataBundle, Double> f = MODEL.getPricingFunction(PUT); final double df = Math.exp(-R * T); assertEquals(f.evaluate(data.withSpot(STRIKE - delta)), df * PAYMENT, EPS); assertEquals(f.evaluate(data.withSpot(STRIKE + delta)), 0, EPS); final CashOrNothingOptionDefinition call = new CashOrNothingOptionDefinition(STRIKE, EXPIRY, true, PAYMENT); f = MODEL.getPricingFunction(call); assertEquals(f.evaluate(data.withSpot(STRIKE + delta)), df * PAYMENT, EPS); assertEquals(f.evaluate(data.withSpot(STRIKE - delta)), 0, EPS); }
@Test public void test() { assertEquals(MODEL.getPricingFunction(PUT).evaluate(DATA), 2.6710, 1e-4); }