1 | // $Id: AveragerWeighted.cc 1487 2008-09-10 08:41:36Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
6 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
7 | Copyright (C) 2008 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 3 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "AveragerWeighted.h" |
---|
26 | #include "Averager.h" |
---|
27 | |
---|
28 | namespace theplu { |
---|
29 | namespace yat { |
---|
30 | namespace statistics { |
---|
31 | |
---|
32 | AveragerWeighted::AveragerWeighted(void) |
---|
33 | : w_(Averager()), wx_(Averager()), wwx_(0), wxx_(0) |
---|
34 | { |
---|
35 | } |
---|
36 | |
---|
37 | AveragerWeighted::AveragerWeighted(const AveragerWeighted& a) |
---|
38 | : w_(Averager(a.sum_w(),a.sum_ww(),1)), |
---|
39 | wx_(Averager(a.sum_wx(),a.sum_wwxx(),1)), |
---|
40 | wwx_(a.sum_wwx()), |
---|
41 | wxx_(a.sum_wxx()) |
---|
42 | { |
---|
43 | } |
---|
44 | |
---|
45 | void AveragerWeighted::add(const double d, const double w) |
---|
46 | { |
---|
47 | if (!w) |
---|
48 | return; |
---|
49 | w_.add(w); wx_.add(w*d); wwx_+=w*w*d; wxx_+=w*d*d; |
---|
50 | } |
---|
51 | |
---|
52 | double AveragerWeighted::mean(void) const |
---|
53 | { |
---|
54 | return sum_wx()/sum_w(); |
---|
55 | } |
---|
56 | |
---|
57 | double AveragerWeighted::n(void) const |
---|
58 | { |
---|
59 | return sum_w()*sum_w()/sum_ww(); |
---|
60 | } |
---|
61 | |
---|
62 | void AveragerWeighted::rescale(double a) |
---|
63 | { |
---|
64 | wx_.rescale(a); |
---|
65 | wwx_*=a; |
---|
66 | wxx_*=a*a; |
---|
67 | } |
---|
68 | |
---|
69 | void AveragerWeighted::reset(void) |
---|
70 | { |
---|
71 | wx_.reset(); w_.reset(); wwx_=0; wxx_=0; |
---|
72 | } |
---|
73 | |
---|
74 | double AveragerWeighted::std(void) const |
---|
75 | { |
---|
76 | return sqrt(variance()); |
---|
77 | } |
---|
78 | |
---|
79 | double AveragerWeighted::standard_error(void) const |
---|
80 | { |
---|
81 | return sqrt(sum_ww()/(sum_w()*sum_w()*sum_w()) * sum_xx_centered()); |
---|
82 | } |
---|
83 | |
---|
84 | double AveragerWeighted::sum_w(void) const |
---|
85 | { |
---|
86 | return w_.sum_x(); |
---|
87 | } |
---|
88 | |
---|
89 | double AveragerWeighted::sum_ww(void) const |
---|
90 | { |
---|
91 | return w_.sum_xx(); |
---|
92 | } |
---|
93 | |
---|
94 | double AveragerWeighted::sum_wwx(void) const |
---|
95 | { |
---|
96 | return wwx_; |
---|
97 | } |
---|
98 | |
---|
99 | double AveragerWeighted::sum_wwxx(void) const |
---|
100 | { |
---|
101 | return wx_.sum_xx(); |
---|
102 | } |
---|
103 | |
---|
104 | double AveragerWeighted::sum_wx(void) const |
---|
105 | { |
---|
106 | return wx_.sum_x(); |
---|
107 | } |
---|
108 | |
---|
109 | double AveragerWeighted::sum_wxx(void) const |
---|
110 | { |
---|
111 | return wxx_; |
---|
112 | } |
---|
113 | |
---|
114 | double AveragerWeighted::sum_xx_centered(void) const |
---|
115 | { |
---|
116 | return sum_wxx() - mean()*mean()*sum_w(); |
---|
117 | } |
---|
118 | |
---|
119 | double AveragerWeighted::variance(const double m) const |
---|
120 | { |
---|
121 | return (sum_wxx()-2*m*sum_wx())/sum_w()+m*m; |
---|
122 | } |
---|
123 | |
---|
124 | double AveragerWeighted::variance(void) const |
---|
125 | { |
---|
126 | return sum_xx_centered()/sum_w(); |
---|
127 | } |
---|
128 | |
---|
129 | const Averager& AveragerWeighted::wx(void) const |
---|
130 | { |
---|
131 | return wx_; |
---|
132 | } |
---|
133 | |
---|
134 | const Averager& AveragerWeighted::w(void) const |
---|
135 | { |
---|
136 | return w_; |
---|
137 | } |
---|
138 | |
---|
139 | const AveragerWeighted& AveragerWeighted::operator+=(const AveragerWeighted& a) |
---|
140 | { |
---|
141 | wx_+=a.wx(); w_+=a.w(); wwx_+=a.sum_wwx(); wxx_+=a.sum_wxx(); |
---|
142 | return *this; |
---|
143 | } |
---|
144 | |
---|
145 | }}} // of namespace statistics, yat, and theplu |
---|