forum.alglib.net
http://forum.alglib.net/

lsfitlinear error?
http://forum.alglib.net/viewtopic.php?f=2&t=824
Page 1 of 1

Author:  MarcF [ Fri Apr 26, 2013 5:41 pm ]
Post subject:  lsfitlinear error?

Why does the following fail to find a simple linear fit??

Code:
double[] y = new double[] { 6.95, 8.57, 10.3, 12.2, 14.0 };
double[,] x = new double[,] {{1},{2},{3},{4},{5}};

int info;
double[] c;
alglib.lsfitreport rep;
alglib.lsfitlinear(y, x, out info, out c, out rep);

//rep.r2 value equals 0.25
//c[0] value equals 3.16
//Expected result is 1.77 ?????


Regards,
MarcF

Author:  Sergey.Bochkanov [ Fri Apr 26, 2013 6:48 pm ]
Post subject:  Re: lsfitlinear error?

Why do you think that it fails? lsfitlinear is a GENERAL least squares solver. In your formulation it solves problem of fitting experimental points by y(x)=c[0]*x - fitting without constant term, because you've specified only linear term in x matrix. And it fits data as good as possible - for c[0]=3.16 we have RSS (residual sum of squares) equal to 23.5, which is many times lower than RSS=129.8 for c[0]=1.77

If you want to perform linear fit by y(x)=c[0]+c[1]*x, you have to pass [[1,1],[1,2],[1,3],[1,4],[1,5]], where first item in each row correspond to constant term, second item - to linear term. Alternatively, you can use http://www.alglib.net/translator/man/manual.cpp.html#sub_polynomialfit to perform polynomial fitting.

Author:  MarcF [ Fri Apr 26, 2013 10:30 pm ]
Post subject:  Re: lsfitlinear error?

thanks for the clarification, it makes sense now. I don't think this is particularly clear in the documentation though.

Marc

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/