use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.
the class ErfTest method erfxHasLowError.
private void erfxHasLowError(BaseErf erf, double expected) {
RandomGenerator rg = new Well19937c(30051977);
int range = 8;
double max = 0;
for (int xi = -range; xi <= range; xi++) {
for (int i = 0; i < 5; i++) {
double x = xi + rg.nextDouble();
double o = erf.erf(x);
double e = org.apache.commons.math3.special.Erf.erf(x);
double error = Math.abs(o - e);
if (max < error)
max = error;
//System.out.printf("x=%f, e=%f, o=%f, error=%f\n", x, e, o, error);
Assert.assertTrue(error < expected);
}
}
System.out.printf("erfx %s max error = %g\n", erf.name, max);
}
use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.
the class PoissonCalculatorTest method instanceAndFastMethodIsApproximatelyEqualToStaticMethod.
@Test
public void instanceAndFastMethodIsApproximatelyEqualToStaticMethod() {
DoubleEquality eq = new DoubleEquality(3e-4, 0);
RandomGenerator rg = new Well19937c(30051977);
// Test for different x. The calculator approximation begins
int n = 100;
double[] u = new double[n];
double[] x = new double[n];
double e, o;
for (double testx : new double[] { Math.nextAfter(PoissonCalculator.APPROXIMATION_X, -1), PoissonCalculator.APPROXIMATION_X, Math.nextUp(PoissonCalculator.APPROXIMATION_X), PoissonCalculator.APPROXIMATION_X * 1.1, PoissonCalculator.APPROXIMATION_X * 2, PoissonCalculator.APPROXIMATION_X * 10 }) {
String X = Double.toString(testx);
Arrays.fill(x, testx);
PoissonCalculator pc = new PoissonCalculator(x);
e = PoissonCalculator.maximumLogLikelihood(x);
o = pc.getMaximumLogLikelihood();
System.out.printf("[%s] Instance MaxLL = %g vs %g (error = %g)\n", X, e, o, DoubleEquality.relativeError(e, o));
Assert.assertTrue("Instance Max LL not equal", eq.almostEqualRelativeOrAbsolute(e, o));
o = PoissonCalculator.fastMaximumLogLikelihood(x);
System.out.printf("[%s] Fast MaxLL = %g vs %g (error = %g)\n", X, e, o, DoubleEquality.relativeError(e, o));
Assert.assertTrue("Fast Max LL not equal", eq.almostEqualRelativeOrAbsolute(e, o));
// Generate data around the value
for (int i = 0; i < n; i++) u[i] = x[i] + rg.nextDouble() - 0.5;
e = PoissonCalculator.logLikelihood(u, x);
o = pc.logLikelihood(u);
System.out.printf("[%s] Instance LL = %g vs %g (error = %g)\n", X, e, o, DoubleEquality.relativeError(e, o));
Assert.assertTrue("Instance LL not equal", eq.almostEqualRelativeOrAbsolute(e, o));
o = PoissonCalculator.fastLogLikelihood(u, x);
System.out.printf("[%s] Fast LL = %g vs %g (error = %g)\n", X, e, o, DoubleEquality.relativeError(e, o));
Assert.assertTrue("Fast LL not equal", eq.almostEqualRelativeOrAbsolute(e, o));
e = PoissonCalculator.logLikelihoodRatio(u, x);
o = pc.getLogLikelihoodRatio(o);
System.out.printf("[%s] Instance LLR = %g vs %g (error = %g)\n", X, e, o, DoubleEquality.relativeError(e, o));
Assert.assertTrue("Instance LLR not equal", eq.almostEqualRelativeOrAbsolute(e, o));
}
}
use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.
the class ErfTest method erfxxHasLowError.
private void erfxxHasLowError(BaseErf erf, double expected) {
RandomGenerator rg = new Well19937c(30051977);
int range = 3;
double max = 0;
for (int xi = -range; xi <= range; xi++) {
for (int xi2 = -range; xi2 <= range; xi2++) {
for (int i = 0; i < 5; i++) {
double x = xi + rg.nextDouble();
for (int j = 0; j < 5; j++) {
double x2 = xi2 + rg.nextDouble();
double o = erf.erf(x, x2);
double e = org.apache.commons.math3.special.Erf.erf(x, x2);
double error = Math.abs(o - e);
if (max < error)
max = error;
//System.out.printf("x=%f, x2=%f, e=%f, o=%f, error=%f\n", x, x2, e, o, error);
Assert.assertTrue(error < expected);
}
}
}
}
System.out.printf("erfxx %s max error = %g\n", erf.name, max);
}
use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.
the class FastMLEGradient2ProcedureTest method createFakeData.
private double[] createFakeData(double[] params) {
int n = blockWidth * blockWidth;
RandomGenerator r = rdg.getRandomGenerator();
for (int i = 0; i < params.length; i++) {
params[i] = r.nextDouble();
}
double[] y = new double[n];
for (int i = 0; i < y.length; i++) {
y[i] = r.nextDouble() * 10;
}
return y;
}
use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.
the class LVMGradientProcedureTest method createFakeData.
private double[] createFakeData(double[] params) {
int n = blockWidth * blockWidth;
RandomGenerator r = rdg.getRandomGenerator();
for (int i = 0; i < params.length; i++) {
params[i] = r.nextDouble();
}
double[] y = new double[n];
for (int i = 0; i < y.length; i++) {
y[i] = r.nextDouble();
}
return y;
}
Aggregations