Top Description Inners Fields Constructors Methods
org.apache.commons.math3.fitting

public Class GaussianCurveFitter

extends AbstractCurveFitter
Class Inheritance
Imports
java.util.ArrayList, .Collection, .Collections, .Comparator, .List, org.apache.commons.math3.analysis.function.Gaussian, org.apache.commons.math3.exception.NotStrictlyPositiveException, .NullArgumentException, .NumberIsTooSmallException, .OutOfRangeException, .ZeroException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.fitting.leastsquares.LeastSquaresBuilder, .LeastSquaresProblem, org.apache.commons.math3.linear.DiagonalMatrix, org.apache.commons.math3.util.FastMath

Fits points to a Gaussian function.
The initial guess values must be passed in the following order: The optimal values will be returned in the same order.

Usage example:

  WeightedObservedPoints obs = new WeightedObservedPoints();
  obs.add(4.0254623,  531026.0);
  obs.add(4.03128248, 984167.0);
  obs.add(4.03839603, 1887233.0);
  obs.add(4.04421621, 2687152.0);
  obs.add(4.05132976, 3461228.0);
  obs.add(4.05326982, 3580526.0);
  obs.add(4.05779662, 3439750.0);
  obs.add(4.0636168,  2877648.0);
  obs.add(4.06943698, 2175960.0);
  obs.add(4.07525716, 1447024.0);
  obs.add(4.08237071, 717104.0);
  obs.add(4.08366408, 620014.0);
  double[] parameters = GaussianCurveFitter.create().fit(obs.toList());
Since
3.3

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
GaussianCurveFitter.ParameterGuesser

Guesses the parameters norm, mean, and sigma of a org.apache.commons.math3.analysis.function.Gaussian.Parametric based on the specified observed points.

Field Summary

Modifier and TypeField and Description
private static final Gaussian.Parametric
FUNCTION

Parametric function to be fitted.

private final double[]
initialGuess

Initial guess.

private final int
maxIter

Maximum number of iterations of the optimization algorithm.

Constructor Summary

AccessConstructor and Description
private
GaussianCurveFitter(double[]
Initial guess. If set to null, the initial guess will be estimated using the ParameterGuesser.
initialGuess
,
int
Maximum number of iterations of the optimization algorithm.
maxIter
)

Contructor used by the factory methods.

Method Summary

Modifier and TypeMethod and Description
public static GaussianCurveFitter

Returns:

a curve fitter.
create
()

Creates a default curve fitter.

protected LeastSquaresProblem
getProblem(Collection<WeightedObservedPoint>
Sample points.
observations
)

Implements abstract org.apache.commons.math3.fitting.AbstractCurveFitter.getProblem.

Creates a least squares problem corresponding to the appropriate curve.

public GaussianCurveFitter

Returns:

a new instance.
withMaxIterations
(int
maximum number of iterations
newMaxIter
)

Configure the maximum number of iterations.

public GaussianCurveFitter

Returns:

a new instance.
withStartPoint
(double[]
new start point (initial guess)
newStart
)

Configure the start point (initial guess).

Inherited from org.apache.commons.math3.fitting.AbstractCurveFitter:
fitgetOptimizer

Field Detail

FUNCTIONback to summary
private static final Gaussian.Parametric FUNCTION

Parametric function to be fitted.

initialGuessback to summary
private final double[] initialGuess

Initial guess.

maxIterback to summary
private final int maxIter

Maximum number of iterations of the optimization algorithm.

Constructor Detail

GaussianCurveFitterback to summary
private GaussianCurveFitter(double[] initialGuess, int maxIter)

Contructor used by the factory methods.

Parameters
initialGuess:double[]

Initial guess. If set to null, the initial guess will be estimated using the ParameterGuesser.

maxIter:int

Maximum number of iterations of the optimization algorithm.

Method Detail

createback to summary
public static GaussianCurveFitter create()

Creates a default curve fitter. The initial guess for the parameters will be ParameterGuesser computed automatically, and the maximum number of iterations of the optimization algorithm is set to Integer#MAX_VALUE.

Returns:GaussianCurveFitter

a curve fitter.

See Also
withStartPoint(double[]), withMaxIterations(int)
getProblemback to summary
protected LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> observations)

Implements abstract org.apache.commons.math3.fitting.AbstractCurveFitter.getProblem.

Doc from org.apache.commons.math3.fitting.AbstractCurveFitter.getProblem.

Creates a least squares problem corresponding to the appropriate curve.

Parameters
observations:Collection<WeightedObservedPoint>

Sample points.

Returns:LeastSquaresProblem

the least squares problem to use for fitting the curve to the given points.

Annotations
@Override
withMaxIterationsback to summary
public GaussianCurveFitter withMaxIterations(int newMaxIter)

Configure the maximum number of iterations.

Parameters
newMaxIter:int

maximum number of iterations

Returns:GaussianCurveFitter

a new instance.

withStartPointback to summary
public GaussianCurveFitter withStartPoint(double[] newStart)

Configure the start point (initial guess).

Parameters
newStart:double[]

new start point (initial guess)

Returns:GaussianCurveFitter

a new instance.

org.apache.commons.math3.fitting back to summary

public Class GaussianCurveFitter.ParameterGuesser

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.commons.math3.fitting.GaussianCurveFitter.ParameterGuesser

Guesses the parameters norm, mean, and sigma of a org.apache.commons.math3.analysis.function.Gaussian.Parametric based on the specified observed points.

Field Summary

Modifier and TypeField and Description
private final double
mean

Mean.

