1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
5 #ifndef __CPPLUGINS__EXTENSIONS__ALGORITHMS__ITERATIVEGAUSSIANMODELESTIMATOR__HXX__
6 #define __CPPLUGINS__EXTENSIONS__ALGORITHMS__ITERATIVEGAUSSIANMODELESTIMATOR__HXX__
10 #include <vnl/vnl_math.h>
11 #include <vnl/vnl_inverse.h>
13 // -------------------------------------------------------------------------
14 template< class S, unsigned int D >
16 cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
22 this->m_Cov.set_size( D, D );
23 this->m_Mean.set_size( D, 1 );
25 // Compute covariance matrix and mean vector
26 unsigned long N = this->m_Samples.size( );
27 this->m_Mean = this->m_S1;
29 this->m_Mean /= S( N );
32 this->m_Cov = this->m_S2;
33 this->m_Cov -= ( this->m_S1 * this->m_S1.transpose( ) ) / S( N );
34 this->m_Cov /= S( N - 1 );
37 this->m_Cov.fill( S( 0 ) );
40 TMatrix Es = ( this->m_S1 * this->m_S1.transpose( ) );
41 for( unsigned int i = 0; i < D; ++i )
43 this->m_Mean[ i ][ 0 ] = this->m_S1[ i ][ 0 ];
45 this->m_Mean[ i ][ 0 ] /= S( this->m_N );
47 for( unsigned int j = 0; j < D; ++j )
49 this->m_Cov[ i ][ j ] = S( 0 );
51 this->m_Cov[ i ][ j ] -= Es[ i ][ j ] / S( this->m_N );
54 this->m_Cov[ i ][ j ] += this->m_S2[ i ][ j ];
55 this->m_Cov[ i ][ j ] /= S( this->m_N - 1 );
64 // Compute inverse and determinant
65 S det = vnl_determinant( this->m_Cov );
66 if( !( det > S( 0 ) ) )
68 this->m_InvCov.set_size( D, D );
69 this->m_InvCov.fill( S( 0 ) );
70 this->m_DensityCoeff = S( 0 );
74 this->m_InvCov = vnl_inverse( this->m_Cov );
75 double _2piD = std::pow( double( 2 ) * double( vnl_math::pi ), D );
76 this->m_DensityCoeff = S( 1 ) / S( std::sqrt( _2piD * double( det ) ) );
80 // Compute minimum and maximum probabilities from input samples
82 for( unsigned long i = 0; i < this->m_Samples.size( ); ++i )
84 for( unsigned int d = 0; d < D; ++d )
85 sample[ d ] = this->m_Samples[ i ][ d ][ 0 ];
86 S p = this->Probability( sample );
89 this->m_MinimumProbability = p;
90 this->m_MaximumProbability = p;
94 if( p < this->m_MinimumProbability ) this->m_MinimumProbability = p;
95 if( this->m_MaximumProbability < p ) this->m_MaximumProbability = p;
100 this->m_Updated = true;
103 std::cout << "--------------------" << std::endl;
104 std::cout << this->m_Cov << std::endl;
105 std::cout << "--------------------" << std::endl;
106 std::cout << this->m_InvCov << std::endl;
107 std::cout << "--------------------" << std::endl;
108 std::cout << this->m_Mean << std::endl;
112 // -------------------------------------------------------------------------
113 template< class S, unsigned int D >
115 S cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
116 Probability( const V& sample ) const
119 for( unsigned int d = 0; d < D; ++d )
120 c[ d ][ 0 ] = S( sample[ d ] ) - this->m_Mean[ d ][ 0 ];
121 double v = double( ( c.transpose( ) * ( this->m_InvCov * c ) )[ 0 ][ 0 ] );
124 return( S( std::exp( -v ) ) );
127 // -------------------------------------------------------------------------
128 template< class S, unsigned int D >
129 S cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
130 Probability( const S& s_x, const S& s_y, ... ) const
132 static S sample[ D ];
134 std::va_list args_lst;
135 va_start( args_lst, s_y );
138 for( unsigned int d = 2; d < D; ++d )
139 sample[ d ] = S( va_arg( args_lst, double ) );
141 return( this->Probability( sample ) );
144 // -------------------------------------------------------------------------
145 template< class S, unsigned int D >
146 template< class V, class M >
148 cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
149 GetModel( V& m, M& E ) const
151 for( unsigned int i = 0; i < D; ++i )
153 m[ i ] = double( this->m_Mean[ i ][ 0 ] );
154 for( unsigned int j = 0; j < D; ++j )
155 E[ i ][ j ] = double( this->m_Cov[ i ][ j ] );
160 // -------------------------------------------------------------------------
161 template< class S, unsigned int D >
163 cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
166 this->m_Updated = false;
167 this->m_Samples.clear( );
168 this->m_S1.set_size( D, 1 );
169 this->m_S2.set_size( D, D );
170 this->m_S1.fill( S( 0 ) );
171 this->m_S2.fill( S( 0 ) );
175 // -------------------------------------------------------------------------
176 template< class S, unsigned int D >
179 cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
180 AddSample( const V& sample )
183 for( unsigned int d = 0; d < D; ++d )
184 s[ d ][ 0 ] = S( sample[ d ] );
186 this->m_Samples.push_back( s );
188 this->m_S2 += s * s.transpose( );
192 << sample[ 0 ] << " " << sample[ 1 ] << " " << sample[ 2 ] << std::endl;
197 this->m_S1[ i ][ 0 ] += S( sample[ i ] );
198 for( unsigned int j = i; j < D; ++j )
200 this->m_S2[ i ][ j ] += S( sample[ i ] ) * S( sample[ j ] );
201 this->m_S2[ j ][ i ] = this->m_S2[ i ][ j ];
207 this->m_Updated = false;
211 // -------------------------------------------------------------------------
212 template< class S, unsigned int D >
214 cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
215 AddSample( const S& s_x, const S& s_y, ... )
217 static S sample[ D ];
219 std::va_list args_lst;
220 va_start( args_lst, s_y );
223 for( unsigned int d = 2; d < D; ++d )
224 sample[ d ] = S( va_arg( args_lst, double ) );
226 this->AddSample( sample );
229 // -------------------------------------------------------------------------
230 template< class S, unsigned int D >
231 cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
232 IterativeGaussianModelEstimator( )
238 // -------------------------------------------------------------------------
239 template< class S, unsigned int D >
240 cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
241 ~IterativeGaussianModelEstimator( )
245 #endif // __CPPLUGINS__EXTENSIONS__ALGORITHMS__ITERATIVEGAUSSIANMODELESTIMATOR__HXX__