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