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>
24 #include <ost/message.hh>
26 
27 namespace ost { namespace img { namespace alg {
28 
29 template<unsigned int MAX_MOMENT=4>
31 {
32 public:
34  mean_(0.0),
35  sum_(0.0),
36  sum2_(0.0),
37  m2_(0.0),
38  m3_(0.0),
39  m4_(0.0),
40  n_(1)
41  {}
42 
43  void operator()(Real val)
44  {
45  Real delta,delta_n,delta_n2,term;
46  if(MAX_MOMENT>0){
47  delta = val - mean_;
48  delta_n = delta / n_;
49  }
50  if(MAX_MOMENT>3){
51  delta_n2 = delta_n * delta_n;
52  }
53  if(MAX_MOMENT>1){
54  term = delta * delta_n * (n_-1);
55  }
56  if(MAX_MOMENT>3){
57  m4_ += term * delta_n2 * (n_*n_ - 3*n_ + 3) + 6 * delta_n2 * m2_ - 4 * delta_n * m3_;
58  }
59  if(MAX_MOMENT>2){
60  m3_ += term * delta_n * (n_ - 2) - 3 * delta_n * m2_;
61  }
62  if(MAX_MOMENT>1){
63  m2_ += term;
64  }
65  if(MAX_MOMENT>0){
66  mean_ += delta_n;
67  }
68  n_+=1;
69  sum_+=val;
70  sum2_+=val*val;
71  }
72 
74  {
75  StatAccumulator acc(acc2);
76  acc+=*this;
77  return acc;
78  }
79 
80  StatAccumulator& operator+=(const StatAccumulator& acc)
81  {
82  if(acc.n_==1){
83  return *this;
84  }
85  if(n_==1){
86  mean_=acc.mean_;
87  sum_=acc.sum_;
88  sum2_=acc.sum2_;
89  m2_=acc.m2_;
90  m3_=acc.m3_;
91  m4_=acc.m4_;
92  n_=acc.n_;
93  return *this;
94  }
95  Real delta,delta_n,delta_n2,na,nanb;
96  Real nb=acc.n_-1;
97  if(MAX_MOMENT>0){
98  na=n_-1;
99  delta = acc.mean_ - mean_;
100  delta_n = delta / (na+nb);
101  }
102  if(MAX_MOMENT>1){
103  nanb=na*nb;
104  }
105  if(MAX_MOMENT>2){
106  delta_n2 = delta_n * delta_n;
107  }
108  if(MAX_MOMENT>3){
109  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_);
110  }
111  if(MAX_MOMENT>2){
112  m3_+=acc.m3_+delta*delta_n2*nanb*(na-nb)+3.0*delta_n*(na*acc.m2_-nb*m2_);
113  }
114  if(MAX_MOMENT>1){
115  m2_ += acc.m2_+delta*delta_n*nanb;
116  }
117  if(MAX_MOMENT>0){
118  mean_ += nb*delta_n;
119  }
120  sum_+=acc.sum_;
121  sum2_+=acc.sum2_;
122  n_+=nb;
123  return *this;
124  }
125 
126  Real GetNumSamples() const
127  {
128  return n_-1.0;
129  }
130 
131  Real GetMean() const
132  {
133  if(MAX_MOMENT<1){
134  throw Error("Mean was not calculated.");
135  }
136  return mean_;
137  }
138 
139  Real GetSum() const
140  {
141  return sum_;
142  }
143 
144  Real GetVariance() const
145  {
146  if(MAX_MOMENT<2){
147  throw Error("Variance was not calculated.");
148  }
149  if(n_==1.0){
150  return 0.0;
151  }
152  return m2_/(n_-1);
153  }
154 
155  Real GetStandardDeviation() const
156  {
157  return sqrt(GetVariance());
158  }
159 
160  Real GetRootMeanSquare() const
161  {
162  if(n_==1.0){
163  return 0.0;
164  }
165  return sqrt(sum2_/(n_-1));
166  }
167 
168  Real GetSkewness() const
169  {
170  if(MAX_MOMENT<3){
171  throw Error("Skewness was not calculated.");
172  }
173  if(m2_<1e-20){
174  return 0.0;
175  }else{
176  return m3_/sqrt(m2_*m2_*m2_);
177  }
178  }
179 
180  Real GetKurtosis() const
181  {
182  if(MAX_MOMENT<4){
183  throw Error("Kurtosis was not calculated.");
184  }
185  if(m2_<1e-20){
186  return 0.0;
187  }else{
188  return ((n_-1)*m4_) / (m2_*m2_);
189  }
190  }
191 
192 private:
193  Real mean_;
194  Real sum_;
195  Real sum2_;
196  Real m2_;
197  Real m3_;
198  Real m4_;
199  Real n_;
200 };
201 
202 }}} //ns
203 #endif // OST_STAT_ACCUMULATOR_HH