@Test public void testGaussHermite() { final double rootPI = Math.sqrt(Math.PI); final double upper = Double.POSITIVE_INFINITY; final double lower = Double.NEGATIVE_INFINITY; final GaussHermiteQuadratureIntegrator1D integrator = new GaussHermiteQuadratureIntegrator1D(10); assertEquals(rootPI, integrator.integrateFromPolyFunc(ONE), 1e-15); assertEquals(rootPI, integrator.integrate(DF3, lower, upper), EPS); }
@Test public void testBlackFormula() { double fwd = 5; double strike = 6.5; double t = 1.5; double vol = 0.35; double expected = BlackFormulaRepository.price(fwd, strike, t, vol, true); Function<Double, Double> func = getBlackIntergrand(fwd, strike, t, vol); Function<Double, Double> fullIntergrand = new Function<Double, Double>() { @Override public Double apply(Double x) { return func.apply(x) * Math.exp(-x * x); } }; RungeKuttaIntegrator1D rk = new RungeKuttaIntegrator1D(1e-15); double resRK = rk.integrate(fullIntergrand, 0., 10.); //The strike > fwd, so can start the integration at z=0 (i.e. s = fwd) assertEquals(expected, resRK, 1e-15, "Runge Kutta"); GaussHermiteQuadratureIntegrator1D gh = new GaussHermiteQuadratureIntegrator1D(40); double resGH = gh.integrateFromPolyFunc(func); assertEquals(expected, resGH, 1e-2, "Gauss Hermite"); //terrible accuracy even with 40 points }
@Test public void testGaussHermite2() { final RungeKuttaIntegrator1D rk = new RungeKuttaIntegrator1D(1e-15); final Double expected = 2 * rk.integrate(COS_EXP, 0., 10.); final GaussHermiteQuadratureIntegrator1D gh = new GaussHermiteQuadratureIntegrator1D(11); final double res1 = gh.integrateFromPolyFunc(COS); assertEquals(expected, res1, 1e-15); //11 points gets you machine precision final double res2 = gh.integrate(COS_EXP, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); assertEquals(expected, res2, 1e-15); }