OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
stat_accumulator.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 
20 #ifndef OST_STAT_ACCUMULATOR_HH
21 #define OST_STAT_ACCUMULATOR_HH
22 
23 #include <ost/base.hh>
25 
26 namespace ost { namespace img { namespace alg {
27 
28 template<unsigned int MAX_MOMENT=4>
30 {
31 public:
33  mean_(0.0),
34  sum_(0.0),
35  sum2_(0.0),
36  m2_(0.0),
37  m3_(0.0),
38  m4_(0.0),
39  n_(1)
40  {}
41 
42  void operator()(Real val)
43  {
44  Real delta,delta_n,delta_n2,term;
45  if(MAX_MOMENT>0){
46  delta = val - mean_;
47  delta_n = delta / n_;
48  }
49  if(MAX_MOMENT>3){
50  delta_n2 = delta_n * delta_n;
51  }
52  if(MAX_MOMENT>1){
53  term = delta * delta_n * (n_-1);
54  }
55  if(MAX_MOMENT>3){
56  m4_ += term * delta_n2 * (n_*n_ - 3*n_ + 3) + 6 * delta_n2 * m2_ - 4 * delta_n * m3_;
57  }
58  if(MAX_MOMENT>2){
59  m3_ += term * delta_n * (n_ - 2) - 3 * delta_n * m2_;
60  }
61  if(MAX_MOMENT>1){
62  m2_ += term;
63  }
64  if(MAX_MOMENT>0){
65  mean_ += delta_n;
66  }
67  n_+=1;
68  sum_+=val;
69  sum2_+=val*val;
70  }
71 
73  {
74  StatAccumulator acc(acc2);
75  acc+=*this;
76  return acc;
77  }
78 
79  StatAccumulator& operator+=(const StatAccumulator& acc)
80  {
81  if(acc.n_==1){
82  return *this;
83  }
84  if(n_==1){
85  mean_=acc.mean_;
86  sum_=acc.sum_;
87  sum2_=acc.sum2_;
88  m2_=acc.m2_;
89  m3_=acc.m3_;
90  m4_=acc.m4_;
91  n_=acc.n_;
92  return *this;
93  }
94  Real delta,delta_n,delta_n2,na,nanb;
95  Real nb=acc.n_-1;
96  if(MAX_MOMENT>0){
97  na=n_-1;
98  delta = acc.mean_ - mean_;
99  delta_n = delta / (na+nb);
100  }
101  if(MAX_MOMENT>1){
102  nanb=na*nb;
103  }
104  if(MAX_MOMENT>2){
105  delta_n2 = delta_n * delta_n;
106  }
107  if(MAX_MOMENT>3){
108  m4_+=acc.m4_+delta*delta_n*delta_n2*nanb*(na*na-nanb+nb*nb)+6.0*delta_n2*(na*na*acc.m2_+nb*nb*m2_)+4.0*delta_n*(na*acc.m3_-nb*m3_);
109  }
110  if(MAX_MOMENT>2){
111  m3_+=acc.m3_+delta*delta_n2*nanb*(na-nb)+3.0*delta_n*(na*acc.m2_-nb*m2_);
112  }
113  if(MAX_MOMENT>1){
114  m2_ += acc.m2_+delta*delta_n*nanb;
115  }
116  if(MAX_MOMENT>0){
117  mean_ += nb*delta_n;
118  }
119  sum_+=acc.sum_;
120  sum2_+=acc.sum2_;
121  n_+=nb;
122  return *this;
123  }
124 
125  Real GetNumSamples() const
126  {
127  return n_-1.0;
128  }
129 
130  Real GetMean() const
131  {
132  if(MAX_MOMENT<1){
133  throw Error("Mean was not calculated.");
134  }
135  return mean_;
136  }
137 
138  Real GetSum() const
139  {
140  return sum_;
141  }
142 
143  Real GetVariance() const
144  {
145  if(MAX_MOMENT<2){
146  throw Error("Variance was not calculated.");
147  }
148  if(n_==1.0){
149  return 0.0;
150  }
151  return m2_/(n_-1);
152  }
153 
154  Real GetStandardDeviation() const
155  {
156  return sqrt(GetVariance());
157  }
158 
159  Real GetRootMeanSquare() const
160  {
161  if(n_==1.0){
162  return 0.0;
163  }
164  return sqrt(sum2_/(n_-1));
165  }
166 
167  Real GetSkewness() const
168  {
169  if(MAX_MOMENT<3){
170  throw Error("Skewness was not calculated.");
171  }
172  if(m2_<1e-20){
173  return 0.0;
174  }else{
175  return m3_/sqrt(m2_*m2_*m2_);
176  }
177  }
178 
179  Real GetKurtosis() const
180  {
181  if(MAX_MOMENT<4){
182  throw Error("Kurtosis was not calculated.");
183  }
184  if(m2_<1e-20){
185  return 0.0;
186  }else{
187  return ((n_-1)*m4_) / (m2_*m2_);
188  }
189  }
190 
191 private:
192  Real mean_;
193  Real sum_;
194  Real sum2_;
195  Real m2_;
196  Real m3_;
197  Real m4_;
198  Real n_;
199 };
200 
201 }}} //ns
202 #endif // OST_STAT_ACCUMULATOR_HH