running_stat_meat.hpp
Go to the documentation of this file.
1 // Copyright (C) 2009-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2009-2011 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12 
13 
16 
17 
18 
19 template<typename eT>
20 inline
22  {
24  }
25 
26 
27 
28 template<typename eT>
29 inline
31  : d_count( eT(0))
32  , i_count(uword(0))
33  {
35  }
36 
37 
38 
39 template<typename eT>
40 inline
41 const arma_counter<eT>&
43  {
45  {
46  i_count++;
47  }
48  else
49  {
50  d_count += eT(ARMA_MAX_UWORD);
51  i_count = 0;
52  }
53 
54  return *this;
55  }
56 
57 
58 
59 template<typename eT>
60 inline
61 void
63  {
64  operator++();
65  }
66 
67 
68 
69 template<typename eT>
70 inline
71 void
73  {
74  d_count = eT(0);
75  i_count = uword(0);
76  }
77 
78 
79 
80 template<typename eT>
81 inline
82 eT
84  {
85  return d_count + eT(i_count);
86  }
87 
88 
89 
90 template<typename eT>
91 inline
92 eT
94  {
96  {
97  return d_count + eT(i_count + 1);
98  }
99  else
100  {
101  return d_count + eT(ARMA_MAX_UWORD) + eT(1);
102  }
103  }
104 
105 
106 
107 template<typename eT>
108 inline
109 eT
111  {
112  if(i_count > 0)
113  {
114  return d_count + eT(i_count - 1);
115  }
116  else
117  {
118  return d_count - eT(1);
119  }
120  }
121 
122 
123 
124 //
125 
126 
127 
128 template<typename eT>
130  {
132  }
133 
134 
135 
136 template<typename eT>
138  : r_mean ( eT(0))
139  , r_var (typename running_stat<eT>::T(0))
140  , min_val ( eT(0))
141  , max_val ( eT(0))
142  , min_val_norm(typename running_stat<eT>::T(0))
143  , max_val_norm(typename running_stat<eT>::T(0))
144  {
146  }
147 
148 
149 
151 template<typename eT>
152 inline
153 void
155  {
157 
158  if( arma_isfinite(sample) == false )
159  {
160  arma_warn(true, "running_stat: sample ignored as it is non-finite" );
161  return;
162  }
163 
164  running_stat_aux::update_stats(*this, sample);
165  }
166 
167 
168 
170 template<typename eT>
171 inline
172 void
173 running_stat<eT>::operator() (const std::complex< typename running_stat<eT>::T >& sample)
174  {
176 
177  arma_type_check(( is_same_type<eT, std::complex< typename running_stat<eT>::T > >::value == false ));
178 
179  if( arma_isfinite(sample) == false )
180  {
181  arma_warn(true, "running_stat: sample ignored as it is non-finite" );
182  return;
183  }
184 
185  running_stat_aux::update_stats(*this, sample);
186  }
187 
188 
189 
191 template<typename eT>
192 inline
193 void
195  {
197 
198  typedef typename running_stat<eT>::T T;
199 
200  counter.reset();
201 
202  r_mean = eT(0);
203  r_var = T(0);
204 
205  min_val = eT(0);
206  max_val = eT(0);
207 
208  min_val_norm = T(0);
209  max_val_norm = T(0);
210  }
211 
212 
213 
215 template<typename eT>
216 inline
217 eT
219  {
221 
222  return r_mean;
223  }
224 
225 
226 
228 template<typename eT>
229 inline
230 typename running_stat<eT>::T
231 running_stat<eT>::var(const uword norm_type) const
232  {
234 
235  const T N = counter.value();
236 
237  if(N > T(1))
238  {
239  if(norm_type == 0)
240  {
241  return r_var;
242  }
243  else
244  {
245  const T N_minus_1 = counter.value_minus_1();
246  return (N_minus_1/N) * r_var;
247  }
248  }
249  else
250  {
251  return T(0);
252  }
253  }
254 
255 
256 
258 template<typename eT>
259 inline
260 typename running_stat<eT>::T
261 running_stat<eT>::stddev(const uword norm_type) const
262  {
264 
265  return std::sqrt( (*this).var(norm_type) );
266  }
267 
268 
269 
271 template<typename eT>
272 inline
273 eT
275  {
277 
278  return min_val;
279  }
280 
281 
282 
284 template<typename eT>
285 inline
286 eT
288  {
290 
291  return max_val;
292  }
293 
294 
295 
297 template<typename eT>
298 inline
301  {
303 
304  return counter.value();
305  }
306 
307 
308 
310 template<typename eT>
311 inline
312 void
314  {
316 
317  typedef typename running_stat<eT>::T T;
318 
319  const T N = x.counter.value();
320 
321  if(N > T(0))
322  {
323  if(sample < x.min_val)
324  {
325  x.min_val = sample;
326  }
327 
328  if(sample > x.max_val)
329  {
330  x.max_val = sample;
331  }
332 
333  const T N_plus_1 = x.counter.value_plus_1();
334  const T N_minus_1 = x.counter.value_minus_1();
335 
336  // note: variance has to be updated before the mean
337 
338  const eT tmp = sample - x.r_mean;
339 
340  x.r_var = N_minus_1/N * x.r_var + (tmp*tmp)/N_plus_1;
341 
342  x.r_mean = x.r_mean + (sample - x.r_mean)/N_plus_1;
343  //x.r_mean = (N/N_plus_1)*x.r_mean + sample/N_plus_1;
344  //x.r_mean = (x.r_mean + sample/N) * N/N_plus_1;
345  }
346  else
347  {
348  x.r_mean = sample;
349  x.min_val = sample;
350  x.max_val = sample;
351 
352  // r_var is initialised to zero
353  // in the constructor and reset()
354  }
355 
356  x.counter++;
357  }
358 
359 
360 
362 template<typename T>
363 inline
364 void
365 running_stat_aux::update_stats(running_stat< std::complex<T> >& x, const T sample)
366  {
368 
369  running_stat_aux::update_stats(x, std::complex<T>(sample));
370  }
371 
372 
373 
375 template<typename T>
376 inline
377 void
378 running_stat_aux::update_stats(running_stat< std::complex<T> >& x, const std::complex<T>& sample)
379  {
381 
382  typedef typename std::complex<T> eT;
383 
384  const T sample_norm = std::norm(sample);
385  const T N = x.counter.value();
386 
387  if(N > T(0))
388  {
389  if(sample_norm < x.min_val_norm)
390  {
391  x.min_val_norm = sample_norm;
392  x.min_val = sample;
393  }
394 
395  if(sample_norm > x.max_val_norm)
396  {
397  x.max_val_norm = sample_norm;
398  x.max_val = sample;
399  }
400 
401  const T N_plus_1 = x.counter.value_plus_1();
402  const T N_minus_1 = x.counter.value_minus_1();
403 
404  x.r_var = N_minus_1/N * x.r_var + std::norm(sample - x.r_mean)/N_plus_1;
405 
406  x.r_mean = x.r_mean + (sample - x.r_mean)/N_plus_1;
407  //x.r_mean = (N/N_plus_1)*x.r_mean + sample/N_plus_1;
408  //x.r_mean = (x.r_mean + sample/N) * N/N_plus_1;
409  }
410  else
411  {
412  x.r_mean = sample;
413  x.min_val = sample;
414  x.max_val = sample;
415  x.min_val_norm = sample_norm;
416  x.max_val_norm = sample_norm;
417 
418  // r_var is initialised to zero
419  // in the constructor and reset()
420  }
421 
422  x.counter++;
423  }
424 
425 
426 
eT value() const
void reset()
set all statistics to zero
T stddev(const uword norm_type=0) const
standard deviation
eT value_minus_1() const
arma_inline const eOp< T1, eop_sqrt > sqrt(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_elem.hpp:403
const arma_counter & operator++()
eT mean() const
mean or average value
arma_aligned eT max_val
arma_warn_unused T1::pod_type norm(const Base< typename T1::elem_type, T1 > &X, const uword k, const typename arma_float_or_cx_only< typename T1::elem_type >::result *junk=0)
Definition: fn_norm.hpp:379
arma_aligned eT d_count
u32 uword
Definition: typedef.hpp:85
T var(const uword norm_type=0) const
variance
#define arma_type_check(condition)
arma_aligned T max_val_norm
eT max() const
maximum value
#define arma_extra_debug_sigprint_this
Definition: debug.hpp:1117
T count() const
number of samples so far
get_pod_type< eT >::result T
arma_aligned eT min_val
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
void arma_cold arma_warn(const bool state, const T1 &x)
print a message to the warn stream
Definition: debug.hpp:298
eT value_plus_1() const
arma_aligned arma_counter< T > counter
#define ARMA_MAX_UWORD
Definition: typedef.hpp:91
arma_aligned T r_var
arma_aligned T min_val_norm
static void update_stats(running_stat< eT > &x, const eT sample)
update statistics to reflect new sample
void operator()(const T sample)
arma_inline bool arma_isfinite(eT val)
Definition: cmath_wrap.hpp:29
eT min() const
minimum value
arma_aligned uword i_count
arma_aligned eT r_mean


armadillo_matrix
Author(s):
autogenerated on Fri Apr 16 2021 02:31:58