Polynomial<T>.LeastSquaresFit Method

Definition

Namespace: Numerics.NET.Curves.Generic
Assembly: Numerics.NET.Generic (in Numerics.NET.Generic.dll) Version: 9.0.3

Overload List

LeastSquaresFit(Vector<T>, Vector<T>, Int32) Returns the Polynomial<T> that is the best least squares fit through the given set of points.
LeastSquaresFit(Vector<T>, Vector<T>, Int32, Vector<T>) Returns the Polynomial<T> that is the best weighted least squares fit through the given set of points.

LeastSquaresFit(Vector<T>, Vector<T>, Int32)

Returns the Polynomial<T> that is the best least squares fit through the given set of points.
C#
public static Polynomial<T> LeastSquaresFit(
	Vector<T> xValues,
	Vector<T> yValues,
	int degree
)

Parameters

xValues  Vector<T>
An array of numbers containing the X-coordinates of the points.
yValues  Vector<T>
An array of numbers containing the Y-coordinates of the points.
degree  Int32
The degree of the least squares polynomial. Must be smaller than the number of data points.

Return Value

Polynomial<T>
A Polynomial<T> that is the least squares fit through the given set of points.

Remarks

The least squares fit of a polynomial through a set of points is the polynomial that minimizes the sum of the squares of the residuals. The residuals are the differences between the predicted and the actual values.

The coordinates of the points are provided in two T arrays. These must have the same number of elements, or an exception is thrown.

In rare cases, the least squares polynomial is a vertical line. In this case, a DivideByZeroException exception is thrown.

Exceptions

ArgumentNullExceptionxValues is null.

-or-

yValues is null.
DimensionMismatchException The arrays xValues and yValues have different lengths.
DivideByZeroExceptionThe least-squares polynomial is a vertical line.
ArgumentOutOfRangeExceptiondegree is less than zero.
InsufficientDataExceptiondegree is greater than or equal to the number of data points.

LeastSquaresFit(Vector<T>, Vector<T>, Int32, Vector<T>)

Returns the Polynomial<T> that is the best weighted least squares fit through the given set of points.
C#
public static Polynomial<T> LeastSquaresFit(
	Vector<T> xValues,
	Vector<T> yValues,
	int degree,
	Vector<T>? weights
)

Parameters

xValues  Vector<T>
A vector containing the X-coordinates of the points.
yValues  Vector<T>
A vector containing the Y-coordinates of the points.
degree  Int32
The degree of the least squares polynomial. Must be smaller than the number of data points.
weights  Vector<T>
A vector containing the weights of the observations.

Return Value

Polynomial<T>
A Polynomial<T> that is the least squares fit through the given set of points.

Remarks

The least squares fit of a polynomial through a set of points is the polynomial that minimizes the sum of the squares of the residuals. The residuals are the differences between the predicted and the actual values.

The coordinates of the points are provided in two arrays. These must have the same number of elements, or an exception is thrown.

In rare cases, the least squares polynomial is a vertical line. In this case, a DivideByZeroException exception is thrown.

Exceptions

ArgumentNullExceptionxValues is null.

-or-

yValues is null.
DimensionMismatchException The arrays xValues and yValues have different lengths.
-or- The arrays xValues and weights have different lengths.
DivideByZeroExceptionThe least-squares polynomial is a vertical line.
ArgumentOutOfRangeExceptiondegree is less than zero or greater than the number of data points.

See Also