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