Search in sources :

Example 11 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.

the class ErfTest method erfxHasLowError.

private void erfxHasLowError(BaseErf erf, double expected) {
    RandomGenerator rg = new Well19937c(30051977);
    int range = 8;
    double max = 0;
    for (int xi = -range; xi <= range; xi++) {
        for (int i = 0; i < 5; i++) {
            double x = xi + rg.nextDouble();
            double o = erf.erf(x);
            double e = org.apache.commons.math3.special.Erf.erf(x);
            double error = Math.abs(o - e);
            if (max < error)
                max = error;
            //System.out.printf("x=%f, e=%f, o=%f, error=%f\n", x, e, o, error);
            Assert.assertTrue(error < expected);
        }
    }
    System.out.printf("erfx %s max error = %g\n", erf.name, max);
}
Also used : Well19937c(org.apache.commons.math3.random.Well19937c) RandomGenerator(org.apache.commons.math3.random.RandomGenerator)

Example 12 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.

the class PoissonCalculatorTest method instanceAndFastMethodIsApproximatelyEqualToStaticMethod.

@Test
public void instanceAndFastMethodIsApproximatelyEqualToStaticMethod() {
    DoubleEquality eq = new DoubleEquality(3e-4, 0);
    RandomGenerator rg = new Well19937c(30051977);
    // Test for different x. The calculator approximation begins
    int n = 100;
    double[] u = new double[n];
    double[] x = new double[n];
    double e, o;
    for (double testx : new double[] { Math.nextAfter(PoissonCalculator.APPROXIMATION_X, -1), PoissonCalculator.APPROXIMATION_X, Math.nextUp(PoissonCalculator.APPROXIMATION_X), PoissonCalculator.APPROXIMATION_X * 1.1, PoissonCalculator.APPROXIMATION_X * 2, PoissonCalculator.APPROXIMATION_X * 10 }) {
        String X = Double.toString(testx);
        Arrays.fill(x, testx);
        PoissonCalculator pc = new PoissonCalculator(x);
        e = PoissonCalculator.maximumLogLikelihood(x);
        o = pc.getMaximumLogLikelihood();
        System.out.printf("[%s] Instance MaxLL = %g vs %g (error = %g)\n", X, e, o, DoubleEquality.relativeError(e, o));
        Assert.assertTrue("Instance Max LL not equal", eq.almostEqualRelativeOrAbsolute(e, o));
        o = PoissonCalculator.fastMaximumLogLikelihood(x);
        System.out.printf("[%s] Fast MaxLL = %g vs %g (error = %g)\n", X, e, o, DoubleEquality.relativeError(e, o));
        Assert.assertTrue("Fast Max LL not equal", eq.almostEqualRelativeOrAbsolute(e, o));
        // Generate data around the value
        for (int i = 0; i < n; i++) u[i] = x[i] + rg.nextDouble() - 0.5;
        e = PoissonCalculator.logLikelihood(u, x);
        o = pc.logLikelihood(u);
        System.out.printf("[%s] Instance LL = %g vs %g (error = %g)\n", X, e, o, DoubleEquality.relativeError(e, o));
        Assert.assertTrue("Instance LL not equal", eq.almostEqualRelativeOrAbsolute(e, o));
        o = PoissonCalculator.fastLogLikelihood(u, x);
        System.out.printf("[%s] Fast LL = %g vs %g (error = %g)\n", X, e, o, DoubleEquality.relativeError(e, o));
        Assert.assertTrue("Fast LL not equal", eq.almostEqualRelativeOrAbsolute(e, o));
        e = PoissonCalculator.logLikelihoodRatio(u, x);
        o = pc.getLogLikelihoodRatio(o);
        System.out.printf("[%s] Instance LLR = %g vs %g (error = %g)\n", X, e, o, DoubleEquality.relativeError(e, o));
        Assert.assertTrue("Instance LLR not equal", eq.almostEqualRelativeOrAbsolute(e, o));
    }
}
Also used : DoubleEquality(gdsc.core.utils.DoubleEquality) Well19937c(org.apache.commons.math3.random.Well19937c) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) Test(org.junit.Test)

Example 13 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.

the class ErfTest method erfxxHasLowError.

private void erfxxHasLowError(BaseErf erf, double expected) {
    RandomGenerator rg = new Well19937c(30051977);
    int range = 3;
    double max = 0;
    for (int xi = -range; xi <= range; xi++) {
        for (int xi2 = -range; xi2 <= range; xi2++) {
            for (int i = 0; i < 5; i++) {
                double x = xi + rg.nextDouble();
                for (int j = 0; j < 5; j++) {
                    double x2 = xi2 + rg.nextDouble();
                    double o = erf.erf(x, x2);
                    double e = org.apache.commons.math3.special.Erf.erf(x, x2);
                    double error = Math.abs(o - e);
                    if (max < error)
                        max = error;
                    //System.out.printf("x=%f, x2=%f, e=%f, o=%f, error=%f\n", x, x2, e, o, error);
                    Assert.assertTrue(error < expected);
                }
            }
        }
    }
    System.out.printf("erfxx %s max error = %g\n", erf.name, max);
}
Also used : Well19937c(org.apache.commons.math3.random.Well19937c) RandomGenerator(org.apache.commons.math3.random.RandomGenerator)

Example 14 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.

the class FastMLEGradient2ProcedureTest method createFakeData.

private double[] createFakeData(double[] params) {
    int n = blockWidth * blockWidth;
    RandomGenerator r = rdg.getRandomGenerator();
    for (int i = 0; i < params.length; i++) {
        params[i] = r.nextDouble();
    }
    double[] y = new double[n];
    for (int i = 0; i < y.length; i++) {
        y[i] = r.nextDouble() * 10;
    }
    return y;
}
Also used : RandomGenerator(org.apache.commons.math3.random.RandomGenerator)

Example 15 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.

the class LVMGradientProcedureTest method createFakeData.

private double[] createFakeData(double[] params) {
    int n = blockWidth * blockWidth;
    RandomGenerator r = rdg.getRandomGenerator();
    for (int i = 0; i < params.length; i++) {
        params[i] = r.nextDouble();
    }
    double[] y = new double[n];
    for (int i = 0; i < y.length; i++) {
        y[i] = r.nextDouble();
    }
    return y;
}
Also used : RandomGenerator(org.apache.commons.math3.random.RandomGenerator)

Aggregations

RandomGenerator (org.apache.commons.math3.random.RandomGenerator)70 Well19937c (org.apache.commons.math3.random.Well19937c)25 Random (java.util.Random)20 Test (org.testng.annotations.Test)18 RandomGeneratorFactory (org.apache.commons.math3.random.RandomGeneratorFactory)16 Assert (org.testng.Assert)16 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)14 Collectors (java.util.stream.Collectors)12 IntStream (java.util.stream.IntStream)12 Arrays (java.util.Arrays)10 List (java.util.List)10 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)10 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)9 NormalDistribution (org.apache.commons.math3.distribution.NormalDistribution)8 ModeledSegment (org.broadinstitute.hellbender.tools.exome.ModeledSegment)8 AllelicCountCollection (org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection)8 java.util (java.util)6 GammaDistribution (org.apache.commons.math3.distribution.GammaDistribution)6 BinomialDistribution (org.apache.commons.math3.distribution.BinomialDistribution)5