19

I am looking to perform a polynomial least squares regression and am looking for a C# library to do the calculations for me.

I pass in the data points and the degree of polynomal (2nd order, 3rd order, etc) and it returns either the C0, C1, C2 etc. constant values or the calculated values "predictions".

Note: I am using Least Squares to create some forecasting reports for disk usage, database size and table size.

Robert Wilkinson
  • 1,199
  • 1
  • 13
  • 23
  • [Link for my linear least squares solution](http://stackoverflow.com/a/18618362/2239678) Simple API that solves the linear least squares problem. – andrei.ciprian Sep 04 '13 at 15:52

4 Answers4

13

Here is a link for C# code on to do exactly this: http://www.trentfguidry.net/post/2009/08/01/Linear-Regression-of-Polynomial-Coefficients.aspx

Good luck!

Edit: Apparently the above link is broken. I made another solution awhile back: http://procbits.com/2011/05/02/linear-regression-in-c-sharp-least-squares/

JP Richardson
  • 38,609
  • 36
  • 119
  • 151
  • 2
    If so, I coded up a solution: http://procbits.com/2011/05/02/linear-regression-in-c-sharp-least-squares/ – JP Richardson Jul 14 '11 at 19:54
  • 3
    I found a link to the original C# code on archive.org here http://web.archive.org/web/20091004001612/http://www.trentfguidry.net/post/2009/08/01/Linear-Regression-of-Polynomial-Coefficients.aspx ... it works perfectly, and all the sub-linked articles which are required are all present. – dodgy_coder May 27 '13 at 06:42
  • 1
    @JP forgive my ignorance but isn't your solution just a simple linear regression (i.e. y = a + bx). I think the OP needed a polynomial curve fit such as y = a + bx + cx^2 + dx^3? – dodgy_coder May 27 '13 at 06:45
  • 4
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – BMW Jun 07 '15 at 12:23
  • @dodgy_coder yes, you're correct. I'm not sure how I missed that. – JP Richardson Jun 08 '15 at 13:36
  • @BMW I agree. When I answered this question 4 years ago, I think the standards may have been different. But I honestly don't remember. Do you think it'd be prudent for me to paste in the code from my old blog post? – JP Richardson Jun 08 '15 at 13:37
  • I just realised, that's 4 years old answer. It will be good if you can update with detail. – BMW Jun 08 '15 at 23:50
1

In the general case you want an "optimizer" or "mimimizer". See http://en.wikipedia.org/wiki/Optimization_(mathematics)#Solvers for some exmples. I see that the first link (http://en.wikipedia.org/wiki/IMSL_Numerical_Libraries) claims to have c# support.


Edit: For the limited use that you propose (linear or quadratic polynomials), you could just go to any copy of Numerical Recipies, grab a straight ahead implementation, and translate to your language. A general minimizer is overkill.

But note, also, that polynomials may be poor predictors.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
1

You can check library form ALGLIB under GPL licence 2.0. They have source code for C#, C++,...

http://www.alglib.net/interpolation/leastsquares.php

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
0

You may want to check out alglib. It is in C++ instead of C#, but you might be able to write a wrapper over it.

Adam Tegen
  • 25,378
  • 33
  • 125
  • 153