1 | // $Id: AveragerPairWeighted.cc 1486 2008-09-09 21:17:19Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005 Peter Johansson, Markus Ringnér |
---|
5 | Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
6 | Copyright (C) 2008 Peter Johansson |
---|
7 | |
---|
8 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
9 | |
---|
10 | The yat library is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU General Public License as |
---|
12 | published by the Free Software Foundation; either version 3 of the |
---|
13 | License, or (at your option) any later version. |
---|
14 | |
---|
15 | The yat library is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with this program; if not, write to the Free Software |
---|
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
23 | 02111-1307, USA. |
---|
24 | */ |
---|
25 | |
---|
26 | #include "AveragerPairWeighted.h" |
---|
27 | #include "AveragerPair.h" |
---|
28 | #include "Averager.h" |
---|
29 | |
---|
30 | #include <cassert> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace statistics { |
---|
35 | |
---|
36 | |
---|
37 | AveragerPairWeighted::AveragerPairWeighted(void) |
---|
38 | : wxy_(0), w_(0) |
---|
39 | { |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | void AveragerPairWeighted::add(const double x, const double y, |
---|
44 | const double wx, const double wy) |
---|
45 | { |
---|
46 | if(wx==0.0 || wy==0.0) { |
---|
47 | return; |
---|
48 | } |
---|
49 | assert(!std::isnan(x) && "x is nan"); |
---|
50 | assert(!std::isnan(y) && "y is nan"); |
---|
51 | assert(!std::isnan(wx) && "wx is nan"); |
---|
52 | assert(!std::isnan(wy) && "wy is nan"); |
---|
53 | double w=wx*wy; |
---|
54 | x_.add(x,w); |
---|
55 | y_.add(y,w); |
---|
56 | wxy_ += w*x*y; |
---|
57 | w_ +=w; |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | double AveragerPairWeighted::correlation(void) const |
---|
62 | { |
---|
63 | return covariance() / sqrt(x_.variance()*y_.variance()); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | double AveragerPairWeighted::covariance(void) const |
---|
68 | { |
---|
69 | return sum_xy_centered()/sum_w(); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | double AveragerPairWeighted::msd(void) const |
---|
74 | { |
---|
75 | return (x_.sum_wxx()+y_.sum_wxx()-2*wxy_)/w_; |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | double AveragerPairWeighted::n(void) const |
---|
80 | { |
---|
81 | return x_.n(); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | void AveragerPairWeighted::reset(void) |
---|
86 | { |
---|
87 | x_.reset(); y_.reset(); wxy_=0; w_=0; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | double AveragerPairWeighted::sum_w(void) const |
---|
92 | { |
---|
93 | return w_; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | double AveragerPairWeighted::sum_xy(void) const |
---|
98 | { |
---|
99 | return wxy_; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | double AveragerPairWeighted::sum_xy_centered(void) const |
---|
104 | { |
---|
105 | return sum_xy() - x_.sum_wx()*y_.mean(); |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | const AveragerWeighted& AveragerPairWeighted::x_averager(void) const |
---|
110 | { |
---|
111 | return x_; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | const AveragerWeighted& AveragerPairWeighted::y_averager(void) const |
---|
116 | { |
---|
117 | return y_; |
---|
118 | } |
---|
119 | |
---|
120 | }}} // of namespace statistics, yat, and theplu |
---|