source: trunk/c++_tools/statistics/Linear.cc @ 586

Last change on this file since 586 was 586, checked in by Peter, 15 years ago

closes #23 redesign of regression classes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.2 KB
Line 
1// $Id: Linear.cc 586 2006-06-19 09:56:04Z peter $
2
3#include <c++_tools/statistics/Linear.h>
4
5#include <c++_tools/statistics/AveragerPair.h>
6#include <c++_tools/gslapi/vector.h>
7
8#include <gsl/gsl_fit.h>
9
10
11namespace theplu {
12namespace statistics {
13namespace regression {
14
15
16  void Linear::fit(const gslapi::vector& x, const gslapi::vector& y) 
17  {
18    ap_.reset();
19    for (size_t i=0; i<x.size(); i++)
20      ap_.add(x(i),y(i));
21
22    alpha_ = ap_.y_averager().mean();
23    beta_ = ap_.covariance() / ap_.x_averager().variance();
24
25    // calculating deviation between data and model
26    msd_ = (ap_.y_averager().sum_xx_centered() - ap_.sum_xy_centered() *
27            ap_.sum_xy_centered()/ap_.x_averager().sum_xx_centered() )/x.size();
28    r2_= 1-msd_/ap_.x_averager().variance();
29    alpha_var_ = msd_ / x.size();
30    beta_var_ = msd_ / ap_.x_averager().sum_xx_centered();
31    m_x_ = ap_.x_averager().mean();
32  }
33
34  double Linear::predict(const double x) const
35  { 
36    return alpha_ + beta_ * (x-m_x_); 
37  }
38
39  double Linear::prediction_error(const double x) const
40  {
41    return sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)+msd_ ); 
42  }
43
44  double Linear::standard_error(const double x) const
45  {
46    return sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)); 
47  }
48
49}}} // of namespaces regression, statisitcs and thep
Note: See TracBrowser for help on using the repository browser.