Search in sources :

Example 1 with Differentiable

use of de.lab4inf.math.Differentiable in project symja_android_library by axkr.

the class NewtonRootFinder method findroot.

/*
   * (non-Javadoc)
   * @see de.lab4inf.math.roots.AbstractRootFinder#root(de.lab4inf.math.Function, double, double, double)
   */
@Override
protected double findroot(final Function f, final double... guess) {
    Differentiable d = null;
    if (f instanceof Differentiable) {
        d = (Differentiable) f;
    } else {
        getLogger().warn(NO_DERIVATIVE_NOT_RECOMMENDED);
        d = new FunctionWrapper(f);
    }
    double x0 = guess[0];
    if (guess.length >= 2) {
        x0 = BisectionRootFinder.bisection(f, x0, guess[1], 0.005);
    }
    return newton(d, x0, getEpsilon());
}
Also used : Differentiable(de.lab4inf.math.Differentiable)

Example 2 with Differentiable

use of de.lab4inf.math.Differentiable in project symja_android_library by axkr.

the class NewtonRootFinder method modnewton.

/**
 * Modified Newton method for multiple roots.
 *
 * @param fct the function to find the root of
 * @param dF the first derivative of the function
 * @param x0 the initial guess as starting value
 * @param eps the accuracy to reach
 * @return the root
 */
public static double modnewton(final Function fct, final Function dF, final double x0, final double eps) {
    Function d2F;
    if (dF instanceof Differentiable) {
        d2F = ((Differentiable) dF).getDerivative();
    } else {
        getLogger().warn(NO_DERIVATIVE_NOT_RECOMMENDED);
        d2F = new Differentiator(dF);
    }
    return modnewton(fct, dF, d2F, x0, eps);
}
Also used : Function(de.lab4inf.math.Function) Differentiable(de.lab4inf.math.Differentiable) Differentiator(de.lab4inf.math.differentiation.Differentiator)

Example 3 with Differentiable

use of de.lab4inf.math.Differentiable in project symja_android_library by axkr.

the class ProbabilityDistribution method quantileGamma.

/**
 * Calculate the quantile of the gamma distribution. E.g. the inverse of the cumulative
 * distribution function.
 *
 * @param k the shape parameter
 * @param t the scale parameter
 * @param p the cumulative probability
 * @return the quantile xp with cdf(xp)=p
 */
public static double quantileGamma(final double k, final double t, final double p) {
    double x;
    checkProbabilty(p);
    if (p == 1.0) {
        return Double.MAX_VALUE;
    } else if (p == 0.0) {
        return 0.0;
    }
    // create the error function err(x) = cdf(x) - p
    // used for the Newton iteration.
    final Differentiable fct = new GammaError(k, p);
    /*
     * Find a first x0 starting guess for small,large and intermediate cases separately.
     */
    if (p < 0.05) {
        x = exp((lngamma(k) + log(p)) / k);
    } else if (p > 0.95) {
        x = -log(1 - p) + Gamma.lngamma(k);
    } else {
        final double sk = sqrt(k);
        x = quantileNormal(p);
        if (x < -sk) {
            x = k;
        } else {
            x = sk * x + k;
        }
    }
    x = quantile(x, fct);
    return t * x;
}
Also used : Differentiable(de.lab4inf.math.Differentiable)

Example 4 with Differentiable

use of de.lab4inf.math.Differentiable in project symja_android_library by axkr.

the class ProbabilityDistribution method quantileBeta.

/**
 * Calculate the quantile xp for the beta distribution. E.g. if p=cdf(xp) return xp.
 *
 * @param a first parameter of the beta function
 * @param b second parameter of the beta function
 * @param p the cdf
 * @return xp with cdf(xp)=p
 */
public static double quantileBeta(final double a, final double b, final double p) {
    checkProbabilty(p);
    double x;
    // create the error function err(x) = cdf(x) - p
    // used for the Newton iteration.
    final Differentiable fct = new BetaError(a, b, p);
    final double x0 = 0, x1 = 1, f0 = fct.f(x0), f1 = fct.f(x1);
    if (f0 == 0) {
        return x0;
    } else if (f1 == 0) {
        return x1;
    }
    // find an initial starting guess
    x = (a * p + b * (1 - p)) / (a + b);
    x = quantile(x, fct);
    return x;
}
Also used : Differentiable(de.lab4inf.math.Differentiable)

Example 5 with Differentiable

use of de.lab4inf.math.Differentiable in project symja_android_library by axkr.

the class ProbabilityDistribution method quantileStudent.

/**
 * Calculate the quantile xp for the student distribution. E.g. if p=cdf(xp) return xp.
 *
 * @param n degree of freedom for the student t-function
 * @param p the cdf
 * @return xp with cdf(xp)=p
 */
public static double quantileStudent(final int n, final double p) {
    checkProbabilty(p);
    double x;
    // create the error function err(x) = cdf(x) - p
    // used for the Newton iteration.
    final Differentiable fct = new StudentError(n, p);
    final double x0 = 0, x1 = 1, f0 = fct.f(x0), f1 = fct.f(x1);
    if (f0 == 0) {
        return x0;
    } else if (f1 == 0) {
        return x1;
    }
    // find an initial starting guess
    // student goes for larger n like normal gaussian...
    x = quantileNormal(p);
    x = quantile(x, fct);
    return x;
}
Also used : Differentiable(de.lab4inf.math.Differentiable)

Aggregations

Differentiable (de.lab4inf.math.Differentiable)6 Function (de.lab4inf.math.Function)1 Differentiator (de.lab4inf.math.differentiation.Differentiator)1