source: trunk/yat/statistics/Averager.cc @ 2559

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

minor speed-up. refs #671

  • 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 2559 2011-09-24 21:11:48Z 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
15  License, or (at your option) any later version.
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    double R = delta * static_cast<double>(n) / static_cast<double>(n+n_);
57    mean_ += R;
58    m2_ += n_ * delta * R;
59    n_  += n;
60    assert(n_>-1);
61  }
62
63  double Averager::cv(void) const
64  {
65    return std()/mean();
66  }
67
68  double Averager::mean(void) const
69  {
70    return mean_;
71  }
72
73  long Averager::n(void) const
74  {
75    return n_;
76  }
77
78  void Averager::rescale(double a)
79  {
80    mean_  *= a;
81    m2_ *= a*a;
82  }
83
84  void Averager::reset(void)
85  {
86    n_=0;
87    mean_=m2_=0.0;
88  }
89
90  double Averager::standard_error(void) const
91  {
92    return sqrt(variance()/n_);
93  }
94
95  double Averager::std(void) const
96  {
97    return sqrt(variance());
98  }
99
100  double Averager::std(double m) const
101  {
102    return sqrt(variance(m));
103  }
104
105  double Averager::sum_x(void)  const
106  {
107    return n_*mean_;
108  }
109
110  double Averager::sum_xx(void) const
111  {
112    return m2_+n_*mean_*mean_;
113  }
114
115  double Averager::sum_xx_centered(void)  const
116  {
117    return m2_;
118  }
119
120  double Averager::variance(double m) const
121  {
122    return sum_xx()/n() + m*m - 2*m*mean();
123  }
124
125  double Averager::variance(void) const
126  {
127    return sum_xx_centered()/n_;
128  }
129
130  double Averager::variance_unbiased(void) const
131  {
132    return (n_>1) ? sum_xx_centered()/(n_-1) : 
133      std::numeric_limits<double>::quiet_NaN();
134  }
135
136  const Averager& Averager::operator=(const Averager& a)
137  {
138    if (this != &a) { // avoid self-assignment
139      n_  = a.n_;
140      mean_  = a.mean_;
141      m2_ = a.m2_;
142    }
143    return *this;
144  }
145
146  const Averager& Averager::operator+=(const Averager& a)
147  {
148    mean_ += (n()*mean() + a.n()*a.mean()) / (n() + a.n());
149    double delta = mean_ = a.mean();
150    m2_ += a.m2_ + n()*a.n()*delta*delta/(n()+a.n());
151    n_+=a.n_;
152    return *this;
153  }
154
155}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.