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

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

Addresses #170.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.4 KB
Line 
1// $Id: Linear.cc 718 2006-12-26 09:56:26Z 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  Linear::Linear(void)
35    : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0),
36      chisq_(0), m_x_(0)
37  {
38  }
39
40  Linear::~Linear(void)
41  {
42  }
43
44  double Linear::alpha(void) const
45  {
46    return alpha_;
47  }
48
49  double Linear::alpha_err(void) const
50  {
51    return sqrt(alpha_var_);
52  }
53
54  double Linear::beta(void) const
55  {
56    return beta_;
57  }
58
59  double Linear::beta_err(void) const
60  {
61    return sqrt(beta_var_);
62  }
63
64  double Linear::chisq(void) const 
65  { 
66    return chisq_; 
67  }
68
69  void Linear::fit(const utility::vector& x, const utility::vector& y) 
70  {
71    ap_.reset();
72    for (size_t i=0; i<x.size(); i++)
73      ap_.add(x(i),y(i));
74
75    alpha_ = ap_.y_averager().mean();
76    beta_ = ap_.covariance() / ap_.x_averager().variance();
77
78    // calculating deviation between data and model
79    chisq_ = ( (ap_.y_averager().sum_xx_centered() - ap_.sum_xy_centered()*
80              ap_.sum_xy_centered()/ap_.x_averager().sum_xx_centered() ) / 
81               (x.size()-2) );
82    r2_= 1-chisq_/ap_.x_averager().variance();
83    alpha_var_ = chisq_ / x.size();
84    beta_var_ = chisq_ / ap_.x_averager().sum_xx_centered();
85    m_x_ = ap_.x_averager().mean();
86  }
87
88  double Linear::predict(const double x) const
89  { 
90    return alpha_ + beta_ * (x-m_x_); 
91  }
92
93  double Linear::r2(void) const
94  {
95    return r2_;
96  }
97
98  double Linear::standard_error(const double x) const
99  {
100    return sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)); 
101  }
102
103}}} // of namespaces regression, yat, and theplu
Note: See TracBrowser for help on using the repository browser.