source: trunk/yat/regression/Linear.cc @ 682

Last change on this file since 682 was 682, checked in by Jari Häkkinen, 16 years ago

Addresses #153. Moved regression files to .../yat/regression.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.0 KB
Line 
1// $Id: Linear.cc 682 2006-10-11 22:06:38Z jari $
2
3/*
4  Copyright (C) The authors contributing to this file.
5
6  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "Linear.h"
25#include "yat/statistics/AveragerPair.h"
26#include "yat/utility/vector.h"
27
28#include <gsl/gsl_fit.h>
29
30namespace theplu {
31namespace yat {
32namespace regression {
33
34  void Linear::fit(const utility::vector& x, const utility::vector& y) 
35  {
36    ap_.reset();
37    for (size_t i=0; i<x.size(); i++)
38      ap_.add(x(i),y(i));
39
40    alpha_ = ap_.y_averager().mean();
41    beta_ = ap_.covariance() / ap_.x_averager().variance();
42
43    // calculating deviation between data and model
44    msd_ = (ap_.y_averager().sum_xx_centered() - ap_.sum_xy_centered() *
45            ap_.sum_xy_centered()/ap_.x_averager().sum_xx_centered() )/x.size();
46    r2_= 1-msd_/ap_.x_averager().variance();
47    alpha_var_ = msd_ / x.size();
48    beta_var_ = msd_ / ap_.x_averager().sum_xx_centered();
49    m_x_ = ap_.x_averager().mean();
50  }
51
52  double Linear::predict(const double x) const
53  { 
54    return alpha_ + beta_ * (x-m_x_); 
55  }
56
57  double Linear::prediction_error(const double x) const
58  {
59    return sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)+msd_ ); 
60  }
61
62  double Linear::standard_error(const double x) const
63  {
64    return sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)); 
65  }
66
67}}} // of namespaces regression, yat, and theplu
Note: See TracBrowser for help on using the repository browser.