Error thrown from Fit.Polynomial

(Dave Nadler) #1

I tried to fit a simple polynomial:
polyFit = MathNet.Numerics.Fit.Polynomial(xSlope, yExpectedPRgap, 3);
Input arrays are both double[3] and do work with a line fit as in:
Tuple<double, double> PRcoeffs = MathNet.Numerics.Fit.Line(xSlope, yExpectedPRgap);

Fit.Polynomial throws the exception below; what am I missing?
Best Regards, Dave

        "Matrix dimensions must agree: 3x4."    "Matrix dimensions must agree: 3x4."    string

        StackTrace    "   at MathNet.Numerics.LinearAlgebra.Double.Factorization.DenseQR.Create(DenseMatrix matrix, QRMethod method)

at MathNet.Numerics.LinearAlgebra.Double.DenseMatrix.QR(QRMethod method)
at MathNet.Numerics.LinearRegression.MultipleRegression.QR[T](Matrix1 x, Vector1 y)
at MathNet.Numerics.LinearRegression.MultipleRegression.DirectMethod[T](Matrix1 x, Vector1 y, DirectRegressionMethod method)
at MathNet.Numerics.Fit.Polynomial(Double[] x, Double[] y, Int32 order, DirectRegressionMethod …" string

(Christoph Rüegg) #2

Ah yes, we really need to improve these exception messages, they are not helpful at all.

In this specific case, a more reasonable exception message would be something along the line of:

Fitting to a polynomial of order 3 requires at least 4 samples. Only 3 samples have been provided.

That being said, we could also effectively downgrade the order if not enough samples have been provided, so it would actually return a polynomial of order 2 in this case (with the coefficient of order 3 is still present but zero).

(Christoph Rüegg) #3

Specifically for polynomials I’m currently leaning towards the latter (downgrade order), but for any other fitting routines we’ll need to throw an exception if not enough samples are provided (with better exception text).

Any thoughts on this?

(Dave Nadler) #4

OK, I’m definitely missing something.
A quadratic (polynomial order 3) is exactly described by 3 points.
I’m using a fit for a calibration operation and set the minimum to 3,
hoping that the user will provide more points for ‘better’ calibration.

So, why are 4 points required here?

Thanks for the help!
Best Regards, Dave

(Christoph Rüegg) #5

The order of a polynomial is not a very precise term apparently. What we’re referring to is technically the degree of the polynomial. A polynomial of degree 3 is a cubic polynomial, and has 4 coefficients. For your quadratic polynomial (degree 2) you’d specify order = 2.


(Dave Nadler) #6

Ah Hah!
I understood order ::= number of coefficients…
Thanks for the help,
Best Regards, Dave