source: trunk/yat/statistics/AveragerPair.cc @ 2565

Last change on this file since 2565 was 2565, checked in by Peter, 10 years ago

refs #671

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