Search in sources :

Example 11 with DoubleEquality

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));
        }
    }
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) ArrayList(java.util.ArrayList) DoubleEquality(gdsc.core.utils.DoubleEquality) Well19937c(org.apache.commons.math3.random.Well19937c)

Example 12 with DoubleEquality

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]));
        }
    }
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) ArrayList(java.util.ArrayList) DoubleEquality(gdsc.core.utils.DoubleEquality) Well19937c(org.apache.commons.math3.random.Well19937c)

Example 13 with DoubleEquality

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;
}
Also used : DoubleEquality(gdsc.core.utils.DoubleEquality) Test(org.junit.Test)

Example 14 with DoubleEquality

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;
}
Also used : DoubleEquality(gdsc.core.utils.DoubleEquality) Test(org.junit.Test)

Example 15 with DoubleEquality

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;
}
Also used : DoubleEquality(gdsc.core.utils.DoubleEquality) Test(org.junit.Test)

Aggregations

DoubleEquality (gdsc.core.utils.DoubleEquality)15 Well19937c (org.apache.commons.math3.random.Well19937c)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 RandomDataGenerator (org.apache.commons.math3.random.RandomDataGenerator)6 Gaussian2DFunction (gdsc.smlm.function.gaussian.Gaussian2DFunction)3 EllipticalGaussian2DFunction (gdsc.smlm.function.gaussian.EllipticalGaussian2DFunction)2 ValueProcedure (gdsc.smlm.function.ValueProcedure)1 SingleCircularGaussian2DFunction (gdsc.smlm.function.gaussian.SingleCircularGaussian2DFunction)1 SingleEllipticalGaussian2DFunction (gdsc.smlm.function.gaussian.SingleEllipticalGaussian2DFunction)1 SingleFixedGaussian2DFunction (gdsc.smlm.function.gaussian.SingleFixedGaussian2DFunction)1 SingleFreeCircularGaussian2DFunction (gdsc.smlm.function.gaussian.SingleFreeCircularGaussian2DFunction)1 SingleNBFixedGaussian2DFunction (gdsc.smlm.function.gaussian.SingleNBFixedGaussian2DFunction)1 ErfGaussian2DFunction (gdsc.smlm.function.gaussian.erf.ErfGaussian2DFunction)1 SingleFreeCircularErfGaussian2DFunction (gdsc.smlm.function.gaussian.erf.SingleFreeCircularErfGaussian2DFunction)1 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)1 DenseMatrix64F (org.ejml.data.DenseMatrix64F)1