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));
}
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;
}
}
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);
}
}
}
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));
}
}
}
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);
}
}
Aggregations