1 | // $Id: Averager.cc 1297 2008-05-13 14:20:23Z 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, 2008 Jari Häkkinen, Peter Johansson |
---|
7 | |
---|
8 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
9 | |
---|
10 | The yat library is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU General Public License as |
---|
12 | published by the Free Software Foundation; either version 2 of the |
---|
13 | License, or (at your option) any later version. |
---|
14 | |
---|
15 | The yat library is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with this program; if not, write to the Free Software |
---|
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
23 | 02111-1307, USA. |
---|
24 | */ |
---|
25 | |
---|
26 | #include "Averager.h" |
---|
27 | |
---|
28 | #include <cassert> |
---|
29 | #include <limits> |
---|
30 | |
---|
31 | namespace theplu { |
---|
32 | namespace yat { |
---|
33 | namespace statistics { |
---|
34 | |
---|
35 | Averager::Averager(void) |
---|
36 | : n_(0), x_(0), xx_(0) |
---|
37 | { |
---|
38 | } |
---|
39 | |
---|
40 | Averager::Averager(double x, double xx, long n) |
---|
41 | : n_(n), x_(x), xx_(xx) |
---|
42 | { |
---|
43 | } |
---|
44 | |
---|
45 | Averager::Averager(const Averager& a) |
---|
46 | : n_(a.n_), x_(a.x_), xx_(a.xx_) |
---|
47 | { |
---|
48 | } |
---|
49 | |
---|
50 | void Averager::add(double d, long n) |
---|
51 | { |
---|
52 | assert(!std::isnan(d)); |
---|
53 | n_ += n; |
---|
54 | assert(n_>-1); |
---|
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 | 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 |
---|