pdf_test.cpp
Go to the documentation of this file.
00001 // Copyright (C) 2007 Klaas Gadeyne <first dot last at gmail dot com>
00002 // Copyright (C) 2007 Tinne De Laet <first dot last at mech dot kuleuven dot be>
00003 //
00004 // This program is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation; either version 2 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 //
00018 
00019 #include "pdf_test.hpp"
00020 #include "approxEqual.hpp"
00021 
00022 // Registers the fixture into the 'registry'
00023 CPPUNIT_TEST_SUITE_REGISTRATION( PdfTest );
00024 using namespace BFL;
00025 
00026 #define DIMENSION_INPUT 2
00027 #define MU_0 2.8
00028 #define MU_1 3.4
00029 #define MU_2 3.4
00030 #define MU_3 3.4
00031 #define SIGMA 0.01
00032 #define SIGMA2 0.00
00033 #define WIDTH_0 1
00034 #define WIDTH_1 2
00035 const unsigned int NUM_COND_ARGS = 2;
00036 const unsigned int DIMENSION = 2;
00037 const unsigned int NUM_SAMPLES = 500;
00038 
00039 //discrete pdf
00040 #define NUM_DS 5
00041 //discrete conditional pdf
00042 #define NUM_COND_ARGS_DIS 1
00043 
00044 void
00045 PdfTest::setUp()
00046 {
00047   epsilon = 0.0001;
00048 
00049   _mu.resize(DIMENSION);
00050   _mu(1) = MU_0; _mu(2) = MU_1;
00051   _sigma.resize(DIMENSION);
00052   _sigma = 0.0;
00053   for (unsigned int rows=1; rows < DIMENSION + 1; rows++){ _sigma(rows,rows)=SIGMA; }
00054   _width.resize(DIMENSION);
00055   _width(1) = WIDTH_0 ;
00056   _width(2) = WIDTH_1 ;
00057 }
00058 
00059 void
00060 PdfTest::tearDown()
00061 {
00062 }
00063 
00064 void
00065 PdfTest::testGaussian()
00066 {
00067   Gaussian first_gaussian(DIMENSION);
00068   first_gaussian.ExpectedValueSet(_mu);
00069   first_gaussian.CovarianceSet(_sigma);
00070   Sample<ColumnVector> test_sample ;
00071   CPPUNIT_ASSERT_EQUAL( true, first_gaussian.SampleFrom(test_sample,DEFAULT,NULL));
00072 
00073   Gaussian a_gaussian(_mu,_sigma);
00074 
00075   /* Sampling */
00076   // one sample
00077   Sample<ColumnVector> a_sample ;
00078     CPPUNIT_ASSERT_EQUAL( true, a_gaussian.SampleFrom(a_sample,DEFAULT,NULL));
00079   // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
00080   // erf(4/sqrt(2)) for each sample
00081   for(int j = 1 ; j <= DIMENSION; j++)
00082   {
00083     CPPUNIT_ASSERT( (a_sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
00084     CPPUNIT_ASSERT( (a_sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
00085   }
00086   // Box-Muller is not implemented yet
00087   CPPUNIT_ASSERT_EQUAL( false, a_gaussian.SampleFrom(a_sample,BOXMULLER,NULL));
00088   CPPUNIT_ASSERT_EQUAL( true, a_gaussian.SampleFrom(a_sample,CHOLESKY,NULL));
00089   // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
00090   // erf(4/sqrt(2)) for each sample
00091   for(int j = 1 ; j <= DIMENSION; j++)
00092   {
00093     CPPUNIT_ASSERT( (a_sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
00094     CPPUNIT_ASSERT( (a_sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
00095   }
00096 
00097   // list of samples
00098   vector<Sample<ColumnVector> > los(NUM_SAMPLES);
00099   CPPUNIT_ASSERT_EQUAL( true, a_gaussian.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
00100   // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
00101   // erf(4/sqrt(2)) for each sample
00102   ColumnVector sample_mean(DIMENSION);
00103   sample_mean = 0.0;
00104   for(int i = 0 ; i < NUM_SAMPLES; i++)
00105   {
00106     a_sample = los[i];
00107     sample_mean += a_sample.ValueGet();
00108     for(int j = 1 ; j <= DIMENSION; j++)
00109     {
00110         CPPUNIT_ASSERT( (a_sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
00111         CPPUNIT_ASSERT( (a_sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
00112     }
00113   }
00114   // check whether mean and of samples is in withing the expected 3 sigma border
00115   // 3 sigma test : REMARK: this test WILL occasionaly fail with probability
00116   // erf(3/sqrt(2)) for each sample
00117   sample_mean = sample_mean / (double)NUM_SAMPLES;
00118   for(int j = 1 ; j <= DIMENSION; j++)
00119   {
00120     CPPUNIT_ASSERT( sample_mean(j) <= _mu(j) + 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
00121     CPPUNIT_ASSERT( sample_mean(j) >= _mu(j) - 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
00122   }
00123   // Box-Muller is not implemented yet
00124   CPPUNIT_ASSERT_EQUAL( false, a_gaussian.SampleFrom(los,NUM_SAMPLES,BOXMULLER,NULL));
00125   CPPUNIT_ASSERT_EQUAL( true, a_gaussian.SampleFrom(los,NUM_SAMPLES,CHOLESKY,NULL));
00126   // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
00127   // erf(4/sqrt(2)) for each sample
00128   sample_mean = 0.0;
00129   for(int i = 0 ; i < NUM_SAMPLES; i++)
00130   {
00131     a_sample = los[i];
00132     sample_mean += a_sample.ValueGet();
00133     for(int j = 1 ; j <= DIMENSION; j++)
00134     {
00135         CPPUNIT_ASSERT( (a_sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
00136         CPPUNIT_ASSERT( (a_sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
00137     }
00138   }
00139   // check whether mean and of samples is in withing the expected 3 sigma border
00140   // 3 sigma test : REMARK: this test WILL occasionaly fail with probability
00141   // erf(3/sqrt(2)) for each sample
00142   sample_mean = sample_mean / (double)NUM_SAMPLES;
00143   for(int j = 1 ; j <= DIMENSION; j++)
00144   {
00145     CPPUNIT_ASSERT( sample_mean(j) <= _mu(j) + 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
00146     CPPUNIT_ASSERT( sample_mean(j) >= _mu(j) - 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
00147   }
00148 
00149   /* Setting and Getting mean/covariance */
00150   CPPUNIT_ASSERT_EQUAL( _mu, a_gaussian.ExpectedValueGet());
00151   CPPUNIT_ASSERT_EQUAL( _sigma, a_gaussian.CovarianceGet());
00152 
00153   /* Copy Constructor etc */
00154   Gaussian b_gaussian(a_gaussian);
00155   CPPUNIT_ASSERT_EQUAL( _mu, b_gaussian.ExpectedValueGet());
00156   CPPUNIT_ASSERT_EQUAL( _sigma, b_gaussian.CovarianceGet());
00157 
00158   /* Create Gaussian, allocate memory afterwards */
00159   Gaussian c_gaussian;
00160   c_gaussian.ExpectedValueSet(_mu);
00161   CPPUNIT_ASSERT_EQUAL( _mu, c_gaussian.ExpectedValueGet());
00162   c_gaussian.CovarianceSet(_sigma);
00163   CPPUNIT_ASSERT_EQUAL( _sigma, c_gaussian.CovarianceGet());
00164   
00165   /* Clone */
00166   Gaussian* clone = NULL;
00167   clone = c_gaussian.Clone();
00168   CPPUNIT_ASSERT_EQUAL( c_gaussian.ExpectedValueGet(), clone->ExpectedValueGet());
00169   CPPUNIT_ASSERT_EQUAL( c_gaussian.CovarianceGet(), clone->CovarianceGet());
00170 }
00171 
00172 void
00173 PdfTest::testUniform()
00174 {
00175   Uniform a_uniform(_mu,_width);
00176 
00177   /* Setting and Getting center and widths */
00178   CPPUNIT_ASSERT_EQUAL(approxEqual( _mu, a_uniform.CenterGet(),epsilon),true);
00179   CPPUNIT_ASSERT_EQUAL(approxEqual( _width, a_uniform.WidthGet(),epsilon),true);
00180 
00181   /* Sampling */
00182   vector<Sample<ColumnVector> > los(NUM_SAMPLES);
00183   CPPUNIT_ASSERT_EQUAL( true, a_uniform.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
00184   // test if samples are located in the area
00185   vector<Sample<ColumnVector > >::iterator los_it;
00186   for (los_it = los.begin(); los_it!=los.end(); los_it++)
00187   {
00188        ColumnVector current_sample = los_it->ValueGet();
00189        for (int i = 1; i < _mu.rows()+1 ; i++)
00190        {
00191             CPPUNIT_ASSERT(current_sample(i) > (_mu(i)-_width(i)/2) ) ;
00192             CPPUNIT_ASSERT(current_sample(i) < (_mu(i)+_width(i)/2) ) ;
00193        }
00194    }
00195   // Box-Muller is not implemented yet
00196   CPPUNIT_ASSERT_EQUAL( false, a_uniform.SampleFrom(los,NUM_SAMPLES,BOXMULLER,NULL));
00197   Sample<ColumnVector> a_sample ;
00198   CPPUNIT_ASSERT_EQUAL( true, a_uniform.SampleFrom(a_sample,DEFAULT,NULL));
00199   // Box-Muller is not implemented yet
00200   CPPUNIT_ASSERT_EQUAL( false, a_uniform.SampleFrom(a_sample,BOXMULLER,NULL));
00201 
00202   /* Getting the probability */
00203   double area = 1;
00204   for (int i =1 ; i < DIMENSION + 1 ; i++) area = area * _width(i);
00205   CPPUNIT_ASSERT_EQUAL(1/area, (double)a_uniform.ProbabilityGet(_mu));
00206   CPPUNIT_ASSERT_EQUAL(0.0, (double)a_uniform.ProbabilityGet(_mu + _width));
00207   CPPUNIT_ASSERT_EQUAL(0.0, (double)a_uniform.ProbabilityGet(_mu - _width));
00208   ColumnVector test_prob(DIMENSION);
00209   test_prob = _mu;
00210   test_prob(DIMENSION) = _mu(DIMENSION) + _width(DIMENSION)*2/3;
00211   CPPUNIT_ASSERT_EQUAL(0.0, (double)a_uniform.ProbabilityGet(test_prob));
00212 
00213   /* Copy Constructor etc */
00214   Uniform b_uniform(a_uniform);
00215   CPPUNIT_ASSERT_EQUAL(approxEqual( _mu, b_uniform.CenterGet(),epsilon),true);
00216   CPPUNIT_ASSERT_EQUAL(approxEqual( _width, b_uniform.WidthGet(),epsilon),true);
00217 
00218   /* Create Uniform, allocate memory afterwards */
00219   Uniform c_uniform;
00220   c_uniform.UniformSet(_mu,_width);
00221   CPPUNIT_ASSERT_EQUAL(approxEqual( _mu, c_uniform.CenterGet(),epsilon),true);
00222   CPPUNIT_ASSERT_EQUAL(approxEqual( _width, c_uniform.WidthGet(),epsilon),true);
00223   /* Getting the probability */
00224   area = 1;
00225   for (int i =1 ; i < DIMENSION + 1 ; i++) area = area * _width(i);
00226   CPPUNIT_ASSERT_EQUAL(1/area, (double)c_uniform.ProbabilityGet(_mu));
00227   CPPUNIT_ASSERT_EQUAL(0.0, (double)c_uniform.ProbabilityGet(_mu + _width));
00228   CPPUNIT_ASSERT_EQUAL(0.0, (double)c_uniform.ProbabilityGet(_mu - _width));
00229   test_prob = _mu;
00230   test_prob(DIMENSION) = _mu(DIMENSION) + _width(DIMENSION)*2/3;
00231   CPPUNIT_ASSERT_EQUAL(0.0, (double)c_uniform.ProbabilityGet(test_prob));
00232 
00233   /* Clone */
00234   Uniform* clone = NULL;
00235   clone = c_uniform.Clone();
00236   CPPUNIT_ASSERT_EQUAL( c_uniform.CenterGet(), clone->CenterGet());
00237   CPPUNIT_ASSERT_EQUAL( c_uniform.WidthGet(), clone->WidthGet());
00238 }
00239 
00240 void
00241 PdfTest::testDiscretePdf()
00242 {
00243   DiscretePdf a_discretepdf(NUM_DS);
00244   vector<Probability> uniform_vector(NUM_DS);
00245   for (int state = 0; state < NUM_DS ; state++) uniform_vector[state] = (Probability)(1.0/NUM_DS );
00246 
00247   /* Check initial uniform distribution */
00248   vector<Probability> result_proba = a_discretepdf.ProbabilitiesGet();
00249   for (int state = 0; state < NUM_DS ; state++) CPPUNIT_ASSERT_EQUAL( (double) (uniform_vector[state]), double(result_proba[state] )) ;
00250 
00251   /* Check dimension */
00252   CPPUNIT_ASSERT_EQUAL(1,(int)a_discretepdf.DimensionGet());
00253 
00254   /* Check the number of states */
00255   CPPUNIT_ASSERT_EQUAL(NUM_DS,(int)a_discretepdf.NumStatesGet());
00256 
00257   /* Copy constructor */
00258   DiscretePdf b_discretepdf(a_discretepdf);
00259   vector<Probability> result_probb = b_discretepdf.ProbabilitiesGet();
00260   for (int state = 0; state < NUM_DS ; state++) CPPUNIT_ASSERT_EQUAL( (double) (result_proba[state]), double(result_probb[state] )) ;
00261   CPPUNIT_ASSERT_EQUAL(a_discretepdf.DimensionGet(), b_discretepdf.DimensionGet());
00262   CPPUNIT_ASSERT_EQUAL(a_discretepdf.NumStatesGet(), b_discretepdf.NumStatesGet());
00263 
00264   /* Set and Get probabilities */
00265   // set one probability
00266   double prob_new = 0.57;
00267   int new_el = NUM_DS-1;
00268   CPPUNIT_ASSERT_EQUAL(true, a_discretepdf.ProbabilitySet(new_el,prob_new));
00269   CPPUNIT_ASSERT_EQUAL(prob_new, (double) a_discretepdf.ProbabilityGet(new_el));
00270   // check if the sum of the probabilities is still one!
00271   double sumProb = 0.0;
00272   for (int state = 0; state < NUM_DS ; state++)
00273   {
00274     sumProb = sumProb + a_discretepdf.ProbabilityGet(state);
00275   }
00276   CPPUNIT_ASSERT_EQUAL(1.0, sumProb);
00277 
00278   // set all probabilities one by one
00279   for (int new_el_c = 0; new_el_c < NUM_DS; new_el_c++)
00280   {
00281      CPPUNIT_ASSERT_EQUAL(true, a_discretepdf.ProbabilitySet(new_el_c,prob_new));
00282      CPPUNIT_ASSERT_EQUAL(prob_new, (double) a_discretepdf.ProbabilityGet(new_el_c));
00283   }
00284   // check if the sum of the probabilities is still one!
00285   sumProb = 0.0;
00286   for (int state = 0; state < NUM_DS ; state++)
00287   {
00288     sumProb = sumProb + a_discretepdf.ProbabilityGet(state);
00289   }
00290   CPPUNIT_ASSERT_EQUAL(1.0, sumProb);
00291 
00292   // all at the same time
00293   vector<Probability> prob_vec(NUM_DS);
00294   prob_vec[0] = 0.9;
00295   for (int state = 1; state < NUM_DS  ; state++)
00296   {
00297      prob_vec[state] = (Probability)( (1-0.9)/(NUM_DS-1) );
00298   }
00299   a_discretepdf.ProbabilitiesSet(prob_vec);
00300   result_proba = a_discretepdf.ProbabilitiesGet();
00301   for (int state = 0; state < NUM_DS ; state++) CPPUNIT_ASSERT_EQUAL( (double) (prob_vec[state]), double(result_proba[state] )) ;
00302 
00303   // check if the sum of the probabilities is still one!
00304   sumProb = 0.0;
00305   for (int state = 0; state < NUM_DS ; state++)
00306   {
00307     sumProb = sumProb + a_discretepdf.ProbabilityGet(state);
00308   }
00309   CPPUNIT_ASSERT_EQUAL(1.0, sumProb);
00310 
00311   // special case for setting probability : set a new value for a probability
00312   // which was == 1
00313   DiscretePdf c_discretepdf(NUM_DS);
00314   vector<Probability> prob_vecc(NUM_DS);
00315   prob_vecc[0] = 1.0;
00316   for (int state = 1; state < NUM_DS  ; state++)
00317   {
00318     prob_vecc[state] = 0.0;
00319   }
00320   c_discretepdf.ProbabilitiesSet(prob_vecc);
00321   Probability new_prob0 = 0.5;
00322   double new_prob_other = (1-new_prob0)/(NUM_DS-1);
00323   c_discretepdf.ProbabilitySet(0,new_prob0);
00324   CPPUNIT_ASSERT_EQUAL((double)new_prob0, (double)(c_discretepdf.ProbabilityGet(0)));
00325   for (int state = 1; state < NUM_DS  ; state++)
00326   {
00327     CPPUNIT_ASSERT_EQUAL( new_prob_other, (double)(c_discretepdf.ProbabilityGet(state)));
00328   }
00329 
00330   /* Sampling */
00331   DiscretePdf d_discretepdf(NUM_DS);
00332   vector<Probability> prob_vecd(NUM_DS);
00333   prob_vecd[0] = 0.1;
00334   prob_vecd[1] = 0.2;
00335   prob_vecd[2] = 0.35;
00336   prob_vecd[3] = 0.05;
00337   prob_vecd[4] = 0.3;
00338   d_discretepdf.ProbabilitiesSet(prob_vecd);
00339   vector<Sample<int> > los(NUM_SAMPLES);
00340   vector<Sample<int> >::iterator it;
00341 
00342   // check most probable state
00343   CPPUNIT_ASSERT_EQUAL( 2, d_discretepdf.MostProbableStateGet());
00344 
00345   CPPUNIT_ASSERT_EQUAL( true, d_discretepdf.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
00346   // test if samples are distributed according to probabilities
00347   // remark that this test occasionally fails
00348   vector<unsigned int> num_samples(NUM_DS,0);
00349   for (it = los.begin(); it!=los.end();it++)
00350   {
00351     num_samples[it->ValueGet()] +=1;
00352   }
00353   for (int i = 0 ; i< NUM_DS ; i++)
00354   {
00355     //TODO: find some theoretical limits depending on the number of samples
00356     CPPUNIT_ASSERT( approxEqual( prob_vecd[i] , (double)num_samples[i] / NUM_SAMPLES, 0.05 ) );
00357   }
00358 
00359   // Ripley
00360   CPPUNIT_ASSERT_EQUAL( true, d_discretepdf.SampleFrom(los,NUM_SAMPLES,RIPLEY,NULL));
00361   // test if samples are distributed according to probabilities
00362   // remark that this test occasionally fails
00363   vector<unsigned int> num_samples2(NUM_DS,0);
00364   for (it = los.begin(); it!=los.end();it++)
00365   {
00366     num_samples2[it->ValueGet()] +=1;
00367   }
00368   for (int i = 0 ; i< NUM_DS ; i++)
00369   {
00370     //TODO: find some theoretical limits depending on the number of samples
00371    CPPUNIT_ASSERT( approxEqual( prob_vecd[i] , (double)num_samples2[i] / NUM_SAMPLES, 0.05 ) );
00372   }
00373 
00374   prob_vecd[0] = 0.0; 
00375   prob_vecd[1] = 0.0; 
00376   prob_vecd[2] = 1.0; 
00377   prob_vecd[3] = 0.0; 
00378   prob_vecd[4] = 0.0; 
00379   d_discretepdf.ProbabilitiesSet(prob_vecd);
00380 
00381   // check most probable state
00382   CPPUNIT_ASSERT_EQUAL( 2, d_discretepdf.MostProbableStateGet());
00383 
00384   CPPUNIT_ASSERT_EQUAL( true, d_discretepdf.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
00385   // test if samples are distributed according to probabilities
00386   // remark that this test occasionally fails
00387   vector<unsigned int> num_samples3(NUM_DS,0);
00388   for (it = los.begin(); it!=los.end();it++)
00389   {
00390     num_samples3[it->ValueGet()] +=1;
00391   }
00392   for (int i = 0 ; i< NUM_DS ; i++)
00393   {
00394     //TODO: find some theoretical limits depending on the number of samples
00395     CPPUNIT_ASSERT( approxEqual( prob_vecd[i] , (double)num_samples3[i] / NUM_SAMPLES, 0.05 ) );
00396   }
00397 
00398   // Ripley
00399   CPPUNIT_ASSERT_EQUAL( true, d_discretepdf.SampleFrom(los,NUM_SAMPLES,RIPLEY,NULL));
00400   // test if samples are distributed according to probabilities
00401   // remark that this test occasionally fails
00402   vector<unsigned int> num_samples4(NUM_DS,0);
00403   for (it = los.begin(); it!=los.end();it++)
00404   {
00405     num_samples4[it->ValueGet()] +=1;
00406   }
00407   for (int i = 0 ; i< NUM_DS ; i++)
00408   {
00409     //TODO: find some theoretical limits depending on the number of samples
00410    CPPUNIT_ASSERT( approxEqual( prob_vecd[i] , (double)num_samples4[i] / NUM_SAMPLES, 0.05 ) );
00411   }
00412 
00413   Sample<int> a_sample ;
00414   CPPUNIT_ASSERT_EQUAL( true, d_discretepdf.SampleFrom(a_sample,DEFAULT,NULL));
00415   // Ripley not implemented for one sample
00416   CPPUNIT_ASSERT_EQUAL( false, d_discretepdf.SampleFrom(a_sample,RIPLEY,NULL));
00417 
00418   /* Clone */
00419   DiscretePdf* clone = NULL;
00420   clone = d_discretepdf.Clone();
00421   CPPUNIT_ASSERT_EQUAL( d_discretepdf.NumStatesGet(), clone->NumStatesGet());
00422   vector<Probability> result_probd = d_discretepdf.ProbabilitiesGet();
00423   vector<Probability> result_probClone = clone->ProbabilitiesGet();
00424   for( int i = 0; i< d_discretepdf.NumStatesGet() ; i++)
00425   {
00426     CPPUNIT_ASSERT_EQUAL( (double)result_probd[i],(double)result_probClone[i]);
00427   }
00428 }
00429 
00430 void
00431 PdfTest::testLinearAnalyticConditionalGaussian()
00432 {
00433   //creating additive gaussian noise
00434   Gaussian noise(_mu,_sigma);
00435 
00436   // Conditional gaussian with NUM_CONDITIONAL_ARGS conditional args and state vector of
00437   // dimension DIMENSION
00438   Matrix a(DIMENSION,DIMENSION); a = 1.0;
00439   Matrix b(DIMENSION,DIMENSION); b = 1.0;
00440   vector<Matrix> v(2); v[0] = a; v[1] = b;
00441 
00442   LinearAnalyticConditionalGaussian a_condgaussian(v, noise);
00443 
00444   /* Dimension check*/
00445   CPPUNIT_ASSERT_EQUAL( NUM_COND_ARGS, a_condgaussian.NumConditionalArgumentsGet());
00446 
00447   /* Matrix Check */
00448   for (unsigned int i=0; i < NUM_COND_ARGS; i++)
00449   {
00450     CPPUNIT_ASSERT_EQUAL( v[i], a_condgaussian.MatrixGet(i));
00451   }
00452 
00453   /* Getting mean/covariance  of the additive noise*/
00454   CPPUNIT_ASSERT_EQUAL( _mu, a_condgaussian.AdditiveNoiseMuGet());
00455   CPPUNIT_ASSERT_EQUAL( _sigma, a_condgaussian.AdditiveNoiseSigmaGet());
00456 
00457   /* Setting and getting the conditional args; one at a time */
00458   ColumnVector x2(DIMENSION); x2(1) = 1.0; x2(DIMENSION) = 0.5;
00459   ColumnVector u2(DIMENSION_INPUT); u2(1) = 1.0; u2(DIMENSION_INPUT) = 0.5;
00460 
00461   a_condgaussian.ConditionalArgumentSet(0,x2);
00462   a_condgaussian.ConditionalArgumentSet(1,u2);
00463 
00464   CPPUNIT_ASSERT_EQUAL( DIMENSION, a_condgaussian.DimensionGet());
00465   CPPUNIT_ASSERT_EQUAL( x2, a_condgaussian.ConditionalArgumentGet(0));
00466   CPPUNIT_ASSERT_EQUAL( u2, a_condgaussian.ConditionalArgumentGet(1));
00467 
00468 
00469   /* Setting and getting the conditional args; all together */
00470   ColumnVector x(DIMENSION); x(1) = 1.0; x(DIMENSION) = 0.5;
00471   ColumnVector u(DIMENSION_INPUT); u(1) = 1.0; u(DIMENSION_INPUT) = 0.5;
00472 
00473   std::vector<ColumnVector> cond_args(NUM_COND_ARGS);
00474   cond_args[0] = x;
00475   cond_args[NUM_COND_ARGS-1] = u;
00476 
00477   a_condgaussian.ConditionalArgumentsSet(cond_args);
00478 
00479   CPPUNIT_ASSERT_EQUAL( DIMENSION, a_condgaussian.DimensionGet());
00480   for (unsigned int i=0; i < NUM_COND_ARGS; i++)
00481   {
00482     CPPUNIT_ASSERT_EQUAL( cond_args[i], a_condgaussian.ConditionalArgumentsGet()[i]);
00483   }
00484 
00485   /* Sampling */
00486   vector<Sample<ColumnVector> > los(NUM_SAMPLES);
00487   CPPUNIT_ASSERT_EQUAL( true, a_condgaussian.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
00488   // Box-Muller is not implemented yet
00489   CPPUNIT_ASSERT_EQUAL( false, a_condgaussian.SampleFrom(los,NUM_SAMPLES,BOXMULLER,NULL));
00490   CPPUNIT_ASSERT_EQUAL( true, a_condgaussian.SampleFrom(los,NUM_SAMPLES,CHOLESKY,NULL));
00491   Sample<ColumnVector> a_sample ;
00492   CPPUNIT_ASSERT_EQUAL( true, a_condgaussian.SampleFrom(a_sample,DEFAULT,NULL));
00493   // Box-Muller is not implemented yet
00494   CPPUNIT_ASSERT_EQUAL( false, a_condgaussian.SampleFrom(a_sample,BOXMULLER,NULL));
00495   CPPUNIT_ASSERT_EQUAL( true, a_condgaussian.SampleFrom(a_sample,CHOLESKY,NULL));
00496 
00497   /* Test dfGet */
00498   for (unsigned int i=0; i < NUM_COND_ARGS; i++)
00499   {
00500     CPPUNIT_ASSERT_EQUAL( v[i], a_condgaussian.dfGet(i));
00501   }
00502 
00503   /* Setting and Getting mean/covariance */
00504   // calculate expected value
00505   ColumnVector exp(DIMENSION); exp = 0.0;
00506   for (unsigned int i=0; i < NUM_COND_ARGS; i++)
00507   {
00508      exp = exp + v[i]*cond_args[i];
00509   }
00510   exp += _mu;
00511   CPPUNIT_ASSERT_EQUAL( exp, a_condgaussian.ExpectedValueGet());
00512   CPPUNIT_ASSERT_EQUAL( _sigma, a_condgaussian.CovarianceGet());
00513 
00514   /* Copy Constructor etc */
00515   LinearAnalyticConditionalGaussian b_condgaussian(a_condgaussian);
00516   CPPUNIT_ASSERT_EQUAL( _mu, b_condgaussian.AdditiveNoiseMuGet());
00517   CPPUNIT_ASSERT_EQUAL( _sigma, b_condgaussian.AdditiveNoiseSigmaGet());
00518 
00519   /* Setting and getting matrices */
00520   Matrix a2(DIMENSION,DIMENSION); a2 = 2.1;
00521   Matrix b2(DIMENSION,DIMENSION); b2 = 1.5;
00522 
00523   a_condgaussian.MatrixSet(0,a2);
00524   a_condgaussian.MatrixSet(1,b2);
00525 
00526   CPPUNIT_ASSERT_EQUAL( a2, a_condgaussian.MatrixGet(0));
00527   CPPUNIT_ASSERT_EQUAL( b2, a_condgaussian.MatrixGet(1));
00528 
00529   /* Setting and Getting mean/covariance  of the additive noise*/
00530   ColumnVector mu2;
00531   SymmetricMatrix sigma2;
00532   mu2.resize(DIMENSION);
00533   mu2(1) = MU_1; mu2(2) = MU_0;
00534   sigma2.resize(DIMENSION);
00535   sigma2 = 0.0;
00536   double sig2 = 0.05;
00537   for (unsigned int rows=1; rows < DIMENSION + 1; rows++){ sigma2(rows,rows)=sig2; }
00538 
00539   a_condgaussian.AdditiveNoiseMuSet(mu2);
00540   a_condgaussian.AdditiveNoiseSigmaSet(sigma2);
00541 
00542   CPPUNIT_ASSERT_EQUAL( mu2, a_condgaussian.AdditiveNoiseMuGet());
00543   CPPUNIT_ASSERT_EQUAL( sigma2, a_condgaussian.AdditiveNoiseSigmaGet());
00544 
00545   /* TODO: What would be the best way to test ProbabilityGet? */
00546 
00547   /* Clone */
00548   LinearAnalyticConditionalGaussian* clone = NULL;
00549   clone = a_condgaussian.Clone();
00550   CPPUNIT_ASSERT_EQUAL( a_condgaussian.AdditiveNoiseMuGet(), clone->AdditiveNoiseMuGet());
00551   CPPUNIT_ASSERT_EQUAL( a_condgaussian.AdditiveNoiseSigmaGet(), clone->AdditiveNoiseSigmaGet());
00552   CPPUNIT_ASSERT_EQUAL( a_condgaussian.MatrixGet(0), clone->MatrixGet(0));
00553   CPPUNIT_ASSERT_EQUAL( a_condgaussian.MatrixGet(1), clone->MatrixGet(1));
00554 }
00555 
00556 void
00557 PdfTest::testDiscreteConditionalPdf()
00558 {
00559   int cond_arg_dims[NUM_COND_ARGS_DIS] = { NUM_DS };
00560   int state_k;int state_kMinusOne;
00561 
00562   DiscreteConditionalPdf a_discretecondpdf(NUM_DS,NUM_COND_ARGS_DIS,cond_arg_dims);
00563 
00564   /* Get the dimension  */
00565   CPPUNIT_ASSERT_EQUAL( 1, (int)a_discretecondpdf.DimensionGet());
00566 
00567   /* Get the dimension  */
00568   CPPUNIT_ASSERT_EQUAL( NUM_DS, (int)a_discretecondpdf.NumStatesGet());
00569 
00570   /* Get number of conditional arguments  */
00571   CPPUNIT_ASSERT_EQUAL( NUM_COND_ARGS_DIS, (int)a_discretecondpdf.NumConditionalArgumentsGet());
00572 
00573   std::vector<int> cond_args(NUM_COND_ARGS_DIS);
00574   /* Set and Get all Probabilities*/
00575   double prob_diag = 0.9;
00576   double prob_nondiag = (1-0.9)/(NUM_DS-1);
00577   for (state_kMinusOne = 0 ; state_kMinusOne < NUM_DS ;  state_kMinusOne++)
00578     {
00579        cond_args[0] = state_kMinusOne;
00580        for (state_k = 0 ; state_k < NUM_DS ;  state_k++)
00581              {
00582            if (state_kMinusOne == state_k) a_discretecondpdf.ProbabilitySet(prob_diag,state_k,cond_args);
00583            else a_discretecondpdf.ProbabilitySet(prob_nondiag,state_k,cond_args);
00584              }
00585     }
00586 
00587   /* Set and Get Conditional Arguments */
00588   int cond_arg = NUM_DS - 1;
00589   a_discretecondpdf.ConditionalArgumentSet(0, cond_arg);
00590   CPPUNIT_ASSERT_EQUAL( cond_arg, a_discretecondpdf.ConditionalArgumentGet(0));
00591 
00592   /* Get the probability for the states given the conditional argument set */
00593   for (cond_arg = 0 ; cond_arg < NUM_DS ;  cond_arg++)
00594   {
00595     a_discretecondpdf.ConditionalArgumentSet(0, cond_arg);
00596     for (state_k = 0 ; state_k < NUM_DS ;  state_k++)
00597     {
00598       if( state_k == cond_arg) CPPUNIT_ASSERT_EQUAL( prob_diag, (double)a_discretecondpdf.ProbabilityGet(state_k));
00599       else CPPUNIT_ASSERT_EQUAL( prob_nondiag, (double)a_discretecondpdf.ProbabilityGet(state_k));
00600     }
00601   }
00602 
00603   /* Sampling */
00604   vector<Sample<int> > los(NUM_SAMPLES);
00605   CPPUNIT_ASSERT_EQUAL( true, a_discretecondpdf.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
00606   Sample<int> a_sample ;
00607   CPPUNIT_ASSERT_EQUAL( true, a_discretecondpdf.SampleFrom(a_sample,DEFAULT,NULL));
00608 
00609   /* Copy */
00610   DiscreteConditionalPdf b_discretecondpdf(a_discretecondpdf);
00611   CPPUNIT_ASSERT_EQUAL( a_discretecondpdf.DimensionGet(),b_discretecondpdf.DimensionGet() );
00612   CPPUNIT_ASSERT_EQUAL( a_discretecondpdf.NumConditionalArgumentsGet(), b_discretecondpdf.NumConditionalArgumentsGet());
00613   CPPUNIT_ASSERT_EQUAL( a_discretecondpdf.NumStatesGet(),b_discretecondpdf.NumStatesGet());
00614   for (cond_arg = 0 ; cond_arg < NUM_DS ;  cond_arg++)
00615   {
00616     a_discretecondpdf.ConditionalArgumentSet(0, cond_arg);
00617     b_discretecondpdf.ConditionalArgumentSet(0, cond_arg);
00618     for (state_k = 0 ; state_k < NUM_DS ;  state_k++)
00619     {
00620        CPPUNIT_ASSERT_EQUAL( (double)a_discretecondpdf.ProbabilityGet(state_k) , (double)b_discretecondpdf.ProbabilityGet(state_k));
00621     }
00622   }
00623   
00624   /* Clone */
00625   DiscreteConditionalPdf* clone = NULL;
00626   clone = a_discretecondpdf.Clone();
00627   CPPUNIT_ASSERT_EQUAL( a_discretecondpdf.DimensionGet(), clone->DimensionGet());
00628   CPPUNIT_ASSERT_EQUAL( a_discretecondpdf.NumConditionalArgumentsGet(), clone->NumConditionalArgumentsGet());
00629   CPPUNIT_ASSERT_EQUAL( a_discretecondpdf.NumStatesGet(),clone->NumStatesGet());
00630   for (cond_arg = 0 ; cond_arg < NUM_DS ;  cond_arg++)
00631   {
00632     a_discretecondpdf.ConditionalArgumentSet(0, cond_arg);
00633     clone->ConditionalArgumentSet(0, cond_arg);
00634     for (state_k = 0 ; state_k < NUM_DS ;  state_k++)
00635        CPPUNIT_ASSERT_EQUAL( (double)a_discretecondpdf.ProbabilityGet(state_k) , (double)clone->ProbabilityGet(state_k));
00636   }
00637 }
00638 
00639 void
00640 PdfTest::testMcpdf()
00641 {
00642   /* Set and Get Dimension and number of samples*/
00643   MCPdf<ColumnVector> a_mcpdf(NUM_SAMPLES,DIMENSION);
00644   CPPUNIT_ASSERT_EQUAL( DIMENSION, a_mcpdf.DimensionGet());
00645   CPPUNIT_ASSERT_EQUAL( NUM_SAMPLES , a_mcpdf.NumSamplesGet());
00646 
00647   // Generating (exact) Samples from a Gaussian density with
00648   // cholesky sampling
00649   Gaussian gaussian(_mu,_sigma);
00650 
00651   vector<Sample<ColumnVector> > exact_samples(NUM_SAMPLES);
00652   vector<Sample<ColumnVector> >::iterator it;
00653   gaussian.SampleFrom(exact_samples, NUM_SAMPLES,CHOLESKY,NULL);
00654 
00655   /* Getting and setting the list of samples (non-weighted)*/
00656   a_mcpdf.ListOfSamplesSet(exact_samples);
00657   const vector<WeightedSample<ColumnVector> > mcpdf_samples = a_mcpdf.ListOfSamplesGet();
00658   for (unsigned int i = 0; i < NUM_SAMPLES ; i++)
00659   {
00660      CPPUNIT_ASSERT_EQUAL( exact_samples[i].ValueGet(), mcpdf_samples[i].ValueGet());
00661      CPPUNIT_ASSERT_EQUAL( exact_samples[i].ValueGet(), a_mcpdf.SampleGet(i).ValueGet());
00662      CPPUNIT_ASSERT_EQUAL( 1.0/NUM_SAMPLES, mcpdf_samples[i].WeightGet());
00663   }
00664 
00665   /* List of samples update + getting and setting the list of samples (weighted)*/
00666   vector<WeightedSample<ColumnVector> > samples_weighted = mcpdf_samples;
00667   for (unsigned int i = 0 ; i < NUM_SAMPLES ; i++)
00668   {
00669      //set a weight
00670      samples_weighted[i].WeightSet(i+1);
00671   }
00672   double tot_weight = (double)(NUM_SAMPLES+1)*((double)NUM_SAMPLES)/2.0;
00673   CPPUNIT_ASSERT_EQUAL( true , a_mcpdf.ListOfSamplesUpdate(samples_weighted) );
00674   for (unsigned int i = 0; i < NUM_SAMPLES ; i++)
00675   {
00676      CPPUNIT_ASSERT_EQUAL( samples_weighted[i].ValueGet(), a_mcpdf.SampleGet(i).ValueGet());
00677      CPPUNIT_ASSERT_EQUAL( (double)(samples_weighted[i].WeightGet())/tot_weight, a_mcpdf.SampleGet(i).WeightGet());
00678   }
00679 
00680   /* Copy Constructor etc */
00681   MCPdf<ColumnVector> b_mcpdf(a_mcpdf);
00682   for (unsigned int i = 0; i < NUM_SAMPLES ; i++)
00683   {
00684      CPPUNIT_ASSERT_EQUAL( a_mcpdf.SampleGet(i).ValueGet(), b_mcpdf.SampleGet(i).ValueGet());
00685      CPPUNIT_ASSERT_EQUAL( a_mcpdf.SampleGet(i).WeightGet(), b_mcpdf.SampleGet(i).WeightGet());
00686   }
00687 
00688   /* Sampling */
00689   vector<Sample<ColumnVector> > samples_test(NUM_SAMPLES);
00690   CPPUNIT_ASSERT_EQUAL( true, a_mcpdf.SampleFrom(samples_test,NUM_SAMPLES,RIPLEY,NULL));
00691   CPPUNIT_ASSERT_EQUAL( true, a_mcpdf.SampleFrom(samples_test,NUM_SAMPLES,DEFAULT,NULL));
00692 
00693   /* Expected Value*/
00694   vector<WeightedSample<ColumnVector> > los = a_mcpdf.ListOfSamplesGet();
00695   vector<WeightedSample<ColumnVector> >::iterator it2;
00696   ColumnVector cumSum(DIMENSION);
00697   cumSum=0.0;
00698   double sumWeights = 0.0;
00699   for ( it2 = los.begin() ; it2!= los.end() ; it2++ )
00700   {
00701     cumSum += ( it2->ValueGet() * it2->WeightGet() );
00702     sumWeights += it2->WeightGet();
00703   }
00704   CPPUNIT_ASSERT_EQUAL( approxEqual(cumSum/sumWeights, a_mcpdf.ExpectedValueGet(), epsilon),true);
00705 
00706   /* Covariance  + cumsumPDF*/
00707   ColumnVector mean(a_mcpdf.ExpectedValueGet());
00708   ColumnVector diff(DIMENSION); // Temporary storage
00709   Matrix diffsum(DIMENSION, DIMENSION);
00710   diffsum = 0.0;
00711   vector<double> cumPDF = a_mcpdf.CumulativePDFGet();
00712   vector<double>::iterator cumPDFit;
00713   cumPDFit = cumPDF.begin(); *cumPDFit = 0.0;
00714   double cumSumW = 0.0;
00715   for (it2 = los.begin(); it2 != los.end(); it2++)
00716   {
00717     diff = (it2->ValueGet() - mean);
00718     diffsum += diff * (diff.transpose() * it2->WeightGet());
00719     cumPDFit++;
00720     cumSumW += ( it2->WeightGet() / sumWeights);
00721     // test cumulative sum
00722     CPPUNIT_ASSERT_EQUAL(approxEqual(cumSumW, *cumPDFit, epsilon), true);
00723   }
00724   CPPUNIT_ASSERT_EQUAL(approxEqual(diffsum/sumWeights, (Matrix)a_mcpdf.CovarianceGet(),epsilon),true);
00725 
00726   /* ProbabilityGet */
00727 
00728   /**************************
00729   // MCPDF with unsigned int
00730   *************************/
00731 
00732   /* Set and Get Dimension and number of samples*/
00733   MCPdf<unsigned int> a_mcpdf_uint(NUM_SAMPLES,1);
00734   unsigned int one = 1;
00735   CPPUNIT_ASSERT_EQUAL(one, a_mcpdf_uint.DimensionGet());
00736   CPPUNIT_ASSERT_EQUAL( NUM_SAMPLES , a_mcpdf_uint.NumSamplesGet());
00737 
00738 
00739   // Generating (exact) Samples from a discrete pdf
00740   unsigned int num_states = 10;
00741   DiscretePdf discrete(num_states);
00742 
00743   vector<Sample<int> > samples_discrete_int(NUM_SAMPLES);
00744   vector<Sample<int> >::iterator it_discrete_int;
00745   discrete.SampleFrom(samples_discrete_int, NUM_SAMPLES);
00746 
00747   vector<Sample<unsigned int> > samples_discrete(NUM_SAMPLES);
00748   vector<Sample<unsigned int> >::iterator it_discrete;
00749   it_discrete = samples_discrete.begin();
00750 
00751   Sample<unsigned int> temp_sample;
00752   for(it_discrete_int = samples_discrete_int.begin(); it_discrete_int != samples_discrete_int.end(); it_discrete_int++)
00753   {
00754     temp_sample.ValueSet((*it_discrete_int).ValueGet());
00755     (*it_discrete)= temp_sample;
00756     it_discrete++;
00757 }
00758 
00759   /* Getting and setting the list of samples (non-weighted)*/
00760   a_mcpdf_uint.ListOfSamplesSet(samples_discrete);
00761   const vector<WeightedSample<unsigned int> > mcpdf_samples_uint = a_mcpdf_uint.ListOfSamplesGet();
00762   for (unsigned int i = 0; i < NUM_SAMPLES ; i++)
00763   {
00764      CPPUNIT_ASSERT_EQUAL( samples_discrete[i].ValueGet(), mcpdf_samples_uint[i].ValueGet());
00765      CPPUNIT_ASSERT_EQUAL( samples_discrete[i].ValueGet(), a_mcpdf_uint.SampleGet(i).ValueGet());
00766      CPPUNIT_ASSERT_EQUAL( 1.0/NUM_SAMPLES, mcpdf_samples_uint[i].WeightGet());
00767   }
00768 
00769   /* List of samples update + getting and setting the list of samples (weighted)*/
00770   vector<WeightedSample<unsigned int > > samples_weighted_uint = mcpdf_samples_uint;
00771   for (unsigned int i = 0 ; i < NUM_SAMPLES ; i++)
00772   {
00773      //set a weight
00774      samples_weighted_uint[i].WeightSet(i+1);
00775   }
00776   double tot_weight_uint = (double)(NUM_SAMPLES+1)*((double)NUM_SAMPLES)/2.0;
00777   CPPUNIT_ASSERT_EQUAL( true , a_mcpdf_uint.ListOfSamplesUpdate(samples_weighted_uint) );
00778   for (unsigned int i = 0; i < NUM_SAMPLES ; i++)
00779   {
00780      CPPUNIT_ASSERT_EQUAL( samples_weighted_uint[i].ValueGet(), a_mcpdf_uint.SampleGet(i).ValueGet());
00781      CPPUNIT_ASSERT_EQUAL( (double)(samples_weighted_uint[i].WeightGet())/tot_weight_uint, a_mcpdf_uint.SampleGet(i).WeightGet());
00782   }
00783 
00785   MCPdf<unsigned int> b_mcpdf_uint(a_mcpdf_uint);
00786   for (unsigned int i = 0; i < NUM_SAMPLES ; i++)
00787   {
00788      CPPUNIT_ASSERT_EQUAL( a_mcpdf_uint.SampleGet(i).ValueGet(), b_mcpdf_uint.SampleGet(i).ValueGet());
00789      CPPUNIT_ASSERT_EQUAL( a_mcpdf_uint.SampleGet(i).WeightGet(), b_mcpdf_uint.SampleGet(i).WeightGet());
00790   }
00791 
00792   /* Sampling */
00793   vector<Sample<unsigned int> > samples_test_uint(NUM_SAMPLES);
00794   CPPUNIT_ASSERT_EQUAL( true, a_mcpdf_uint.SampleFrom(samples_test_uint,NUM_SAMPLES,DEFAULT,NULL));
00795 
00796   /* Expected Value*/
00797   vector<WeightedSample<unsigned int> > los_uint = a_mcpdf_uint.ListOfSamplesGet();
00798   vector<WeightedSample<unsigned int> >::iterator it2_uint;
00799   double cumSum_double;
00800   cumSum_double=0.0;
00801   sumWeights = 0.0;
00802   for ( it2_uint = los_uint.begin() ; it2_uint!= los_uint.end() ; it2_uint++ )
00803   {
00804     cumSum_double += ( (double)it2_uint->ValueGet() * it2_uint->WeightGet() );
00805     sumWeights += it2_uint->WeightGet();
00806   }
00807   CPPUNIT_ASSERT_EQUAL( approxEqual( (unsigned int ) (cumSum_double/sumWeights + 0.5) , a_mcpdf_uint.ExpectedValueGet(), epsilon),true);
00808 
00809   /* Covariance  + cumsumPDF*/
00810   unsigned int  mean_uint = a_mcpdf_uint.ExpectedValueGet();
00811   unsigned int  diff_uint;
00812   double diffsum_uint;
00813   diffsum_uint = 0.0;
00814   vector<double> cumPDF_uint = a_mcpdf_uint.CumulativePDFGet();
00815   vector<double>::iterator cumPDFit_uint;
00816   cumPDFit_uint = cumPDF_uint.begin(); *cumPDFit_uint = 0.0;
00817   double cumSumW_uint = 0.0;
00818   for (it2_uint = los_uint.begin(); it2_uint != los_uint.end(); it2_uint++)
00819   {
00820     diff_uint =  (it2_uint->ValueGet() - mean_uint);
00821     diffsum_uint += (double)(diff_uint * diff_uint)  * it2_uint->WeightGet();
00822     cumPDFit_uint++;
00823     cumSumW_uint += ( it2_uint->WeightGet() / sumWeights);
00824     // test cumulative sum
00825     CPPUNIT_ASSERT_EQUAL(approxEqual(cumSumW_uint, *cumPDFit_uint, epsilon), true);
00826   }
00827   Matrix test_diff(1,1);
00828   test_diff(1,1) = diffsum_uint/sumWeights;
00829   CPPUNIT_ASSERT_EQUAL(approxEqual(test_diff, (Matrix)a_mcpdf_uint.CovarianceGet(),epsilon),true);
00830   /* ProbabilityGet */
00831 }
00832 
00833 
00834 
00835 class MyType
00836 {
00837 public:
00838   // empty constructor
00839   MyType() {};
00840 
00841   // unsigned int constructor
00842   //MyType(unsigned int dim) {};
00843 
00844   ~MyType() {};
00845 };
00846 
00847 
00848 void 
00849 PdfTest::testMcpdfType()
00850 {
00851   MCPdf<MyType> a_mcpdf(NUM_SAMPLES,DIMENSION);
00852 }
00853 
00854 void
00855 PdfTest::testMixture()
00856 {
00857   /*******************************
00858   A) TEMPlATE = COLUMNVECTOR
00859   *******************************/
00860 
00861   // Constructor with DIMENSION
00862   Mixture<ColumnVector> mixture(DIMENSION);
00863   ColumnVector cv1(DIMENSION,0.0);
00864   cv1 = 0.0;
00865   Sample<ColumnVector> sample ;
00866   vector<Sample<ColumnVector> > los(NUM_SAMPLES);
00867 
00868   CPPUNIT_ASSERT_EQUAL( DIMENSION, mixture.DimensionGet() );
00869   CPPUNIT_ASSERT_EQUAL( 0, (int)mixture.NumComponentsGet() );
00870 
00871   // all these calls results in assertions since numComponents = 0
00872   /*
00873   (double)mixture.ProbabilityGet(cv1) ;
00874   mixture.SampleFrom(sample) ;
00875   mixture.SampleFrom(los,NUM_SAMPLES) ;
00876   mixture.SampleFrom(los,NUM_SAMPLES,RIPLEY) ;
00877   mixture.ExpectedValueGet() ;
00878   mixture.WeightsGet() ;
00879   mixture.WeightGet(0) ;
00880   vector<Probability> weightVec(1);
00881   weightVec[0]=Probability(1.0);
00882   mixture.WeightsSet(weightVec) ;
00883   mixture.WeightSet(0,Probability(1.0)) ;
00884   mixture.MostProbableComponentGet();
00885   */
00886 
00887   Gaussian comp1(_mu,_sigma);
00888   comp1.ExpectedValueSet(_mu);
00889   comp1.CovarianceSet(_sigma);
00890   CPPUNIT_ASSERT_EQUAL(true, mixture.AddComponent(comp1));
00891   CPPUNIT_ASSERT_EQUAL(1.0, (double)mixture.WeightGet(0));
00892 
00893   //one component tests
00894   CPPUNIT_ASSERT_EQUAL( DIMENSION, mixture.DimensionGet() );
00895   CPPUNIT_ASSERT_EQUAL( 1, (int)mixture.NumComponentsGet() );
00896   CPPUNIT_ASSERT_EQUAL((double)comp1.ProbabilityGet(cv1),(double)mixture.ProbabilityGet(cv1)) ;
00897   ColumnVector expected_mix = mixture.ExpectedValueGet() ;
00898   ColumnVector expected_comp1 = comp1.ExpectedValueGet() ;
00899   for(int j = 1 ; j <= DIMENSION; j++)
00900         CPPUNIT_ASSERT_EQUAL(expected_comp1(j), expected_mix(j) ) ;
00901   vector<Probability> vecW(1);
00902   vecW[0]=1.0;
00903   vector<Probability> mixWeights = mixture.WeightsGet() ;
00904   for(int j = 1 ; j <= mixture.NumComponentsGet(); j++)
00905         CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
00906   CPPUNIT_ASSERT_EQUAL(1.0,(double)mixture.WeightGet(0) ) ;
00907   vecW[0]=Probability(1.0);
00908   mixture.WeightsSet(vecW) ;
00909   for(int j = 1 ; j <= mixture.NumComponentsGet(); j++)
00910         CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
00911   mixture.WeightSet(0,Probability(1.0)) ;
00912   CPPUNIT_ASSERT_EQUAL(1.0 , (double)mixture.WeightGet(0) ) ;
00913   CPPUNIT_ASSERT_EQUAL(0,(int)mixture.MostProbableComponentGet() );
00914 
00915 
00916   // sampling with one component test
00917   CPPUNIT_ASSERT_EQUAL( true, mixture.SampleFrom(sample,DEFAULT,NULL));
00918   // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
00919   // erf(4/sqrt(2)) for each sample
00920   for(int j = 1 ; j <= DIMENSION; j++)
00921   {
00922     CPPUNIT_ASSERT( (sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
00923     CPPUNIT_ASSERT( (sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
00924   }
00925   // Box-Muller and Choleskyis not implemented yet
00926   CPPUNIT_ASSERT_EQUAL( false, mixture.SampleFrom(sample,BOXMULLER,NULL));
00927   CPPUNIT_ASSERT_EQUAL( false, mixture.SampleFrom(sample,CHOLESKY,NULL));
00928 
00929   // list of samples
00930   CPPUNIT_ASSERT_EQUAL( true, mixture.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
00931   // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
00932   // erf(4/sqrt(2)) for each sample
00933   ColumnVector sample_mean(DIMENSION);
00934   sample_mean = 0.0;
00935   for(int i = 0 ; i < NUM_SAMPLES; i++)
00936   {
00937     sample = los[i];
00938     sample_mean += sample.ValueGet();
00939     for(int j = 1 ; j <= DIMENSION; j++)
00940     {
00941         CPPUNIT_ASSERT( (sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
00942         CPPUNIT_ASSERT( (sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
00943     }
00944   }
00945   // check whether mean and of samples is in withing the expected 3 sigma border
00946   // 3 sigma test : REMARK: this test WILL occasionaly fail with probability
00947   // erf(3/sqrt(2)) for each sample
00948   sample_mean = sample_mean / (double)NUM_SAMPLES;
00949   for(int j = 1 ; j <= DIMENSION; j++)
00950   {
00951     CPPUNIT_ASSERT( sample_mean(j) <= _mu(j) + 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
00952     CPPUNIT_ASSERT( sample_mean(j) >= _mu(j) - 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
00953   }
00954   // Box-Muller and cholesky is not implemented 
00955   CPPUNIT_ASSERT_EQUAL( false, mixture.SampleFrom(los,NUM_SAMPLES,BOXMULLER,NULL));
00956   CPPUNIT_ASSERT_EQUAL( false, mixture.SampleFrom(los,NUM_SAMPLES,CHOLESKY,NULL));
00957   CPPUNIT_ASSERT_EQUAL( true, mixture.SampleFrom(los,NUM_SAMPLES,RIPLEY,NULL));
00958   // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
00959   // erf(4/sqrt(2)) for each sample
00960   sample_mean = 0.0;
00961   for(int i = 0 ; i < NUM_SAMPLES; i++)
00962   {
00963     sample = los[i];
00964     sample_mean += sample.ValueGet();
00965     for(int j = 1 ; j <= DIMENSION; j++)
00966     {
00967         CPPUNIT_ASSERT( (sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
00968         CPPUNIT_ASSERT( (sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
00969     }
00970   }
00971   // check whether mean and of samples is in withing the expected 3 sigma border
00972   // 3 sigma test : REMARK: this test WILL occasionaly fail with probability
00973   // erf(3/sqrt(2)) for each sample
00974   sample_mean = sample_mean / (double)NUM_SAMPLES;
00975   for(int j = 1 ; j <= DIMENSION; j++)
00976   {
00977     CPPUNIT_ASSERT( sample_mean(j) <= _mu(j) + 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
00978     CPPUNIT_ASSERT( sample_mean(j) >= _mu(j) - 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
00979   }
00980 
00981   // DELETING COMPONENT
00982   CPPUNIT_ASSERT_EQUAL(true, mixture.DeleteComponent(0));
00983 
00984   // One component with weight w1
00985   Gaussian comp2(_mu,_sigma);
00986   Mixture<ColumnVector> mixture1(DIMENSION);
00987   Probability w1 = 0.5;
00988   CPPUNIT_ASSERT_EQUAL(true, mixture1.AddComponent(comp2,w1));
00989   CPPUNIT_ASSERT_EQUAL(1.0, (double)mixture1.WeightGet(0));
00990   CPPUNIT_ASSERT_EQUAL( DIMENSION, mixture1.DimensionGet() );
00991   CPPUNIT_ASSERT_EQUAL( 1, (int)mixture1.NumComponentsGet() );
00992   CPPUNIT_ASSERT_EQUAL((double)comp2.ProbabilityGet(cv1),(double)mixture1.ProbabilityGet(cv1)) ;
00993 
00994   //AddComponent (default weight addef = 0)
00995   ColumnVector mu3(DIMENSION);
00996   mu3(1) =1.2; mu3(2) = 1.5;
00997   SymmetricMatrix sigma3(DIMENSION);
00998   sigma3 = 0.0;
00999   for (unsigned int rows=1; rows < DIMENSION + 1; rows++){ sigma3(rows,rows)=2.3; }
01000   Gaussian comp3(mu3,sigma3);
01001   CPPUNIT_ASSERT_EQUAL(true, mixture1.AddComponent(comp3));
01002   CPPUNIT_ASSERT_EQUAL(1.0, (double)mixture1.WeightGet(0));
01003   CPPUNIT_ASSERT_EQUAL(0.0, (double)mixture1.WeightGet(1));
01004   CPPUNIT_ASSERT_EQUAL( DIMENSION, mixture1.DimensionGet() );
01005   CPPUNIT_ASSERT_EQUAL( 2, (int)mixture1.NumComponentsGet() );
01006   CPPUNIT_ASSERT_EQUAL((double)comp2.ProbabilityGet(cv1),(double)mixture1.ProbabilityGet(cv1)) ;
01007   vecW.resize(mixture1.NumComponentsGet());
01008   vecW[0]=1.0;
01009   vecW[1]=0.0;
01010   mixWeights = mixture1.WeightsGet() ;
01011   for(int j = 1 ; j <= mixture1.NumComponentsGet(); j++)
01012         CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
01013   //WeightSet and WeightGet
01014   CPPUNIT_ASSERT_EQUAL(true, mixture1.WeightSet(1,0.2));
01015   vecW[0]=0.8;
01016   vecW[1]=0.2;
01017   mixWeights = mixture1.WeightsGet() ;
01018   for(int j = 1 ; j <= mixture1.NumComponentsGet(); j++)
01019         CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
01020   //WeightsSet (with non-normalized vector of Probabilities) and WeightsGet
01021   vecW[0]=0.4;
01022   vecW[1]=1.6;
01023   CPPUNIT_ASSERT_EQUAL(true, mixture1.WeightsSet(vecW));
01024   mixWeights = mixture1.WeightsGet() ;
01025   double sumVecW = 0.0;  
01026   for(int j = 0 ; j < vecW.size(); j++)
01027     sumVecW += (double)vecW[j];
01028   for(int j = 0 ; j < vecW.size(); j++)
01029     vecW[j] = (double)vecW[j]/sumVecW;
01030   for(int j = 1 ; j <= mixture1.NumComponentsGet(); j++)
01031         CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
01032   //ProbabilityGet
01033   Probability prob = 0.0;
01034   prob = vecW[0] * comp2.ProbabilityGet(cv1) + vecW[1] * comp3.ProbabilityGet(cv1);
01035   CPPUNIT_ASSERT_EQUAL((double)prob,(double)mixture1.ProbabilityGet(cv1)) ;
01036   //ExpectedValueGet
01037   expected_mix = mixture1.ExpectedValueGet() ;
01038   ColumnVector expected_true = comp2.ExpectedValueGet() * (double)vecW[0] + comp3.ExpectedValueGet() * (double)vecW[1];
01039   for(int j = 1 ; j <= DIMENSION; j++)
01040         CPPUNIT_ASSERT_EQUAL(expected_true(j), expected_mix(j) ) ;
01041   //MostProbableComponentGet
01042   CPPUNIT_ASSERT_EQUAL(1,mixture1.MostProbableComponentGet());
01043   vecW[0]=0.8;
01044   vecW[1]=0.2;
01045   CPPUNIT_ASSERT_EQUAL(true, mixture1.WeightsSet(vecW));
01046   CPPUNIT_ASSERT_EQUAL(0,mixture1.MostProbableComponentGet());
01047   vecW[0]=0.5;
01048   vecW[1]=0.5;
01049   CPPUNIT_ASSERT_EQUAL(true, mixture1.WeightsSet(vecW));
01050   CPPUNIT_ASSERT_EQUAL(0,mixture1.MostProbableComponentGet());
01051 
01052   // DELETING COMPONENT
01053   CPPUNIT_ASSERT_EQUAL(true, mixture1.DeleteComponent(1));
01054   CPPUNIT_ASSERT_EQUAL( 1, (int)mixture1.NumComponentsGet() );
01055   CPPUNIT_ASSERT_EQUAL(1.0, (double)mixture1.WeightGet(0));
01056   CPPUNIT_ASSERT_EQUAL( DIMENSION, mixture1.DimensionGet() );
01057   CPPUNIT_ASSERT_EQUAL((double)comp2.ProbabilityGet(cv1),(double)mixture1.ProbabilityGet(cv1)) ;
01058 
01059   // CONSTRUCTOR WITH VECTOR OF PDFS
01060   //AddComponent (default weight addef = 0)
01061   ColumnVector muVec1(DIMENSION);
01062   ColumnVector muVec2(DIMENSION);
01063   muVec1(1) =1.2; muVec1(2) = 1.5;
01064   muVec2(1) =0.2; muVec2(2) = -1.1;
01065   SymmetricMatrix sigmaVec1(DIMENSION);
01066   SymmetricMatrix sigmaVec2(DIMENSION);
01067   sigmaVec1 = 0.0;
01068   sigmaVec2 = 0.0;
01069   double sigmaValVec1 = 2.3;
01070   double sigmaValVec2 = 0.3;
01071   for (unsigned int rows=1; rows < DIMENSION + 1; rows++){
01072     sigmaVec1(rows,rows)=sigmaValVec1;
01073     sigmaVec2(rows,rows)=sigmaValVec2;
01074   }
01075 
01076   Gaussian compVec1(muVec1,sigmaVec1);
01077   Gaussian compVec2(muVec2,sigmaVec2);
01078 
01079   //vector<Pdf<ColumnVector>*> componentVec(2);
01080   vector<Gaussian*> componentVec(2);
01081   componentVec[0] = &compVec1;
01082   componentVec[1] = &compVec2;
01083 
01084   Mixture<ColumnVector> mixtureVec(componentVec);
01085 
01086   CPPUNIT_ASSERT_EQUAL(0.5, (double)mixtureVec.WeightGet(0));
01087   CPPUNIT_ASSERT_EQUAL(0.5, (double)mixtureVec.WeightGet(1));
01088   vecW[0]=0.5;
01089   vecW[1]=0.5;
01090   mixWeights = mixtureVec.WeightsGet() ;
01091   for(int j = 1 ; j <= mixtureVec.NumComponentsGet(); j++)
01092         CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
01093   CPPUNIT_ASSERT_EQUAL( DIMENSION, mixtureVec.DimensionGet() );
01094   CPPUNIT_ASSERT_EQUAL( 2, (int)mixtureVec.NumComponentsGet() );
01095   for(int j = 1 ; j <= mixtureVec.NumComponentsGet(); j++)
01096         CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
01097   vecW.resize(mixtureVec.NumComponentsGet());
01098   vecW[0]=1.0;
01099   vecW[1]=0.0;
01100   mixWeights = mixtureVec.WeightsGet() ;
01101   ColumnVector expectedComp(DIMENSION);
01102   ColumnVector expectedMix(DIMENSION);
01103   for(int j = 1 ; j <= mixtureVec.NumComponentsGet(); j++)
01104   {
01105         expectedComp = componentVec[j-1]->ExpectedValueGet();
01106         expectedMix = mixtureVec.ComponentGet(j-1)->ExpectedValueGet();
01107         for(int i = 1 ; i <= DIMENSION; i++)
01108             CPPUNIT_ASSERT_EQUAL(expectedComp(i), expectedMix(i) ) ;
01109   }
01110   //WeightSet and WeightGet
01111   vecW[0]=0.8;
01112   vecW[1]=0.2;
01113   CPPUNIT_ASSERT_EQUAL(true, mixtureVec.WeightsSet(vecW));
01114   mixWeights = mixtureVec.WeightsGet() ;
01115   for(int j = 1 ; j <= mixtureVec.NumComponentsGet(); j++)
01116         CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
01117   //WeightsSet (with non-normalized vector of Probabilities) and WeightsGet
01118   vecW[0]=0.4;
01119   vecW[1]=1.6;
01120   CPPUNIT_ASSERT_EQUAL(true, mixtureVec.WeightsSet(vecW));
01121   mixWeights = mixtureVec.WeightsGet() ;
01122   sumVecW = 0.0;  
01123   for(int j = 0 ; j < vecW.size(); j++)
01124     sumVecW += (double)vecW[j];
01125   for(int j = 0 ; j < vecW.size(); j++)
01126     vecW[j] = (double)vecW[j]/sumVecW;
01127   for(int j = 1 ; j <= mixtureVec.NumComponentsGet(); j++)
01128         CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
01129   //ProbabilityGet
01130   prob = 0.0;
01131   for(int j = 1 ; j <= componentVec.size();j++)
01132   {
01133     prob = prob + vecW[j-1] * componentVec[j-1]->ProbabilityGet(cv1) ;
01134   }
01135   CPPUNIT_ASSERT_EQUAL((double)prob,(double)mixtureVec.ProbabilityGet(cv1)) ;
01136   //ExpectedValueGet
01137   expectedMix = mixtureVec.ExpectedValueGet() ;
01138   expectedComp = 0.0; 
01139   for(int j = 1 ; j <= componentVec.size();j++)
01140     expectedComp = expectedComp + componentVec[j-1]->ExpectedValueGet() * (double)vecW[j-1] ;
01141   for(int j = 1 ; j <= DIMENSION; j++)
01142         CPPUNIT_ASSERT_EQUAL(expectedComp(j), expectedMix(j) ) ;
01143   //MostProbableComponentGet
01144   CPPUNIT_ASSERT_EQUAL(1,mixtureVec.MostProbableComponentGet());
01145   vecW[0]=0.5;
01146   vecW[1]=0.5;
01147   CPPUNIT_ASSERT_EQUAL(true, mixtureVec.WeightsSet(vecW));
01148   CPPUNIT_ASSERT_EQUAL(0,mixtureVec.MostProbableComponentGet());
01149 
01150   /*******************************
01151   B) TEMPlATE = INT
01152   *******************************/
01153   // One component with weight w1
01154   DiscretePdf disc1(NUM_DS);
01155   vector<Probability> probs1(NUM_DS);
01156   disc1.ProbabilitySet(1,0.8);
01157 
01158   unsigned int dim_int = 1;
01159   Mixture<int> mixture_int(dim_int);
01160   w1 = 0.5;
01161   CPPUNIT_ASSERT_EQUAL(true, mixture_int.AddComponent(disc1,w1));
01162   CPPUNIT_ASSERT_EQUAL(1.0, (double)mixture_int.WeightGet(0));
01163   CPPUNIT_ASSERT_EQUAL( 1, (int)mixture_int.NumComponentsGet() );
01164   for(int state = 0; state<NUM_DS ; state++)
01165     CPPUNIT_ASSERT_EQUAL((double)disc1.ProbabilityGet(state),(double)mixture_int.ProbabilityGet(state)) ;
01166 
01167   //AddComponent (default weight addef = 0)
01168   DiscretePdf disc2(NUM_DS);
01169   vector<Probability> probs2(2);
01170   disc2.ProbabilitySet(2,0.5);
01171 
01172   CPPUNIT_ASSERT_EQUAL(true, mixture_int.AddComponent(disc2));
01173   CPPUNIT_ASSERT_EQUAL(1.0, (double)mixture_int.WeightGet(0));
01174   CPPUNIT_ASSERT_EQUAL(0.0, (double)mixture_int.WeightGet(1));
01175   CPPUNIT_ASSERT_EQUAL( 1, (int)mixture_int.DimensionGet() );
01176   CPPUNIT_ASSERT_EQUAL( 2, (int)mixture_int.NumComponentsGet() );
01177   for(int state = 0; state<NUM_DS ; state++)
01178     CPPUNIT_ASSERT_EQUAL((double)disc1.ProbabilityGet(state),(double)mixture_int.ProbabilityGet(state)) ;
01179   vecW.resize(mixture_int.NumComponentsGet());
01180   vecW[0]=1.0;
01181   vecW[1]=0.0;
01182   mixWeights = mixture_int.WeightsGet() ;
01183   for(int j = 1 ; j <= mixture_int.NumComponentsGet(); j++)
01184         CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
01185   //WeightSet and WeightGet
01186   CPPUNIT_ASSERT_EQUAL(true, mixture_int.WeightSet(1,0.2));
01187   vecW[0]=0.8;
01188   vecW[1]=0.2;
01189   mixWeights = mixture_int.WeightsGet() ;
01190   for(int j = 1 ; j <= mixture_int.NumComponentsGet(); j++)
01191         CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
01192   //WeightsSet (with non-normalized vector of Probabilities) and WeightsGet
01193   vecW[0]=0.4;
01194   vecW[1]=1.6;
01195   CPPUNIT_ASSERT_EQUAL(true, mixture_int.WeightsSet(vecW));
01196   mixWeights = mixture_int.WeightsGet() ;
01197   sumVecW = 0.0;  
01198   for(int j = 0 ; j < vecW.size(); j++)
01199     sumVecW += (double)vecW[j];
01200   for(int j = 0 ; j < vecW.size(); j++)
01201     vecW[j] = (double)vecW[j]/sumVecW;
01202   for(int j = 1 ; j <= mixture_int.NumComponentsGet(); j++)
01203         CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
01204   //ProbabilityGet
01205   for(int state = 0; state<NUM_DS ; state++)
01206   {
01207     prob = vecW[0] * disc1.ProbabilityGet(state) + vecW[1] * disc2.ProbabilityGet(state);
01208     CPPUNIT_ASSERT_EQUAL((double)prob,(double)mixture_int.ProbabilityGet(state)) ;
01209   }
01210   //ExpectedValueGet
01211   probs1 = disc1.ProbabilitiesGet();
01212   probs2 = disc2.ProbabilitiesGet();
01213   vector<Probability> probs(probs1.size());
01214   // search for most probable state
01215   int mostProbState = -1;
01216   double probMostProbState = -1.0;
01217   double probState = 0.0;
01218   for( int i = 0 ; i<probs.size() ; i++)
01219   {
01220     probState = (double)(probs1[i]) * (double)vecW[0] + (double)probs2[i] * (double) vecW[1];
01221     if(probState > probMostProbState)
01222     {
01223         probMostProbState = probState;
01224         mostProbState = i;
01225     }
01226   }
01227   CPPUNIT_ASSERT_EQUAL(mostProbState ,mixture_int.ExpectedValueGet());
01228   //MostProbableComponentGet
01229   CPPUNIT_ASSERT_EQUAL(1,mixture_int.MostProbableComponentGet());
01230   vecW[0]=0.8;
01231   vecW[1]=0.2;
01232   CPPUNIT_ASSERT_EQUAL(true, mixture_int.WeightsSet(vecW));
01233   CPPUNIT_ASSERT_EQUAL(0,mixture_int.MostProbableComponentGet());
01234   vecW[0]=0.5;
01235   vecW[1]=0.5;
01236   CPPUNIT_ASSERT_EQUAL(true, mixture_int.WeightsSet(vecW));
01237   CPPUNIT_ASSERT_EQUAL(0,mixture_int.MostProbableComponentGet());
01238  }


bfl
Author(s): Klaas Gadeyne, Wim Meeussen, Tinne Delaet and many others. See web page for a full contributor list. ROS package maintained by Wim Meeussen.
autogenerated on Sun Oct 5 2014 22:29:53