1 | // $Id: Linear.cc 730 2007-01-06 11:02:21Z 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 | { |
---|
35 | } |
---|
36 | |
---|
37 | Linear::~Linear(void) |
---|
38 | { |
---|
39 | } |
---|
40 | |
---|
41 | double Linear::alpha(void) const |
---|
42 | { |
---|
43 | return alpha_; |
---|
44 | } |
---|
45 | |
---|
46 | double Linear::alpha_var(void) const |
---|
47 | { |
---|
48 | return alpha_var_; |
---|
49 | } |
---|
50 | |
---|
51 | double Linear::beta(void) const |
---|
52 | { |
---|
53 | return beta_; |
---|
54 | } |
---|
55 | |
---|
56 | double Linear::beta_var(void) const |
---|
57 | { |
---|
58 | return beta_var_; |
---|
59 | } |
---|
60 | |
---|
61 | void Linear::fit(const utility::vector& x, const utility::vector& y) |
---|
62 | { |
---|
63 | ap_.reset(); |
---|
64 | for (size_t i=0; i<x.size(); i++) |
---|
65 | ap_.add(x(i),y(i)); |
---|
66 | |
---|
67 | alpha_ = ap_.y_averager().mean(); |
---|
68 | beta_ = ap_.sum_xy_centered() / ap_.x_averager().sum_xx_centered(); |
---|
69 | |
---|
70 | // calculating deviation between data and model |
---|
71 | chisq_ = (ap_.y_averager().sum_xx_centered() - ap_.sum_xy_centered()* |
---|
72 | ap_.sum_xy_centered()/ap_.x_averager().sum_xx_centered() ); |
---|
73 | alpha_var_ = s2() / x.size(); |
---|
74 | beta_var_ = s2() / ap_.x_averager().sum_xx_centered(); |
---|
75 | } |
---|
76 | |
---|
77 | double Linear::predict(const double x) const |
---|
78 | { |
---|
79 | return alpha_ + beta_ * (x - ap_.x_averager().mean()); |
---|
80 | } |
---|
81 | |
---|
82 | double Linear::s2(void) const |
---|
83 | { |
---|
84 | return chisq()/(ap_.n()-2); |
---|
85 | } |
---|
86 | |
---|
87 | double Linear::standard_error2(const double x) const |
---|
88 | { |
---|
89 | return alpha_var_+beta_var_*(x-ap_.x_averager().mean())* |
---|
90 | (x-ap_.x_averager().mean()); |
---|
91 | } |
---|
92 | |
---|
93 | }}} // of namespaces regression, yat, and theplu |
---|