1 | // $Id: Linear.cc 727 2007-01-04 15:06:14Z peter $ |
---|
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 | namespace theplu { |
---|
29 | namespace yat { |
---|
30 | namespace regression { |
---|
31 | |
---|
32 | Linear::Linear(void) |
---|
33 | : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0), |
---|
34 | chisq_(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 | double Linear::chisq(void) const |
---|
63 | { |
---|
64 | return chisq_; |
---|
65 | } |
---|
66 | |
---|
67 | void Linear::fit(const utility::vector& x, const utility::vector& y) |
---|
68 | { |
---|
69 | ap_.reset(); |
---|
70 | for (size_t i=0; i<x.size(); i++) |
---|
71 | ap_.add(x(i),y(i)); |
---|
72 | |
---|
73 | alpha_ = ap_.y_averager().mean(); |
---|
74 | beta_ = ap_.covariance() / ap_.x_averager().variance(); |
---|
75 | |
---|
76 | // calculating deviation between data and model |
---|
77 | chisq_ = (ap_.y_averager().sum_xx_centered() - ap_.sum_xy_centered()* |
---|
78 | ap_.sum_xy_centered()/ap_.x_averager().sum_xx_centered() ); |
---|
79 | r2_= 1-chisq_/ap_.x_averager().sum_xx_centered(); |
---|
80 | alpha_var_ = s2() / x.size(); |
---|
81 | beta_var_ = s2() / ap_.x_averager().sum_xx_centered(); |
---|
82 | } |
---|
83 | |
---|
84 | double Linear::predict(const double x) const |
---|
85 | { |
---|
86 | return alpha_ + beta_ * (x - ap_.x_averager().mean()); |
---|
87 | } |
---|
88 | |
---|
89 | double Linear::r2(void) const |
---|
90 | { |
---|
91 | return r2_; |
---|
92 | } |
---|
93 | |
---|
94 | double Linear::s2(void) const |
---|
95 | { |
---|
96 | return chisq()/(ap_.n()-2); |
---|
97 | } |
---|
98 | |
---|
99 | double Linear::standard_error2(const double x) const |
---|
100 | { |
---|
101 | return alpha_var_+beta_var_*(x-ap_.x_averager().mean())* |
---|
102 | (x-ap_.x_averager().mean()); |
---|
103 | } |
---|
104 | |
---|
105 | }}} // of namespaces regression, yat, and theplu |
---|