Search in sources :

Example 16 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class BinomialFitterTest method canFitBinomialWithUnknownNUsingLeastSquaresEstimator.

@SeededTest
void canFitBinomialWithUnknownNUsingLeastSquaresEstimator(RandomSeed seed) {
    Assumptions.assumeTrue(TestSettings.allow(nonEssentialTestComplexity));
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final boolean zeroTruncated = false;
    final boolean maximumLikelihood = false;
    for (final int n : N) {
        for (final double p : P) {
            fitBinomial(rg, n, p, zeroTruncated, maximumLikelihood, 1, n);
        }
    }
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 17 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class JumpDistanceAnalysisTest method canDoBenchmark.

/**
 * This is not actually a test but runs the fitting algorithm many times to collect benchmark data
 * to file.
 */
@SeededTest
void canDoBenchmark(RandomSeed seed) {
    // Skip this as it is slow
    Assumptions.assumeTrue(false);
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    out = null;
    try {
        final FileOutputStream fos = new FileOutputStream("JumpDistanceAnalysisTest.dat");
        out = new OutputStreamWriter(fos, "UTF-8");
        // Run the fitting to produce benchmark data for a mixed population of 2
        final int n = 2;
        writeHeader(n);
        for (int repeat = 10; repeat-- > 0; ) {
            resetData();
            for (final boolean mle : new boolean[] { true, false }) {
                for (int f = 1; f <= 9; f++) {
                    final double fraction = f / 10.0;
                    final String title = String.format("%s Dual=%.1f", (mle) ? "MLE" : "LSQ", fraction);
                    for (int samples = 500, k = 0; k < 6; samples *= 2, k++) {
                        for (int i = 0; i < D.length; i++) {
                            for (int j = i + 1; j < D.length; j++) {
                                try {
                                    fit(rg, title, samples, 0, new double[] { D[i], D[j] }, new double[] { fraction, 1 - fraction }, mle);
                                } catch (final AssertionError ex) {
                                // Carry on with the benchmarking
                                }
                                // If the fit had the correct N then no need to repeat
                                if (fitN == n) {
                                    continue;
                                }
                                try {
                                    fit(rg, title + " Fixed", samples, n, new double[] { D[i], D[j] }, new double[] { fraction, 1 - fraction }, mle);
                                } catch (final AssertionError ex) {
                                // Carry on with the benchmarking
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (final Exception ex) {
        throw new AssertionError("Failed to complete benchmark", ex);
    } finally {
        closeOutput();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 18 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class JumpDistanceAnalysisTest method fitDualPopulation.

// @formatter:on
private void fitDualPopulation(RandomSeed seed, boolean mle, double fraction) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MAXIMUM));
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final String title = String.format("%s Dual=%.1f", (mle) ? "MLE" : "LSQ", fraction);
    AssertionError error = null;
    for (int i = 0; i < D.length; i++) {
        NEXT_D: for (int j = i + 1; j < D.length; j++) {
            for (int samples = 500, k = 0; k < 6; samples *= 2, k++) {
                try {
                    fit(rg, title, samples, 0, new double[] { D[i], D[j] }, new double[] { fraction, 1 - fraction }, mle);
                    error = null;
                    continue NEXT_D;
                } catch (final AssertionError ex) {
                    error = ex;
                }
            }
            if (error != null) {
                throw error;
            }
        }
    }
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider)

Example 19 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class WPoissonGradientProcedureTest method gradientProcedureUnrolledComputesSameAsGradientProcedure.

private void gradientProcedureUnrolledComputesSameAsGradientProcedure(RandomSeed seed, int nparams, boolean precomputed) {
    final int iter = 10;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    createFakeParams(rng, nparams, iter, paramsList);
    final Gradient1Function func = new FakeGradientFunction(blockWidth, nparams);
    final double[] v = (precomputed) ? dataCache.computeIfAbsent(seed, WPoissonGradientProcedureTest::createData) : null;
    final IntArrayFormatSupplier msg = getMessage(nparams, "[%d] Observations: Not same linear @ %d");
    for (int i = 0; i < paramsList.size(); i++) {
        final double[] y = createFakeData(rng);
        final WPoissonGradientProcedure p1 = new WPoissonGradientProcedure(y, v, func);
        p1.computeFisherInformation(paramsList.get(i));
        final WPoissonGradientProcedure p2 = WPoissonGradientProcedureUtils.create(y, v, func);
        p2.computeFisherInformation(paramsList.get(i));
        // Exactly the same ...
        Assertions.assertArrayEquals(p1.getLinear(), p2.getLinear(), msg.set(1, i));
    }
}
Also used : Gradient1Function(uk.ac.sussex.gdsc.smlm.function.Gradient1Function) ArrayList(java.util.ArrayList) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) IntArrayFormatSupplier(uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 20 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class ChiSquaredDistributionTableTest method canPerformChiSquaredTest.

@SeededTest
void canPerformChiSquaredTest(RandomSeed seed) {
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    final ChiSquareTest test = new ChiSquareTest();
    for (final int n : new int[] { 10, 50, 100 }) {
        final double[] x = SimpleArrayUtils.newArray(n, 0.5, 1.0);
        final long[] l = new long[x.length];
        for (int i = 0; i < x.length; i++) {
            l[i] = GdscSmlmTestUtils.createPoissonSampler(rng, x[i]).sample();
        }
        final double chi2 = test.chiSquare(x, l);
        final double ep = test.chiSquareTest(x, l);
        final int df = x.length - 1;
        final double o = ChiSquaredDistributionTable.computeQValue(chi2, df);
        Assertions.assertEquals(ep, o, 1e-10);
        final ChiSquaredDistributionTable upperTable = ChiSquaredDistributionTable.createUpperTailed(o, df);
        final double upper = chi2 * 1.01;
        final double lower = chi2 * 0.99;
        Assertions.assertTrue(upperTable.reject(upper, df), "Upper did not reject higher");
        Assertions.assertFalse(upperTable.reject(o, df), "Upper did not reject actual value");
        Assertions.assertFalse(upperTable.reject(lower, df), "Upper did not accept lower");
    }
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) ChiSquareTest(org.apache.commons.math3.stat.inference.ChiSquareTest) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)213 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)145 SharedStateContinuousSampler (org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler)17 TimingService (uk.ac.sussex.gdsc.test.utils.TimingService)14 Rectangle (java.awt.Rectangle)13 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)13 SpeedTag (uk.ac.sussex.gdsc.test.junit5.SpeedTag)12 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)10 ArrayList (java.util.ArrayList)10 NormalizedGaussianSampler (org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler)9 StoredDataStatistics (uk.ac.sussex.gdsc.core.utils.StoredDataStatistics)8 CalibrationWriter (uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter)8 ContinuousSampler (org.apache.commons.rng.sampling.distribution.ContinuousSampler)6 ImageExtractor (uk.ac.sussex.gdsc.core.utils.ImageExtractor)6 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)6 Gaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction)6 FloatProcessor (ij.process.FloatProcessor)5 ErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.ErfGaussian2DFunction)5 TimingResult (uk.ac.sussex.gdsc.test.utils.TimingResult)5 File (java.io.File)4