# SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.analysis.curvefit

## Class LeastSquares

• All Implemented Interfaces:
CurveFitting

public class LeastSquares
extends Object
implements CurveFitting
This method obtains a least squares estimate of a polynomial to fit the input data, by a weighted sum of orthogonal polynomials up to a specified order. That is, $f(x) \approx f_n(x) = a_1 \phi_1(x) + a_2 \phi_2(x) + \cdots + a_n \phi_n (x)$ where $$\left\{\phi_i(x)\right\}$$ are a set of orthogonal polynomials. The idea behind this is to avoid ill-conditioned linear algebra that arises when fitting coefficients of the target polynomial.

Unlike interpolation, this method does not guarantee that the fitted curve will pass through each point (unless the order of the polynomial is equal to the number of data points) and it is hence appropriate to use with observations that include noise.

Wikipedia: Least squares (function approximation)
• ### Nested Class Summary

Nested Classes
Modifier and Type Class and Description
static interface  LeastSquares.Weighting
This interface defines a weighting for observations.
• ### Field Summary

Fields
Modifier and Type Field and Description
static LeastSquares.Weighting UNIFORM_WEIGHTING
A uniform weighting.
• ### Constructor Summary

Constructors
Constructor and Description
LeastSquares(int m)
Construct a new instance of this algorithm, which uses uniform weighting for the observations.
LeastSquares(int m, LeastSquares.Weighting weighting)
Construct a new instance of this algorithm, which will use a weighted sum of orthogonal polynomials up to order n (the number of points).
• ### Method Summary

All Methods
Modifier and Type Method and Description
UnivariateRealFunction fit(OrderedPairs f)
Fit a real valued function from a discrete set of data points.
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Field Detail

• #### UNIFORM_WEIGHTING

public static final LeastSquares.Weighting UNIFORM_WEIGHTING
A uniform weighting.
• ### Constructor Detail

• #### LeastSquares

public LeastSquares(int m,
LeastSquares.Weighting weighting)
Construct a new instance of this algorithm, which will use a weighted sum of orthogonal polynomials up to order n (the number of points).
Parameters:
m - the maximum degree of the orthogonal polynomials (m ≤ n)
weighting - the function used to determine the weight for each observation
• #### LeastSquares

public LeastSquares(int m)
Construct a new instance of this algorithm, which uses uniform weighting for the observations.
Parameters:
m - the maximum order of the orthogonal polynomials
• ### Method Detail

• #### fit

public UnivariateRealFunction fit(OrderedPairs f)
Description copied from interface: CurveFitting
Fit a real valued function from a discrete set of data points.
Specified by:
fit in interface CurveFitting
Parameters:
f - a discrete set of data points
Returns:
the fitted function