1 | // $Id: AveragerPairWeighted.h 490 2006-01-04 20:32:05Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_statistics_averager_pair_weighted_ |
---|
4 | #define _theplu_statistics_averager_pair_weighted_ |
---|
5 | |
---|
6 | #include <c++_tools/statistics/AveragerWeighted.h> |
---|
7 | #include <c++_tools/gslapi/vector.h> |
---|
8 | |
---|
9 | #include <cmath> |
---|
10 | //#include <utility> |
---|
11 | |
---|
12 | |
---|
13 | namespace theplu{ |
---|
14 | namespace statistics{ |
---|
15 | /// |
---|
16 | /// Class for taking care of mean and covariance of two variables in |
---|
17 | /// a weighted manner. |
---|
18 | /// |
---|
19 | /// <a href="../Statistics/index.html">Weighted Statistics document</a> |
---|
20 | /// |
---|
21 | /// If nothing else stated, each function fulfills the |
---|
22 | /// following:<br> <ul><li>Setting a weight to zero corresponds to |
---|
23 | /// removing the data point from the dataset.</li><li> Setting all |
---|
24 | /// weights to unity, the yields the same result as from |
---|
25 | /// corresponding function in AveragerPair.</li><li> Rescaling weights |
---|
26 | /// does not change the performance of the object.</li></ul> |
---|
27 | /// |
---|
28 | /// @see Averager AveragerWeighted AveragerPair |
---|
29 | /// |
---|
30 | class AveragerPairWeighted |
---|
31 | { |
---|
32 | public: |
---|
33 | |
---|
34 | AveragerPairWeighted() |
---|
35 | : wxy_(0), w_(0) |
---|
36 | { |
---|
37 | } |
---|
38 | |
---|
39 | /// |
---|
40 | /// Adding a pair of data points with value \a x and \a y, and |
---|
41 | /// their weights. If either of the weights are zero the addition |
---|
42 | /// is ignored |
---|
43 | /// |
---|
44 | void add(const double x, const double y, |
---|
45 | const double wx, const double wy); |
---|
46 | |
---|
47 | /// |
---|
48 | /// Adding pair of data and corresponding weight in vectors |
---|
49 | /// |
---|
50 | void add(const gslapi::vector& x, |
---|
51 | const gslapi::vector& y, |
---|
52 | const gslapi::vector& wx, |
---|
53 | const gslapi::vector& wy); |
---|
54 | |
---|
55 | /// |
---|
56 | /// @brief Pearson correlation coefficient. |
---|
57 | /// |
---|
58 | /// @return \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sqrt{\sum |
---|
59 | /// w_xw_y (y-m_y)^2\sum w_xw_y (y-m_y)^2}} \f$ where m is |
---|
60 | /// calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$ |
---|
61 | /// |
---|
62 | inline double correlation(void) const |
---|
63 | { return covariance() / ( x_.std()*y_.std() ); } |
---|
64 | |
---|
65 | /// |
---|
66 | /// \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sum w_xw_y} \f$ where m |
---|
67 | /// is calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$ |
---|
68 | /// |
---|
69 | inline double covariance(void) const { return sum_xy_centered()/sum_w(); } |
---|
70 | |
---|
71 | /// |
---|
72 | /// reset everything to zero |
---|
73 | /// |
---|
74 | inline void reset(void) { x_.reset(); y_.reset(); wxy_=0; w_=0; } |
---|
75 | |
---|
76 | /// |
---|
77 | /// @return \f$ \sum w_xw_y \f$ |
---|
78 | /// |
---|
79 | inline double sum_w(void) const { return w_; } |
---|
80 | |
---|
81 | /// |
---|
82 | /// @return \f$ \sum w_xw_yxy \f$ |
---|
83 | /// |
---|
84 | inline double sum_xy(void) const { return wxy_; } |
---|
85 | |
---|
86 | /// |
---|
87 | /// @return \f$ \sum w_xw_y (x-m_x)(y-m_y) \f$ where m is calculated as |
---|
88 | /// \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$ |
---|
89 | /// |
---|
90 | inline double sum_xy_centered(void) const |
---|
91 | { return sum_xy() - x_.sum_wx()*y_.mean(); } |
---|
92 | |
---|
93 | /// |
---|
94 | /// @note the weights are calculated as \f$ w = w_x * w_y \f$ |
---|
95 | /// |
---|
96 | /// @return AveragerWeighted for x |
---|
97 | /// |
---|
98 | inline const AveragerWeighted& x_averager(void) const { return x_; } |
---|
99 | |
---|
100 | /// |
---|
101 | /// @note the weights are calculated as \f$ w = w_x * w_y \f$ |
---|
102 | /// |
---|
103 | /// @return AveragerWeighted for y |
---|
104 | /// |
---|
105 | inline const AveragerWeighted& y_averager(void) const { return y_; } |
---|
106 | |
---|
107 | private: |
---|
108 | AveragerWeighted x_; // weighted averager with w = w_x*w_y |
---|
109 | AveragerWeighted y_; // weighted averager with w = w_x*w_y |
---|
110 | double wxy_; |
---|
111 | double w_; |
---|
112 | |
---|
113 | }; |
---|
114 | |
---|
115 | }} // of namespace statistics and namespace theplu |
---|
116 | |
---|
117 | #endif |
---|