1 | // $Id: Linear.cc 1392 2008-07-28 19:35:30Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004, 2005, 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2008 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 2 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "Linear.h" |
---|
26 | #include "yat/statistics/AveragerPair.h" |
---|
27 | #include "yat/utility/VectorBase.h" |
---|
28 | |
---|
29 | namespace theplu { |
---|
30 | namespace yat { |
---|
31 | namespace regression { |
---|
32 | |
---|
33 | Linear::Linear(void) |
---|
34 | : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0) |
---|
35 | { |
---|
36 | } |
---|
37 | |
---|
38 | Linear::~Linear(void) |
---|
39 | { |
---|
40 | } |
---|
41 | |
---|
42 | double Linear::alpha(void) const |
---|
43 | { |
---|
44 | return alpha_; |
---|
45 | } |
---|
46 | |
---|
47 | double Linear::alpha_var(void) const |
---|
48 | { |
---|
49 | return alpha_var_; |
---|
50 | } |
---|
51 | |
---|
52 | double Linear::beta(void) const |
---|
53 | { |
---|
54 | return beta_; |
---|
55 | } |
---|
56 | |
---|
57 | double Linear::beta_var(void) const |
---|
58 | { |
---|
59 | return beta_var_; |
---|
60 | } |
---|
61 | |
---|
62 | void Linear::fit(const utility::VectorBase& x, const utility::VectorBase& y) |
---|
63 | { |
---|
64 | ap_.reset(); |
---|
65 | for (size_t i=0; i<x.size(); i++) |
---|
66 | ap_.add(x(i),y(i)); |
---|
67 | |
---|
68 | alpha_ = ap_.y_averager().mean(); |
---|
69 | beta_ = ap_.sum_xy_centered() / ap_.x_averager().sum_xx_centered(); |
---|
70 | |
---|
71 | // calculating deviation between data and model |
---|
72 | chisq_ = (ap_.y_averager().sum_xx_centered() - ap_.sum_xy_centered()* |
---|
73 | ap_.sum_xy_centered()/ap_.x_averager().sum_xx_centered() ); |
---|
74 | alpha_var_ = s2() / x.size(); |
---|
75 | beta_var_ = s2() / ap_.x_averager().sum_xx_centered(); |
---|
76 | } |
---|
77 | |
---|
78 | double Linear::predict(const double x) const |
---|
79 | { |
---|
80 | return alpha_ + beta_ * (x - ap_.x_averager().mean()); |
---|
81 | } |
---|
82 | |
---|
83 | double Linear::s2(void) const |
---|
84 | { |
---|
85 | return chisq()/(ap_.n()-2); |
---|
86 | } |
---|
87 | |
---|
88 | double Linear::standard_error2(const double x) const |
---|
89 | { |
---|
90 | return alpha_var_+beta_var_*(x-ap_.x_averager().mean())* |
---|
91 | (x-ap_.x_averager().mean()); |
---|
92 | } |
---|
93 | |
---|
94 | }}} // of namespaces regression, yat, and theplu |
---|