]> Creatis software - FrontAlgorithms.git/blobdiff - lib/fpa/Common/IncrementalMeanAndVariance.cxx
...
[FrontAlgorithms.git] / lib / fpa / Common / IncrementalMeanAndVariance.cxx
diff --git a/lib/fpa/Common/IncrementalMeanAndVariance.cxx b/lib/fpa/Common/IncrementalMeanAndVariance.cxx
new file mode 100644 (file)
index 0000000..ea88f03
--- /dev/null
@@ -0,0 +1,74 @@
+// =========================================================================
+// @author Leonardo Florez Valencia
+// @email florez-l@javeriana.edu.co
+// =========================================================================
+#include <fpa/Common/IncrementalMeanAndVariance.h>
+#include <cmath>
+
+// -------------------------------------------------------------------------
+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$