35 #include "../wrappers/matrix/vector_wrapper.h" 36 #include "../wrappers/matrix/matrix_wrapper.h" 37 #include "../wrappers/rng/rng.h" 79 Mixture(
const unsigned int dimension=0);
84 template <
typename U>
Mixture(
const U &componentVector);
103 const unsigned int num_samples,
105 void * args = NULL)
const;
128 bool WeightsSet(vector<Probability> & weights);
192 #ifdef __CONSTRUCTOR__ 193 cout <<
"Mixture constructor\n";
194 #endif // __CONSTRUCTOR__ 198 template<
typename T>
template <
typename U>
218 (*_componentPdfs)[i] = (componentVector[i])->
Clone();
220 #ifdef __CONSTRUCTOR__ 221 cout <<
"Mixture constructor\n";
222 #endif // __CONSTRUCTOR__ 225 template<
typename T >
231 (*_componentWeights) = my_mixture.
WeightsGet();
241 #ifdef __CONSTRUCTOR__ 242 cout <<
"Mixture copy constructor\n";
243 #endif // __CONSTRUCTOR__ 249 #ifdef __CONSTRUCTOR__ 250 cout <<
"Mixture destructor\n";
256 delete (*_componentPdfs)[i];
280 prob= prob + (*_componentWeights)[i] * (*_componentPdfs)[i]->ProbabilityGet(state);
287 const unsigned int num_samples,
299 list_samples.resize(num_samples);
301 std::vector<double> unif_samples(num_samples);
302 for (
unsigned int i = 0; i < num_samples ; i++)
303 unif_samples[i] =
runif();
306 unif_samples[num_samples-1] = pow(unif_samples[num_samples-1],
307 double (1.0/num_samples));
309 for (
int i = num_samples-2; i >= 0 ; i--)
310 unif_samples[i] = pow(unif_samples[i],
double (1.0/(i+1))) * unif_samples[i+1];
313 unsigned int index = 0;
315 vector<double>::const_iterator CumPDFit =
_cumWeights.begin();
316 typename vector<Sample<T> >::iterator sit = list_samples.begin();
318 for (
unsigned int i = 0; i < num_samples ; i++)
320 while ( unif_samples[i] > *CumPDFit )
323 assert(index <= num_states);
328 (*_componentPdfs)[index-1]->SampleFrom(*sit,method,args);
334 cerr <<
"Mixture::Samplefrom(T, void *): No such sampling method" << endl;
347 double unif_sample; unif_sample =
runif();
349 unsigned int index = 0;
357 (*_componentPdfs)[index-1]->SampleFrom(one_sample,method,args);
361 cerr <<
"Mixture::Samplefrom(T, void *): No such sampling method" 370 cerr <<
"Mixture ExpectedValueGet: not implemented for the template parameters you use." 371 << endl <<
"Use template specialization as shown in mixture.cpp " << endl;
374 return expectedValue;
377 template <
typename T>
381 cerr <<
"Mixture CovarianceGet: not implemented since so far I don't believe its usefull" 382 << endl <<
"If you decide to implement is: Use template specialization as shown in mcpdf.cpp " << endl;
385 MatrixWrapper::SymmetricMatrix result;
401 return (*_componentWeights)[componentNumber];
409 *_componentWeights = weights;
419 assert((
double)weight<=1.0);
423 (*_componentWeights)[0] = weight;
431 if ((
double)old_weight!=1.0) {
432 double normalization_factor = (1-weight)/(1-old_weight);
435 (*_componentWeights)[i] = (
Probability)( (
double)( (*_componentWeights)[i] )* normalization_factor);
444 (*_componentWeights)[componentNumber] = weight;
453 int index_mostProbable= -1;
457 if ( (*_componentWeights)[component] > prob_mostProbable)
459 index_mostProbable= component;
460 prob_mostProbable= (*_componentWeights)[component];
463 return index_mostProbable;
474 (*_componentPdfs).push_back(pdf.
Clone() );
494 (*_componentPdfs).push_back(pdf.
Clone() );
517 Pdf<T>* pointer = (*_componentPdfs)[componentNumber];
519 (*_componentPdfs).erase((*_componentPdfs).begin()+componentNumber);
520 (*_componentWeights).erase((*_componentWeights).begin()+componentNumber);
551 cerr <<
"Mixture method called which requires that the number of components is not zero" 560 double SumOfWeights = 0.0;
562 SumOfWeights += (*_componentWeights)[i];
564 if (SumOfWeights > 0){
566 (*_componentWeights)[i] = (
Probability)( (
double) ( (*_componentWeights)[i]) /SumOfWeights);
571 cerr <<
"Mixture::NormalizeProbs(): SumOfWeights = " << SumOfWeights << endl;
581 static vector<double>::iterator CumWeightsit;
590 CumSum += ( (*_componentWeights)[i] );
591 *CumWeightsit = CumSum;
bool WeightSet(unsigned int componentNumber, Probability w)
Function to change/set the weigth of a single component.
int MostProbableComponentGet() const
Get the index of the most probable component, if a few component are.
Class PDF: Virtual Base class representing Probability Density Functions.
Probability WeightGet(unsigned int componentNumber) const
Get the component weight of component "componentNumber".
Mixture(const unsigned int dimension=0)
Constructor: An equal weight is set for all components.
vector< Probability > WeightsGet() const
Get all component weights.
MatrixWrapper::SymmetricMatrix CovarianceGet() const
Get the Covariance Matrix E[(x - E[x])^2] of the Analytic pdf.
vector< Pdf< T > *> * _componentPdfs
Pointer to the vector of component pdfs.
bool WeightsSet(vector< Probability > &weights)
Set all component weights.
bool AddComponent(Pdf< T > &pdf)
Add a component pdf: THIS IS A NON-REALTIME OPERATION.
void TestNotInit() const
Called when a the number of components=0 and if method is called which.
unsigned int _numComponents
The number of components.
vector< double > _cumWeights
Vector containing the cumulative component weights (for efficient sampling)
Class representing a mixture of PDFs, the mixture can contain different.
bool CumWeightsUpdate()
Updates the cumWeights.
bool SampleFrom(vector< Sample< T > > &list_samples, const unsigned int num_samples, int method=DEFAULT, void *args=NULL) const
unsigned int NumComponentsGet() const
Get the number of components.
virtual Pdf< T > * Clone() const =0
Pure virtual clone function.
Probability ProbabilityGet(const T &state) const
Implementation of virtual base class method.
unsigned int DimensionGet() const
Get the dimension of the argument.
virtual bool SampleFrom(vector< Sample< T > > &list_samples, const unsigned int num_samples, int method=DEFAULT, void *args=NULL) const
Draw multiple samples from the Pdf (overloaded)
vector< Probability > * _componentWeights
Pointer to the vector of mixture weights, the sum of the elements = 1.
virtual Mixture * Clone() const
Clone function.
Class representing a probability (a double between 0 and 1)
Pdf< T > * ComponentGet(unsigned int componentNumber) const
Get the pointer to the component pdf of component "componentNumber".
#define NUMERIC_PRECISION
bool NormalizeWeights()
Normalize the component weigths (eg. after setting a component weight)
bool DeleteComponent(unsigned int componentNumber)
Delete a component pdf: THIS IS A NON_REALTIME OPERATION.
virtual ~Mixture()
Destructor.
T ExpectedValueGet() const
Get the expected value E[x] of the pdf.
vector< Pdf< T > *> ComponentsGet() const
Get the vector of pointers to the component pdfs.