private final double
norm

Normalization factor.

private final double
sigma

Standard deviation.

Constructor Summary

AccessConstructor and Description
public
ParameterGuesser(Collection<WeightedObservedPoint>
Observed points from which to guess the parameters of the Gaussian.
observations
)

Constructs instance with the specified observed points.

Method Summary

Modifier and TypeMethod and Description
private double[]

Returns:

the guessed parameters (normalization factor, mean and sigma).
basicGuess
(WeightedObservedPoint[]
Observed points, sorted.
points
)

Guesses the parameters based on the specified observed points.

private int

Returns:

the index in specified points array.
findMaxY
(WeightedObservedPoint[]
Points to search.
points
)

Finds index of point in specified points with the largest Y.

private WeightedObservedPoint[]

Returns:

the array containing two points suitable for determining X at the specified Y.
getInterpolationPointsForY
(WeightedObservedPoint[]
Points to use for interpolation.
points
,
int
Index within points from which to start search for interpolation bounds points.
startIdx
,
int
Index step for search for interpolation bounds points.
idxStep
,
double
Y value for which X should be determined.
y
)

Gets the two bounding interpolation points from the specified points suitable for determining X at the specified Y.

public double[]

Returns:

the guessed parameters, in the following order:
  • Normalization factor
  • Mean
  • Standard deviation
guess
()

Gets an estimation of the parameters.

private double

Returns:

the value of X for the specified Y.
interpolateXAtY
(WeightedObservedPoint[]
Points to use for interpolation.
points
,
int
Index within points from which to start the search for interpolation bounds points.
startIdx
,
int
Index step for searching interpolation bounds points.
idxStep
,
double
Y value for which X should be determined.
y
)

Interpolates using the specified points to determine X at the specified Y.

private boolean

Returns:

true if value is between boundary1 and boundary2 (inclusive), false otherwise.
isBetween
(double
Value to test whether it is between boundary1 and boundary2.
value
,
double
One end of the range.
boundary1
,
double
Other end of the range.
boundary2
)

Determines whether a value is between two other values.

private List<WeightedObservedPoint>

Returns:

the input observations, sorted.
sortObservations
(Collection<WeightedObservedPoint>
Input observations.
unsorted
)

Sort the observations.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

meanback to summary
private final double mean

Mean.

normback to summary
private final double norm

Normalization factor.

sigmaback to summary
private final double sigma

Standard deviation.

Constructor Detail

ParameterGuesserback to summary
public ParameterGuesser(Collection<WeightedObservedPoint> observations)

Constructs instance with the specified observed points.

Parameters
observations:Collection<WeightedObservedPoint>

Observed points from which to guess the parameters of the Gaussian.

Exceptions
NullArgumentException:
if observations is null.
NumberIsTooSmallException:
if there are less than 3 observations.

Method Detail

basicGuessback to summary
private double[] basicGuess(WeightedObservedPoint[] points)

Guesses the parameters based on the specified observed points.

Parameters
points:WeightedObservedPoint[]

Observed points, sorted.

Returns:double[]

the guessed parameters (normalization factor, mean and sigma).

findMaxYback to summary
private int findMaxY(WeightedObservedPoint[] points)

Finds index of point in specified points with the largest Y.

Parameters
points:WeightedObservedPoint[]

Points to search.

Returns:int

the index in specified points array.

getInterpolationPointsForYback to summary
private WeightedObservedPoint[] getInterpolationPointsForY(WeightedObservedPoint[] points, int startIdx, int idxStep, double y) throws OutOfRangeException

Gets the two bounding interpolation points from the specified points suitable for determining X at the specified Y.

Parameters
points:WeightedObservedPoint[]

Points to use for interpolation.

startIdx:int

Index within points from which to start search for interpolation bounds points.

idxStep:int

Index step for search for interpolation bounds points.

y:double

Y value for which X should be determined.

Returns:WeightedObservedPoint[]

the array containing two points suitable for determining X at the specified Y.

Exceptions
OutOfRangeException:
if specified y is not within the range of the specified points.
ZeroException:
if idxStep is 0.
guessback to summary
public double[] guess()

Gets an estimation of the parameters.

Returns:double[]

the guessed parameters, in the following order:

  • Normalization factor
  • Mean
  • Standard deviation
interpolateXAtYback to summary
private double interpolateXAtY(WeightedObservedPoint[] points, int startIdx, int idxStep, double y) throws OutOfRangeException

Interpolates using the specified points to determine X at the specified Y.

Parameters
points:WeightedObservedPoint[]

Points to use for interpolation.

startIdx:int

Index within points from which to start the search for interpolation bounds points.

idxStep:int

Index step for searching interpolation bounds points.

y:double

Y value for which X should be determined.

Returns:double

the value of X for the specified Y.

Exceptions
OutOfRangeException:
if specified y is not within the range of the specified points.
ZeroException:
if idxStep is 0.
isBetweenback to summary
private boolean isBetween(double value, double boundary1, double boundary2)

Determines whether a value is between two other values.

Parameters
value:double

Value to test whether it is between boundary1 and boundary2.

boundary1:double

One end of the range.

boundary2:double

Other end of the range.

Returns:boolean

true if value is between boundary1 and boundary2 (inclusive), false otherwise.

sortObservationsback to summary
private List<WeightedObservedPoint> sortObservations(Collection<WeightedObservedPoint> unsorted)

Sort the observations.

Parameters
unsorted:Collection<WeightedObservedPoint>

Input observations.

Returns:List<WeightedObservedPoint>

the input observations, sorted.