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