source: branches/0.4-stable/yat/statistics/Averager.cc @ 1294

Last change on this file since 1294 was 1294, checked in by Jari Häkkinen, 15 years ago

Reverting changes that break the interface of Averager and AveragerPair? classes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.7 KB
Line 
1// $Id: Averager.cc 1294 2008-05-12 08:21:19Z jari $
2
3/*
4  Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér
6  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
7  Copyright (C) 2008 Peter Johansson
8
9  This file is part of the yat library, http://trac.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 2 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 this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "Averager.h"
28
29#include <cassert>
30#include <limits>
31
32namespace theplu {
33namespace yat {
34namespace statistics {
35
36  Averager::Averager(void)
37    : n_(0), x_(0), xx_(0) 
38  {
39  }
40
41  Averager::Averager(double x, double xx, unsigned long n)
42    : n_(n), x_(x), xx_(xx) 
43  {
44  }
45
46  Averager::Averager(const Averager& a)
47    : n_(a.n_), x_(a.x_), xx_(a.xx_)
48  {
49  }
50
51  void Averager::add(double d, unsigned long n)
52  {
53    assert(!std::isnan(d));
54    n_  += n;
55    x_  += n*d;
56    xx_ += n*d*d;
57  }
58
59  double Averager::cv(void) const
60  {
61    return std()/mean();
62  }
63
64  double Averager::mean(void) const
65  {
66    return x_/n_;
67  }
68
69  unsigned long Averager::n(void) const
70  {
71    return n_;
72  }
73
74  void Averager::rescale(double a)
75  {
76    x_  *= a;
77    xx_ *= a*a;
78  }
79
80  void Averager::reset(void)
81  {
82    n_=0;
83    x_=xx_=0.0;
84  }
85
86  double Averager::standard_error(void) const
87  {
88    return sqrt(variance()/n_);
89  }
90
91  double Averager::std(void) const
92  {
93    return sqrt(variance());
94  }
95
96  double Averager::std(double m) const
97  {
98    return sqrt(variance(m));
99  }
100
101  double Averager::sum_x(void)  const
102  {
103    return x_;
104  }
105
106  double Averager::sum_xx(void) const
107  {
108    return xx_;
109  }
110
111  double Averager::sum_xx_centered(void)  const
112  {
113    return xx_-x_*x_/n_;
114  }
115
116  double Averager::variance(double m) const
117  {
118    return (xx_ - 2*m*x_ + m*m*n()) /n_;
119  }
120
121  double Averager::variance(void) const
122  {
123    return sum_xx_centered()/n_;
124  }
125
126  double Averager::variance_unbiased(void) const
127  {
128    return (n_>1) ? sum_xx_centered()/(n_-1) : 
129      std::numeric_limits<double>::quiet_NaN();
130  }
131
132  const Averager& Averager::operator=(const Averager& a)
133  {
134    n_  = a.n_;
135    x_  = a.x_;
136    xx_ = a.xx_;
137    return *this;
138  }
139
140  const Averager& Averager::operator+=(const Averager& a)
141  {
142    n_+=a.n_;
143    x_+=a.x_;
144    xx_+=a.xx_;
145    return *this;
146  }
147
148}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.