VasicekInterestRateModel implements DiscountBondModel<VasicekDataBundle> { @Override public Function1D<VasicekDataBundle, Double> getDiscountBondFunction(final ZonedDateTime time, final ZonedDateTime maturity) { Validate.notNull(time); Validate.notNull(maturity); return new Function1D<VasicekDataBundle, Double>() { @Override public Double evaluate(final VasicekDataBundle data) { Validate.notNull(data); final double lt = data.getLongTermInterestRate(); final double speed = data.getReversionSpeed(); final double dt = DateUtils.getDifferenceInYears(time, maturity); final double t = DateUtils.getDifferenceInYears(data.getDate(), time); final double sigma = data.getShortRateVolatility(t); final double r = data.getShortRate(t); final double sigmaSq = sigma * sigma; final double speedSq = speed * speed; final double rInfinity = lt - 0.5 * sigmaSq / speedSq; final double factor = 1 - Math.exp(-speed * dt); final double a = rInfinity * (factor / speed - dt) - sigmaSq * factor * factor / (4 * speedSq * speed); final double b = factor / speed; return Math.exp(a - r * b); } }; }  @Override Function1D<VasicekDataBundle, 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((VasicekDataBundle) null); }
@Test public void test() { final YieldCurve shortRate = YieldCurve.from(ConstantDoublesCurve.from(0.05)); final double longRate = 0.06; final double speed = 0.01; final VolatilityCurve sigma = new VolatilityCurve(ConstantDoublesCurve.from(0.01)); VasicekDataBundle data = new VasicekDataBundle(shortRate, sigma, TODAY, longRate, speed); assertEquals(MODEL.getDiscountBondFunction(START, START).evaluate(data), 1, 0); data = new VasicekDataBundle(shortRate, new VolatilityCurve(ConstantDoublesCurve.from(0)), TODAY, longRate, speed); final double factor = (1 - Math.exp(-speed * YEARS)); final double lnA = longRate * (factor / speed - YEARS); assertEquals(MODEL.getDiscountBondFunction(START, MATURITY).evaluate(data), Math.exp(lnA - data.getShortRate(0) * factor / speed), 0); }