# source:trunk/yat/statistics/Averager.cc@2558

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

refs #671. Follow Knuth's algorithm to calculate average and variance online

• Property svn:eol-style set to `native`
• Property svn:keywords set to `Author Date Id Revision`
File size: 2.9 KB
Line
1// \$Id: Averager.cc 2558 2011-09-24 20:53:04Z peter \$
2
3/*
4  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2005 Peter Johansson
6  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2011 Peter Johansson
9
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include "Averager.h"
27
28#include <cassert>
29#include <limits>
30
31namespace theplu {
32namespace yat {
33namespace statistics {
34
35  Averager::Averager(void)
36    : n_(0), mean_(0), m2_(0)
37  {
38  }
39
40
41  Averager::Averager(double x, double xx, long n)
42    : n_(n), mean_(x/n), m2_(xx-x*x/n)
43  {
44  }
45
46
47  Averager::Averager(const Averager& a)
48    : n_(a.n_), mean_(a.mean_), m2_(a.m2_)
49  {
50  }
51
52
53  void Averager::add(double d, long n)
54  {
55    double delta = d - mean_;
56    mean_ += n*delta/(n+n_);
57    m2_ += delta*delta*n_*n/(n_+n);
58    n_  += n;
59    assert(n_>-1);
60  }
61
62  double Averager::cv(void) const
63  {
64    return std()/mean();
65  }
66
67  double Averager::mean(void) const
68  {
69    return mean_;
70  }
71
72  long Averager::n(void) const
73  {
74    return n_;
75  }
76
77  void Averager::rescale(double a)
78  {
79    mean_  *= a;
80    m2_ *= a*a;
81  }
82
83  void Averager::reset(void)
84  {
85    n_=0;
86    mean_=m2_=0.0;
87  }
88
89  double Averager::standard_error(void) const
90  {
91    return sqrt(variance()/n_);
92  }
93
94  double Averager::std(void) const
95  {
96    return sqrt(variance());
97  }
98
99  double Averager::std(double m) const
100  {
101    return sqrt(variance(m));
102  }
103
104  double Averager::sum_x(void)  const
105  {
106    return n_*mean_;
107  }
108
109  double Averager::sum_xx(void) const
110  {
111    return m2_+n_*mean_*mean_;
112  }
113
114  double Averager::sum_xx_centered(void)  const
115  {
116    return m2_;
117  }
118
119  double Averager::variance(double m) const
120  {
121    return sum_xx()/n() + m*m - 2*m*mean();
122  }
123
124  double Averager::variance(void) const
125  {
126    return sum_xx_centered()/n_;
127  }
128
129  double Averager::variance_unbiased(void) const
130  {
131    return (n_>1) ? sum_xx_centered()/(n_-1) :
132      std::numeric_limits<double>::quiet_NaN();
133  }
134
135  const Averager& Averager::operator=(const Averager& a)
136  {
137    if (this != &a) { // avoid self-assignment
138      n_  = a.n_;
139      mean_  = a.mean_;
140      m2_ = a.m2_;
141    }
142    return *this;
143  }
144
145  const Averager& Averager::operator+=(const Averager& a)
146  {
147    mean_ += (n()*mean() + a.n()*a.mean()) / (n() + a.n());
148    double delta = mean_ = a.mean();
149    m2_ += a.m2_ + n()*a.n()*delta*delta/(n()+a.n());
150    n_+=a.n_;
151    return *this;
152  }
153
154}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.