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