@Test public void testRungeKutta() { final RungeKuttaIntegrator1D integrator = new RungeKuttaIntegrator1D(); final double lower = -1; final double upper = 2; assertEquals(F1.apply(upper) - F1.apply(lower), integrator.integrate(DF1, lower, upper), EPS); }
return calculateRungeKuttaFourthOrder(f, x, h / 2.0, fl, f1, fm) + calculateRungeKuttaFourthOrder(f, x + h / 2.0, h / 2.0, fm, f2, fu);
Math.max(cutOffStrike, 2d * strike)); // To ensure that the integral covers a good part of the smile double upper = Math.min(upper0, 1d); // To ensure that we don't miss the meaningful part res = integrator.integrate(integrant, strike, upper); double reminder = integrant.apply(upper) * upper; double error = reminder / res; int count = 0; while (Math.abs(error) > integrator.getRelativeTolerance() && count < MAX_COUNT) { res += integrator.integrate(integrant, upper, 2d * upper); upper *= 2d; reminder = integrant.apply(upper) * upper; if (count == MAX_COUNT) { log.info("Maximum iteration count, " + MAX_COUNT + ", has been reached. Relative error is greater than " + integrator.getRelativeTolerance());
public void interpolation_sample_9999_100_expected_shortfall() { Function<Double, Double> func = new Function<Double, Double>() { @Override public Double apply(Double level) { return QUANTILE_SAMPLE_INTERPOLATION.quantileWithExtrapolationFromUnsorted(level, UNSORTED_100); } }; double expectedShortfallExpected = INTEG.integrate(func, TOL_INTEGRAL, LEVEL5) / LEVEL5; double expectedShortfallComputed = QUANTILE_SAMPLE_INTERPOLATION.expectedShortfallFromUnsorted(LEVEL5, UNSORTED_100); assertEquals(expectedShortfallComputed, expectedShortfallExpected, TOL_INTEGRAL); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNegativeAbsTol() { new RungeKuttaIntegrator1D(-1.0); }
public void interpolation_samplePlusOne_9999_100_expected_shortfall() { Function<Double, Double> func = new Function<Double, Double>() { @Override public Double apply(Double level) { return QUANTILE_SAMPLE1_INTERPOLATION.quantileWithExtrapolationFromUnsorted(level, UNSORTED_100); } }; double expectedShortfallExpected = INTEG.integrate(func, TOL_INTEGRAL, LEVEL5) / LEVEL5; double expectedShortfallComputed = QUANTILE_SAMPLE1_INTERPOLATION.expectedShortfallFromUnsorted(LEVEL5, UNSORTED_100); assertEquals(expectedShortfallComputed, expectedShortfallExpected, TOL_INTEGRAL); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testLessTahnOneStep() { new RungeKuttaIntegrator1D(0); }
@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); }
public void index_nearest_one_9999_100_expected_shortfall() { Function<Double, Double> func = new Function<Double, Double>() { @Override public Double apply(Double level) { return QUANTILE_SAMPLE1_NEAREST_INDEX.quantileWithExtrapolationFromUnsorted(level, UNSORTED_100); } }; double expectedShortfallExpected = INTEG.integrate(func, TOL_INTEGRAL, LEVEL5) / LEVEL5; double expectedShortfallComputed = QUANTILE_SAMPLE1_NEAREST_INDEX.expectedShortfallFromUnsorted(LEVEL5, UNSORTED_100); assertEquals(expectedShortfallComputed, expectedShortfallExpected, TOL_INTEGRAL); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNegativeRelTol() { new RungeKuttaIntegrator1D(1e-7, -1.0); }
@Override public Double integrate(Function<Double, Double> f, Double lower, Double upper) { ArgChecker.notNull(lower, "lower"); ArgChecker.notNull(upper, "upper"); if (Double.isNaN(lower) || Double.isInfinite(lower) || Double.isInfinite(upper) || Double.isNaN(upper)) { throw new IllegalArgumentException("lower or upper was NaN or Inf"); } double h = (upper - lower) / _minSteps; double f1, f2, f3, x; x = lower; f1 = f.apply(x); if (Double.isNaN(f1) || Double.isInfinite(f1)) { throw new IllegalArgumentException("function evaluation returned NaN or Inf"); } double result = 0.0; for (int i = 0; i < _minSteps; i++) { f2 = f.apply(x + h / 2.0); if (Double.isNaN(f2) || Double.isInfinite(f2)) { throw new IllegalArgumentException("function evaluation returned NaN or Inf"); } f3 = f.apply(x + h); if (Double.isNaN(f3) || Double.isInfinite(f3)) { throw new IllegalArgumentException("function evaluation returned NaN or Inf"); } result += calculateRungeKuttaFourthOrder(f, x, h, f1, f2, f3); f1 = f3; x += h; } return result; }
@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 }
public void interpolation_midway_9999_100_expected_shortfall() { Function<Double, Double> func = new Function<Double, Double>() { @Override public Double apply(Double level) { return QUANTILE_MIDWAY_INTERPOLATION.quantileWithExtrapolationFromUnsorted(level, UNSORTED_100); } }; double expectedShortfallExpected = INTEG.integrate(func, TOL_INTEGRAL, LEVEL5) / LEVEL5; double expectedShortfallComputed = QUANTILE_MIDWAY_INTERPOLATION.expectedShortfallFromUnsorted(LEVEL5, UNSORTED_100); assertEquals(expectedShortfallComputed, expectedShortfallExpected, TOL_INTEGRAL); }
@Test public void testCutoff() { final double eps = 1e-9; final int minSteps = 10; final Integrator1D<Double, Double> integrator = new RungeKuttaIntegrator1D(eps, eps, minSteps); final double lower = -1.0; final double upper = 1.0; assertEquals(0.0, integrator.integrate(SIN_INV_X, lower, upper), eps); }
cmsPeriod, swap, swaptionVolatilities, forward, strike, expiryTime, tenor, cutOffStrike, eta); double factor = dfPayment * intProv.g(forward) / intProv.h(forward); RungeKuttaIntegrator1D integrator = new RungeKuttaIntegrator1D(ABS_TOL, REL_TOL_STRIKE, NUM_ITER); double[] kpkpp = intProv.kpkpp(strike); double firstPart; } else { firstPart = -kpkpp[0] * intProv.bs(strike); thirdPart = -integrator.integrate(integrant, -shift + ZERO_SHIFT, strike);
public void index_nearest_095_100_expected_shortfall() { Function<Double, Double> func = new Function<Double, Double>() { @Override public Double apply(Double level) { return QUANTILE_NEAREST_INDEX.quantileWithExtrapolationFromUnsorted(level, UNSORTED_100); } }; double expectedShortfallExpected = INTEG.integrate(func, TOL_INTEGRAL, LEVEL3) / LEVEL3; double expectedShortfallComputed = QUANTILE_NEAREST_INDEX.expectedShortfallFromUnsorted( LEVEL3, UNSORTED_100); assertEquals(expectedShortfallComputed, expectedShortfallExpected, TOL_INTEGRAL); }
@Test /** Numerical integral vs a known explicit solution. */ public void integrate() { // Test function. BiFunction<Double, Double, Double> f = (x1, x2) -> x1 + Math.sin(x2); double absTol = 1.0E-6; double relTol = 1.0E-6; int minSteps = 6; RungeKuttaIntegrator1D integrator1D = new RungeKuttaIntegrator1D(absTol, relTol, minSteps); IntegratorRepeated2D integrator2D = new IntegratorRepeated2D(integrator1D); Double[] lower; Double[] upper; double result, resultExpected; // First set of limits. lower = new Double[] {0.0, 1.0}; upper = new Double[] {2.0, 10.0}; result = integrator2D.integrate(f, lower, upper); resultExpected = (upper[0] * upper[0] - lower[0] * lower[0]) / 2.0 * (upper[1] - lower[1]) + (upper[0] - lower[0]) * (-Math.cos(upper[1]) + Math.cos(lower[1])); assertEquals("Integration 2D - repeated 1D", resultExpected, result, 1E-8); // Second set of limits. lower = new Double[] {0.25, 5.25}; upper = new Double[] {25.25, 35.25}; result = integrator2D.integrate(f, lower, upper); resultExpected = (upper[0] * upper[0] - lower[0] * lower[0]) / 2.0 * (upper[1] - lower[1]) + (upper[0] - lower[0]) * (-Math.cos(upper[1]) + Math.cos(lower[1])); assertEquals("Integration 2D - repeated 1D", resultExpected, result, 1E-6); }
.priceAdjointSabr(Math.max(0d, strikeCpn + shift), intProv.getPutCall()) // handle tiny but negative number .getDerivatives().multipliedBy(factor2).toArray(); RungeKuttaIntegrator1D integrator = new RungeKuttaIntegrator1D(ABS_TOL, REL_TOL_VEGA, NUM_ITER); double[] totalSensi = new double[4]; for (int loopparameter = 0; loopparameter < 4; loopparameter++) { integrateCall(integrator, integrant, swaptionVolatilities, forward, strikeCpn, expiryTime, tenor); } else { integralPart = -dfPayment * integrator.integrate(integrant, -shift + ZERO_SHIFT, strikeCpn);
public void index_nearest_9999_100_expected_shortfall() { Function<Double, Double> func = new Function<Double, Double>() { @Override public Double apply(Double level) { return QUANTILE_NEAREST_INDEX.quantileWithExtrapolationFromUnsorted(level, UNSORTED_100); } }; double expectedShortfallExpected = INTEG.integrate(func, TOL_INTEGRAL, LEVEL5) / LEVEL5; double expectedShortfallComputed = QUANTILE_NEAREST_INDEX.expectedShortfallFromUnsorted( LEVEL5, UNSORTED_100); assertEquals(expectedShortfallComputed, expectedShortfallExpected, TOL_INTEGRAL); }
/** * */ @Test public void equalsHashCodetest() { final Integrator1D<Double, Double> integBase = new SimpsonIntegrator1D(); final Integrator1D<Double, Double> integ0 = new AdaptiveCompositeIntegrator1D(integBase); final Integrator1D<Double, Double> integ1 = new AdaptiveCompositeIntegrator1D(integBase); final Integrator1D<Double, Double> integ2 = new AdaptiveCompositeIntegrator1D(new RungeKuttaIntegrator1D()); final Integrator1D<Double, Double> integ3 = new AdaptiveCompositeIntegrator1D(integBase, 1., 1.e-5); final Integrator1D<Double, Double> integ4 = new AdaptiveCompositeIntegrator1D(integBase, 2., 1.e-5); final Integrator1D<Double, Double> integ5 = new AdaptiveCompositeIntegrator1D(integBase, 1., 1.e-6); assertTrue(integ0.equals(integ0)); assertTrue(integ0.equals(integ1)); assertTrue(integ1.equals(integ0)); assertTrue(integ1.hashCode() == integ0.hashCode()); assertTrue(!(integ0.equals(integ2))); assertTrue(!(integ0.equals(integ3))); assertTrue(!(integ0.equals(integ4))); assertTrue(!(integ0.equals(integ5))); assertTrue(!(integ0.equals(integBase))); assertTrue(!(integ0.equals(null))); assertTrue(!(integ3.equals(integ5))); assertTrue(!(integ1.hashCode() == INTEGRATOR.hashCode())); } }