19 #include "../bfl_err.h" 20 #include "../wrappers/rng/rng.h" 32 ,_num_states(num_states)
35 _Values_p =
new vector<Probability>(num_states);
42 #ifdef __CONSTRUCTOR__ 43 cout <<
"DiscretePdf constructor\n";
44 #endif // __CONSTRUCTOR__ 54 #ifdef __CONSTRUCTOR__ 55 cout <<
"DiscretePdf copy constructor\n";
56 #endif // __CONSTRUCTOR__ 61 #ifdef __CONSTRUCTOR__ 62 cout <<
"DiscretePdf destructor\n";
95 if (old_prob_state!=1) {
96 double normalization_factor = (1-a)/(1-old_prob_state);
99 (*_Values_p)[i] = (
Probability)( (
double)( (*_Values_p)[i] )* normalization_factor);
108 (*_Values_p)[state] = a;
129 const unsigned int num_samples,
140 list_samples.resize(num_samples);
142 std::vector<double> unif_samples(num_samples);
143 for (
unsigned int i = 0; i < num_samples ; i++)
144 unif_samples[i] =
runif();
147 unif_samples[num_samples-1] = pow(unif_samples[num_samples-1],
148 double (1.0/num_samples));
150 for (
int i = num_samples-2; i >= 0 ; i--)
151 unif_samples[i] = pow(unif_samples[i],
double (1.0/(i+1))) * unif_samples[i+1];
154 unsigned int index = 0;
156 vector<double>::const_iterator CumPDFit =
_CumPDF.begin();
157 vector<Sample<int> >::iterator sit = list_samples.begin();
159 for (
unsigned int i = 0; i < num_samples ; i++)
161 while ( unif_samples[i] > *CumPDFit )
164 assert(index <= num_states);
174 cerr <<
"DiscretePdf::Samplefrom(int, void *): No such sampling method" << endl;
188 double unif_sample; unif_sample =
runif();
190 unsigned int index = 0;
191 while ( unif_sample >
_CumPDF[index] )
201 cerr <<
"DiscretePdf::Samplefrom(int, void *): No such sampling method" 209 double SumOfProbs = 0.0;
211 SumOfProbs += (*_Values_p)[i];
215 (*_Values_p)[i] = (
Probability)( (
double) ( (*_Values_p)[i]) /SumOfProbs);
220 cerr <<
"DiscretePdf::NormalizeProbs(): SumOfProbs = " << SumOfProbs << endl;
229 static vector<double>::iterator CumPDFit;
238 CumSum += ( (*_Values_p)[i] );
252 int index_mostProbableState = -1;
256 if ( (*
_Values_p)[state] >= prob_mostProbableState)
258 index_mostProbableState = state;
259 prob_mostProbableState = (*_Values_p)[state];
262 return index_mostProbableState;
virtual ~DiscretePdf()
Destructor.
Class representing a probability (a double between 0 and 1)
bool ProbabilitiesSet(vector< Probability > &values)
Set all probabilities.
virtual DiscretePdf * Clone() const
Clone function.
bool ProbabilitySet(int state, Probability a)
Function to change/set the probability of a single state.
unsigned int _num_states
The number of discrete state.
void ValueSet(const T &value)
Set the value of the Sample.
bool CumPDFUpdate()
Updates the cumPDF.
vector< Probability > * _Values_p
Pointer to the discrete PDF-values, the sum of the elements = 1.
Class PDF: Virtual Base class representing Probability Density Functions.
int MostProbableStateGet()
Get the index of the most probable state.
bool SampleFrom(vector< Sample< int > > &list_samples, const unsigned int num_samples, int method=DEFAULT, void *args=NULL) const
vector< double > _CumPDF
STL-vector containing the Cumulative PDF (for efficient sampling)
Class representing a PDF on a discrete variable.
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)
Probability ProbabilityGet(const int &state) const
Implementation of virtual base class method.
unsigned int NumStatesGet() const
Get the number of discrete States.
vector< Probability > ProbabilitiesGet() const
Get all probabilities.
#define NUMERIC_PRECISION
bool NormalizeProbs()
Normalize all the probabilities (eg. after setting a probability)
DiscretePdf(unsigned int num_states=0)
Constructor (dimension = number of classes) An equal probability is set for all classes.
vector< Probability > ProbabilitiesGet() const
Get all probabilities.