Posted by
Klaus Spanderen-2 on
URL: http://quantlib.414.s1.nabble.com/Multiple-linear-regression-weighted-regression-tp5716p5717.html
Hi Boris
On Wednesday 14 January 2009 19:20:58 Boris Skorodumov wrote:
> I see, there is a *<ql/math/linearleastsquaresregression.hpp>* which is
> capable to carry out something like *y~f(x)*. I was wondering whether I can
> do something like *y ~ b0 + b1*x1+b2*x2+...*. LinearLeastSquaresRegression
> accepts only one vector x and one vector y.
You have to use objects of type LinearLeastSquaresRegression<Array>
or LinearLeastSquaresRegression<std::vector<Real> >. I've just uploaded a test
case for multi dimensional least squares regression and hope that illustrates
the usage. Please have a look at
http://quantlib.svn.sourceforge.net/viewvc/quantlib/trunk/QuantLib/test-suite/linearleastsquaresregression.cpp?revision=15855&view=markup> Also for the case of *y ~ b0 + b1*x*, errors of estimated coefficients
> somewhat different from those in R for example.
IMO R "assumes a good fit" under the assumption that the model
y = b0 + b1*x + eps is correct, whereas the errors calculated by the quantlib
follow the definition given in the Numerical Recipes ยง15.2 and equation
15.4.19 (but w/o indvidual weights or \sigma_i for the design matrix of the
fitting problem). You can get the errors as calculated by R via
error(R) = \sqrt( \chi^2/(N-2)) * error(QL)
The last lines of your example are
Real chiSq = 0.0;
for (Size i=0; i < y.size(); ++i)
chiSq += square<Real>()(y[i] - (m.a()[0] + m.a()[1]*x[i]))
/(x.size()-2);
for (unsigned int i=0; i < v.size(); ++i)
std::cout << m.error()[i]*::sqrt(chiSq)
<< " " << m.a()[i] << std::endl;
hope that helps
Klaus
------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users