X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2Ffpa%2FCommon%2FIncrementalMeanAndVariance.cxx;fp=lib%2Ffpa%2FCommon%2FIncrementalMeanAndVariance.cxx;h=ea88f0324c06827d3240f53a8385ddb13640506d;hb=2047276c8f1a02432fbcc7014722d460d6c1e60f;hp=0000000000000000000000000000000000000000;hpb=3c639e5da479c7216a0a302ffa156ac6762caeed;p=FrontAlgorithms.git diff --git a/lib/fpa/Common/IncrementalMeanAndVariance.cxx b/lib/fpa/Common/IncrementalMeanAndVariance.cxx new file mode 100644 index 0000000..ea88f03 --- /dev/null +++ b/lib/fpa/Common/IncrementalMeanAndVariance.cxx @@ -0,0 +1,74 @@ +// ========================================================================= +// @author Leonardo Florez Valencia +// @email florez-l@javeriana.edu.co +// ========================================================================= +#include +#include + +// ------------------------------------------------------------------------- +fpa::Common::IncrementalMeanAndVariance:: +IncrementalMeanAndVariance( ) +{ + this->Clear( ); +} + +// ------------------------------------------------------------------------- +fpa::Common::IncrementalMeanAndVariance:: +~IncrementalMeanAndVariance( ) +{ +} + +// ------------------------------------------------------------------------- +double fpa::Common::IncrementalMeanAndVariance:: +GetMean( ) const +{ + return( this->m_M ); +} + +// ------------------------------------------------------------------------- +double fpa::Common::IncrementalMeanAndVariance:: +GetVariance( ) const +{ + return( this->m_V ); +} + +// ------------------------------------------------------------------------- +double fpa::Common::IncrementalMeanAndVariance:: +GetDeviation( ) const +{ + return( std::sqrt( this->m_V ) ); +} + +// ------------------------------------------------------------------------- +unsigned long fpa::Common::IncrementalMeanAndVariance:: +GetNumberOfSamples( ) const +{ + return( ( unsigned long )( this->m_N ) ); +} + +// ------------------------------------------------------------------------- +void fpa::Common::IncrementalMeanAndVariance:: +Clear( ) +{ + this->m_M = double( 0 ); + this->m_V = double( 0 ); + this->m_N = double( 0 ); +} + +// ------------------------------------------------------------------------- +void fpa::Common::IncrementalMeanAndVariance:: +AddValue( double v ) +{ + this->m_N += double( 1 ); + double d = v - this->m_M; + if( this->m_N > double( 1 ) ) + { + double o = ( this->m_N - double( 2 ) ) / ( this->m_N - double( 1 ) ); + this->m_V = ( o * this->m_V ) + ( ( d * d ) / this->m_N ); + } + else + this->m_V = double( 0 ); + this->m_M += d / this->m_N; +} + +// eof - $RCSfile$