use of gdsc.core.utils.DoubleEquality in project GDSC-SMLM by aherbert.
the class LVMGradientProcedureTest method gradientProcedureComputesGradient.
private void gradientProcedureComputesGradient(ErfGaussian2DFunction func, Type type, boolean precomputed) {
int nparams = func.getNumberOfGradients();
int[] indices = func.gradientIndices();
int iter = 100;
rdg = new RandomDataGenerator(new Well19937c(30051977));
ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
ArrayList<double[]> yList = new ArrayList<double[]>(iter);
createData(1, iter, paramsList, yList, true);
double delta = 1e-3;
DoubleEquality eq = new DoubleEquality(1e-3, 1e-3);
final double[] b = (precomputed) ? new double[func.size()] : null;
for (int i = 0; i < paramsList.size(); i++) {
double[] y = yList.get(i);
double[] a = paramsList.get(i);
double[] a2 = a.clone();
LVMGradientProcedure p;
if (precomputed) {
// Mock fitting part of the function already
for (int j = 0; j < b.length; j++) {
b[j] = y[j] * 0.5;
}
p = LVMGradientProcedureFactory.create(y, PrecomputedGradient1Function.wrapGradient1Function(func, b), type);
} else
p = LVMGradientProcedureFactory.create(y, func, type);
p.gradient(a);
//double s = p.value;
double[] beta = p.beta.clone();
for (int j = 0; j < nparams; j++) {
int k = indices[j];
double d = Precision.representableDelta(a[k], (a[k] == 0) ? 1e-3 : a[k] * delta);
a2[k] = a[k] + d;
p.value(a2);
double s1 = p.value;
a2[k] = a[k] - d;
p.value(a2);
double s2 = p.value;
a2[k] = a[k];
// Apply a factor of -2 to compute the actual gradients:
// See Numerical Recipes in C++, 2nd Ed. Equation 15.5.6 for Nonlinear Models
beta[j] *= -2;
double gradient = (s1 - s2) / (2 * d);
//System.out.printf("[%d,%d] %f (%s %f+/-%f) %f ?= %f\n", i, k, s, func.getName(k), a[k], d, beta[j],
// gradient);
Assert.assertTrue("Not same gradient @ " + j, eq.almostEqualRelativeOrAbsolute(beta[j], gradient));
}
}
}
use of gdsc.core.utils.DoubleEquality in project GDSC-SMLM by aherbert.
the class FastMLEGradient2ProcedureTest method gradientCalculatorComputesGradient.
private void gradientCalculatorComputesGradient(ErfGaussian2DFunction func) {
// Check the first and second derivatives
int nparams = func.getNumberOfGradients();
int[] indices = func.gradientIndices();
int iter = 100;
rdg = new RandomDataGenerator(new Well19937c(30051977));
ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
ArrayList<double[]> yList = new ArrayList<double[]>(iter);
createData(1, iter, paramsList, yList, true);
double delta = 1e-5;
DoubleEquality eq = new DoubleEquality(1e-4, 1e-3);
for (int i = 0; i < paramsList.size(); i++) {
double[] y = yList.get(i);
double[] a = paramsList.get(i);
double[] a2 = a.clone();
FastMLEGradient2Procedure p = FastMLEGradient2ProcedureFactory.create(y, func);
//double ll = p.computeLogLikelihood(a);
p.computeSecondDerivative(a);
double[] d1 = p.d1.clone();
double[] d2 = p.d2.clone();
for (int j = 0; j < nparams; j++) {
int k = indices[j];
double d = Precision.representableDelta(a[k], (a[k] == 0) ? delta : a[k] * delta);
a2[k] = a[k] + d;
double llh = p.computeLogLikelihood(a2);
p.computeFirstDerivative(a2);
double[] d1h = p.d1.clone();
a2[k] = a[k] - d;
double lll = p.computeLogLikelihood(a2);
p.computeFirstDerivative(a2);
double[] d1l = p.d1.clone();
a2[k] = a[k];
double gradient1 = (llh - lll) / (2 * d);
double gradient2 = (d1h[j] - d1l[j]) / (2 * d);
//System.out.printf("[%d,%d] ll - %f (%s %f+/-%f) d1 %f ?= %f : d2 %f ?= %f\n", i, k, ll, func.getName(k), a[k], d,
// gradient1, d1[j], gradient2, d2[j]);
Assert.assertTrue("Not same gradient1 @ " + j, eq.almostEqualRelativeOrAbsolute(gradient1, d1[j]));
Assert.assertTrue("Not same gradient2 @ " + j, eq.almostEqualRelativeOrAbsolute(gradient2, d2[j]));
}
}
}
use of gdsc.core.utils.DoubleEquality in project GDSC-SMLM by aherbert.
the class PoissonLikelihoodWrapperTest method fitNBEllipticalComputesGradient.
@Test
public void fitNBEllipticalComputesGradient() {
// The elliptical function gradient evaluation is worse
DoubleEquality tmp = eq;
eq = eqPerDatum;
functionComputesGradient(GaussianFunctionFactory.FIT_SIMPLE_NB_ELLIPTICAL);
eq = tmp;
}
use of gdsc.core.utils.DoubleEquality in project GDSC-SMLM by aherbert.
the class SCMOSLikelihoodWrapperTest method fitEllipticalComputesGradient.
@Test
public void fitEllipticalComputesGradient() {
// The elliptical function gradient evaluation is worse
DoubleEquality tmp = eq;
eq = eqPerDatum;
functionComputesGradient(GaussianFunctionFactory.FIT_ELLIPTICAL);
eq = tmp;
}
use of gdsc.core.utils.DoubleEquality in project GDSC-SMLM by aherbert.
the class PoissonLikelihoodWrapperTest method fitEllipticalComputesGradient.
@Test
public void fitEllipticalComputesGradient() {
// The elliptical function gradient evaluation is worse
DoubleEquality tmp = eq;
eq = eqPerDatum;
functionComputesGradient(GaussianFunctionFactory.FIT_ELLIPTICAL);
eq = tmp;
}
Aggregations