Example usage for org.apache.commons.math3.analysis.solvers UnivariateSolverUtils solve

List of usage examples for org.apache.commons.math3.analysis.solvers UnivariateSolverUtils solve

Introduction

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

Prototype

public static double solve(UnivariateFunction function, double x0, double x1)
        throws NullArgumentException, NoBracketingException 

Source Link

Document

Convenience method to find a zero of a univariate real function.

Usage

From source file:gedi.util.math.stat.distributions.GeneralizedExtremeValueDistribution.java

public static GeneralizedExtremeValueDistribution fitDataByMoments(double[] data, int start, int end) {
    int n = end - start;
    if (n < 2)
        throw new RuntimeException("Too few data!");
    Arrays.sort(data, start, end);

    // compute moments
    final double[] b = new double[3];
    for (int j = 1; j <= n; j++) {
        int index = j - 1 - start;
        b[0] += data[index];//ww w. j a v  a2  s.c om
        b[1] += data[index] * ((j - 1.0) / (n - 1.0));
        b[2] += data[index] * ((j - 1.0) / (n - 1.0)) * ((j - 2.0) / (n - 2.0));
    }
    b[0] /= n;
    b[1] /= n;
    b[2] /= n;

    // solve parameters
    UnivariateFunction f = new UnivariateFunction() {
        public double value(double k) {
            return (3 * b[2] - b[0]) / (2 * b[1] - b[0]) - (1 - Math.pow(3, -k)) / (1 - Math.pow(2, -k));
        }
    };
    double k;

    if (Math.signum(f.value(-0.5)) != Math.signum(f.value(0.5)))
        k = UnivariateSolverUtils.solve(f, -0.5, 0.5);
    else {
        double c = (2 * b[1] - b[0]) / (3 * b[2] - b[0]) - Math.log(2) / Math.log(3);
        k = 7.859 * c + 2.9554 * c * c;
    }

    double g = Gamma.gamma(1 + k);
    double alpha = ((2 * b[1] - b[0]) * k) / (g * (1 - Math.pow(2, -k)));
    double xi = b[0] + alpha * (g - 1) / k;

    double location = xi;
    double scale = alpha;
    double shape = -k;

    return new GeneralizedExtremeValueDistribution(location, scale, shape);
}