Quantcast
Viewing all articles
Browse latest Browse all 971

New Post: How to do a weighted polynomial fit?

Hi Thomas,

A weighted version of Fit.Polynomial using WeightedRegression (see also) could look like this:
double[] PolynomialWeighted(double[] x, double[] y, double[] w, int order)
{
    var design = Matrix<double>.Build.Dense(
        x.Length, order + 1, (i, j) => Math.Pow(x[i], j));

    return WeightedRegression.Weighted(
        design, Vector<double>.Build.Dense(y),
        Matrix<double>.Build.DenseOfDiagonalArray(w)).ToArray();
}
Example:
var x = Enumerable.Range(1, 6).Select(Convert.ToDouble).ToArray();
var y = new[] { 4.986, 2.347, 2.061, -2.995, -2.352, -5.782 };
var w = new[] { 0.5, 1.0, 1.0, 1.0, 1.0, 0.5 };
var order = 2;

var resp = Fit.Polynomial(x, y, order).Dump();
var resp2 = Fit.PolynomialWeighted(x, y, w, order).Dump();
However, note that WeightedRegression actually uses normal equations (with Cholesky decomposition) internally, so it is not very robust and not a good choice for polynomials of high order. Fit.Polynomial uses a QR decomposition which is a bit better, but still not very good for polynomials. At some point we'll want to replace the polynomial case with a specialized algorithm.

To my understanding, scypi optimize.curve_fit uses Levenberg-Marquardt internally which we do not provide yet either, although we're currently working on integration non-linear fitting and minimization routines.

Thanks,
Christoph

Viewing all articles
Browse latest Browse all 971

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>