Mercurial > hg > segmenter-vamp-plugin
comparison armadillo-2.4.4/include/armadillo_bits/running_stat_meat.hpp @ 0:8b6102e2a9b0
Armadillo Library
author | maxzanoni76 <max.zanoni@eecs.qmul.ac.uk> |
---|---|
date | Wed, 11 Apr 2012 09:27:06 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8b6102e2a9b0 |
---|---|
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 | |
14 //! \addtogroup running_stat | |
15 //! @{ | |
16 | |
17 | |
18 | |
19 template<typename eT> | |
20 inline | |
21 arma_counter<eT>::~arma_counter() | |
22 { | |
23 arma_extra_debug_sigprint_this(this); | |
24 } | |
25 | |
26 | |
27 | |
28 template<typename eT> | |
29 inline | |
30 arma_counter<eT>::arma_counter() | |
31 : d_count( eT(0)) | |
32 , i_count(uword(0)) | |
33 { | |
34 arma_extra_debug_sigprint_this(this); | |
35 } | |
36 | |
37 | |
38 | |
39 template<typename eT> | |
40 inline | |
41 const arma_counter<eT>& | |
42 arma_counter<eT>::operator++() | |
43 { | |
44 if(i_count < ARMA_MAX_UWORD) | |
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 | |
62 arma_counter<eT>::operator++(int) | |
63 { | |
64 operator++(); | |
65 } | |
66 | |
67 | |
68 | |
69 template<typename eT> | |
70 inline | |
71 void | |
72 arma_counter<eT>::reset() | |
73 { | |
74 d_count = eT(0); | |
75 i_count = uword(0); | |
76 } | |
77 | |
78 | |
79 | |
80 template<typename eT> | |
81 inline | |
82 eT | |
83 arma_counter<eT>::value() const | |
84 { | |
85 return d_count + eT(i_count); | |
86 } | |
87 | |
88 | |
89 | |
90 template<typename eT> | |
91 inline | |
92 eT | |
93 arma_counter<eT>::value_plus_1() const | |
94 { | |
95 if(i_count < ARMA_MAX_UWORD) | |
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 | |
110 arma_counter<eT>::value_minus_1() const | |
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> | |
129 running_stat<eT>::~running_stat() | |
130 { | |
131 arma_extra_debug_sigprint_this(this); | |
132 } | |
133 | |
134 | |
135 | |
136 template<typename eT> | |
137 running_stat<eT>::running_stat() | |
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 { | |
145 arma_extra_debug_sigprint_this(this); | |
146 } | |
147 | |
148 | |
149 | |
150 //! update statistics to reflect new sample | |
151 template<typename eT> | |
152 inline | |
153 void | |
154 running_stat<eT>::operator() (const typename running_stat<eT>::T sample) | |
155 { | |
156 arma_extra_debug_sigprint(); | |
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 | |
169 //! update statistics to reflect new sample (version for complex numbers) | |
170 template<typename eT> | |
171 inline | |
172 void | |
173 running_stat<eT>::operator() (const std::complex< typename running_stat<eT>::T >& sample) | |
174 { | |
175 arma_extra_debug_sigprint(); | |
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 | |
190 //! set all statistics to zero | |
191 template<typename eT> | |
192 inline | |
193 void | |
194 running_stat<eT>::reset() | |
195 { | |
196 arma_extra_debug_sigprint(); | |
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 | |
214 //! mean or average value | |
215 template<typename eT> | |
216 inline | |
217 eT | |
218 running_stat<eT>::mean() const | |
219 { | |
220 arma_extra_debug_sigprint(); | |
221 | |
222 return r_mean; | |
223 } | |
224 | |
225 | |
226 | |
227 //! variance | |
228 template<typename eT> | |
229 inline | |
230 typename running_stat<eT>::T | |
231 running_stat<eT>::var(const uword norm_type) const | |
232 { | |
233 arma_extra_debug_sigprint(); | |
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 | |
257 //! standard deviation | |
258 template<typename eT> | |
259 inline | |
260 typename running_stat<eT>::T | |
261 running_stat<eT>::stddev(const uword norm_type) const | |
262 { | |
263 arma_extra_debug_sigprint(); | |
264 | |
265 return std::sqrt( (*this).var(norm_type) ); | |
266 } | |
267 | |
268 | |
269 | |
270 //! minimum value | |
271 template<typename eT> | |
272 inline | |
273 eT | |
274 running_stat<eT>::min() const | |
275 { | |
276 arma_extra_debug_sigprint(); | |
277 | |
278 return min_val; | |
279 } | |
280 | |
281 | |
282 | |
283 //! maximum value | |
284 template<typename eT> | |
285 inline | |
286 eT | |
287 running_stat<eT>::max() const | |
288 { | |
289 arma_extra_debug_sigprint(); | |
290 | |
291 return max_val; | |
292 } | |
293 | |
294 | |
295 | |
296 //! number of samples so far | |
297 template<typename eT> | |
298 inline | |
299 typename get_pod_type<eT>::result | |
300 running_stat<eT>::count() const | |
301 { | |
302 arma_extra_debug_sigprint(); | |
303 | |
304 return counter.value(); | |
305 } | |
306 | |
307 | |
308 | |
309 //! update statistics to reflect new sample | |
310 template<typename eT> | |
311 inline | |
312 void | |
313 running_stat_aux::update_stats(running_stat<eT>& x, const eT sample) | |
314 { | |
315 arma_extra_debug_sigprint(); | |
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 | |
361 //! update statistics to reflect new sample (version for complex numbers) | |
362 template<typename T> | |
363 inline | |
364 void | |
365 running_stat_aux::update_stats(running_stat< std::complex<T> >& x, const T sample) | |
366 { | |
367 arma_extra_debug_sigprint(); | |
368 | |
369 running_stat_aux::update_stats(x, std::complex<T>(sample)); | |
370 } | |
371 | |
372 | |
373 | |
374 //! alter statistics to reflect new sample (version for complex numbers) | |
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 { | |
380 arma_extra_debug_sigprint(); | |
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 | |
427 //! @} |