Example usage for org.apache.commons.math.analysis.solvers UnivariateRealSolverFactory newDefaultSolver

List of usage examples for org.apache.commons.math.analysis.solvers UnivariateRealSolverFactory newDefaultSolver

Introduction

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

Prototype

public abstract UnivariateRealSolver newDefaultSolver();

Source Link

Document

Create a new UnivariateRealSolver .

Usage

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

/**
 * Gets the implied volatility of a {@link Option} using a {@link UnivariateRealSolverFactory}.
 *///ww w .  ja v  a  2  s .  com
@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);
}