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

Last change on this file since 1271 was 1271, checked in by Peter, 13 years ago

replaced u_int with unsigned int or size_t

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