1 | // $Id: AveragerWeighted.cc 1392 2008-07-28 19:35:30Z peter $ |
---|
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 2 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 this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
24 | 02111-1307, USA. |
---|
25 | */ |
---|
26 | |
---|
27 | #include "AveragerWeighted.h" |
---|
28 | #include "Averager.h" |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace yat { |
---|
32 | namespace statistics { |
---|
33 | |
---|
34 | AveragerWeighted::AveragerWeighted(void) |
---|
35 | : w_(Averager()), wx_(Averager()), wwx_(0), wxx_(0) |
---|
36 | { |
---|
37 | } |
---|
38 | |
---|
39 | AveragerWeighted::AveragerWeighted(const AveragerWeighted& a) |
---|
40 | : w_(Averager(a.sum_w(),a.sum_ww(),1)), |
---|
41 | wx_(Averager(a.sum_wx(),a.sum_wwxx(),1)), |
---|
42 | wwx_(a.sum_wwx()), |
---|
43 | wxx_(a.sum_wxx()) |
---|
44 | { |
---|
45 | } |
---|
46 | |
---|
47 | void AveragerWeighted::add(const double d, const double w) |
---|
48 | { |
---|
49 | if (!w) |
---|
50 | return; |
---|
51 | w_.add(w); wx_.add(w*d); wwx_+=w*w*d; wxx_+=w*d*d; |
---|
52 | } |
---|
53 | |
---|
54 | double AveragerWeighted::mean(void) const |
---|
55 | { |
---|
56 | return sum_wx()/sum_w(); |
---|
57 | } |
---|
58 | |
---|
59 | double AveragerWeighted::n(void) const |
---|
60 | { |
---|
61 | return sum_w()*sum_w()/sum_ww(); |
---|
62 | } |
---|
63 | |
---|
64 | void AveragerWeighted::rescale(double a) |
---|
65 | { |
---|
66 | wx_.rescale(a); |
---|
67 | wwx_*=a; |
---|
68 | wxx_*=a*a; |
---|
69 | } |
---|
70 | |
---|
71 | void AveragerWeighted::reset(void) |
---|
72 | { |
---|
73 | wx_.reset(); w_.reset(); wwx_=0; wxx_=0; |
---|
74 | } |
---|
75 | |
---|
76 | double AveragerWeighted::std(void) const |
---|
77 | { |
---|
78 | return sqrt(variance()); |
---|
79 | } |
---|
80 | |
---|
81 | double AveragerWeighted::standard_error(void) const |
---|
82 | { |
---|
83 | return sqrt(sum_ww()/(sum_w()*sum_w()*sum_w()) * sum_xx_centered()); |
---|
84 | } |
---|
85 | |
---|
86 | double AveragerWeighted::sum_w(void) const |
---|
87 | { |
---|
88 | return w_.sum_x(); |
---|
89 | } |
---|
90 | |
---|
91 | double AveragerWeighted::sum_ww(void) const |
---|
92 | { |
---|
93 | return w_.sum_xx(); |
---|
94 | } |
---|
95 | |
---|
96 | double AveragerWeighted::sum_wwx(void) const |
---|
97 | { |
---|
98 | return wwx_; |
---|
99 | } |
---|
100 | |
---|
101 | double AveragerWeighted::sum_wwxx(void) const |
---|
102 | { |
---|
103 | return wx_.sum_xx(); |
---|
104 | } |
---|
105 | |
---|
106 | double AveragerWeighted::sum_wx(void) const |
---|
107 | { |
---|
108 | return wx_.sum_x(); |
---|
109 | } |
---|
110 | |
---|
111 | double AveragerWeighted::sum_wxx(void) const |
---|
112 | { |
---|
113 | return wxx_; |
---|
114 | } |
---|
115 | |
---|
116 | double AveragerWeighted::sum_xx_centered(void) const |
---|
117 | { |
---|
118 | return sum_wxx() - mean()*mean()*sum_w(); |
---|
119 | } |
---|
120 | |
---|
121 | double AveragerWeighted::variance(const double m) const |
---|
122 | { |
---|
123 | return (sum_wxx()-2*m*sum_wx())/sum_w()+m*m; |
---|
124 | } |
---|
125 | |
---|
126 | double AveragerWeighted::variance(void) const |
---|
127 | { |
---|
128 | return sum_xx_centered()/sum_w(); |
---|
129 | } |
---|
130 | |
---|
131 | const Averager& AveragerWeighted::wx(void) const |
---|
132 | { |
---|
133 | return wx_; |
---|
134 | } |
---|
135 | |
---|
136 | const Averager& AveragerWeighted::w(void) const |
---|
137 | { |
---|
138 | return w_; |
---|
139 | } |
---|
140 | |
---|
141 | const AveragerWeighted& AveragerWeighted::operator+=(const AveragerWeighted& a) |
---|
142 | { |
---|
143 | wx_+=a.wx(); w_+=a.w(); wwx_+=a.sum_wwx(); wxx_+=a.sum_wxx(); |
---|
144 | return *this; |
---|
145 | } |
---|
146 | |
---|
147 | }}} // of namespace statistics, yat, and theplu |
---|