Search in sources :

Example 1 with WeightedObservedPoint

use of org.apache.commons.math3.fitting.WeightedObservedPoint in project ffx by mjschnie.

the class BlockAverager method powerFit.

/**
 * Returns [A,B] such that f(x) = A*(x^B) is minimized for the input points.
 * As described at
 * http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html
 */
private double[] powerFit(List<WeightedObservedPoint> obs) {
    int n = obs.size();
    double sumlnxlny = 0.0;
    double sumlnx = 0.0;
    double sumlny = 0.0;
    double sumlnxsq = 0.0;
    for (int i = 0; i < n; i++) {
        final double x = obs.get(i).getX() * obs.get(i).getWeight();
        final double y = obs.get(i).getY() * obs.get(i).getWeight();
        final double lnx = log(x);
        final double lny = log(y);
        sumlnxlny += lnx * lny;
        sumlnx += lnx;
        sumlny += lny;
        sumlnxsq += lnx * lnx;
    }
    final double b = (n * sumlnxlny - sumlnx * sumlny) / (n * sumlnxsq - sumlnx * sumlnx);
    final double a = (sumlny - b * sumlnx) / n;
    final double B = b;
    final double A = Math.exp(a);
    double[] ret = { A, B };
    return ret;
}
Also used : WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint)

Example 2 with WeightedObservedPoint

use of org.apache.commons.math3.fitting.WeightedObservedPoint in project GDSC-SMLM by aherbert.

the class PcPalmMolecules method fitGaussian.

@Nullable
private static double[] fitGaussian(float[] x, float[] y) {
    final WeightedObservedPoints obs = new WeightedObservedPoints();
    for (int i = 0; i < x.length; i++) {
        obs.add(x[i], y[i]);
    }
    final Collection<WeightedObservedPoint> observations = obs.toList();
    final GaussianCurveFitter fitter = GaussianCurveFitter.create().withMaxIterations(2000);
    final GaussianCurveFitter.ParameterGuesser guess = new GaussianCurveFitter.ParameterGuesser(observations);
    double[] initialGuess = null;
    try {
        initialGuess = guess.guess();
        return fitter.withStartPoint(initialGuess).fit(observations);
    } catch (final TooManyEvaluationsException ex) {
        // Use the initial estimate
        return initialGuess;
    } catch (final Exception ex) {
        // Just in case there is another exception type, or the initial estimate failed
        return null;
    }
}
Also used : WeightedObservedPoints(org.apache.commons.math3.fitting.WeightedObservedPoints) TooManyEvaluationsException(org.apache.commons.math3.exception.TooManyEvaluationsException) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint) ClusterPoint(uk.ac.sussex.gdsc.core.clustering.ClusterPoint) GaussianCurveFitter(org.apache.commons.math3.fitting.GaussianCurveFitter) DataException(uk.ac.sussex.gdsc.core.data.DataException) TooManyEvaluationsException(org.apache.commons.math3.exception.TooManyEvaluationsException) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Example 3 with WeightedObservedPoint

use of org.apache.commons.math3.fitting.WeightedObservedPoint in project GDSC-SMLM by aherbert.

the class FIRE method fitGaussian.

/**
	 * Fit gaussian.
	 *
	 * @param x
	 *            the x
	 * @param y
	 *            the y
	 * @return new double[] { norm, mean, sigma }
	 */
private double[] fitGaussian(float[] x, float[] y) {
    WeightedObservedPoints obs = new WeightedObservedPoints();
    for (int i = 0; i < x.length; i++) obs.add(x[i], y[i]);
    Collection<WeightedObservedPoint> observations = obs.toList();
    GaussianCurveFitter fitter = GaussianCurveFitter.create().withMaxIterations(2000);
    GaussianCurveFitter.ParameterGuesser guess = new GaussianCurveFitter.ParameterGuesser(observations);
    double[] initialGuess = null;
    try {
        initialGuess = guess.guess();
        return fitter.withStartPoint(initialGuess).fit(observations);
    } catch (TooManyEvaluationsException e) {
        // Use the initial estimate
        return initialGuess;
    } catch (Exception e) {
        // Just in case there is another exception type, or the initial estimate failed
        return null;
    }
}
Also used : WeightedObservedPoints(org.apache.commons.math3.fitting.WeightedObservedPoints) TooManyEvaluationsException(org.apache.commons.math3.exception.TooManyEvaluationsException) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint) GaussianCurveFitter(org.apache.commons.math3.fitting.GaussianCurveFitter) TooManyEvaluationsException(org.apache.commons.math3.exception.TooManyEvaluationsException)

