pdf_test.cpp
Go to the documentation of this file.
1 // Copyright (C) 2007 Klaas Gadeyne <first dot last at gmail dot com>
2 // Copyright (C) 2007 Tinne De Laet <first dot last at mech dot kuleuven dot be>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
19 #include "pdf_test.hpp"
20 #include "approxEqual.hpp"
21 
22 // Registers the fixture into the 'registry'
24 using namespace BFL;
25 
26 #define DIMENSION_INPUT 2
27 #define MU_0 2.8
28 #define MU_1 3.4
29 #define MU_2 3.4
30 #define MU_3 3.4
31 #define SIGMA 0.01
32 #define SIGMA2 0.00
33 #define WIDTH_0 1
34 #define WIDTH_1 2
35 const unsigned int NUM_COND_ARGS = 2;
36 const unsigned int DIMENSION = 2;
37 const unsigned int NUM_SAMPLES = 500;
38 
39 //discrete pdf
40 #define NUM_DS 5
41 //discrete conditional pdf
42 #define NUM_COND_ARGS_DIS 1
43 
44 void
46 {
47  epsilon = 0.0001;
48 
49  _mu.resize(DIMENSION);
50  _mu(1) = MU_0; _mu(2) = MU_1;
51  _sigma.resize(DIMENSION);
52  _sigma = 0.0;
53  for (unsigned int rows=1; rows < DIMENSION + 1; rows++){ _sigma(rows,rows)=SIGMA; }
54  _width.resize(DIMENSION);
55  _width(1) = WIDTH_0 ;
56  _width(2) = WIDTH_1 ;
57 }
58 
59 void
61 {
62 }
63 
64 void
66 {
67  Gaussian first_gaussian(DIMENSION);
68  first_gaussian.ExpectedValueSet(_mu);
69  first_gaussian.CovarianceSet(_sigma);
70  Sample<ColumnVector> test_sample ;
71  CPPUNIT_ASSERT_EQUAL( true, first_gaussian.SampleFrom(test_sample,DEFAULT,NULL));
72 
73  Gaussian a_gaussian(_mu,_sigma);
74 
75  /* Sampling */
76  // one sample
77  Sample<ColumnVector> a_sample ;
78  CPPUNIT_ASSERT_EQUAL( true, a_gaussian.SampleFrom(a_sample,DEFAULT,NULL));
79  // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
80  // erf(4/sqrt(2)) for each sample
81  for(int j = 1 ; j <= DIMENSION; j++)
82  {
83  CPPUNIT_ASSERT( (a_sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
84  CPPUNIT_ASSERT( (a_sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
85  }
86  // Box-Muller is not implemented yet
87  CPPUNIT_ASSERT_EQUAL( false, a_gaussian.SampleFrom(a_sample,BOXMULLER,NULL));
88  CPPUNIT_ASSERT_EQUAL( true, a_gaussian.SampleFrom(a_sample,CHOLESKY,NULL));
89  // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
90  // erf(4/sqrt(2)) for each sample
91  for(int j = 1 ; j <= DIMENSION; j++)
92  {
93  CPPUNIT_ASSERT( (a_sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
94  CPPUNIT_ASSERT( (a_sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
95  }
96 
97  // list of samples
98  vector<Sample<ColumnVector> > los(NUM_SAMPLES);
99  CPPUNIT_ASSERT_EQUAL( true, a_gaussian.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
100  // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
101  // erf(4/sqrt(2)) for each sample
102  ColumnVector sample_mean(DIMENSION);
103  sample_mean = 0.0;
104  for(int i = 0 ; i < NUM_SAMPLES; i++)
105  {
106  a_sample = los[i];
107  sample_mean += a_sample.ValueGet();
108  for(int j = 1 ; j <= DIMENSION; j++)
109  {
110  CPPUNIT_ASSERT( (a_sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
111  CPPUNIT_ASSERT( (a_sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
112  }
113  }
114  // check whether mean and of samples is in withing the expected 3 sigma border
115  // 3 sigma test : REMARK: this test WILL occasionaly fail with probability
116  // erf(3/sqrt(2)) for each sample
117  sample_mean = sample_mean / (double)NUM_SAMPLES;
118  for(int j = 1 ; j <= DIMENSION; j++)
119  {
120  CPPUNIT_ASSERT( sample_mean(j) <= _mu(j) + 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
121  CPPUNIT_ASSERT( sample_mean(j) >= _mu(j) - 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
122  }
123  // Box-Muller is not implemented yet
124  CPPUNIT_ASSERT_EQUAL( false, a_gaussian.SampleFrom(los,NUM_SAMPLES,BOXMULLER,NULL));
125  CPPUNIT_ASSERT_EQUAL( true, a_gaussian.SampleFrom(los,NUM_SAMPLES,CHOLESKY,NULL));
126  // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
127  // erf(4/sqrt(2)) for each sample
128  sample_mean = 0.0;
129  for(int i = 0 ; i < NUM_SAMPLES; i++)
130  {
131  a_sample = los[i];
132  sample_mean += a_sample.ValueGet();
133  for(int j = 1 ; j <= DIMENSION; j++)
134  {
135  CPPUNIT_ASSERT( (a_sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
136  CPPUNIT_ASSERT( (a_sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
137  }
138  }
139  // check whether mean and of samples is in withing the expected 3 sigma border
140  // 3 sigma test : REMARK: this test WILL occasionaly fail with probability
141  // erf(3/sqrt(2)) for each sample
142  sample_mean = sample_mean / (double)NUM_SAMPLES;
143  for(int j = 1 ; j <= DIMENSION; j++)
144  {
145  CPPUNIT_ASSERT( sample_mean(j) <= _mu(j) + 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
146  CPPUNIT_ASSERT( sample_mean(j) >= _mu(j) - 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
147  }
148 
149  /* Setting and Getting mean/covariance */
150  CPPUNIT_ASSERT_EQUAL( _mu, a_gaussian.ExpectedValueGet());
151  CPPUNIT_ASSERT_EQUAL( _sigma, a_gaussian.CovarianceGet());
152 
153  /* Copy Constructor etc */
154  Gaussian b_gaussian(a_gaussian);
155  CPPUNIT_ASSERT_EQUAL( _mu, b_gaussian.ExpectedValueGet());
156  CPPUNIT_ASSERT_EQUAL( _sigma, b_gaussian.CovarianceGet());
157 
158  /* Create Gaussian, allocate memory afterwards */
159  Gaussian c_gaussian;
160  c_gaussian.ExpectedValueSet(_mu);
161  CPPUNIT_ASSERT_EQUAL( _mu, c_gaussian.ExpectedValueGet());
162  c_gaussian.CovarianceSet(_sigma);
163  CPPUNIT_ASSERT_EQUAL( _sigma, c_gaussian.CovarianceGet());
164 
165  /* Clone */
166  Gaussian* clone = NULL;
167  clone = c_gaussian.Clone();
168  CPPUNIT_ASSERT_EQUAL( c_gaussian.ExpectedValueGet(), clone->ExpectedValueGet());
169  CPPUNIT_ASSERT_EQUAL( c_gaussian.CovarianceGet(), clone->CovarianceGet());
170 }
171 
172 void
174 {
175  Uniform a_uniform(_mu,_width);
176 
177  /* Setting and Getting center and widths */
178  CPPUNIT_ASSERT_EQUAL(approxEqual( _mu, a_uniform.CenterGet(),epsilon),true);
179  CPPUNIT_ASSERT_EQUAL(approxEqual( _width, a_uniform.WidthGet(),epsilon),true);
180 
181  /* Sampling */
182  vector<Sample<ColumnVector> > los(NUM_SAMPLES);
183  CPPUNIT_ASSERT_EQUAL( true, a_uniform.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
184  // test if samples are located in the area
185  vector<Sample<ColumnVector > >::iterator los_it;
186  for (los_it = los.begin(); los_it!=los.end(); los_it++)
187  {
188  ColumnVector current_sample = los_it->ValueGet();
189  for (int i = 1; i < _mu.rows()+1 ; i++)
190  {
191  CPPUNIT_ASSERT(current_sample(i) > (_mu(i)-_width(i)/2) ) ;
192  CPPUNIT_ASSERT(current_sample(i) < (_mu(i)+_width(i)/2) ) ;
193  }
194  }
195  // Box-Muller is not implemented yet
196  CPPUNIT_ASSERT_EQUAL( false, a_uniform.SampleFrom(los,NUM_SAMPLES,BOXMULLER,NULL));
197  Sample<ColumnVector> a_sample ;
198  CPPUNIT_ASSERT_EQUAL( true, a_uniform.SampleFrom(a_sample,DEFAULT,NULL));
199  // Box-Muller is not implemented yet
200  CPPUNIT_ASSERT_EQUAL( false, a_uniform.SampleFrom(a_sample,BOXMULLER,NULL));
201 
202  /* Getting the probability */
203  double area = 1;
204  for (int i =1 ; i < DIMENSION + 1 ; i++) area = area * _width(i);
205  CPPUNIT_ASSERT_EQUAL(1/area, (double)a_uniform.ProbabilityGet(_mu));
206  CPPUNIT_ASSERT_EQUAL(0.0, (double)a_uniform.ProbabilityGet(_mu + _width));
207  CPPUNIT_ASSERT_EQUAL(0.0, (double)a_uniform.ProbabilityGet(_mu - _width));
208  ColumnVector test_prob(DIMENSION);
209  test_prob = _mu;
210  test_prob(DIMENSION) = _mu(DIMENSION) + _width(DIMENSION)*2/3;
211  CPPUNIT_ASSERT_EQUAL(0.0, (double)a_uniform.ProbabilityGet(test_prob));
212 
213  /* Copy Constructor etc */
214  Uniform b_uniform(a_uniform);
215  CPPUNIT_ASSERT_EQUAL(approxEqual( _mu, b_uniform.CenterGet(),epsilon),true);
216  CPPUNIT_ASSERT_EQUAL(approxEqual( _width, b_uniform.WidthGet(),epsilon),true);
217 
218  /* Create Uniform, allocate memory afterwards */
219  Uniform c_uniform;
220  c_uniform.UniformSet(_mu,_width);
221  CPPUNIT_ASSERT_EQUAL(approxEqual( _mu, c_uniform.CenterGet(),epsilon),true);
222  CPPUNIT_ASSERT_EQUAL(approxEqual( _width, c_uniform.WidthGet(),epsilon),true);
223  /* Getting the probability */
224  area = 1;
225  for (int i =1 ; i < DIMENSION + 1 ; i++) area = area * _width(i);
226  CPPUNIT_ASSERT_EQUAL(1/area, (double)c_uniform.ProbabilityGet(_mu));
227  CPPUNIT_ASSERT_EQUAL(0.0, (double)c_uniform.ProbabilityGet(_mu + _width));
228  CPPUNIT_ASSERT_EQUAL(0.0, (double)c_uniform.ProbabilityGet(_mu - _width));
229  test_prob = _mu;
230  test_prob(DIMENSION) = _mu(DIMENSION) + _width(DIMENSION)*2/3;
231  CPPUNIT_ASSERT_EQUAL(0.0, (double)c_uniform.ProbabilityGet(test_prob));
232 
233  /* Clone */
234  Uniform* clone = NULL;
235  clone = c_uniform.Clone();
236  CPPUNIT_ASSERT_EQUAL( c_uniform.CenterGet(), clone->CenterGet());
237  CPPUNIT_ASSERT_EQUAL( c_uniform.WidthGet(), clone->WidthGet());
238 }
239 
240 void
242 {
243  DiscretePdf a_discretepdf(NUM_DS);
244  vector<Probability> uniform_vector(NUM_DS);
245  for (int state = 0; state < NUM_DS ; state++) uniform_vector[state] = (Probability)(1.0/NUM_DS );
246 
247  /* Check initial uniform distribution */
248  vector<Probability> result_proba = a_discretepdf.ProbabilitiesGet();
249  for (int state = 0; state < NUM_DS ; state++) CPPUNIT_ASSERT_EQUAL( (double) (uniform_vector[state]), double(result_proba[state] )) ;
250 
251  /* Check dimension */
252  CPPUNIT_ASSERT_EQUAL(1,(int)a_discretepdf.DimensionGet());
253 
254  /* Check the number of states */
255  CPPUNIT_ASSERT_EQUAL(NUM_DS,(int)a_discretepdf.NumStatesGet());
256 
257  /* Copy constructor */
258  DiscretePdf b_discretepdf(a_discretepdf);
259  vector<Probability> result_probb = b_discretepdf.ProbabilitiesGet();
260  for (int state = 0; state < NUM_DS ; state++) CPPUNIT_ASSERT_EQUAL( (double) (result_proba[state]), double(result_probb[state] )) ;
261  CPPUNIT_ASSERT_EQUAL(a_discretepdf.DimensionGet(), b_discretepdf.DimensionGet());
262  CPPUNIT_ASSERT_EQUAL(a_discretepdf.NumStatesGet(), b_discretepdf.NumStatesGet());
263 
264  /* Set and Get probabilities */
265  // set one probability
266  double prob_new = 0.57;
267  int new_el = NUM_DS-1;
268  CPPUNIT_ASSERT_EQUAL(true, a_discretepdf.ProbabilitySet(new_el,prob_new));
269  CPPUNIT_ASSERT_EQUAL(prob_new, (double) a_discretepdf.ProbabilityGet(new_el));
270  // check if the sum of the probabilities is still one!
271  double sumProb = 0.0;
272  for (int state = 0; state < NUM_DS ; state++)
273  {
274  sumProb = sumProb + a_discretepdf.ProbabilityGet(state);
275  }
276  CPPUNIT_ASSERT_EQUAL(1.0, sumProb);
277 
278  // set all probabilities one by one
279  for (int new_el_c = 0; new_el_c < NUM_DS; new_el_c++)
280  {
281  CPPUNIT_ASSERT_EQUAL(true, a_discretepdf.ProbabilitySet(new_el_c,prob_new));
282  CPPUNIT_ASSERT_EQUAL(prob_new, (double) a_discretepdf.ProbabilityGet(new_el_c));
283  }
284  // check if the sum of the probabilities is still one!
285  sumProb = 0.0;
286  for (int state = 0; state < NUM_DS ; state++)
287  {
288  sumProb = sumProb + a_discretepdf.ProbabilityGet(state);
289  }
290  CPPUNIT_ASSERT_EQUAL(1.0, sumProb);
291 
292  // all at the same time
293  vector<Probability> prob_vec(NUM_DS);
294  prob_vec[0] = 0.9;
295  for (int state = 1; state < NUM_DS ; state++)
296  {
297  prob_vec[state] = (Probability)( (1-0.9)/(NUM_DS-1) );
298  }
299  a_discretepdf.ProbabilitiesSet(prob_vec);
300  result_proba = a_discretepdf.ProbabilitiesGet();
301  for (int state = 0; state < NUM_DS ; state++) CPPUNIT_ASSERT_EQUAL( (double) (prob_vec[state]), double(result_proba[state] )) ;
302 
303  // check if the sum of the probabilities is still one!
304  sumProb = 0.0;
305  for (int state = 0; state < NUM_DS ; state++)
306  {
307  sumProb = sumProb + a_discretepdf.ProbabilityGet(state);
308  }
309  CPPUNIT_ASSERT_EQUAL(1.0, sumProb);
310 
311  // special case for setting probability : set a new value for a probability
312  // which was == 1
313  DiscretePdf c_discretepdf(NUM_DS);
314  vector<Probability> prob_vecc(NUM_DS);
315  prob_vecc[0] = 1.0;
316  for (int state = 1; state < NUM_DS ; state++)
317  {
318  prob_vecc[state] = 0.0;
319  }
320  c_discretepdf.ProbabilitiesSet(prob_vecc);
321  Probability new_prob0 = 0.5;
322  double new_prob_other = (1-new_prob0)/(NUM_DS-1);
323  c_discretepdf.ProbabilitySet(0,new_prob0);
324  CPPUNIT_ASSERT_EQUAL((double)new_prob0, (double)(c_discretepdf.ProbabilityGet(0)));
325  for (int state = 1; state < NUM_DS ; state++)
326  {
327  CPPUNIT_ASSERT_EQUAL( new_prob_other, (double)(c_discretepdf.ProbabilityGet(state)));
328  }
329 
330  /* Sampling */
331  DiscretePdf d_discretepdf(NUM_DS);
332  vector<Probability> prob_vecd(NUM_DS);
333  prob_vecd[0] = 0.1;
334  prob_vecd[1] = 0.2;
335  prob_vecd[2] = 0.35;
336  prob_vecd[3] = 0.05;
337  prob_vecd[4] = 0.3;
338  d_discretepdf.ProbabilitiesSet(prob_vecd);
339  vector<Sample<int> > los(NUM_SAMPLES);
340  vector<Sample<int> >::iterator it;
341 
342  // check most probable state
343  CPPUNIT_ASSERT_EQUAL( 2, d_discretepdf.MostProbableStateGet());
344 
345  CPPUNIT_ASSERT_EQUAL( true, d_discretepdf.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
346  // test if samples are distributed according to probabilities
347  // remark that this test occasionally fails
348  vector<unsigned int> num_samples(NUM_DS,0);
349  for (it = los.begin(); it!=los.end();it++)
350  {
351  num_samples[it->ValueGet()] +=1;
352  }
353  for (int i = 0 ; i< NUM_DS ; i++)
354  {
355  //TODO: find some theoretical limits depending on the number of samples
356  CPPUNIT_ASSERT( approxEqual( prob_vecd[i] , (double)num_samples[i] / NUM_SAMPLES, 0.05 ) );
357  }
358 
359  // Ripley
360  CPPUNIT_ASSERT_EQUAL( true, d_discretepdf.SampleFrom(los,NUM_SAMPLES,RIPLEY,NULL));
361  // test if samples are distributed according to probabilities
362  // remark that this test occasionally fails
363  vector<unsigned int> num_samples2(NUM_DS,0);
364  for (it = los.begin(); it!=los.end();it++)
365  {
366  num_samples2[it->ValueGet()] +=1;
367  }
368  for (int i = 0 ; i< NUM_DS ; i++)
369  {
370  //TODO: find some theoretical limits depending on the number of samples
371  CPPUNIT_ASSERT( approxEqual( prob_vecd[i] , (double)num_samples2[i] / NUM_SAMPLES, 0.05 ) );
372  }
373 
374  prob_vecd[0] = 0.0;
375  prob_vecd[1] = 0.0;
376  prob_vecd[2] = 1.0;
377  prob_vecd[3] = 0.0;
378  prob_vecd[4] = 0.0;
379  d_discretepdf.ProbabilitiesSet(prob_vecd);
380 
381  // check most probable state
382  CPPUNIT_ASSERT_EQUAL( 2, d_discretepdf.MostProbableStateGet());
383 
384  CPPUNIT_ASSERT_EQUAL( true, d_discretepdf.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
385  // test if samples are distributed according to probabilities
386  // remark that this test occasionally fails
387  vector<unsigned int> num_samples3(NUM_DS,0);
388  for (it = los.begin(); it!=los.end();it++)
389  {
390  num_samples3[it->ValueGet()] +=1;
391  }
392  for (int i = 0 ; i< NUM_DS ; i++)
393  {
394  //TODO: find some theoretical limits depending on the number of samples
395  CPPUNIT_ASSERT( approxEqual( prob_vecd[i] , (double)num_samples3[i] / NUM_SAMPLES, 0.05 ) );
396  }
397 
398  // Ripley
399  CPPUNIT_ASSERT_EQUAL( true, d_discretepdf.SampleFrom(los,NUM_SAMPLES,RIPLEY,NULL));
400  // test if samples are distributed according to probabilities
401  // remark that this test occasionally fails
402  vector<unsigned int> num_samples4(NUM_DS,0);
403  for (it = los.begin(); it!=los.end();it++)
404  {
405  num_samples4[it->ValueGet()] +=1;
406  }
407  for (int i = 0 ; i< NUM_DS ; i++)
408  {
409  //TODO: find some theoretical limits depending on the number of samples
410  CPPUNIT_ASSERT( approxEqual( prob_vecd[i] , (double)num_samples4[i] / NUM_SAMPLES, 0.05 ) );
411  }
412 
413  Sample<int> a_sample ;
414  CPPUNIT_ASSERT_EQUAL( true, d_discretepdf.SampleFrom(a_sample,DEFAULT,NULL));
415  // Ripley not implemented for one sample
416  CPPUNIT_ASSERT_EQUAL( false, d_discretepdf.SampleFrom(a_sample,RIPLEY,NULL));
417 
418  /* Clone */
419  DiscretePdf* clone = NULL;
420  clone = d_discretepdf.Clone();
421  CPPUNIT_ASSERT_EQUAL( d_discretepdf.NumStatesGet(), clone->NumStatesGet());
422  vector<Probability> result_probd = d_discretepdf.ProbabilitiesGet();
423  vector<Probability> result_probClone = clone->ProbabilitiesGet();
424  for( int i = 0; i< d_discretepdf.NumStatesGet() ; i++)
425  {
426  CPPUNIT_ASSERT_EQUAL( (double)result_probd[i],(double)result_probClone[i]);
427  }
428 }
429 
430 void
432 {
433  //creating additive gaussian noise
434  Gaussian noise(_mu,_sigma);
435 
436  // Conditional gaussian with NUM_CONDITIONAL_ARGS conditional args and state vector of
437  // dimension DIMENSION
438  Matrix a(DIMENSION,DIMENSION); a = 1.0;
439  Matrix b(DIMENSION,DIMENSION); b = 1.0;
440  vector<Matrix> v(2); v[0] = a; v[1] = b;
441 
442  LinearAnalyticConditionalGaussian a_condgaussian(v, noise);
443 
444  /* Dimension check*/
445  CPPUNIT_ASSERT_EQUAL( NUM_COND_ARGS, a_condgaussian.NumConditionalArgumentsGet());
446 
447  /* Matrix Check */
448  for (unsigned int i=0; i < NUM_COND_ARGS; i++)
449  {
450  CPPUNIT_ASSERT_EQUAL( v[i], a_condgaussian.MatrixGet(i));
451  }
452 
453  /* Getting mean/covariance of the additive noise*/
454  CPPUNIT_ASSERT_EQUAL( _mu, a_condgaussian.AdditiveNoiseMuGet());
455  CPPUNIT_ASSERT_EQUAL( _sigma, a_condgaussian.AdditiveNoiseSigmaGet());
456 
457  /* Setting and getting the conditional args; one at a time */
458  ColumnVector x2(DIMENSION); x2(1) = 1.0; x2(DIMENSION) = 0.5;
459  ColumnVector u2(DIMENSION_INPUT); u2(1) = 1.0; u2(DIMENSION_INPUT) = 0.5;
460 
461  a_condgaussian.ConditionalArgumentSet(0,x2);
462  a_condgaussian.ConditionalArgumentSet(1,u2);
463 
464  CPPUNIT_ASSERT_EQUAL( DIMENSION, a_condgaussian.DimensionGet());
465  CPPUNIT_ASSERT_EQUAL( x2, a_condgaussian.ConditionalArgumentGet(0));
466  CPPUNIT_ASSERT_EQUAL( u2, a_condgaussian.ConditionalArgumentGet(1));
467 
468 
469  /* Setting and getting the conditional args; all together */
470  ColumnVector x(DIMENSION); x(1) = 1.0; x(DIMENSION) = 0.5;
471  ColumnVector u(DIMENSION_INPUT); u(1) = 1.0; u(DIMENSION_INPUT) = 0.5;
472 
473  std::vector<ColumnVector> cond_args(NUM_COND_ARGS);
474  cond_args[0] = x;
475  cond_args[NUM_COND_ARGS-1] = u;
476 
477  a_condgaussian.ConditionalArgumentsSet(cond_args);
478 
479  CPPUNIT_ASSERT_EQUAL( DIMENSION, a_condgaussian.DimensionGet());
480  for (unsigned int i=0; i < NUM_COND_ARGS; i++)
481  {
482  CPPUNIT_ASSERT_EQUAL( cond_args[i], a_condgaussian.ConditionalArgumentsGet()[i]);
483  }
484 
485  /* Sampling */
486  vector<Sample<ColumnVector> > los(NUM_SAMPLES);
487  CPPUNIT_ASSERT_EQUAL( true, a_condgaussian.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
488  // Box-Muller is not implemented yet
489  CPPUNIT_ASSERT_EQUAL( false, a_condgaussian.SampleFrom(los,NUM_SAMPLES,BOXMULLER,NULL));
490  CPPUNIT_ASSERT_EQUAL( true, a_condgaussian.SampleFrom(los,NUM_SAMPLES,CHOLESKY,NULL));
491  Sample<ColumnVector> a_sample ;
492  CPPUNIT_ASSERT_EQUAL( true, a_condgaussian.SampleFrom(a_sample,DEFAULT,NULL));
493  // Box-Muller is not implemented yet
494  CPPUNIT_ASSERT_EQUAL( false, a_condgaussian.SampleFrom(a_sample,BOXMULLER,NULL));
495  CPPUNIT_ASSERT_EQUAL( true, a_condgaussian.SampleFrom(a_sample,CHOLESKY,NULL));
496 
497  /* Test dfGet */
498  for (unsigned int i=0; i < NUM_COND_ARGS; i++)
499  {
500  CPPUNIT_ASSERT_EQUAL( v[i], a_condgaussian.dfGet(i));
501  }
502 
503  /* Setting and Getting mean/covariance */
504  // calculate expected value
505  ColumnVector exp(DIMENSION); exp = 0.0;
506  for (unsigned int i=0; i < NUM_COND_ARGS; i++)
507  {
508  exp = exp + v[i]*cond_args[i];
509  }
510  exp += _mu;
511  CPPUNIT_ASSERT_EQUAL( exp, a_condgaussian.ExpectedValueGet());
512  CPPUNIT_ASSERT_EQUAL( _sigma, a_condgaussian.CovarianceGet());
513 
514  /* Copy Constructor etc */
515  LinearAnalyticConditionalGaussian b_condgaussian(a_condgaussian);
516  CPPUNIT_ASSERT_EQUAL( _mu, b_condgaussian.AdditiveNoiseMuGet());
517  CPPUNIT_ASSERT_EQUAL( _sigma, b_condgaussian.AdditiveNoiseSigmaGet());
518 
519  /* Setting and getting matrices */
520  Matrix a2(DIMENSION,DIMENSION); a2 = 2.1;
521  Matrix b2(DIMENSION,DIMENSION); b2 = 1.5;
522 
523  a_condgaussian.MatrixSet(0,a2);
524  a_condgaussian.MatrixSet(1,b2);
525 
526  CPPUNIT_ASSERT_EQUAL( a2, a_condgaussian.MatrixGet(0));
527  CPPUNIT_ASSERT_EQUAL( b2, a_condgaussian.MatrixGet(1));
528 
529  /* Setting and Getting mean/covariance of the additive noise*/
530  ColumnVector mu2;
531  SymmetricMatrix sigma2;
532  mu2.resize(DIMENSION);
533  mu2(1) = MU_1; mu2(2) = MU_0;
534  sigma2.resize(DIMENSION);
535  sigma2 = 0.0;
536  double sig2 = 0.05;
537  for (unsigned int rows=1; rows < DIMENSION + 1; rows++){ sigma2(rows,rows)=sig2; }
538 
539  a_condgaussian.AdditiveNoiseMuSet(mu2);
540  a_condgaussian.AdditiveNoiseSigmaSet(sigma2);
541 
542  CPPUNIT_ASSERT_EQUAL( mu2, a_condgaussian.AdditiveNoiseMuGet());
543  CPPUNIT_ASSERT_EQUAL( sigma2, a_condgaussian.AdditiveNoiseSigmaGet());
544 
545  /* TODO: What would be the best way to test ProbabilityGet? */
546 
547  /* Clone */
548  LinearAnalyticConditionalGaussian* clone = NULL;
549  clone = a_condgaussian.Clone();
550  CPPUNIT_ASSERT_EQUAL( a_condgaussian.AdditiveNoiseMuGet(), clone->AdditiveNoiseMuGet());
551  CPPUNIT_ASSERT_EQUAL( a_condgaussian.AdditiveNoiseSigmaGet(), clone->AdditiveNoiseSigmaGet());
552  CPPUNIT_ASSERT_EQUAL( a_condgaussian.MatrixGet(0), clone->MatrixGet(0));
553  CPPUNIT_ASSERT_EQUAL( a_condgaussian.MatrixGet(1), clone->MatrixGet(1));
554 }
555 
556 void
558 {
559  int cond_arg_dims[NUM_COND_ARGS_DIS] = { NUM_DS };
560  int state_k;int state_kMinusOne;
561 
562  DiscreteConditionalPdf a_discretecondpdf(NUM_DS,NUM_COND_ARGS_DIS,cond_arg_dims);
563 
564  /* Get the dimension */
565  CPPUNIT_ASSERT_EQUAL( 1, (int)a_discretecondpdf.DimensionGet());
566 
567  /* Get the dimension */
568  CPPUNIT_ASSERT_EQUAL( NUM_DS, (int)a_discretecondpdf.NumStatesGet());
569 
570  /* Get number of conditional arguments */
571  CPPUNIT_ASSERT_EQUAL( NUM_COND_ARGS_DIS, (int)a_discretecondpdf.NumConditionalArgumentsGet());
572 
573  std::vector<int> cond_args(NUM_COND_ARGS_DIS);
574  /* Set and Get all Probabilities*/
575  double prob_diag = 0.9;
576  double prob_nondiag = (1-0.9)/(NUM_DS-1);
577  for (state_kMinusOne = 0 ; state_kMinusOne < NUM_DS ; state_kMinusOne++)
578  {
579  cond_args[0] = state_kMinusOne;
580  for (state_k = 0 ; state_k < NUM_DS ; state_k++)
581  {
582  if (state_kMinusOne == state_k) a_discretecondpdf.ProbabilitySet(prob_diag,state_k,cond_args);
583  else a_discretecondpdf.ProbabilitySet(prob_nondiag,state_k,cond_args);
584  }
585  }
586 
587  /* Set and Get Conditional Arguments */
588  int cond_arg = NUM_DS - 1;
589  a_discretecondpdf.ConditionalArgumentSet(0, cond_arg);
590  CPPUNIT_ASSERT_EQUAL( cond_arg, a_discretecondpdf.ConditionalArgumentGet(0));
591 
592  /* Get the probability for the states given the conditional argument set */
593  for (cond_arg = 0 ; cond_arg < NUM_DS ; cond_arg++)
594  {
595  a_discretecondpdf.ConditionalArgumentSet(0, cond_arg);
596  for (state_k = 0 ; state_k < NUM_DS ; state_k++)
597  {
598  if( state_k == cond_arg) CPPUNIT_ASSERT_EQUAL( prob_diag, (double)a_discretecondpdf.ProbabilityGet(state_k));
599  else CPPUNIT_ASSERT_EQUAL( prob_nondiag, (double)a_discretecondpdf.ProbabilityGet(state_k));
600  }
601  }
602 
603  /* Sampling */
604  vector<Sample<int> > los(NUM_SAMPLES);
605  CPPUNIT_ASSERT_EQUAL( true, a_discretecondpdf.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
606  Sample<int> a_sample ;
607  CPPUNIT_ASSERT_EQUAL( true, a_discretecondpdf.SampleFrom(a_sample,DEFAULT,NULL));
608 
609  /* Copy */
610  DiscreteConditionalPdf b_discretecondpdf(a_discretecondpdf);
611  CPPUNIT_ASSERT_EQUAL( a_discretecondpdf.DimensionGet(),b_discretecondpdf.DimensionGet() );
612  CPPUNIT_ASSERT_EQUAL( a_discretecondpdf.NumConditionalArgumentsGet(), b_discretecondpdf.NumConditionalArgumentsGet());
613  CPPUNIT_ASSERT_EQUAL( a_discretecondpdf.NumStatesGet(),b_discretecondpdf.NumStatesGet());
614  for (cond_arg = 0 ; cond_arg < NUM_DS ; cond_arg++)
615  {
616  a_discretecondpdf.ConditionalArgumentSet(0, cond_arg);
617  b_discretecondpdf.ConditionalArgumentSet(0, cond_arg);
618  for (state_k = 0 ; state_k < NUM_DS ; state_k++)
619  {
620  CPPUNIT_ASSERT_EQUAL( (double)a_discretecondpdf.ProbabilityGet(state_k) , (double)b_discretecondpdf.ProbabilityGet(state_k));
621  }
622  }
623 
624  /* Clone */
625  DiscreteConditionalPdf* clone = NULL;
626  clone = a_discretecondpdf.Clone();
627  CPPUNIT_ASSERT_EQUAL( a_discretecondpdf.DimensionGet(), clone->DimensionGet());
628  CPPUNIT_ASSERT_EQUAL( a_discretecondpdf.NumConditionalArgumentsGet(), clone->NumConditionalArgumentsGet());
629  CPPUNIT_ASSERT_EQUAL( a_discretecondpdf.NumStatesGet(),clone->NumStatesGet());
630  for (cond_arg = 0 ; cond_arg < NUM_DS ; cond_arg++)
631  {
632  a_discretecondpdf.ConditionalArgumentSet(0, cond_arg);
633  clone->ConditionalArgumentSet(0, cond_arg);
634  for (state_k = 0 ; state_k < NUM_DS ; state_k++)
635  CPPUNIT_ASSERT_EQUAL( (double)a_discretecondpdf.ProbabilityGet(state_k) , (double)clone->ProbabilityGet(state_k));
636  }
637 }
638 
639 void
641 {
642  /* Set and Get Dimension and number of samples*/
644  CPPUNIT_ASSERT_EQUAL( DIMENSION, a_mcpdf.DimensionGet());
645  CPPUNIT_ASSERT_EQUAL( NUM_SAMPLES , a_mcpdf.NumSamplesGet());
646 
647  // Generating (exact) Samples from a Gaussian density with
648  // cholesky sampling
649  Gaussian gaussian(_mu,_sigma);
650 
651  vector<Sample<ColumnVector> > exact_samples(NUM_SAMPLES);
652  vector<Sample<ColumnVector> >::iterator it;
653  gaussian.SampleFrom(exact_samples, NUM_SAMPLES,CHOLESKY,NULL);
654 
655  /* Getting and setting the list of samples (non-weighted)*/
656  a_mcpdf.ListOfSamplesSet(exact_samples);
657  const vector<WeightedSample<ColumnVector> > mcpdf_samples = a_mcpdf.ListOfSamplesGet();
658  for (unsigned int i = 0; i < NUM_SAMPLES ; i++)
659  {
660  CPPUNIT_ASSERT_EQUAL( exact_samples[i].ValueGet(), mcpdf_samples[i].ValueGet());
661  CPPUNIT_ASSERT_EQUAL( exact_samples[i].ValueGet(), a_mcpdf.SampleGet(i).ValueGet());
662  CPPUNIT_ASSERT_EQUAL( 1.0/NUM_SAMPLES, mcpdf_samples[i].WeightGet());
663  }
664 
665  /* List of samples update + getting and setting the list of samples (weighted)*/
666  vector<WeightedSample<ColumnVector> > samples_weighted = mcpdf_samples;
667  for (unsigned int i = 0 ; i < NUM_SAMPLES ; i++)
668  {
669  //set a weight
670  samples_weighted[i].WeightSet(i+1);
671  }
672  double tot_weight = (double)(NUM_SAMPLES+1)*((double)NUM_SAMPLES)/2.0;
673  CPPUNIT_ASSERT_EQUAL( true , a_mcpdf.ListOfSamplesUpdate(samples_weighted) );
674  for (unsigned int i = 0; i < NUM_SAMPLES ; i++)
675  {
676  CPPUNIT_ASSERT_EQUAL( samples_weighted[i].ValueGet(), a_mcpdf.SampleGet(i).ValueGet());
677  CPPUNIT_ASSERT_EQUAL( (double)(samples_weighted[i].WeightGet())/tot_weight, a_mcpdf.SampleGet(i).WeightGet());
678  }
679 
680  /* Copy Constructor etc */
681  MCPdf<ColumnVector> b_mcpdf(a_mcpdf);
682  for (unsigned int i = 0; i < NUM_SAMPLES ; i++)
683  {
684  CPPUNIT_ASSERT_EQUAL( a_mcpdf.SampleGet(i).ValueGet(), b_mcpdf.SampleGet(i).ValueGet());
685  CPPUNIT_ASSERT_EQUAL( a_mcpdf.SampleGet(i).WeightGet(), b_mcpdf.SampleGet(i).WeightGet());
686  }
687 
688  /* Sampling */
689  vector<Sample<ColumnVector> > samples_test(NUM_SAMPLES);
690  CPPUNIT_ASSERT_EQUAL( true, a_mcpdf.SampleFrom(samples_test,NUM_SAMPLES,RIPLEY,NULL));
691  CPPUNIT_ASSERT_EQUAL( true, a_mcpdf.SampleFrom(samples_test,NUM_SAMPLES,DEFAULT,NULL));
692 
693  /* Expected Value*/
694  vector<WeightedSample<ColumnVector> > los = a_mcpdf.ListOfSamplesGet();
695  vector<WeightedSample<ColumnVector> >::iterator it2;
696  ColumnVector cumSum(DIMENSION);
697  cumSum=0.0;
698  double sumWeights = 0.0;
699  for ( it2 = los.begin() ; it2!= los.end() ; it2++ )
700  {
701  cumSum += ( it2->ValueGet() * it2->WeightGet() );
702  sumWeights += it2->WeightGet();
703  }
704  CPPUNIT_ASSERT_EQUAL( approxEqual(cumSum/sumWeights, a_mcpdf.ExpectedValueGet(), epsilon),true);
705 
706  /* Covariance + cumsumPDF*/
707  ColumnVector mean(a_mcpdf.ExpectedValueGet());
708  ColumnVector diff(DIMENSION); // Temporary storage
709  Matrix diffsum(DIMENSION, DIMENSION);
710  diffsum = 0.0;
711  vector<double> cumPDF = a_mcpdf.CumulativePDFGet();
712  vector<double>::iterator cumPDFit;
713  cumPDFit = cumPDF.begin(); *cumPDFit = 0.0;
714  double cumSumW = 0.0;
715  for (it2 = los.begin(); it2 != los.end(); it2++)
716  {
717  diff = (it2->ValueGet() - mean);
718  diffsum += diff * (diff.transpose() * it2->WeightGet());
719  cumPDFit++;
720  cumSumW += ( it2->WeightGet() / sumWeights);
721  // test cumulative sum
722  CPPUNIT_ASSERT_EQUAL(approxEqual(cumSumW, *cumPDFit, epsilon), true);
723  }
724  CPPUNIT_ASSERT_EQUAL(approxEqual(diffsum/sumWeights, (Matrix)a_mcpdf.CovarianceGet(),epsilon),true);
725 
726  /* ProbabilityGet */
727 
728  /**************************
729  // MCPDF with unsigned int
730  *************************/
731 
732  /* Set and Get Dimension and number of samples*/
733  MCPdf<unsigned int> a_mcpdf_uint(NUM_SAMPLES,1);
734  unsigned int one = 1;
735  CPPUNIT_ASSERT_EQUAL(one, a_mcpdf_uint.DimensionGet());
736  CPPUNIT_ASSERT_EQUAL( NUM_SAMPLES , a_mcpdf_uint.NumSamplesGet());
737 
738 
739  // Generating (exact) Samples from a discrete pdf
740  unsigned int num_states = 10;
741  DiscretePdf discrete(num_states);
742 
743  vector<Sample<int> > samples_discrete_int(NUM_SAMPLES);
744  vector<Sample<int> >::iterator it_discrete_int;
745  discrete.SampleFrom(samples_discrete_int, NUM_SAMPLES);
746 
747  vector<Sample<unsigned int> > samples_discrete(NUM_SAMPLES);
748  vector<Sample<unsigned int> >::iterator it_discrete;
749  it_discrete = samples_discrete.begin();
750 
751  Sample<unsigned int> temp_sample;
752  for(it_discrete_int = samples_discrete_int.begin(); it_discrete_int != samples_discrete_int.end(); it_discrete_int++)
753  {
754  temp_sample.ValueSet((*it_discrete_int).ValueGet());
755  (*it_discrete)= temp_sample;
756  it_discrete++;
757 }
758 
759  /* Getting and setting the list of samples (non-weighted)*/
760  a_mcpdf_uint.ListOfSamplesSet(samples_discrete);
761  const vector<WeightedSample<unsigned int> > mcpdf_samples_uint = a_mcpdf_uint.ListOfSamplesGet();
762  for (unsigned int i = 0; i < NUM_SAMPLES ; i++)
763  {
764  CPPUNIT_ASSERT_EQUAL( samples_discrete[i].ValueGet(), mcpdf_samples_uint[i].ValueGet());
765  CPPUNIT_ASSERT_EQUAL( samples_discrete[i].ValueGet(), a_mcpdf_uint.SampleGet(i).ValueGet());
766  CPPUNIT_ASSERT_EQUAL( 1.0/NUM_SAMPLES, mcpdf_samples_uint[i].WeightGet());
767  }
768 
769  /* List of samples update + getting and setting the list of samples (weighted)*/
770  vector<WeightedSample<unsigned int > > samples_weighted_uint = mcpdf_samples_uint;
771  for (unsigned int i = 0 ; i < NUM_SAMPLES ; i++)
772  {
773  //set a weight
774  samples_weighted_uint[i].WeightSet(i+1);
775  }
776  double tot_weight_uint = (double)(NUM_SAMPLES+1)*((double)NUM_SAMPLES)/2.0;
777  CPPUNIT_ASSERT_EQUAL( true , a_mcpdf_uint.ListOfSamplesUpdate(samples_weighted_uint) );
778  for (unsigned int i = 0; i < NUM_SAMPLES ; i++)
779  {
780  CPPUNIT_ASSERT_EQUAL( samples_weighted_uint[i].ValueGet(), a_mcpdf_uint.SampleGet(i).ValueGet());
781  CPPUNIT_ASSERT_EQUAL( (double)(samples_weighted_uint[i].WeightGet())/tot_weight_uint, a_mcpdf_uint.SampleGet(i).WeightGet());
782  }
783 
785  MCPdf<unsigned int> b_mcpdf_uint(a_mcpdf_uint);
786  for (unsigned int i = 0; i < NUM_SAMPLES ; i++)
787  {
788  CPPUNIT_ASSERT_EQUAL( a_mcpdf_uint.SampleGet(i).ValueGet(), b_mcpdf_uint.SampleGet(i).ValueGet());
789  CPPUNIT_ASSERT_EQUAL( a_mcpdf_uint.SampleGet(i).WeightGet(), b_mcpdf_uint.SampleGet(i).WeightGet());
790  }
791 
792  /* Sampling */
793  vector<Sample<unsigned int> > samples_test_uint(NUM_SAMPLES);
794  CPPUNIT_ASSERT_EQUAL( true, a_mcpdf_uint.SampleFrom(samples_test_uint,NUM_SAMPLES,DEFAULT,NULL));
795 
796  /* Expected Value*/
797  vector<WeightedSample<unsigned int> > los_uint = a_mcpdf_uint.ListOfSamplesGet();
798  vector<WeightedSample<unsigned int> >::iterator it2_uint;
799  double cumSum_double;
800  cumSum_double=0.0;
801  sumWeights = 0.0;
802  for ( it2_uint = los_uint.begin() ; it2_uint!= los_uint.end() ; it2_uint++ )
803  {
804  cumSum_double += ( (double)it2_uint->ValueGet() * it2_uint->WeightGet() );
805  sumWeights += it2_uint->WeightGet();
806  }
807  CPPUNIT_ASSERT_EQUAL( approxEqual( (unsigned int ) (cumSum_double/sumWeights + 0.5) , a_mcpdf_uint.ExpectedValueGet(), epsilon),true);
808 
809  /* Covariance + cumsumPDF*/
810  unsigned int mean_uint = a_mcpdf_uint.ExpectedValueGet();
811  unsigned int diff_uint;
812  double diffsum_uint;
813  diffsum_uint = 0.0;
814  vector<double> cumPDF_uint = a_mcpdf_uint.CumulativePDFGet();
815  vector<double>::iterator cumPDFit_uint;
816  cumPDFit_uint = cumPDF_uint.begin(); *cumPDFit_uint = 0.0;
817  double cumSumW_uint = 0.0;
818  for (it2_uint = los_uint.begin(); it2_uint != los_uint.end(); it2_uint++)
819  {
820  diff_uint = (it2_uint->ValueGet() - mean_uint);
821  diffsum_uint += (double)(diff_uint * diff_uint) * it2_uint->WeightGet();
822  cumPDFit_uint++;
823  cumSumW_uint += ( it2_uint->WeightGet() / sumWeights);
824  // test cumulative sum
825  CPPUNIT_ASSERT_EQUAL(approxEqual(cumSumW_uint, *cumPDFit_uint, epsilon), true);
826  }
827  Matrix test_diff(1,1);
828  test_diff(1,1) = diffsum_uint/sumWeights;
829  CPPUNIT_ASSERT_EQUAL(approxEqual(test_diff, (Matrix)a_mcpdf_uint.CovarianceGet(),epsilon),true);
830  /* ProbabilityGet */
831 }
832 
833 
834 
835 class MyType
836 {
837 public:
838  // empty constructor
839  MyType() {};
840 
841  // unsigned int constructor
842  //MyType(unsigned int dim) {};
843 
844  ~MyType() {};
845 };
846 
847 
848 void
850 {
852 }
853 
854 void
856 {
857  /*******************************
858  A) TEMPlATE = COLUMNVECTOR
859  *******************************/
860 
861  // Constructor with DIMENSION
863  ColumnVector cv1(DIMENSION,0.0);
864  cv1 = 0.0;
865  Sample<ColumnVector> sample ;
866  vector<Sample<ColumnVector> > los(NUM_SAMPLES);
867 
868  CPPUNIT_ASSERT_EQUAL( DIMENSION, mixture.DimensionGet() );
869  CPPUNIT_ASSERT_EQUAL( 0, (int)mixture.NumComponentsGet() );
870 
871  // all these calls results in assertions since numComponents = 0
872  /*
873  (double)mixture.ProbabilityGet(cv1) ;
874  mixture.SampleFrom(sample) ;
875  mixture.SampleFrom(los,NUM_SAMPLES) ;
876  mixture.SampleFrom(los,NUM_SAMPLES,RIPLEY) ;
877  mixture.ExpectedValueGet() ;
878  mixture.WeightsGet() ;
879  mixture.WeightGet(0) ;
880  vector<Probability> weightVec(1);
881  weightVec[0]=Probability(1.0);
882  mixture.WeightsSet(weightVec) ;
883  mixture.WeightSet(0,Probability(1.0)) ;
884  mixture.MostProbableComponentGet();
885  */
886 
887  Gaussian comp1(_mu,_sigma);
888  comp1.ExpectedValueSet(_mu);
889  comp1.CovarianceSet(_sigma);
890  CPPUNIT_ASSERT_EQUAL(true, mixture.AddComponent(comp1));
891  CPPUNIT_ASSERT_EQUAL(1.0, (double)mixture.WeightGet(0));
892 
893  //one component tests
894  CPPUNIT_ASSERT_EQUAL( DIMENSION, mixture.DimensionGet() );
895  CPPUNIT_ASSERT_EQUAL( 1, (int)mixture.NumComponentsGet() );
896  CPPUNIT_ASSERT_EQUAL((double)comp1.ProbabilityGet(cv1),(double)mixture.ProbabilityGet(cv1)) ;
897  ColumnVector expected_mix = mixture.ExpectedValueGet() ;
898  ColumnVector expected_comp1 = comp1.ExpectedValueGet() ;
899  for(int j = 1 ; j <= DIMENSION; j++)
900  CPPUNIT_ASSERT_EQUAL(expected_comp1(j), expected_mix(j) ) ;
901  vector<Probability> vecW(1);
902  vecW[0]=1.0;
903  vector<Probability> mixWeights = mixture.WeightsGet() ;
904  for(int j = 1 ; j <= mixture.NumComponentsGet(); j++)
905  CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
906  CPPUNIT_ASSERT_EQUAL(1.0,(double)mixture.WeightGet(0) ) ;
907  vecW[0]=Probability(1.0);
908  mixture.WeightsSet(vecW) ;
909  for(int j = 1 ; j <= mixture.NumComponentsGet(); j++)
910  CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
911  mixture.WeightSet(0,Probability(1.0)) ;
912  CPPUNIT_ASSERT_EQUAL(1.0 , (double)mixture.WeightGet(0) ) ;
913  CPPUNIT_ASSERT_EQUAL(0,(int)mixture.MostProbableComponentGet() );
914 
915 
916  // sampling with one component test
917  CPPUNIT_ASSERT_EQUAL( true, mixture.SampleFrom(sample,DEFAULT,NULL));
918  // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
919  // erf(4/sqrt(2)) for each sample
920  for(int j = 1 ; j <= DIMENSION; j++)
921  {
922  CPPUNIT_ASSERT( (sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
923  CPPUNIT_ASSERT( (sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
924  }
925  // Box-Muller and Choleskyis not implemented yet
926  CPPUNIT_ASSERT_EQUAL( false, mixture.SampleFrom(sample,BOXMULLER,NULL));
927  CPPUNIT_ASSERT_EQUAL( false, mixture.SampleFrom(sample,CHOLESKY,NULL));
928 
929  // list of samples
930  CPPUNIT_ASSERT_EQUAL( true, mixture.SampleFrom(los,NUM_SAMPLES,DEFAULT,NULL));
931  // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
932  // erf(4/sqrt(2)) for each sample
933  ColumnVector sample_mean(DIMENSION);
934  sample_mean = 0.0;
935  for(int i = 0 ; i < NUM_SAMPLES; i++)
936  {
937  sample = los[i];
938  sample_mean += sample.ValueGet();
939  for(int j = 1 ; j <= DIMENSION; j++)
940  {
941  CPPUNIT_ASSERT( (sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
942  CPPUNIT_ASSERT( (sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
943  }
944  }
945  // check whether mean and of samples is in withing the expected 3 sigma border
946  // 3 sigma test : REMARK: this test WILL occasionaly fail with probability
947  // erf(3/sqrt(2)) for each sample
948  sample_mean = sample_mean / (double)NUM_SAMPLES;
949  for(int j = 1 ; j <= DIMENSION; j++)
950  {
951  CPPUNIT_ASSERT( sample_mean(j) <= _mu(j) + 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
952  CPPUNIT_ASSERT( sample_mean(j) >= _mu(j) - 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
953  }
954  // Box-Muller and cholesky is not implemented
955  CPPUNIT_ASSERT_EQUAL( false, mixture.SampleFrom(los,NUM_SAMPLES,BOXMULLER,NULL));
956  CPPUNIT_ASSERT_EQUAL( false, mixture.SampleFrom(los,NUM_SAMPLES,CHOLESKY,NULL));
957  CPPUNIT_ASSERT_EQUAL( true, mixture.SampleFrom(los,NUM_SAMPLES,RIPLEY,NULL));
958  // 4 sigma test : REMARK: this test WILL occasionaly fail with probability
959  // erf(4/sqrt(2)) for each sample
960  sample_mean = 0.0;
961  for(int i = 0 ; i < NUM_SAMPLES; i++)
962  {
963  sample = los[i];
964  sample_mean += sample.ValueGet();
965  for(int j = 1 ; j <= DIMENSION; j++)
966  {
967  CPPUNIT_ASSERT( (sample.ValueGet())(j) > _mu(j) - 4.0 * sqrt(_sigma(j,j) ) );
968  CPPUNIT_ASSERT( (sample.ValueGet())(j) < _mu(j) + 4.0 * sqrt(_sigma(j,j) ) );
969  }
970  }
971  // check whether mean and of samples is in withing the expected 3 sigma border
972  // 3 sigma test : REMARK: this test WILL occasionaly fail with probability
973  // erf(3/sqrt(2)) for each sample
974  sample_mean = sample_mean / (double)NUM_SAMPLES;
975  for(int j = 1 ; j <= DIMENSION; j++)
976  {
977  CPPUNIT_ASSERT( sample_mean(j) <= _mu(j) + 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
978  CPPUNIT_ASSERT( sample_mean(j) >= _mu(j) - 3.0 * 1/sqrt(NUM_SAMPLES) * sqrt(_sigma(j,j)) );
979  }
980 
981  // DELETING COMPONENT
982  CPPUNIT_ASSERT_EQUAL(true, mixture.DeleteComponent(0));
983 
984  // One component with weight w1
985  Gaussian comp2(_mu,_sigma);
987  Probability w1 = 0.5;
988  CPPUNIT_ASSERT_EQUAL(true, mixture1.AddComponent(comp2,w1));
989  CPPUNIT_ASSERT_EQUAL(1.0, (double)mixture1.WeightGet(0));
990  CPPUNIT_ASSERT_EQUAL( DIMENSION, mixture1.DimensionGet() );
991  CPPUNIT_ASSERT_EQUAL( 1, (int)mixture1.NumComponentsGet() );
992  CPPUNIT_ASSERT_EQUAL((double)comp2.ProbabilityGet(cv1),(double)mixture1.ProbabilityGet(cv1)) ;
993 
994  //AddComponent (default weight addef = 0)
995  ColumnVector mu3(DIMENSION);
996  mu3(1) =1.2; mu3(2) = 1.5;
997  SymmetricMatrix sigma3(DIMENSION);
998  sigma3 = 0.0;
999  for (unsigned int rows=1; rows < DIMENSION + 1; rows++){ sigma3(rows,rows)=2.3; }
1000  Gaussian comp3(mu3,sigma3);
1001  CPPUNIT_ASSERT_EQUAL(true, mixture1.AddComponent(comp3));
1002  CPPUNIT_ASSERT_EQUAL(1.0, (double)mixture1.WeightGet(0));
1003  CPPUNIT_ASSERT_EQUAL(0.0, (double)mixture1.WeightGet(1));
1004  CPPUNIT_ASSERT_EQUAL( DIMENSION, mixture1.DimensionGet() );
1005  CPPUNIT_ASSERT_EQUAL( 2, (int)mixture1.NumComponentsGet() );
1006  CPPUNIT_ASSERT_EQUAL((double)comp2.ProbabilityGet(cv1),(double)mixture1.ProbabilityGet(cv1)) ;
1007  vecW.resize(mixture1.NumComponentsGet());
1008  vecW[0]=1.0;
1009  vecW[1]=0.0;
1010  mixWeights = mixture1.WeightsGet() ;
1011  for(int j = 1 ; j <= mixture1.NumComponentsGet(); j++)
1012  CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
1013  //WeightSet and WeightGet
1014  CPPUNIT_ASSERT_EQUAL(true, mixture1.WeightSet(1,0.2));
1015  vecW[0]=0.8;
1016  vecW[1]=0.2;
1017  mixWeights = mixture1.WeightsGet() ;
1018  for(int j = 1 ; j <= mixture1.NumComponentsGet(); j++)
1019  CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
1020  //WeightsSet (with non-normalized vector of Probabilities) and WeightsGet
1021  vecW[0]=0.4;
1022  vecW[1]=1.6;
1023  CPPUNIT_ASSERT_EQUAL(true, mixture1.WeightsSet(vecW));
1024  mixWeights = mixture1.WeightsGet() ;
1025  double sumVecW = 0.0;
1026  for(int j = 0 ; j < vecW.size(); j++)
1027  sumVecW += (double)vecW[j];
1028  for(int j = 0 ; j < vecW.size(); j++)
1029  vecW[j] = (double)vecW[j]/sumVecW;
1030  for(int j = 1 ; j <= mixture1.NumComponentsGet(); j++)
1031  CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
1032  //ProbabilityGet
1033  Probability prob = 0.0;
1034  prob = vecW[0] * comp2.ProbabilityGet(cv1) + vecW[1] * comp3.ProbabilityGet(cv1);
1035  CPPUNIT_ASSERT_EQUAL((double)prob,(double)mixture1.ProbabilityGet(cv1)) ;
1036  //ExpectedValueGet
1037  expected_mix = mixture1.ExpectedValueGet() ;
1038  ColumnVector expected_true = comp2.ExpectedValueGet() * (double)vecW[0] + comp3.ExpectedValueGet() * (double)vecW[1];
1039  for(int j = 1 ; j <= DIMENSION; j++)
1040  CPPUNIT_ASSERT_EQUAL(expected_true(j), expected_mix(j) ) ;
1041  //MostProbableComponentGet
1042  CPPUNIT_ASSERT_EQUAL(1,mixture1.MostProbableComponentGet());
1043  vecW[0]=0.8;
1044  vecW[1]=0.2;
1045  CPPUNIT_ASSERT_EQUAL(true, mixture1.WeightsSet(vecW));
1046  CPPUNIT_ASSERT_EQUAL(0,mixture1.MostProbableComponentGet());
1047  vecW[0]=0.5;
1048  vecW[1]=0.5;
1049  CPPUNIT_ASSERT_EQUAL(true, mixture1.WeightsSet(vecW));
1050  CPPUNIT_ASSERT_EQUAL(0,mixture1.MostProbableComponentGet());
1051 
1052  // DELETING COMPONENT
1053  CPPUNIT_ASSERT_EQUAL(true, mixture1.DeleteComponent(1));
1054  CPPUNIT_ASSERT_EQUAL( 1, (int)mixture1.NumComponentsGet() );
1055  CPPUNIT_ASSERT_EQUAL(1.0, (double)mixture1.WeightGet(0));
1056  CPPUNIT_ASSERT_EQUAL( DIMENSION, mixture1.DimensionGet() );
1057  CPPUNIT_ASSERT_EQUAL((double)comp2.ProbabilityGet(cv1),(double)mixture1.ProbabilityGet(cv1)) ;
1058 
1059  // CONSTRUCTOR WITH VECTOR OF PDFS
1060  //AddComponent (default weight addef = 0)
1061  ColumnVector muVec1(DIMENSION);
1062  ColumnVector muVec2(DIMENSION);
1063  muVec1(1) =1.2; muVec1(2) = 1.5;
1064  muVec2(1) =0.2; muVec2(2) = -1.1;
1065  SymmetricMatrix sigmaVec1(DIMENSION);
1066  SymmetricMatrix sigmaVec2(DIMENSION);
1067  sigmaVec1 = 0.0;
1068  sigmaVec2 = 0.0;
1069  double sigmaValVec1 = 2.3;
1070  double sigmaValVec2 = 0.3;
1071  for (unsigned int rows=1; rows < DIMENSION + 1; rows++){
1072  sigmaVec1(rows,rows)=sigmaValVec1;
1073  sigmaVec2(rows,rows)=sigmaValVec2;
1074  }
1075 
1076  Gaussian compVec1(muVec1,sigmaVec1);
1077  Gaussian compVec2(muVec2,sigmaVec2);
1078 
1079  //vector<Pdf<ColumnVector>*> componentVec(2);
1080  vector<Gaussian*> componentVec(2);
1081  componentVec[0] = &compVec1;
1082  componentVec[1] = &compVec2;
1083 
1084  Mixture<ColumnVector> mixtureVec(componentVec);
1085 
1086  CPPUNIT_ASSERT_EQUAL(0.5, (double)mixtureVec.WeightGet(0));
1087  CPPUNIT_ASSERT_EQUAL(0.5, (double)mixtureVec.WeightGet(1));
1088  vecW[0]=0.5;
1089  vecW[1]=0.5;
1090  mixWeights = mixtureVec.WeightsGet() ;
1091  for(int j = 1 ; j <= mixtureVec.NumComponentsGet(); j++)
1092  CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
1093  CPPUNIT_ASSERT_EQUAL( DIMENSION, mixtureVec.DimensionGet() );
1094  CPPUNIT_ASSERT_EQUAL( 2, (int)mixtureVec.NumComponentsGet() );
1095  for(int j = 1 ; j <= mixtureVec.NumComponentsGet(); j++)
1096  CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
1097  vecW.resize(mixtureVec.NumComponentsGet());
1098  vecW[0]=1.0;
1099  vecW[1]=0.0;
1100  mixWeights = mixtureVec.WeightsGet() ;
1101  ColumnVector expectedComp(DIMENSION);
1102  ColumnVector expectedMix(DIMENSION);
1103  for(int j = 1 ; j <= mixtureVec.NumComponentsGet(); j++)
1104  {
1105  expectedComp = componentVec[j-1]->ExpectedValueGet();
1106  expectedMix = mixtureVec.ComponentGet(j-1)->ExpectedValueGet();
1107  for(int i = 1 ; i <= DIMENSION; i++)
1108  CPPUNIT_ASSERT_EQUAL(expectedComp(i), expectedMix(i) ) ;
1109  }
1110  //WeightSet and WeightGet
1111  vecW[0]=0.8;
1112  vecW[1]=0.2;
1113  CPPUNIT_ASSERT_EQUAL(true, mixtureVec.WeightsSet(vecW));
1114  mixWeights = mixtureVec.WeightsGet() ;
1115  for(int j = 1 ; j <= mixtureVec.NumComponentsGet(); j++)
1116  CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
1117  //WeightsSet (with non-normalized vector of Probabilities) and WeightsGet
1118  vecW[0]=0.4;
1119  vecW[1]=1.6;
1120  CPPUNIT_ASSERT_EQUAL(true, mixtureVec.WeightsSet(vecW));
1121  mixWeights = mixtureVec.WeightsGet() ;
1122  sumVecW = 0.0;
1123  for(int j = 0 ; j < vecW.size(); j++)
1124  sumVecW += (double)vecW[j];
1125  for(int j = 0 ; j < vecW.size(); j++)
1126  vecW[j] = (double)vecW[j]/sumVecW;
1127  for(int j = 1 ; j <= mixtureVec.NumComponentsGet(); j++)
1128  CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
1129  //ProbabilityGet
1130  prob = 0.0;
1131  for(int j = 1 ; j <= componentVec.size();j++)
1132  {
1133  prob = prob + vecW[j-1] * componentVec[j-1]->ProbabilityGet(cv1) ;
1134  }
1135  CPPUNIT_ASSERT_EQUAL((double)prob,(double)mixtureVec.ProbabilityGet(cv1)) ;
1136  //ExpectedValueGet
1137  expectedMix = mixtureVec.ExpectedValueGet() ;
1138  expectedComp = 0.0;
1139  for(int j = 1 ; j <= componentVec.size();j++)
1140  expectedComp = expectedComp + componentVec[j-1]->ExpectedValueGet() * (double)vecW[j-1] ;
1141  for(int j = 1 ; j <= DIMENSION; j++)
1142  CPPUNIT_ASSERT_EQUAL(expectedComp(j), expectedMix(j) ) ;
1143  //MostProbableComponentGet
1144  CPPUNIT_ASSERT_EQUAL(1,mixtureVec.MostProbableComponentGet());
1145  vecW[0]=0.5;
1146  vecW[1]=0.5;
1147  CPPUNIT_ASSERT_EQUAL(true, mixtureVec.WeightsSet(vecW));
1148  CPPUNIT_ASSERT_EQUAL(0,mixtureVec.MostProbableComponentGet());
1149 
1150  /*******************************
1151  B) TEMPlATE = INT
1152  *******************************/
1153  // One component with weight w1
1154  DiscretePdf disc1(NUM_DS);
1155  vector<Probability> probs1(NUM_DS);
1156  disc1.ProbabilitySet(1,0.8);
1157 
1158  unsigned int dim_int = 1;
1159  Mixture<int> mixture_int(dim_int);
1160  w1 = 0.5;
1161  CPPUNIT_ASSERT_EQUAL(true, mixture_int.AddComponent(disc1,w1));
1162  CPPUNIT_ASSERT_EQUAL(1.0, (double)mixture_int.WeightGet(0));
1163  CPPUNIT_ASSERT_EQUAL( 1, (int)mixture_int.NumComponentsGet() );
1164  for(int state = 0; state<NUM_DS ; state++)
1165  CPPUNIT_ASSERT_EQUAL((double)disc1.ProbabilityGet(state),(double)mixture_int.ProbabilityGet(state)) ;
1166 
1167  //AddComponent (default weight addef = 0)
1168  DiscretePdf disc2(NUM_DS);
1169  vector<Probability> probs2(2);
1170  disc2.ProbabilitySet(2,0.5);
1171 
1172  CPPUNIT_ASSERT_EQUAL(true, mixture_int.AddComponent(disc2));
1173  CPPUNIT_ASSERT_EQUAL(1.0, (double)mixture_int.WeightGet(0));
1174  CPPUNIT_ASSERT_EQUAL(0.0, (double)mixture_int.WeightGet(1));
1175  CPPUNIT_ASSERT_EQUAL( 1, (int)mixture_int.DimensionGet() );
1176  CPPUNIT_ASSERT_EQUAL( 2, (int)mixture_int.NumComponentsGet() );
1177  for(int state = 0; state<NUM_DS ; state++)
1178  CPPUNIT_ASSERT_EQUAL((double)disc1.ProbabilityGet(state),(double)mixture_int.ProbabilityGet(state)) ;
1179  vecW.resize(mixture_int.NumComponentsGet());
1180  vecW[0]=1.0;
1181  vecW[1]=0.0;
1182  mixWeights = mixture_int.WeightsGet() ;
1183  for(int j = 1 ; j <= mixture_int.NumComponentsGet(); j++)
1184  CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
1185  //WeightSet and WeightGet
1186  CPPUNIT_ASSERT_EQUAL(true, mixture_int.WeightSet(1,0.2));
1187  vecW[0]=0.8;
1188  vecW[1]=0.2;
1189  mixWeights = mixture_int.WeightsGet() ;
1190  for(int j = 1 ; j <= mixture_int.NumComponentsGet(); j++)
1191  CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
1192  //WeightsSet (with non-normalized vector of Probabilities) and WeightsGet
1193  vecW[0]=0.4;
1194  vecW[1]=1.6;
1195  CPPUNIT_ASSERT_EQUAL(true, mixture_int.WeightsSet(vecW));
1196  mixWeights = mixture_int.WeightsGet() ;
1197  sumVecW = 0.0;
1198  for(int j = 0 ; j < vecW.size(); j++)
1199  sumVecW += (double)vecW[j];
1200  for(int j = 0 ; j < vecW.size(); j++)
1201  vecW[j] = (double)vecW[j]/sumVecW;
1202  for(int j = 1 ; j <= mixture_int.NumComponentsGet(); j++)
1203  CPPUNIT_ASSERT_EQUAL( (double)vecW[j-1], (double)mixWeights[j-1] );
1204  //ProbabilityGet
1205  for(int state = 0; state<NUM_DS ; state++)
1206  {
1207  prob = vecW[0] * disc1.ProbabilityGet(state) + vecW[1] * disc2.ProbabilityGet(state);
1208  CPPUNIT_ASSERT_EQUAL((double)prob,(double)mixture_int.ProbabilityGet(state)) ;
1209  }
1210  //ExpectedValueGet
1211  probs1 = disc1.ProbabilitiesGet();
1212  probs2 = disc2.ProbabilitiesGet();
1213  vector<Probability> probs(probs1.size());
1214  // search for most probable state
1215  int mostProbState = -1;
1216  double probMostProbState = -1.0;
1217  double probState = 0.0;
1218  for( int i = 0 ; i<probs.size() ; i++)
1219  {
1220  probState = (double)(probs1[i]) * (double)vecW[0] + (double)probs2[i] * (double) vecW[1];
1221  if(probState > probMostProbState)
1222  {
1223  probMostProbState = probState;
1224  mostProbState = i;
1225  }
1226  }
1227  CPPUNIT_ASSERT_EQUAL(mostProbState ,mixture_int.ExpectedValueGet());
1228  //MostProbableComponentGet
1229  CPPUNIT_ASSERT_EQUAL(1,mixture_int.MostProbableComponentGet());
1230  vecW[0]=0.8;
1231  vecW[1]=0.2;
1232  CPPUNIT_ASSERT_EQUAL(true, mixture_int.WeightsSet(vecW));
1233  CPPUNIT_ASSERT_EQUAL(0,mixture_int.MostProbableComponentGet());
1234  vecW[0]=0.5;
1235  vecW[1]=0.5;
1236  CPPUNIT_ASSERT_EQUAL(true, mixture_int.WeightsSet(vecW));
1237  CPPUNIT_ASSERT_EQUAL(0,mixture_int.MostProbableComponentGet());
1238  }
virtual MatrixWrapper::Matrix dfGet(unsigned int i) const
returns derivative from function to n-th conditional variable
Class representing a PDF on a discrete variable.
Definition: discretepdf.h:34
void testUniform()
Definition: pdf_test.cpp:173
virtual MatrixWrapper::ColumnVector ExpectedValueGet() const
Get the expected value E[x] of the pdf.
Definition: gaussian.cpp:181
T ExpectedValueGet() const
Get the expected value E[x] of the pdf.
void ExpectedValueSet(const MatrixWrapper::ColumnVector &mu)
Set the Expected Value.
Definition: gaussian.cpp:193
#define NUM_DS
Definition: pdf_test.cpp:40
bool SampleFrom(vector< Sample< MatrixWrapper::ColumnVector > > &list_samples, const int num_samples, int method=DEFAULT, void *args=NULL) const
bool WeightSet(unsigned int componentNumber, Probability w)
Function to change/set the weigth of a single component.
double epsilon
Probability ProbabilityGet(const int &state) const
Implementation of virtual base class method.
#define BOXMULLER
virtual Gaussian * Clone() const
Clone function.
Definition: gaussian.cpp:70
bool SampleFrom(vector< Sample< MatrixWrapper::ColumnVector > > &list_samples, const int num_samples, int method=DEFAULT, void *args=NULL) const
#define DEFAULT
void MatrixSet(unsigned int i, const MatrixWrapper::Matrix &m)
Set the i-th Matrix for calculation of .
Class representing uniform density.
Definition: uniform.h:26
virtual bool SampleFrom(Sample< int > &one_sample, int method, void *args) const
Draw 1 sample from the Pdf:
#define NUM_COND_ARGS_DIS
Definition: pdf_test.cpp:42
bool ProbabilitiesSet(vector< Probability > &values)
Set all probabilities.
const MatrixWrapper::Matrix & MatrixGet(unsigned int i) const
Get the i-th matrix of the system.
bool ProbabilitySet(int state, Probability a)
Function to change/set the probability of a single state.
Class representing Gaussian (or normal density)
Definition: gaussian.h:27
#define MU_1
Definition: pdf_test.cpp:28
const MatrixWrapper::ColumnVector & AdditiveNoiseMuGet() const
Get the mean Value of the Additive Gaussian uncertainty.
virtual MatrixWrapper::ColumnVector WidthGet() const
Get the Width of the uniform distribution.
Definition: uniform.cpp:123
unsigned int NumStatesGet() const
Get the number of discrete States.
void testMixture()
Definition: pdf_test.cpp:855
bool ListOfSamplesUpdate(const vector< WeightedSample< T > > &list_of_samples)
Update the list of samples (overloaded)
void ValueSet(const T &value)
Set the value of the Sample.
#define WIDTH_1
Definition: pdf_test.cpp:34
vector< Probability > WeightsGet() const
Get all component weights.
void tearDown()
Definition: pdf_test.cpp:60
virtual bool SampleFrom(Sample< MatrixWrapper::ColumnVector > &sample, int method=DEFAULT, void *args=NULL) const
virtual void ConditionalArgumentsSet(std::vector< CondArg > ConditionalArguments)
Set the whole list of conditional arguments.
const WeightedSample< T > & SampleGet(unsigned int i) const
Get one sample.
virtual void ConditionalArgumentSet(unsigned int n_argument, const CondArg &argument)
Set the n-th argument of the list.
bool WeightsSet(vector< Probability > &weights)
Set all component weights.
unsigned int NumComponentsGet() const
Get the number of components.
virtual MatrixWrapper::SymmetricMatrix CovarianceGet() const
Get the Covariance Matrix E[(x - E[x])^2] of the Analytic pdf.
#define SIGMA
Definition: pdf_test.cpp:31
virtual MatrixWrapper::ColumnVector CenterGet() const
Get the center of the uniform.
Definition: uniform.cpp:117
bool AddComponent(Pdf< T > &pdf)
Add a component pdf: THIS IS A NON-REALTIME OPERATION.
const MatrixWrapper::SymmetricMatrix & AdditiveNoiseSigmaGet() const
Get the covariance matrix of the Additive Gaussian uncertainty.
vector< double > & CumulativePDFGet()
Add a sample to the list.
vector< Probability > ProbabilitiesGet() const
Get all probabilities.
Class representing a mixture of PDFs, the mixture can contain different.
Definition: mixture.h:48
T ExpectedValueGet() const
Get the expected value E[x] of the pdf.
virtual Uniform * Clone() const
Clone function.
Definition: uniform.cpp:62
void AdditiveNoiseMuSet(const MatrixWrapper::ColumnVector &mu)
Set the mean Value of the Additive Gaussian uncertainty.
int MostProbableStateGet()
Get the index of the most probable state.
Probability ProbabilityGet(const int &input) const
Get the probability of a certain argument.
void UniformSet(const MatrixWrapper::ColumnVector &center, const MatrixWrapper::ColumnVector &width)
Set the center and width of the uniform.
Definition: uniform.cpp:129
const unsigned int NUM_COND_ARGS
Definition: pdf_test.cpp:35
#define WIDTH_0
Definition: pdf_test.cpp:33
const unsigned int DIMENSION
Definition: pdf_test.cpp:36
bool SampleFrom(vector< Sample< T > > &list_samples, const unsigned int num_samples, int method=DEFAULT, void *args=NULL) const
Monte Carlo Pdf: Sample based implementation of Pdf.
Definition: mcpdf.h:49
unsigned int NumConditionalArgumentsGet() const
Get the Number of conditional arguments.
const CondArg & ConditionalArgumentGet(unsigned int n_argument) const
Get the n-th argument of the list.
void testLinearAnalyticConditionalGaussian()
Definition: pdf_test.cpp:431
#define MU_0
Definition: pdf_test.cpp:27
T & ValueGet()
Get the value of the Sample.
Abstract Class representing all FULLY Discrete Conditional PDF&#39;s.
#define RIPLEY
#define CHOLESKY
int MostProbableComponentGet() const
Get the index of the most probable component, if a few component are.
CPPUNIT_TEST_SUITE_REGISTRATION(PdfTest)
virtual DiscretePdf * Clone() const
Clone function.
Probability ProbabilityGet(const T &state) const
Implementation of virtual base class method.
virtual Probability ProbabilityGet(const MatrixWrapper::ColumnVector &input) const
Get the probability of a certain argument.
Definition: uniform.cpp:67
double WeightGet() const
Get the weight.
MyType()
Definition: pdf_test.cpp:839
MatrixWrapper::SymmetricMatrix CovarianceGet() const
Get the Covariance Matrix E[(x - E[x])^2] of the Analytic pdf.
const unsigned int NUM_SAMPLES
Definition: pdf_test.cpp:37
~MyType()
Definition: pdf_test.cpp:844
void CovarianceSet(const MatrixWrapper::SymmetricMatrix &cov)
Set the Covariance Matrix.
Definition: gaussian.cpp:204
void testMcpdfType()
Definition: pdf_test.cpp:849
Probability WeightGet(unsigned int componentNumber) const
Get the component weight of component "componentNumber".
Class representing a probability (a double between 0 and 1)
Definition: bfl_constants.h:39
bool SampleFrom(Sample< T > &one_sample, int method=DEFAULT, void *args=NULL) const
virtual Probability ProbabilityGet(const MatrixWrapper::ColumnVector &input) const
Get the probability of a certain argument.
Definition: gaussian.cpp:75
const vector< WeightedSample< T > > & ListOfSamplesGet() const
Get the list of weighted samples.
void setUp()
Definition: pdf_test.cpp:45
void testMcpdf()
Definition: pdf_test.cpp:640
unsigned int NumSamplesGet() const
Get number of samples.
void testDiscreteConditionalPdf()
Definition: pdf_test.cpp:557
unsigned int DimensionGet() const
Get the dimension of the argument.
Pdf< T > * ComponentGet(unsigned int componentNumber) const
Get the pointer to the component pdf of component "componentNumber".
bool DeleteComponent(unsigned int componentNumber)
Delete a component pdf: THIS IS A NON_REALTIME OPERATION.
virtual MatrixWrapper::ColumnVector ExpectedValueGet() const
Get the expected value E[x] of the pdf.
void testGaussian()
Definition: pdf_test.cpp:65
virtual LinearAnalyticConditionalGaussian * Clone() const
Clone function.
bool approxEqual(double a, double b, double epsilon)
Definition: approxEqual.cpp:20
bool ListOfSamplesSet(const vector< WeightedSample< T > > &list_of_samples)
Set the list of weighted samples.
void AdditiveNoiseSigmaSet(const MatrixWrapper::SymmetricMatrix &sigma)
Set the covariance of the Additive Gaussian uncertainty.
virtual DiscreteConditionalPdf * Clone() const
Clone function.
void testDiscretePdf()
Definition: pdf_test.cpp:241
virtual T ExpectedValueGet() const
Get the expected value E[x] of the pdf.
T & ValueGet()
Get the value of the Sample.
Definition: asirfilter.h:132
bool SampleFrom(vector< Sample< int > > &list_samples, const unsigned int num_samples, int method=DEFAULT, void *args=NULL) const
void ProbabilitySet(const double &prob, const int &input, const std::vector< int > &condargs) const
Set the probability (Typical for discrete Pdf&#39;s)
const std::vector< CondArg > & ConditionalArgumentsGet() const
Get the whole list of conditional arguments.
unsigned int NumStatesGet() const
Get the number of discrete states.
#define DIMENSION_INPUT
Definition: pdf_test.cpp:26


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 Mon Jun 10 2019 12:47:59