/** * Fits a Gaussian function to the observed points. * * @return the parameters of the Gaussian function that best fits the * observed points (in the same order as above). */ public double[] fit() { final double[] guess = (new ParameterGuesser(getObservations())).guess(); return fit(guess); }
/** * Configure the maximum number of iterations. * @param newMaxIter maximum number of iterations * @return a new instance. */ public PolynomialCurveFitter withMaxIterations(int newMaxIter) { return new PolynomialCurveFitter(initialGuess, newMaxIter); }
/** * Configure the maximum number of iterations. * @param newMaxIter maximum number of iterations * @return a new instance. */ public HarmonicCurveFitter withMaxIterations(int newMaxIter) { return new HarmonicCurveFitter(initialGuess, newMaxIter); }
/** * Fit an harmonic function to the observed points. * An initial guess will be automatically computed. * * @return the parameters of the harmonic function that best fits the * observed points (see the other {@link #fit(double[]) fit} method. * @throws NumberIsTooSmallException if the sample is too short for the * the first guess to be computed. * @throws ZeroException if the first guess cannot be computed because * the abscissa range is zero. */ public double[] fit() { return fit((new ParameterGuesser(getObservations())).guess()); }
/** * Adds a point to the sample. * Calling this method is equivalent to calling * {@code add(1.0, x, y)}. * * @param x Abscissa of the point. * @param y Observed value at {@code x}. After fitting we should * have {@code f(x)} as close as possible to this value. * * @see #add(double, double, double) * @see #add(WeightedObservedPoint) * @see #toList() */ public void add(double x, double y) { add(1d, x, y); }
/** Add an observed (x,y) point to the sample with unit weight. * <p>Calling this method is equivalent to call * {@code addObservedPoint(1.0, x, y)}.</p> * @param x abscissa of the point * @param y observed value of the point at x, after fitting we should * have f(x) as close as possible to this value * @see #addObservedPoint(double, double, double) * @see #addObservedPoint(WeightedObservedPoint) * @see #getObservations() */ public void addObservedPoint(double x, double y) { addObservedPoint(1.0, x, y); }
/** * Fit a curve. * This method compute the coefficients of the curve that best * fit the sample of observed points previously given through calls * to the {@link #addObservedPoint(WeightedObservedPoint) * addObservedPoint} method. * * @param f parametric function to fit. * @param initialGuess first guess of the function parameters. * @return the fitted parameters. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the start point dimension is wrong. */ public double[] fit(T f, final double[] initialGuess) { return fit(Integer.MAX_VALUE, f, initialGuess); }
/** Add an observed weighted (x,y) point to the sample. * @param weight weight of the observed point in the fit * @param x abscissa of the point * @param y observed value of the point at x, after fitting we should * have f(x) as close as possible to this value * @see #addObservedPoint(double, double) * @see #addObservedPoint(WeightedObservedPoint) * @see #getObservations() */ public void addObservedPoint(double weight, double x, double y) { observations.add(new WeightedObservedPoint(weight, x, y)); }
/** * Configure the maximum number of iterations. * @param newMaxIter maximum number of iterations * @return a new instance. */ public GaussianCurveFitter withMaxIterations(int newMaxIter) { return new GaussianCurveFitter(initialGuess, newMaxIter); }
/** * Configure the maximum number of iterations. * @param newMaxIter maximum number of iterations * @return a new instance. */ public SimpleCurveFitter withMaxIterations(int newMaxIter) { return new SimpleCurveFitter(function, initialGuess, newMaxIter); }
/** * Finds index of point in specified points with the largest Y. * * @param points Points to search. * @return the index in specified points array. */ private int findMaxY(WeightedObservedPoint[] points) { int maxYIdx = 0; for (int i = 1; i < points.length; i++) { if (points[i].getY() > points[maxYIdx].getY()) { maxYIdx = i; } } return maxYIdx; }
/** * Creates a default curve fitter. * Zero will be used as initial guess for the coefficients, and the maximum * number of iterations of the optimization algorithm is set to * {@link Integer#MAX_VALUE}. * * @param degree Degree of the polynomial to be fitted. * @return a curve fitter. * * @see #withStartPoint(double[]) * @see #withMaxIterations(int) */ public static PolynomialCurveFitter create(int degree) { return new PolynomialCurveFitter(new double[degree + 1], Integer.MAX_VALUE); }
/** * Creates a default curve fitter. * The initial guess for the parameters will be {@link ParameterGuesser} * computed automatically, and the maximum number of iterations of the * optimization algorithm is set to {@link Integer#MAX_VALUE}. * * @return a curve fitter. * * @see #withStartPoint(double[]) * @see #withMaxIterations(int) */ public static HarmonicCurveFitter create() { return new HarmonicCurveFitter(null, Integer.MAX_VALUE); }
/** * Creates a default curve fitter. * The initial guess for the parameters will be {@link ParameterGuesser} * computed automatically, and the maximum number of iterations of the * optimization algorithm is set to {@link Integer#MAX_VALUE}. * * @return a curve fitter. * * @see #withStartPoint(double[]) * @see #withMaxIterations(int) */ public static GaussianCurveFitter create() { return new GaussianCurveFitter(null, Integer.MAX_VALUE); }
/** * Creates a curve fitter. * The maximum number of iterations of the optimization algorithm is set * to {@link Integer#MAX_VALUE}. * * @param f Function to fit. * @param start Initial guess for the parameters. Cannot be {@code null}. * Its length must be consistent with the number of parameters of the * function to fit. * @return a curve fitter. * * @see #withStartPoint(double[]) * @see #withMaxIterations(int) */ public static SimpleCurveFitter create(ParametricUnivariateFunction f, double[] start) { return new SimpleCurveFitter(f, start, Integer.MAX_VALUE); }
/** * Finds index of point in specified points with the largest Y. * * @param points Points to search. * @return the index in specified points array. */ private int findMaxY(WeightedObservedPoint[] points) { int maxYIdx = 0; for (int i = 1; i < points.length; i++) { if (points[i].getY() > points[maxYIdx].getY()) { maxYIdx = i; } } return maxYIdx; }
/** * Configure the start point (initial guess). * @param newStart new start point (initial guess) * @return a new instance. */ public PolynomialCurveFitter withStartPoint(double[] newStart) { return new PolynomialCurveFitter(newStart.clone(), maxIter); }
/** * Configure the start point (initial guess). * @param newStart new start point (initial guess) * @return a new instance. */ public HarmonicCurveFitter withStartPoint(double[] newStart) { return new HarmonicCurveFitter(newStart.clone(), maxIter); }
/** * Configure the start point (initial guess). * @param newStart new start point (initial guess) * @return a new instance. */ public GaussianCurveFitter withStartPoint(double[] newStart) { return new GaussianCurveFitter(newStart.clone(), maxIter); }
/** * Configure the start point (initial guess). * @param newStart new start point (initial guess) * @return a new instance. */ public SimpleCurveFitter withStartPoint(double[] newStart) { return new SimpleCurveFitter(function, newStart.clone(), maxIter); }