Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Possible issue with PolynomialLeastSquares() Class #434

Closed
Mazecreator opened this issue Feb 17, 2017 · 2 comments
Closed

Possible issue with PolynomialLeastSquares() Class #434

Mazecreator opened this issue Feb 17, 2017 · 2 comments

Comments

@Mazecreator
Copy link

I have been working with the following code snippet and getting erratic results. I suspect there may be something wrong with the PolynomialLeastSquares() class of I just don't fully understand what it does.

If I run this:

Period = 14;
inputs = new double[Period];
outputs = new double[Period];

ols = new PolynomialLeastSquares();
ols.Degree = 3;
		
for(i = 0; i < Period; i++)
{
    inputs[i] = Convert.ToDouble(i);
outputs[i] = inputs[i];
}

// Use OLS to learn the regression
reg = ols.Learn(inputs, outputs);
result = reg.Transform(-1.0);
Print(ols.Degree+"  "+reg.ToString()+"  Int "+ reg.Intercept);

This is what is returned:

3 y(x) = 2.37812678233955E-17x^2 + 1x^1 + 2.77718345656259x^0 Int -2.77718345656258

This appears to be a 2 Degree equation as there is no X^3 and it seems like the X^0 intercept is shown with a different sign from reg.Intercept. If I use a more complex input with higher values and more of a curve, the Intercept starts to get very larger in magnitude. I took a quick look at the source code:
https://github.com/accord-net/framework/blob/development/Sources/Accord.Statistics/Models/Regression/Linear/Fitting/PolynomialLeastSquares.cs

Some things look funny here to me in building the X matrix, I don't see the 1's for the X^0 parameter (maybe you handle that elsewhere), but also the Pow() function seems to never put in the X^Degree factor, but rather an X^(Degree-1), not sure if this helps but if you could help advice when you get time, I would appreciate it.

@cesarsouza
Copy link
Member

cesarsouza commented Feb 18, 2017

Hi Mazecreator,

Many thanks for opening the issue. Indeed there are some issues with the PolynomiaLeastSquares class, that probably got introduced during the rush to port all learning algorithms to the new .Learn() interfaces.

There are at least two problems there, both happening for the roughly the same reason: The .ToString() method and the .Learn are still reflecting the old implementation, where the intercept was stored as the last element of the .Coefficients array (which now it is stored as separate value in the .Intercept property).

I will add more tests and fix it before the next pre-release.

Regards,
Cesar

@cesarsouza
Copy link
Member

Fixed in 3.5.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants