Search in sources :

Example 41 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class ErfTest method canComputePower16.

@Test
void canComputePower16() {
    final DoubleDoubleBiPredicate equality = TestHelper.doublesAreClose(1e-10, 0);
    for (int i = -10; i <= 10; i++) {
        for (final double d : new double[] { 0, 0.1, 0.01, 0.001 }) {
            final double f = i + d;
            final double e = Math.pow(f, 16);
            final double o = uk.ac.sussex.gdsc.smlm.function.Erf.pow16(f);
            TestAssertions.assertTest(e, o, equality, () -> "x=" + f);
        }
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test)

Example 42 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class ErfTest method canComputePower4.

@Test
void canComputePower4() {
    final DoubleDoubleBiPredicate equality = TestHelper.doublesAreClose(1e-10, 0);
    for (int i = -10; i <= 10; i++) {
        for (final double d : new double[] { 0, 0.1, 0.01, 0.001 }) {
            final double f = i + d;
            final double e = Math.pow(f, 4);
            final double o = uk.ac.sussex.gdsc.smlm.function.Erf.pow4(f);
            TestAssertions.assertTest(e, o, equality, () -> "x=" + f);
        }
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test)

Example 43 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class GradientCalculatorSpeedTest method mleGradientCalculatorComputesLikelihood.

@SeededTest
void mleGradientCalculatorComputesLikelihood() {
    // @formatter:off
    final NonLinearFunction func = new NonLinearFunction() {

        double u;

        @Override
        public void initialise(double[] a) {
            u = a[0];
        }

        @Override
        public int[] gradientIndices() {
            return null;
        }

        @Override
        public double eval(int x, double[] dyda) {
            return 0;
        }

        @Override
        public double eval(int x) {
            return u;
        }

        @Override
        public double evalw(int x, double[] dyda, double[] w) {
            return 0;
        }

        @Override
        public double evalw(int x, double[] w) {
            return 0;
        }

        @Override
        public boolean canComputeWeights() {
            return false;
        }

        @Override
        public int getNumberOfGradients() {
            return 0;
        }
    };
    // @formatter:on
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-10, 0);
    final int[] xx = SimpleArrayUtils.natural(100);
    final double[] xxx = SimpleArrayUtils.newArray(100, 0, 1.0);
    for (final double u : new double[] { 0.79, 2.5, 5.32 }) {
        double ll = 0;
        double oll = 0;
        final PoissonDistribution pd = new PoissonDistribution(u);
        // The logLikelihood function for the entire array of observations is then asserted.
        for (final int x : xx) {
            double obs = PoissonCalculator.likelihood(u, x);
            double exp = pd.probability(x);
            TestAssertions.assertTest(exp, obs, predicate, "likelihood");
            obs = PoissonCalculator.logLikelihood(u, x);
            exp = pd.logProbability(x);
            TestAssertions.assertTest(exp, obs, predicate, "log likelihood");
            oll += obs;
            ll += exp;
        }
        final MleGradientCalculator gc = new MleGradientCalculator(1);
        final double o = gc.logLikelihood(xxx, new double[] { u }, func);
        Assertions.assertEquals(oll, o, "sum log likelihood should exactly match the PoissonCalculator");
        TestAssertions.assertTest(ll, o, predicate, "sum log likelihood");
    }
}
Also used : PoissonDistribution(org.apache.commons.math3.distribution.PoissonDistribution) DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) NonLinearFunction(uk.ac.sussex.gdsc.smlm.function.NonLinearFunction) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 44 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class GradientCalculatorSpeedTest method mleCalculatorComputesLogLikelihoodRatio.

