]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Common/IncrementalMeanAndVariance.cxx
ea88f0324c06827d3240f53a8385ddb13640506d
[FrontAlgorithms.git] / lib / fpa / Common / IncrementalMeanAndVariance.cxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5 #include <fpa/Common/IncrementalMeanAndVariance.h>
6 #include <cmath>
7
8 // -------------------------------------------------------------------------
9 fpa::Common::IncrementalMeanAndVariance::
10 IncrementalMeanAndVariance( )
11 {
12   this->Clear( );
13 }
14
15 // -------------------------------------------------------------------------
16 fpa::Common::IncrementalMeanAndVariance::
17 ~IncrementalMeanAndVariance( )
18 {
19 }
20
21 // -------------------------------------------------------------------------
22 double fpa::Common::IncrementalMeanAndVariance::
23 GetMean( ) const
24 {
25   return( this->m_M );
26 }
27
28 // -------------------------------------------------------------------------
29 double fpa::Common::IncrementalMeanAndVariance::
30 GetVariance( ) const
31 {
32   return( this->m_V );
33 }
34
35 // -------------------------------------------------------------------------
36 double fpa::Common::IncrementalMeanAndVariance::
37 GetDeviation( ) const
38 {
39   return( std::sqrt( this->m_V ) );
40 }
41
42 // -------------------------------------------------------------------------
43 unsigned long fpa::Common::IncrementalMeanAndVariance::
44 GetNumberOfSamples( ) const
45 {
46   return( ( unsigned long )( this->m_N ) );
47 }
48
49 // -------------------------------------------------------------------------
50 void fpa::Common::IncrementalMeanAndVariance::
51 Clear( )
52 {
53   this->m_M = double( 0 );
54   this->m_V = double( 0 );
55   this->m_N = double( 0 );
56 }
57
58 // -------------------------------------------------------------------------
59 void fpa::Common::IncrementalMeanAndVariance::
60 AddValue( double v )
61 {
62   this->m_N += double( 1 );
63   double d = v - this->m_M;
64   if( this->m_N > double( 1 ) )
65   {
66     double o = ( this->m_N - double( 2 ) ) / ( this->m_N - double( 1 ) );
67     this->m_V = ( o * this->m_V ) + ( ( d * d ) / this->m_N );
68   }
69   else
70     this->m_V = double( 0 );
71   this->m_M += d / this->m_N;
72 }
73
74 // eof - $RCSfile$