1 | // $Id: LinearWeighted.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 "LinearWeighted.h" |
---|
25 | #include "yat/statistics/AveragerPairWeighted.h" |
---|
26 | #include "yat/utility/vector.h" |
---|
27 | |
---|
28 | #include <cassert> |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace yat { |
---|
32 | namespace regression { |
---|
33 | |
---|
34 | LinearWeighted::LinearWeighted(void) |
---|
35 | : OneDimensionalWeighted(), alpha_(0), alpha_var_(0), beta_(0), |
---|
36 | beta_var_(0) |
---|
37 | { |
---|
38 | } |
---|
39 | |
---|
40 | LinearWeighted::~LinearWeighted(void) |
---|
41 | { |
---|
42 | } |
---|
43 | |
---|
44 | double LinearWeighted::alpha(void) const |
---|
45 | { |
---|
46 | return alpha_; |
---|
47 | } |
---|
48 | |
---|
49 | double LinearWeighted::alpha_var(void) const |
---|
50 | { |
---|
51 | return alpha_var_; |
---|
52 | } |
---|
53 | |
---|
54 | double LinearWeighted::beta(void) const |
---|
55 | { |
---|
56 | return beta_; |
---|
57 | } |
---|
58 | |
---|
59 | double LinearWeighted::beta_var(void) const |
---|
60 | { |
---|
61 | return beta_var_; |
---|
62 | } |
---|
63 | |
---|
64 | void LinearWeighted::fit(const utility::vector& x, |
---|
65 | const utility::vector& y, |
---|
66 | const utility::vector& w) |
---|
67 | { |
---|
68 | assert(x.size()==y.size()); |
---|
69 | assert(x.size()==w.size()); |
---|
70 | |
---|
71 | // AveragerPairWeighted requires 2 weights but works only on the |
---|
72 | // product wx*wy, so we can send in w and a dummie to get what we |
---|
73 | // want. |
---|
74 | ap_.reset(); |
---|
75 | ap_.add_values(x,y,utility::vector(x.size(),1),w); |
---|
76 | |
---|
77 | alpha_ = m_y(); |
---|
78 | beta_ = sxy()/sxx(); |
---|
79 | |
---|
80 | chisq_=0; |
---|
81 | for (size_t i=0; i<x.size(); ++i){ |
---|
82 | double res = predict(x(i))-y(i); |
---|
83 | chisq_ += w(i)*res*res; |
---|
84 | } |
---|
85 | |
---|
86 | alpha_var_ = s2()/ap_.y_averager().sum_w(); |
---|
87 | beta_var_ = s2()/sxx(); |
---|
88 | } |
---|
89 | |
---|
90 | double LinearWeighted::m_x(void) const |
---|
91 | { |
---|
92 | return ap_.x_averager().mean(); |
---|
93 | } |
---|
94 | |
---|
95 | double LinearWeighted::m_y(void) const |
---|
96 | { |
---|
97 | return ap_.y_averager().mean(); |
---|
98 | } |
---|
99 | |
---|
100 | double LinearWeighted::predict(const double x) const |
---|
101 | { |
---|
102 | return alpha_ + beta_ * (x-m_x()); |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | double LinearWeighted::s2(double w) const |
---|
107 | { |
---|
108 | return chisq_/(w*(ap_.y_averager().n()-2)); |
---|
109 | } |
---|
110 | |
---|
111 | double LinearWeighted::standard_error2(const double x) const |
---|
112 | { |
---|
113 | return alpha_var_ + beta_var_*(x-m_x())*(x-m_x()); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | double LinearWeighted::sxx(void) const |
---|
118 | { |
---|
119 | return ap_.x_averager().sum_xx_centered(); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | double LinearWeighted::sxy(void) const |
---|
124 | { |
---|
125 | return ap_.sum_xy_centered(); |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | double LinearWeighted::syy(void) const |
---|
130 | { |
---|
131 | return ap_.y_averager().sum_xx_centered(); |
---|
132 | } |
---|
133 | |
---|
134 | }}} // of namespaces regression, yat, and theplu |
---|