Search in sources :

Example 46 with DoubleDoubleBiPredicate

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

the class LogFactorialTest method testLogFactorial.

/**
 * Test the factorial of a fractional number against reference values.
 */
@ParameterizedTest
@CsvSource({ // @formatter:off
"0.25, -0.098271836421813169", "0.75, -0.084401121020485553", "1.125, 0.057759851530343874", "1.75, 0.47521466691493708", "2.75, 1.4868155785934172", "3.25, 2.1144569274503713", "4.75, 4.3667160366222859", "6.25, 7.0521854507385395", "12.75, 21.903762491828793", "17.125, 33.86331080630174", "27.75, 67.053353891702798", "57.75, 179.43956662288721", "97.75, 353.39188783368269", "137.75, 544.11168543338499", "154.25, 626.38890784701778", "164.25, 677.12339194008723", "170.25, 707.85792962271012", "170.624, 709.78077443669906", // Limit
"170.6243769563027, 709.78271289338397", // Infinite for n!
"170.62437695630274, 709.78271289338409", "170.625, 709.78591682948354", "171.5, 714.28774629753423", "201.25, 869.86189472479612", "271.25, 1252.2956116469838", "411.25, 2068.075277298678" // @formatter:on
})
void testLogFactorial(double n, double expected) {
    final DoubleDoubleBiPredicate tol = TestHelper.doublesAreClose(1e-15);
    TestAssertions.assertTest(expected, LogFactorial.value(n), tol, () -> Double.toString(n));
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 47 with DoubleDoubleBiPredicate

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

the class LogFactorialTest method testLogFactorialInteger.

@Test
void testLogFactorialInteger() {
    // Start at 0!
    BigInteger value = BigInteger.ONE;
    final ArrayList<BigInteger> values = new ArrayList<>(MAX_N + 1);
    values.add(value);
    for (int n = 1; n <= MAX_N; n++) {
        // n! = (n-1)! * n
        value = value.multiply(BigInteger.valueOf(n));
        values.add(value);
        final double expected = Math.log(value.doubleValue());
        Assertions.assertEquals(expected, LogFactorial.value(n));
        Assertions.assertEquals(expected, LogFactorial.value((double) n));
    }
    // Compute some more by log summation
    double base = Math.log(value.doubleValue());
    final DoubleDoubleBiPredicate tol = TestHelper.doublesAreClose(1e-15);
    for (int n = MAX_N + 1; n < MAX_N * 3; n++) {
        base += Math.log(n);
        final double v1 = LogFactorial.value(n);
        final double v2 = LogFactorial.value((double) n);
        Assertions.assertEquals(v1, v2);
        TestAssertions.assertTest(base, v1, tol);
        // Prevent drift in the summation
        base = v1;
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 48 with DoubleDoubleBiPredicate

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

the class PoissonCalculatorTest method canComputeLikelihoodForIntegerData.

@Test
void canComputeLikelihoodForIntegerData() {
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-10, 0);
    for (final double u : photons) {
        final PoissonDistribution pd = new PoissonDistribution(u);
        for (int x = 0; x < 100; x++) {
            double expected = pd.probability(x);
            double observed = PoissonCalculator.likelihood(u, x);
            if (expected > 1e-100) {
                TestAssertions.assertTest(expected, observed, predicate);
            }
            expected = pd.logProbability(x);
            observed = PoissonCalculator.logLikelihood(u, x);
            TestAssertions.assertTest(expected, observed, predicate);
        }
    }
}
Also used : PoissonDistribution(org.apache.commons.math3.distribution.PoissonDistribution) DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test)

Example 49 with DoubleDoubleBiPredicate

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

the class PoissonGammaFunctionTest method canComputeSeparatelyAtC0.

@Test
void canComputeSeparatelyAtC0() {
    final TDoubleArrayList list = new TDoubleArrayList();
    for (int exp = -12; exp < 6; exp++) {
        list.add(Math.pow(10, exp * 0.5));
    }
    for (int x = 2; x < 10; x++) {
        list.add(x / 10.0);
    }
    for (double x = 11; x <= 20; x++) {
        list.add(x / 10.0);
    }
    final double[] p = list.toArray();
    final boolean report = logger.isLoggable(Level.INFO) && TestSettings.allow(TestComplexity.MEDIUM);
    if (report) {
        Arrays.sort(p);
    }
    final double m = 5;
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-8, 0);
    for (final double x : p) {
        final double e = PoissonGammaFunction.poissonGamma(0, x, m);
        // Test the function can be separated into the dirac and the rest
        final double dirac = PoissonGammaFunction.dirac(x);
        final double p0 = PoissonGammaFunction.poissonGammaN(0, x, m);
        TestAssertions.assertTest(e, dirac + p0, predicate);
        // For reporting
        if (report) {
            final double p01 = PoissonGammaFunction.poissonGammaN(1e-10, x, m);
            logger.log(TestLogUtils.getRecord(Level.INFO, "p=%g  Dirac=%s   p0=%s (dirac:p0=%s)   p01=%s  (p0:p01 = %s)", x, dirac, p0, dirac / p0, // uk.ac.sussex.gdsc.core.utils.DoubleEquality.relativeError(p0, dirac),
            p01, p0 / p01));
        }
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) TDoubleArrayList(gnu.trove.list.array.TDoubleArrayList) Test(org.junit.jupiter.api.Test)

Example 50 with DoubleDoubleBiPredicate

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

the class AstigmatismModelManagerTest method canConvertModel.

private static void canConvertModel(DistanceUnit zDistanceUnit, DistanceUnit sdDistanceUnit) {
    // Use a reasonable z-depth function from the Smith, et al (2010) paper (page 377)
    final double sx = 1.08;
    final double sy = 1.01;
    final double gamma = 0.389;
    final double d = 0.531;
    final double Ax = -0.0708;
    final double Bx = -0.073;
    final double Ay = 0.164;
    final double By = 0.0417;
    final double nmPerPixel = 100;
    // Ax = Ay = 0;
    // Bx = By = 0;
    final AstigmatismModel.Builder builder = AstigmatismModel.newBuilder();
    builder.setGamma(gamma);
    builder.setD(d);
    builder.setS0X(sx);
    builder.setAx(Ax);
    builder.setBx(Bx);
    builder.setS0Y(sy);
    builder.setAy(Ay);
    builder.setBy(By);
    builder.setZDistanceUnit(DistanceUnit.UM);
    builder.setSDistanceUnit(DistanceUnit.PIXEL);
    builder.setNmPerPixel(nmPerPixel);
    final AstigmatismModel model1 = builder.build();
    final AstigmatismModel model2 = AstigmatismModelManager.convert(model1, zDistanceUnit, sdDistanceUnit);
    final AstigmatismZModel m1 = AstigmatismModelManager.create(model1);
    final AstigmatismZModel m2 = AstigmatismModelManager.create(model2);
    final TypeConverter<DistanceUnit> zc = UnitConverterUtils.createConverter(DistanceUnit.UM, zDistanceUnit, nmPerPixel);
    final TypeConverter<DistanceUnit> sc = UnitConverterUtils.createConverter(DistanceUnit.PIXEL, sdDistanceUnit, nmPerPixel);
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-8, 0);
    for (double z = -0.5; z <= 0.5; z += 0.1) {
        final double e = sc.convert(m1.getSx(z));
        final double o = m2.getSx(zc.convert(z));
        // logger.fine(FunctionUtils.getSupplier("%f vs %f", e, o);
        TestAssertions.assertTest(e, o, predicate);
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) AstigmatismModel(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel) AstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.AstigmatismZModel) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)

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