source: trunk/yat/statistics/AveragerPair.cc

Last change on this file was 2919, checked in by Peter, 9 years ago

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1// $Id: AveragerPair.cc 2919 2012-12-19 06:54:23Z peter $
2
3/*
4  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2005 Peter Johansson
6  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
7  Copyright (C) 2011, 2012 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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include <config.h>
26
27#include "AveragerPair.h"
28#include "Averager.h"
29
30#include <limits>
31#include <utility>
32
33namespace theplu {
34namespace yat {
35namespace statistics {
36
37  AveragerPair::AveragerPair(void)
38    : xy_centered_(0.0)
39  {
40  }
41
42
43  AveragerPair::AveragerPair(const AveragerPair& a)
44    : x_(a.x_), y_(a.y_), xy_centered_(a.xy_centered_)
45  {
46  }
47
48
49  void AveragerPair::add(const double x, const double y, const long n)
50  {
51    xy_add(x, y, 0, n);
52    x_.add(x,n);
53    y_.add(y,n);
54  }
55
56
57  double AveragerPair::ccc(void) const
58  {
59    return ((2*covariance()) /
60            ((x_.variance()+y_.variance()) +
61             (x_.mean()-y_.mean())*(x_.mean()-y_.mean())));
62  }
63
64
65  double AveragerPair::correlation(void) const
66  {
67    return sum_xy_centered() /
68      std::sqrt(x_.sum_xx_centered() * y_.sum_xx_centered());
69  }
70
71
72  double AveragerPair::covariance(void) const
73  {
74    return xy_centered_ / n();
75  }
76
77
78  double AveragerPair::mean_xy(void) const
79  {
80    if (!n())
81      return std::numeric_limits<double>::quiet_NaN();
82    return sum_xy()/n();
83  }
84
85
86  double AveragerPair::msd(void) const
87  {
88    return sum_squared_deviation()/n();
89  }
90
91
92  long AveragerPair::n(void) const
93  {
94    return x_.n();
95  }
96
97
98  void AveragerPair::reset(void)
99  {
100    x_.reset();
101    y_.reset();
102    xy_centered_ = 0.0;
103  }
104
105
106  const AveragerPair& AveragerPair::operator=(const AveragerPair& a)
107  {
108    x_=a.x_; y_=a.y_; xy_centered_=a.xy_centered_;
109    return *this;
110  }
111
112
113  double AveragerPair::sum_squared_deviation(void) const
114  {
115    return x_averager().sum_xx()+y_averager().sum_xx()-2*sum_xy() ;
116  }
117
118
119  double AveragerPair::sum_xy(void) const
120  {
121    return xy_centered_ + x_.sum_x()*y_.mean();;
122  }
123
124
125  double AveragerPair::sum_xy_centered(void) const
126  {
127    return xy_centered_;
128  }
129
130
131  void AveragerPair::xy_add(double mx, double my, double xy_centered, long n)
132  {
133    if (!n)
134      return;
135    /*
136      For derivation of update formula refer to
137      http://www.janinebennett.org/index_files/ParallelStatisticsAlgorithms.pdf
138    */
139    xy_centered_ += xy_centered;
140    if (this->n())
141      xy_centered_ += static_cast<double>(n*this->n()) / (n+this->n()) *
142        (mx-x_.mean()) * (my-y_.mean());
143
144  }
145
146
147  const Averager& AveragerPair::x_averager(void) const
148  {
149    return x_;
150  }
151
152
153  const Averager& AveragerPair::y_averager(void) const
154  {
155    return y_;
156  }
157
158
159  const AveragerPair& AveragerPair::operator+=(const AveragerPair& a)
160  {
161    xy_add(a.x_averager().mean(), a.y_averager().mean(), a.xy_centered_,a.n());
162    x_+=a.x_averager();
163    y_+=a.y_averager();
164    return *this;
165  }
166
167}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.