Search in sources :

Example 1 with BisectionZeroFinder

use of dr.math.iterations.BisectionZeroFinder in project beast-mcmc by beast-dev.

the class InverseGaussianDistribution method calculateZeroFinderApproximation.

/** Finds the approximation of the inverse Gaussian quantile using a zero finder
     * until it converges
     *
     * @param z            quantile
     * @param m            mean
     * @param shape        shape
     * @param min          min search value
     * @param max          max search value
     * @param initialGuess first guess of the quantile
     * @return estimated x value at quantile z
     */
private static double calculateZeroFinderApproximation(double z, double m, double shape, double min, double max, double initialGuess) {
    final InverseGaussianDistribution f = new InverseGaussianDistribution(m, shape);
    final double y = z;
    BisectionZeroFinder bisectionZeroFinder = new BisectionZeroFinder(new OneVariableFunction() {

        public double value(double x) {
            return f.cdf(x) - y;
        }
    }, min, max);
    bisectionZeroFinder.setInitialValue(initialGuess);
    bisectionZeroFinder.initializeIterations();
    double bestValue = Double.NaN;
    /* I found that the converged value is not necesssarily the best */
    double bestPrecision = 10;
    double precision = 10;
    double previousPrecision = 10;
    int count = 0;
    while (precision > 0.001 && count < 10) {
        bisectionZeroFinder.evaluateIteration();
        precision = Math.abs(f.cdf(bisectionZeroFinder.getResult()) - z);
        if (precision < bestPrecision) {
            bestPrecision = precision;
            bestValue = bisectionZeroFinder.getResult();
        } else if (previousPrecision == precision) {
            count++;
        }
        previousPrecision = precision;
    }
    bisectionZeroFinder.finalizeIterations();
    //return bisectionZeroFinder.getResult();
    return bestValue;
}
Also used : OneVariableFunction(dr.math.interfaces.OneVariableFunction) BisectionZeroFinder(dr.math.iterations.BisectionZeroFinder)

Example 2 with BisectionZeroFinder

use of dr.math.iterations.BisectionZeroFinder in project beast-mcmc by beast-dev.

the class InverseGaussianDistributionTest method testCDFAndQuantile2.

public void testCDFAndQuantile2() {
    final InverseGaussianDistribution f = new InverseGaussianDistribution(1, 1);
    for (double i = 0.01; i < 0.95; i += 0.01) {
        final double y = i;
        BisectionZeroFinder zeroFinder = new BisectionZeroFinder(new OneVariableFunction() {

            public double value(double x) {
                return f.cdf(x) - y;
            }
        }, 0.01, 100);
        zeroFinder.setMaximumIterations(100);
        zeroFinder.evaluate();
        assertEquals(f.quantile(i), zeroFinder.getResult(), 1e-3);
    }
}
Also used : InverseGaussianDistribution(dr.math.distributions.InverseGaussianDistribution) OneVariableFunction(dr.math.interfaces.OneVariableFunction) BisectionZeroFinder(dr.math.iterations.BisectionZeroFinder)

Example 3 with BisectionZeroFinder

use of dr.math.iterations.BisectionZeroFinder in project beast-mcmc by beast-dev.

the class LogNormalDistributionTest method testCDFAndQuantile2.

public void testCDFAndQuantile2() {
    final LogNormalDistribution f = new LogNormalDistribution(1, 1);
    for (double i = 0.01; i < 0.95; i += 0.01) {
        final double y = i;
        BisectionZeroFinder zeroFinder = new BisectionZeroFinder(new OneVariableFunction() {

            public double value(double x) {
                return f.cdf(x) - y;
            }
        }, 0.01, 100);
        zeroFinder.evaluate();
        assertEquals(f.quantile(i), zeroFinder.getResult(), 1e-6);
    }
}
Also used : OneVariableFunction(dr.math.interfaces.OneVariableFunction) BisectionZeroFinder(dr.math.iterations.BisectionZeroFinder) LogNormalDistribution(dr.math.distributions.LogNormalDistribution)

Example 4 with BisectionZeroFinder

use of dr.math.iterations.BisectionZeroFinder in project beast-mcmc by beast-dev.

the class InverseGaussianDistribution method calculateZeroFinderApproximation.

/** Finds the approximation of the inverse Gaussian quantile using a zero finder
     * given a set number of maximum iterations
     * UNUSED METHOD
     *
     * @param z             quantile
     * @param m             mean
     * @param shape         shape
     * @param numIterations number of iterations used to approximate value
     * @param min           min search value
     * @param max           max search value
     * @return estimated x value at quantile z
     */
private static double calculateZeroFinderApproximation(double z, double m, double shape, int numIterations, double min, double max) {
    final InverseGaussianDistribution f = new InverseGaussianDistribution(m, shape);
    final double y = z;
    BisectionZeroFinder bisectionZeroFinder = new BisectionZeroFinder(new OneVariableFunction() {

        public double value(double x) {
            return f.cdf(x) - y;
        }
    }, min, max);
    //}, 0.0001, 100000);
    bisectionZeroFinder.setMaximumIterations(numIterations);
    bisectionZeroFinder.evaluate();
    return bisectionZeroFinder.getResult();
}
Also used : OneVariableFunction(dr.math.interfaces.OneVariableFunction) BisectionZeroFinder(dr.math.iterations.BisectionZeroFinder)

Aggregations

OneVariableFunction (dr.math.interfaces.OneVariableFunction)4 BisectionZeroFinder (dr.math.iterations.BisectionZeroFinder)4 InverseGaussianDistribution (dr.math.distributions.InverseGaussianDistribution)1 LogNormalDistribution (dr.math.distributions.LogNormalDistribution)1