Example 4 with WeightedObservedPoint

use of org.apache.commons.math3.fitting.WeightedObservedPoint in project GDSC-SMLM by aherbert.

the class PCPALMMolecules method fitGaussian.

private double[] fitGaussian(float[] x, float[] y) {
    WeightedObservedPoints obs = new WeightedObservedPoints();
    for (int i = 0; i < x.length; i++) obs.add(x[i], y[i]);
    Collection<WeightedObservedPoint> observations = obs.toList();
    GaussianCurveFitter fitter = GaussianCurveFitter.create().withMaxIterations(2000);
    GaussianCurveFitter.ParameterGuesser guess = new GaussianCurveFitter.ParameterGuesser(observations);
    double[] initialGuess = null;
    try {
        initialGuess = guess.guess();
        return fitter.withStartPoint(initialGuess).fit(observations);
    } catch (TooManyEvaluationsException e) {
        // Use the initial estimate
        return initialGuess;
    } catch (Exception e) {
        // Just in case there is another exception type, or the initial estimate failed
        return null;
    }
}
Also used : WeightedObservedPoints(org.apache.commons.math3.fitting.WeightedObservedPoints) TooManyEvaluationsException(org.apache.commons.math3.exception.TooManyEvaluationsException) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint) ClusterPoint(gdsc.core.clustering.ClusterPoint) GaussianCurveFitter(org.apache.commons.math3.fitting.GaussianCurveFitter) TooManyEvaluationsException(org.apache.commons.math3.exception.TooManyEvaluationsException)

Example 5 with WeightedObservedPoint

use of org.apache.commons.math3.fitting.WeightedObservedPoint in project ffx by mjschnie.

the class BlockAverager method logFit.

/**
 * Returns [A,B] such that f(x)= a + b*ln(x) is minimized for the input
 * points. As described at
 * http://mathworld.wolfram.com/LeastSquaresFittingLogarithmic.html
 */
private double[] logFit(List<WeightedObservedPoint> obs) {
    int n = obs.size();
    double sumylnx = 0.0;
    double sumy = 0.0;
    double sumlnx = 0.0;
    double sumlnxsq = 0.0;
    for (int i = 0; i < n; i++) {
        final double x = obs.get(i).getX() * obs.get(i).getWeight();
        final double y = obs.get(i).getY() * obs.get(i).getWeight();
        final double lnx = log(x);
        final double lny = log(y);
        sumylnx += y * lnx;
        sumy += y;
        sumlnx += lnx;
        sumlnxsq += lnx * lnx;
    }
    final double b = (n * sumylnx - sumy * sumlnx) / (n * sumlnxsq - sumlnx * sumlnx);
    final double a = (sumy - b * sumlnx) / n;
    double[] ret = { a, b };
    return ret;
}
Also used : WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint)

Aggregations

WeightedObservedPoint (org.apache.commons.math3.fitting.WeightedObservedPoint)9 GaussianCurveFitter (org.apache.commons.math3.fitting.GaussianCurveFitter)4 WeightedObservedPoints (org.apache.commons.math3.fitting.WeightedObservedPoints)4 TooManyEvaluationsException (org.apache.commons.math3.exception.TooManyEvaluationsException)3 DataException (uk.ac.sussex.gdsc.core.data.DataException)2 ClusterPoint (gdsc.core.clustering.ClusterPoint)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 PolynomialFunction (org.apache.commons.math3.analysis.polynomials.PolynomialFunction)1 Nullable (uk.ac.sussex.gdsc.core.annotation.Nullable)1 ClusterPoint (uk.ac.sussex.gdsc.core.clustering.ClusterPoint)1 ConversionException (uk.ac.sussex.gdsc.core.data.utils.ConversionException)1