1 | // $Id: AveragerPairWeighted.cc 1743 2009-01-23 14:20:30Z 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 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 "AveragerPairWeighted.h" |
---|
28 | #include "AveragerPair.h" |
---|
29 | #include "Averager.h" |
---|
30 | |
---|
31 | #include <cassert> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace statistics { |
---|
36 | |
---|
37 | |
---|
38 | AveragerPairWeighted::AveragerPairWeighted(void) |
---|
39 | : wxy_(0), w_(0) |
---|
40 | { |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | void AveragerPairWeighted::add(const double x, const double y, |
---|
45 | const double wx, const double wy) |
---|
46 | { |
---|
47 | if(wx==0.0 || wy==0.0) { |
---|
48 | return; |
---|
49 | } |
---|
50 | assert(!std::isnan(x) && "x is nan"); |
---|
51 | assert(!std::isnan(y) && "y is nan"); |
---|
52 | assert(!std::isnan(wx) && "wx is nan"); |
---|
53 | assert(!std::isnan(wy) && "wy is nan"); |
---|
54 | double w=wx*wy; |
---|
55 | x_.add(x,w); |
---|
56 | y_.add(y,w); |
---|
57 | wxy_ += w*x*y; |
---|
58 | w_ +=w; |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | double AveragerPairWeighted::correlation(void) const |
---|
63 | { |
---|
64 | return covariance() / sqrt(x_.variance()*y_.variance()); |
---|
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*wxy_)/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_=0; w_=0; |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | double AveragerPairWeighted::sum_w(void) const |
---|
93 | { |
---|
94 | return w_; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | double AveragerPairWeighted::sum_xy(void) const |
---|
99 | { |
---|
100 | return wxy_; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | double AveragerPairWeighted::sum_xy_centered(void) const |
---|
105 | { |
---|
106 | return sum_xy() - x_.sum_wx()*y_.mean(); |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | const AveragerWeighted& AveragerPairWeighted::x_averager(void) const |
---|
111 | { |
---|
112 | return x_; |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | const AveragerWeighted& AveragerPairWeighted::y_averager(void) const |
---|
117 | { |
---|
118 | return y_; |
---|
119 | } |
---|
120 | |
---|
121 | }}} // of namespace statistics, yat, and theplu |
---|