@SeededTest
void mleCalculatorComputesLogLikelihoodRatio(RandomSeed seed) {
    final EllipticalGaussian2DFunction func = new EllipticalGaussian2DFunction(1, blockWidth, blockWidth);
    final int n = blockWidth * blockWidth;
    final double[] a = new double[1 + Gaussian2DFunction.PARAMETERS_PER_PEAK];
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-10, 0);
    for (int run = 5; run-- > 0; ) {
        a[Gaussian2DFunction.BACKGROUND] = random(rng, background);
        a[Gaussian2DFunction.SIGNAL] = random(rng, amplitude);
        a[Gaussian2DFunction.ANGLE] = random(rng, angle);
        a[Gaussian2DFunction.X_POSITION] = random(rng, xpos);
        a[Gaussian2DFunction.Y_POSITION] = random(rng, ypos);
        a[Gaussian2DFunction.X_SD] = random(rng, xwidth);
        a[Gaussian2DFunction.Y_SD] = random(rng, ywidth);
        // Simulate Poisson process
        func.initialise(a);
        final double[] u = new double[n];
        final double[] x = new double[n];
        for (int i = 0; i < n; i++) {
            final double value = func.eval(i);
            u[i] = value;
            // Add random Poisson noise
            if (value > 0) {
                x[i] = new PoissonSampler(rng, value).sample();
            }
        }
        final int ng = func.getNumberOfGradients();
        final double[][] alpha = new double[ng][ng];
        final double[] beta = new double[ng];
        final GradientCalculator calc = GradientCalculatorUtils.newCalculator(ng, true);
        final double llr = PoissonCalculator.logLikelihoodRatio(u, x);
        final double llr2 = calc.findLinearised(n, x, a, alpha, beta, func);
        // logger.fine(FunctionUtils.getSupplier("llr=%f, llr2=%f", llr, llr2));
        TestAssertions.assertTest(llr, llr2, predicate, "Log-likelihood ratio");
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) SingleEllipticalGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.SingleEllipticalGaussian2DFunction) EllipticalGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.EllipticalGaussian2DFunction) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) PoissonSampler(org.apache.commons.rng.sampling.distribution.PoissonSampler) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 45 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class FastMleGradient2ProcedureTest method gradientProcedureComputesSameLogLikelihoodAsMleGradientCalculator.

@SeededTest
void gradientProcedureComputesSameLogLikelihoodAsMleGradientCalculator(RandomSeed seed) {
    final DoubleDoubleBiPredicate equality = TestHelper.doublesAreClose(1e-5, 0);
    gradientProcedureComputesSameLogLikelihoodAsMleGradientCalculator(seed, 4, equality);
    gradientProcedureComputesSameLogLikelihoodAsMleGradientCalculator(seed, 5, equality);
    gradientProcedureComputesSameLogLikelihoodAsMleGradientCalculator(seed, 6, equality);
    gradientProcedureComputesSameLogLikelihoodAsMleGradientCalculator(seed, 11, equality);
    gradientProcedureComputesSameLogLikelihoodAsMleGradientCalculator(seed, 21, equality);
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)63 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)27 Test (org.junit.jupiter.api.Test)22 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)12 PoissonDistribution (org.apache.commons.math3.distribution.PoissonDistribution)6 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)4 ArrayList (java.util.ArrayList)4 MixtureMultivariateGaussianDistribution (uk.ac.sussex.gdsc.smlm.math3.distribution.fitting.MultivariateGaussianMixtureExpectationMaximization.MixtureMultivariateGaussianDistribution)4 MultivariateGaussianDistribution (uk.ac.sussex.gdsc.smlm.math3.distribution.fitting.MultivariateGaussianMixtureExpectationMaximization.MixtureMultivariateGaussianDistribution.MultivariateGaussianDistribution)4 UnivariateFunction (org.apache.commons.math3.analysis.UnivariateFunction)3 SimpsonIntegrator (org.apache.commons.math3.analysis.integration.SimpsonIntegrator)3 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)3 RealMatrix (org.apache.commons.math3.linear.RealMatrix)3 RealVector (org.apache.commons.math3.linear.RealVector)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 BigDecimal (java.math.BigDecimal)2 MathContext (java.math.MathContext)2 MixtureMultivariateNormalDistribution (org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution)2 MultivariateNormalDistribution (org.apache.commons.math3.distribution.MultivariateNormalDistribution)2 MultivariateJacobianFunction (org.apache.commons.math3.fitting.leastsquares.MultivariateJacobianFunction)2