@Test(expectedExceptions = IllegalArgumentException.class) public void testNegativeRelTol() { new RungeKuttaIntegrator1D(1e-7, -1.0); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNegativeAbsTol() { new RungeKuttaIntegrator1D(-1.0); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testLessTahnOneStep() { new RungeKuttaIntegrator1D(0); }
@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); }
@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); }
@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); }
@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 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())); } }
@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); }
@Test public void test() { final double eps = 1e-9; final int minSteps = 10; final Integrator1D<Double, Double> integrator = new RungeKuttaIntegrator1D(eps, eps, minSteps); double lower = 0; double upper = 2.0; assertEquals(4.0, integrator.integrate(CUBE, lower, upper), eps); lower = 0.0; upper = 1.5; assertEquals(0.625, integrator.integrate(TRIANGLE, lower, upper), eps); lower = -30; upper = 30; assertEquals(1.0, integrator.integrate(MIX_NORM, 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;
.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++) {
double factor = dfPayment / intProv.h(forward) * intProv.g(forward); double strikePart = factor * intProv.k(strikeCpn) * intProv.bs(strikeCpn); RungeKuttaIntegrator1D integrator = new RungeKuttaIntegrator1D(ABS_TOL, REL_TOL, NUM_ITER); double integralPart = 0d; Function<Double, Double> integrant = intProv.integrant();
CmsDeltaIntegrantProvider intProv = new CmsDeltaIntegrantProvider( cmsPeriod, swap, swaptionVolatilities, forward, strikeCpn, expiryTime, tenor, cutOffStrike, eta); RungeKuttaIntegrator1D integrator = new RungeKuttaIntegrator1D(ABS_TOL, REL_TOL, NUM_ITER); double[] bs = intProv.bsbsp(strikeCpn); double[] n = intProv.getNnp();
public void test_presentValue_replication_cap() { SwapIndex index = CAPLET.getIndex(); LocalDate effectiveDate = CAPLET.getUnderlyingSwap().getStartDate(); ResolvedSwap expanded = CAPLET.getUnderlyingSwap(); double tenor = VOLATILITIES.tenor(effectiveDate, CAPLET.getUnderlyingSwap().getEndDate()); double theta = VOLATILITIES.relativeTime( CAPLET.getFixingDate().atTime(index.getFixingTime()).atZone(index.getFixingZone())); double delta = index.getTemplate().getConvention().getFixedLeg() .getDayCount().relativeYearFraction(effectiveDate, PAYMENT); double ptp = RATES_PROVIDER.discountFactor(EUR, PAYMENT); double S0 = PRICER_SWAP.parRate(COUPON.getUnderlyingSwap(), RATES_PROVIDER); CmsIntegrantProvider integrant = new CmsIntegrantProvider(CAPLET, expanded, STRIKE, tenor, theta, S0, -delta, VOLATILITIES_SHIFT, CUT_OFF_STRIKE, MU); // Strike part double h_1S0 = 1.0 / integrant.h(S0); double gS0 = integrant.g(S0); double kK = integrant.k(STRIKE); double bsS0 = integrant.bs(STRIKE); double strikePart = ptp * h_1S0 * gS0 * kK * bsS0; // Integral part RungeKuttaIntegrator1D integrator = new RungeKuttaIntegrator1D(1.0E-7, 1.0E-10, 10); double integralPart = ptp * integrator.integrate(integrant.integrant(), STRIKE, 100.0); double pvExpected = (strikePart + integralPart) * NOTIONAL * ACC_FACTOR; CurrencyAmount pvComputed = PRICER.presentValue(CAPLET, RATES_PROVIDER, VOLATILITIES_SHIFT); assertEquals(pvComputed.getAmount(), pvExpected, TOLERANCE_PV); }