Overview

The fitting package deals with curve fitting for univariate real functions. When a univariate real function y = f(x) does depend on some unknown parameters p0, p1 … pn-1, curve fitting can be used to find these parameters. It does this by fitting the curve so it remains very close to a set of observed points (x0, y0), (x1, y1) … (xk-1, yk-1). This fitting is done by finding the parameters values that minimizes the objective function Σ(yi - f(xi))2. This is actually a least-squares problem.

For all provided curve fitters, the operating principle is the same. Users must

  • create an instance of the fitter using the create factory method of the appropriate class,
  • call the fit with a Collection of observed data points as argument, which will return an array with the parameters that best fit the given data points.

The list of observed data points to be passed to fit can be built by incrementally adding instances to an instance of WeightedObservedPoints, and then retrieve the list of WeightedObservedPoint by calling the toList method on that container. A weight can be associated with each observed point; it allows to take into account uncertainty on some points when they come from noisy measurements for example. If no such information exist and all points should be treated the same, it is safe to put 1.0 as the weight for all points (and this is the default when no weight is passed to the add.

Some fitters require that initial values for the parameters are provided by the user, through the withStartPoint method, before attempting to perform the fit. When that's the case the fitter class usually defines a guessing procedure within a ParameterGuesser inner class, that attempts to provide appropriate initial values based on the user-supplied data. When initial values are required but are not provided, the fit method will internally call the guessing procedure.

Implemented Functions

Fitting of specific functions are provided through the following classes:

  • create an instance of the fitter using the create factory method of the appropriate class,
  • call the fit with a Collection of observed data points as argument, which will return an array with the parameters that best fit the given data points.

The following example shows how to fit data with a polynomial function.

// Collect data.
final WeightedObservedPoints obs = new WeightedObservedPoints();
obs.add(-1.00, 2.021170021833143);
obs.add(-0.99, 2.221135431136975);
obs.add(-0.98, 2.09985277659314);
obs.add(-0.97, 2.0211192647627025);
// ... Lots of lines omitted ...
obs.addt(0.99, -2.4345814727089854);

// Instantiate a third-degree polynomial fitter.
final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(3);

// Retrieve fitted parameters (coefficients of the polynomial function).
final double[] coeff = fitter.fit(obs.toList());

General Case

The AbstractCurveFitter class provides the basic functionality for implementing other curve fitting classes. Users must provide their own implementation of the curve template as a class that implements the ParametricUnivariateFunction interface.

Random sample consensus (RANSAC) algorithm

Overview

RANSAC is a robust method for estimating the parameters of a mathematical model from a set of observed data. It works iteratively selecting random subsets of the input data, fitting a model to these subsets, and then determining how many data points from the entire set are consistent with the estimated model parameters. The model can yields the largest number of inliers (i.e., point that fit well) is considered the best estimate.

The Hipparchus implementation is designed to be generic and can be used with different types of models, such as line, polynomial, etc.

Implemented Functions

RANSAC-based fitting of specific functions are provided through the following classes:

  • create an instance of the model to fit using the IModelFitter interface,
  • call the fit method of RansacFitter with a List of observed data points as argument, which will return a java class containing the parameters that best fit the given data points.

Data points must implement the Fittable interface which provides a getPoint() method returning the point coordinates as a double[].

The following example shows how to fit data with a polynomial model of degree 2.

// Collect data.
final List<Fittable> obs = new ArrayList<>();
obs.add(new SimpleFittable(0.0, -61.422));
obs.add(new SimpleFittable(2.0, -42.28700013));
obs.add(new SimpleFittable(4.0, -58.97612903));
// ... Lots of lines omitted ...
obs.add(new SimpleFittable(498.0, -67.39));

// Instantiate the model to fit.
final PolynomialModelFitter model = new PolynomialModelFitter(2);

// Fit the data.
RansacFitterOutputs<PolynomialModelFitter.Model> fitted = new RansacFitter<>(model, 10, 1000, standardDeviation / 3, 10, 1).fit(obs);

// Retrieve fitted parameters (coefficients of the polynomial function).
final double[] coeff = fitted.getBestModel().getCoefficients();

Back to top

Version: 4.1. Last Published: 2026-11-15.

Reflow Maven skin maintained by Olivier Lamy.