This project has moved. For the latest updates, please go here.

how to do a polynomial fit use Cholesky algorithm

Jun 17, 2014 at 5:07 PM
how to do a polynomial fit using Cholesky algorithm. although the lib provides a funciton named
.Fit.Polynomial to do the polynomial fit, but is there any options that specified the curve-fitting method. thanks.
Coordinator
Jun 17, 2014 at 9:59 PM
Edited Jun 17, 2014 at 10:00 PM
Not directly. It seems we'd better provide routines to provide standard design matrices like the polynomial case (Vandermonde matrix), which can then be combined with the existing methods of the MultipleRegression class.

In the meantime you could use the following function, slightly adapted from Fit.Polynomial but using normal equations and the cholesky decomposition instead of directly the QR decomposition (without normal equations). You cannot usually use the Cholesky decomposition directly (without normal equations) at it requires the matrix to be symmetric positive definite.
/// <summary>
/// Least-Squares fitting the points (x,y) to a k-order polynomial y : x -> p0 + p1*x + p2*x^2 + ... + pk*x^k,
/// returning its best fitting parameters as [p0, p1, p2, ..., pk] array, compatible with Evaluate.Polynomial.
/// </summary>
public static double[] Polynomial(double[] x, double[] y, int order)
{
    var design = Matrix<double>.Build.Dense(x.Length, order + 1, (i, j) => Math.Pow(x[i], j));
    return MultipleRegression.NormalEquations(design, Vector<double>.Build.Dense(y)).ToArray();
}
Thanks,
Christoph