forum.alglib.net

ALGLIB forum
It is currently Thu Mar 28, 2024 10:30 am

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: lsfitlinear error?
PostPosted: Fri Apr 26, 2013 5:41 pm 
Offline

Joined: Fri Apr 26, 2013 5:38 pm
Posts: 2
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


Top
 Profile  
 
 Post subject: Re: lsfitlinear error?
PostPosted: Fri Apr 26, 2013 6:48 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 903
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.


Top
 Profile  
 
 Post subject: Re: lsfitlinear error?
PostPosted: Fri Apr 26, 2013 10:30 pm 
Offline

Joined: Fri Apr 26, 2013 5:38 pm
Posts: 2
thanks for the clarification, it makes sense now. I don't think this is particularly clear in the documentation though.

Marc


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 51 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group