1 | // $Id: LinearWeighted.cc 831 2007-03-27 13:22:09Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005 Peter Johansson |
---|
5 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson |
---|
6 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
7 | |
---|
8 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
9 | |
---|
10 | The yat library is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU General Public License as |
---|
12 | published by the Free Software Foundation; either version 2 of the |
---|
13 | License, or (at your option) any later version. |
---|
14 | |
---|
15 | The yat library is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with this program; if not, write to the Free Software |
---|
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
23 | 02111-1307, USA. |
---|
24 | */ |
---|
25 | |
---|
26 | #include "LinearWeighted.h" |
---|
27 | #include "yat/statistics/AveragerPairWeighted.h" |
---|
28 | #include "yat/utility/vector.h" |
---|
29 | |
---|
30 | #include <cassert> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace regression { |
---|
35 | |
---|
36 | LinearWeighted::LinearWeighted(void) |
---|
37 | : OneDimensionalWeighted(), alpha_(0), alpha_var_(0), beta_(0), |
---|
38 | beta_var_(0) |
---|
39 | { |
---|
40 | } |
---|
41 | |
---|
42 | LinearWeighted::~LinearWeighted(void) |
---|
43 | { |
---|
44 | } |
---|
45 | |
---|
46 | double LinearWeighted::alpha(void) const |
---|
47 | { |
---|
48 | return alpha_; |
---|
49 | } |
---|
50 | |
---|
51 | double LinearWeighted::alpha_var(void) const |
---|
52 | { |
---|
53 | return alpha_var_; |
---|
54 | } |
---|
55 | |
---|
56 | double LinearWeighted::beta(void) const |
---|
57 | { |
---|
58 | return beta_; |
---|
59 | } |
---|
60 | |
---|
61 | double LinearWeighted::beta_var(void) const |
---|
62 | { |
---|
63 | return beta_var_; |
---|
64 | } |
---|
65 | |
---|
66 | void LinearWeighted::fit(const utility::vector& x, |
---|
67 | const utility::vector& y, |
---|
68 | const utility::vector& w) |
---|
69 | { |
---|
70 | assert(x.size()==y.size()); |
---|
71 | assert(x.size()==w.size()); |
---|
72 | |
---|
73 | // AveragerPairWeighted requires 2 weights but works only on the |
---|
74 | // product wx*wy, so we can send in w and a dummie to get what we |
---|
75 | // want. |
---|
76 | ap_.reset(); |
---|
77 | ap_.add_values(x,y,utility::vector(x.size(),1),w); |
---|
78 | |
---|
79 | alpha_ = m_y(); |
---|
80 | beta_ = sxy()/sxx(); |
---|
81 | |
---|
82 | chisq_=0; |
---|
83 | for (size_t i=0; i<x.size(); ++i){ |
---|
84 | double res = predict(x(i))-y(i); |
---|
85 | chisq_ += w(i)*res*res; |
---|
86 | } |
---|
87 | |
---|
88 | alpha_var_ = s2()/ap_.y_averager().sum_w(); |
---|
89 | beta_var_ = s2()/sxx(); |
---|
90 | } |
---|
91 | |
---|
92 | double LinearWeighted::m_x(void) const |
---|
93 | { |
---|
94 | return ap_.x_averager().mean(); |
---|
95 | } |
---|
96 | |
---|
97 | double LinearWeighted::m_y(void) const |
---|
98 | { |
---|
99 | return ap_.y_averager().mean(); |
---|
100 | } |
---|
101 | |
---|
102 | double LinearWeighted::predict(const double x) const |
---|
103 | { |
---|
104 | return alpha_ + beta_ * (x-m_x()); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | double LinearWeighted::s2(double w) const |
---|
109 | { |
---|
110 | return chisq_/(w*(ap_.y_averager().n()-2)); |
---|
111 | } |
---|
112 | |
---|
113 | double LinearWeighted::standard_error2(const double x) const |
---|
114 | { |
---|
115 | return alpha_var_ + beta_var_*(x-m_x())*(x-m_x()); |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | double LinearWeighted::sxx(void) const |
---|
120 | { |
---|
121 | return ap_.x_averager().sum_xx_centered(); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | double LinearWeighted::sxy(void) const |
---|
126 | { |
---|
127 | return ap_.sum_xy_centered(); |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | double LinearWeighted::syy(void) const |
---|
132 | { |
---|
133 | return ap_.y_averager().sum_xx_centered(); |
---|
134 | } |
---|
135 | |
---|
136 | }}} // of namespaces regression, yat, and theplu |
---|