HullWhiteOneFactorInterestRateModel implements DiscountBondModel<HullWhiteOneFactorDataBundle> { @Override public Function1D<HullWhiteOneFactorDataBundle, Double> getDiscountBondFunction(final ZonedDateTime time, final ZonedDateTime maturity) { Validate.notNull(time); Validate.notNull(maturity); return new Function1D<HullWhiteOneFactorDataBundle, Double>() { @Override public Double evaluate(final HullWhiteOneFactorDataBundle data) { Validate.notNull(data); final double t = DateUtils.getDifferenceInYears(data.getDate(), time); final double s = DateUtils.getDifferenceInYears(data.getDate(), maturity); final double rT = data.getShortRate(t); final double rs = data.getShortRate(s); final double pT = Math.exp(-rT * t); final double ps = Math.exp(-rs * s); final Double sigma = data.getShortRateVolatility(t); final double dt = s - t; final double speed = data.getReversionSpeed(); final double b = (1 - Math.exp(-speed * dt)) / speed; final double upT = t + _delta; final double downT = t - _delta; final double dlnPdt = (-data.getShortRate(upT) * upT + data.getShortRate(downT) * downT) / (2 * _delta); final double lnA = Math.log(ps / pT) - b * dlnPdt - sigma * sigma * Math.pow(Math.exp(-speed * s) - Math.exp(-speed * t), 2) * (Math.exp(2 * speed * t) - 1) / (4 * speed * speed * speed); return Math.exp(lnA - b * rT); } }; }  @Override Function1D<HullWhiteOneFactorDataBundle, Double> getDiscountBondFunction(final ZonedDateTime time, final ZonedDateTime maturity);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullTime() { MODEL.getDiscountBondFunction(null, MATURITY); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullMaturity() { MODEL.getDiscountBondFunction(START, null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullData() { MODEL.getDiscountBondFunction(START, MATURITY).evaluate((HullWhiteOneFactorDataBundle) null); }
@Test public void test() { HullWhiteOneFactorDataBundle data = new HullWhiteOneFactorDataBundle(R, SIGMA, TODAY, SPEED); assertEquals(MODEL.getDiscountBondFunction(START, START).evaluate(data), 1, EPS); data = new HullWhiteOneFactorDataBundle(R, new VolatilityCurve(ConstantDoublesCurve.from(0)), TODAY, SPEED); assertEquals(Math.log(MODEL.getDiscountBondFunction(START, MATURITY).evaluate(data)), -RATE * YEARS, EPS); data = new HullWhiteOneFactorDataBundle(R, SIGMA, TODAY, 200); assertEquals(Math.log(MODEL.getDiscountBondFunction(START, MATURITY).evaluate(data)), -RATE * YEARS, EPS); }