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

Last change on this file since 865 was 865, checked in by Peter, 14 years ago

changing URL to http://trac.thep.lu.se/trac/yat

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.5 KB
Line 
1// $Id: Averager.cc 865 2007-09-10 19:41:04Z 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/trac/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
27namespace theplu {
28namespace yat {
29namespace statistics {
30
31  Averager::Averager(void)
32    : n_(0), x_(0), xx_(0) 
33  {
34  }
35
36  Averager::Averager(double x, double xx, u_long n)
37    : n_(n), x_(x), xx_(xx) 
38  {
39  }
40
41  Averager::Averager(const Averager& a)
42    : n_(a.n_), x_(a.x_), xx_(a.xx_)
43  {
44  }
45
46  void Averager::add(double d, u_long n)
47  {
48    n_  += n;
49    x_  += n*d;
50    xx_ += n*d*d;
51  }
52
53  double Averager::cv(void) const
54  {
55    return x_ ? std()/mean() : 0;
56  }
57
58  double Averager::mean(void) const
59  {
60    return n_ ? x_/n_ : 0;
61  }
62
63  u_long Averager::n(void) const
64  {
65    return n_;
66  }
67
68  void Averager::rescale(double a)
69  {
70    x_  *= a;
71    xx_ *= a*a;
72  }
73
74  void Averager::reset(void)
75  {
76    n_=0;
77    x_=xx_=0.0;
78  }
79
80  double Averager::standard_error(void) const
81  {
82    return sqrt(variance()/n_);
83  }
84
85  double Averager::std(void) const
86  {
87    return sqrt(variance());
88  }
89
90  double Averager::std(double m) const
91  {
92    return sqrt(variance(m));
93  }
94
95  double Averager::sum_x(void)  const
96  {
97    return x_;
98  }
99
100  double Averager::sum_xx(void) const
101  {
102    return xx_;
103  }
104
105  double Averager::sum_xx_centered(void)  const
106  {
107    return xx_-x_*x_/n_;
108  }
109
110  double Averager::variance(double m) const
111  {
112    return n_ ? (xx_ - 2*m*x_ + m*m*n()) /n_ : 0;
113  }
114
115  double Averager::variance(void) const
116  {
117    return n_>1 ? sum_xx_centered()/n_ : 0;
118  }
119
120  double Averager::variance_unbiased(void) const
121  {
122    return (n_>1) ? sum_xx_centered()/(n_-1) : 0;
123  }
124
125  const Averager& Averager::operator=(const Averager& a)
126  {
127    n_  = a.n_;
128    x_  = a.x_;
129    xx_ = a.xx_;
130    return *this;
131  }
132
133  const Averager& Averager::operator+=(const Averager& a)
134  {
135    n_+=a.n_;
136    x_+=a.x_;
137    xx_+=a.xx_;
138    return *this;
139  }
140
141}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.