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

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

References #83. Changing project name to yat. Compilation will fail in this revision.

  • 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 675 2006-10-10 12:08:45Z 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 "yat/statistics/Linear.h"
25
26#include "yat/statistics/AveragerPair.h"
27#include "yat/utility/vector.h"
28
29#include <gsl/gsl_fit.h>
30
31
32namespace theplu {
33namespace statistics {
34namespace regression {
35
36
37  void Linear::fit(const utility::vector& x, const utility::vector& y) 
38  {
39    ap_.reset();
40    for (size_t i=0; i<x.size(); i++)
41      ap_.add(x(i),y(i));
42
43    alpha_ = ap_.y_averager().mean();
44    beta_ = ap_.covariance() / ap_.x_averager().variance();
45
46    // calculating deviation between data and model
47    msd_ = (ap_.y_averager().sum_xx_centered() - ap_.sum_xy_centered() *
48            ap_.sum_xy_centered()/ap_.x_averager().sum_xx_centered() )/x.size();
49    r2_= 1-msd_/ap_.x_averager().variance();
50    alpha_var_ = msd_ / x.size();
51    beta_var_ = msd_ / ap_.x_averager().sum_xx_centered();
52    m_x_ = ap_.x_averager().mean();
53  }
54
55  double Linear::predict(const double x) const
56  { 
57    return alpha_ + beta_ * (x-m_x_); 
58  }
59
60  double Linear::prediction_error(const double x) const
61  {
62    return sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)+msd_ ); 
63  }
64
65  double Linear::standard_error(const double x) const
66  {
67    return sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)); 
68  }
69
70}}} // of namespaces regression, statisitcs and thep
Note: See TracBrowser for help on using the repository browser.