]> Creatis software - cpPlugins.git/blob - appli/examples/extensions/example_IterativeGaussianModelEstimator.cxx
...
[cpPlugins.git] / appli / examples / extensions / example_IterativeGaussianModelEstimator.cxx
1 #include <cmath>
2 #include <iostream>
3 #include <random>
4 #include <vector>
5
6 #include <cpExtensions/Algorithms/IterativeGaussianModelEstimator.h>
7
8 // -------------------------------------------------------------------------
9 const unsigned int Dim = 1;
10 typedef double TScalar;
11 typedef
12 cpExtensions::Algorithms::
13 IterativeGaussianModelEstimator< TScalar, Dim > TEstimator;
14
15 // -------------------------------------------------------------------------
16 int main( int argc, char* argv[] )
17 {
18   if( argc < 4 )
19   {
20     std::cerr << "Usage: " << argv[ 0 ] << " mean std samples" << std::endl;
21     return( 1 );
22
23   } // fi
24   TScalar mean = std::atof( argv[ 1 ] );
25   TScalar var = std::atof( argv[ 2 ] );
26   unsigned int samples = std::atoi( argv[ 3 ] );
27   var *= var;
28
29   // Prepare estimator
30   TEstimator::Pointer estimator = TEstimator::New( );
31  
32   // Generate numbers
33   std::random_device r;
34   std::seed_seq seed{ r( ), r( ), r( ), r( ), r( ), r( ), r( ), r( ) };
35   std::mt19937 e( seed );
36   std::normal_distribution< > dist( mean, std::sqrt( var ) );
37   double local_mean = double( 0 );
38   std::vector< double > data;
39   for( unsigned int s = 0; s < samples; ++s )
40   {
41     double v = dist( e );
42     estimator->AddSample( v );
43     local_mean += v;
44     data.push_back( v );
45
46   } // rof
47   local_mean /= double( samples );
48
49   double local_var = double( 0 );
50   for( auto d = data.begin( ); d != data.end( ); ++d )
51     local_var += ( *d - local_mean ) * ( *d - local_mean );
52   local_var /= double( samples - 1 );
53   
54   // Show results
55   std::cout
56     << "Mean: "
57     << mean << " <-> "
58     << estimator->GetMean( )[ 0 ] << " <-> "
59     << local_mean
60     << std::endl;
61   std::cout
62     << "Var: "
63     << var << " <-> "
64     << estimator->GetCovariance( )[ 0 ][ 0 ] << " <-> "
65     << estimator->GetUnbiasedCovariance( )[ 0 ][ 0 ] << " <-> "
66     << local_var
67     << std::endl;
68
69   std::cout << "--------------------------------------" << std::endl;
70   for( unsigned int s = 0; s < 15; ++s )
71   {
72     double v = dist( e );
73     double d = std::sqrt( estimator->SquaredMahalanobis( v ) );
74     std::cout << "Distante to " << v << " is " << d << std::endl;
75
76   } // rof
77
78   return( 0 );
79 }
80
81 // eof - $RCSfile$