private double solveByBisection(double optionPrice, double lowerSigma, double upperSigma) { BisectionSingleRootFinder rootFinder = new BisectionSingleRootFinder(VOL_TOL); Function<Double, Double> func = new Function<Double, Double>() { @Override public Double apply(Double volatility) { double trialPrice = priceFunc.apply(volatility); return trialPrice / optionPrice - 1.0; } }; return rootFinder.getRoot(func, lowerSigma, upperSigma); }
private IsdaCompliantDiscountCurveCalibrator(double accuracy) { this.rootFinder = new NewtonRaphsonSingleRootFinder(accuracy); }
/** * {@inheritDoc} * Vector root finders only need a single starting point; if more than one is provided, the first is used and any other points ignored. */ @Override public DoubleArray getRoot(Function<DoubleArray, DoubleArray> function, DoubleArray... startingPoint) { ArgChecker.notNull(startingPoint, "starting point"); return getRoot(function, startingPoint[0]); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction2() { ROOT_FINDER.getRoot((DoubleFunction1D) null, X1, X2); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testBracketTwoRoots() { getRootFinder().getRoot(F, 1.5, 3.5); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullUpper() { getRootFinder().checkInputs(F, 1., null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction() { ROOT_FINDER.getRoots(null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction6() { ROOT_FINDER.getRoot((DoubleFunction1D) null, X); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testOutsideRoots() { getRootFinder().getRoot(F, 10., 100.); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction() { getRootFinder().checkInputs((DoubleFunction1D) null, 1., 2.); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullVector() { DUMMY.getRoot(LINEAR, (DoubleArray) null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullInitialGuess1() { ROOT_FINDER.getRoot(F1, (Double) null); }
@Test public void test() { RealSingleRootFinder finder = getRootFinder(); assertEquals(finder.getRoot(F, 2.5, 3.5), 3, EPS); assertEquals(finder.getRoot(F, 1.5, 2.5), 2, EPS); assertEquals(finder.getRoot(F, -1.5, 0.5), -1, EPS); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullLower() { getRootFinder().checkInputs(F, null, 2.); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullInitialGuess3() { ROOT_FINDER.getRoot(F1, DF1, null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction4() { ROOT_FINDER.getRoot(null, DF1, X1, X2); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullHigher4() { ROOT_FINDER.getRoot(F1, DF1, X1, null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullDerivative4() { ROOT_FINDER.getRoot(F2, (Function<Double, Double>) null, X); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullInitialGuess2() { ROOT_FINDER.getRoot(F2, (Double) null); }
/** * Uses the function and its derivative. This method uses an initial guess for the root, rather than bounds. * @param function The function, not null * @param derivative The derivative, not null * @param x The initial guess for the root, not null * @return The root * @throws MathException If the root is not found in 1000 attempts. */ public Double getRoot(Function<Double, Double> function, Function<Double, Double> derivative, Double x) { return getRoot(DoubleFunction1D.from(function), DoubleFunction1D.from(derivative), x); }