Example usage for org.apache.commons.math.analysis.solvers UnivariateRealSolver solve

List of usage examples for org.apache.commons.math.analysis.solvers UnivariateRealSolver solve

Introduction

In this page you can find the example usage for org.apache.commons.math.analysis.solvers UnivariateRealSolver solve.

Prototype

@Deprecated
double solve(double min, double max, double startValue)
        throws ConvergenceException, FunctionEvaluationException, IllegalArgumentException;

Source Link

Document

Solve for a zero in the given interval, start at startValue.

Usage

From source file:ch.algotrader.option.OptionUtil.java

/**
 * Gets the implied volatility of a {@link Option} using a {@link UnivariateRealSolverFactory}.
 *//*from   www  .j a v a2s.co  m*/
@SuppressWarnings("deprecation")
public static double getImpliedVolatility(final double underlyingSpot, final double strike,
        final double currentValue, final double years, final double intrest, final double dividend,
        final OptionType type) throws MathException {

    if (years < 0) {
        throw new IllegalArgumentException("years cannot be negative");
    }

    double intrinsicValue = getIntrinsicValue(underlyingSpot, strike, type);
    if (currentValue <= intrinsicValue) {
        throw new IllegalArgumentException(
                "cannot calculate volatility if optionValue is below intrinsic Value");
    }

    UnivariateRealFunction function = volatility -> getOptionPrice(underlyingSpot, strike, volatility, years,
            intrest, dividend, type) - currentValue;

    UnivariateRealSolverFactory factory = UnivariateRealSolverFactory.newInstance();
    UnivariateRealSolver solver = factory.newDefaultSolver();
    solver.setAbsoluteAccuracy(0.0001);

    return solver.solve(function, 0.01, 2.0);
}

From source file:geogebra.kernel.AlgoRoots.java

/** Brent's algo
 * Copied from AlgoRootInterval.java.//from www  .  java  2 s  .  c  o  m
 */
public final static double calcSingleRoot(GeoFunction f, double left, double right) {
    UnivariateRealSolver rootFinder = UnivariateRealSolverFactory.newInstance().newBrentSolver(); //Apache lib

    if (!f.isDefined())
        return Double.NaN;

    double root = Double.NaN;
    Function fun = f.getFunction();

    try {
        // Brent's method
        root = rootFinder.solve(new RealRootAdapter(fun), left, right);
    } catch (Exception e) {
        try {
            // Let's try again by searching for a valid domain first
            double[] borders = RealRootUtil.getDefinedInterval(fun, left, right);
            root = rootFinder.solve(new RealRootAdapter(fun), borders[0], borders[1]);
        } catch (Exception ex) {
            root = Double.NaN;
        } //try-catch
    } //try-catch

    return root;
}

From source file:geogebra.common.kernel.algos.AlgoRoots.java

/**
 * Brent's algo Copied from AlgoRootInterval.java.
 */// ww w.  jav  a2 s.c  o m
public final static double calcSingleRoot(GeoFunction f, double left, double right) {
    UnivariateRealSolver rootFinder = UnivariateRealSolverFactory.newInstance().newBrentSolver(); // Apache lib

    if (!f.isDefined())
        return Double.NaN;

    double root = Double.NaN;
    Function fun = f.getFunction();

    try {
        // Brent's method
        root = rootFinder.solve(new RealRootAdapter(fun), left, right);
    } catch (Exception e) {
        try {
            // Let's try again by searching for a valid domain first
            double[] borders = RealRootUtil.getDefinedInterval(fun, left, right);
            root = rootFinder.solve(new RealRootAdapter(fun), borders[0], borders[1]);
        } catch (Exception ex) {
            root = Double.NaN;
        } // try-catch
    } // try-catch

    return root;
}