2 #ifndef vpdl_mixture_h_ 3 #define vpdl_mixture_h_ 20 # include <vcl_msvc_warnings.h> 31 template<
class T,
unsigned int n=0>
79 template <
class comp_type_>
102 for (
unsigned int i=0; i<
components_.size(); ++i) {
110 for (
unsigned int i=0; i<
components_.size(); ++i) {
119 for (
unsigned int i=0; i<
components_.size(); ++i) {
123 for (
unsigned int i=0; i<rhs.
components_.size(); ++i) {
180 components_.push_back(
new component(d, wght));
197 typedef typename std::vector<component*>::const_iterator comp_itr;
201 dens += (*i)->weight * (*i)->distribution->prob_density(pt);
209 typedef typename std::vector<component*>::const_iterator comp_itr;
213 prob += (*i)->weight * (*i)->distribution->prob_density(pt);
214 sum_w += (*i)->weight;
216 assert(sum_w > T(0));
226 typedef typename std::vector<component*>::const_iterator comp_itr;
227 const unsigned int d = this->
dimension();
233 T w_i = (*i)->distribution->norm_const() * (*i)->weight;
234 dens += w_i * (*i)->distribution->gradient_density(pt,g_i);
244 typedef typename std::vector<component*>::const_iterator comp_itr;
248 prob += (*i)->weight * (*i)->distribution->box_prob(min_pt,max_pt);
249 sum_w += (*i)->weight;
251 assert(sum_w > T(0));
260 typedef typename std::vector<component*>::const_iterator comp_itr;
264 prob += (*i)->weight * (*i)->distribution->cumulative_prob(pt);
265 sum_w += (*i)->weight;
267 assert(sum_w > T(0));
275 const unsigned int d = this->
dimension();
279 typedef typename std::vector<component*>::const_iterator comp_itr;
283 (*i)->distribution->compute_mean(cmp_mean);
284 cmp_mean *= (*i)->weight;
285 sum_w += (*i)->weight;
295 const unsigned int d = this->
dimension();
302 typedef typename std::vector<component*>::const_iterator comp_itr;
307 const T& wgt = (*i)->weight;
308 (*i)->distribution->compute_covar(cmp_covar);
309 (*i)->distribution->compute_mean(cmp_mean);
327 typedef typename std::vector<component*>::const_iterator comp_itr;
338 typedef typename std::vector<component*>::iterator comp_itr;
341 (*i)->weight *= norm;
348 void sort(
unsigned int idx1,
unsigned int idx2)
358 template <
class comp_type_>
367 template <
class comp_type_>
368 void sort(comp_type_ comp,
unsigned int idx1,
unsigned int idx2)
377 #endif // vpdl_mixture_h_ vpdt_field_traits< field_type >::matrix_type matrix
the data type used for matrices.
sort_adaptor(comp_type_ c)
vpdt_field_traits< vector >::matrix_type matrix
the data type used for matrices.
T prob_density(const vector &pt) const
Compute the probability density at this point.
base class for multiple component distributions
virtual vpdl_distribution< T, n > * clone() const
Create a copy on the heap and return base class pointer.
vpdl_mixture< T, n > & operator=(vpdl_mixture< T, n > const &rhs)
Assignment operator.
bool operator()(const component *const c1, const component *const c2) const
void sort(unsigned int idx1, unsigned int idx2)
Sort the components in the range idx1 to idx2 in order of decreasing weight.
virtual T norm_const() const
The normalization constant for the density.
virtual T gradient_density(const vector &pt, vector &g) const
Compute the gradient of the unnormalized density at a point.
virtual void compute_mean(vector &mean) const
Compute the mean of the distribution.
virtual T cumulative_prob(const vector &pt) const
Evaluate the cumulative distribution function at a point.
bool operator<(const component &rhs) const
Used to sort by decreasing weight.
const vpdl_distribution< T, n > & distribution(unsigned int index) const
Access (const) a component distribution of the mixture.
T weight(unsigned int index) const
Return the weight of a component in the mixture.
vpdl_mixture(vpdl_mixture< T, n > const &other)
void sort()
Sort the components in order of decreasing weight.
vpdt_field_default< T, n >::type vector
the data type used for vectors.
A mixture of distributions.
virtual T density(const vector &pt) const
Evaluate the unnormalized density at a point.
T box_prob(const vector &min_pt, const vector &max_pt) const
The probability integrated over a box.
This adaptor allows users to define ordering functors on the components without accessing the compone...
void sort(comp_type_ comp)
Sort the components using any StrictWeakOrdering function.
virtual unsigned int dimension() const =0
Return the run time dimension, which does not equal n when n==0.
void set_weight(unsigned int index, const T &w)
Set the weight of a component in the mixture.
bool insert(const vpdl_distribution< T, n > &d, const T &wght=T(0))
Insert a new component at the end of the vector.
vpdl_distribution< T, n > & distribution(unsigned int index)
Access a component distribution of the mixture.
void sort(comp_type_ comp, unsigned int idx1, unsigned int idx2)
Sort the components in the range idx1 to idx2 using any StrictWeakOrdering function.
component(const component &other)
Copy Constructor.
bool operator()(const component *c1, const component *c2) const
component(const vpdl_distribution< T, n > &d, const T &w=T(0))
Constructor.
void vpdt_set_size(vnl_vector< T > &v, unsigned int s)
Set the size of a vnl_vector.
The field traits class (scalar).
The base class for all probability distributions.
A struct to hold the component distributions and weights.
The base class for all multiple component probability distributions.
virtual void compute_covar(matrix &covar) const
Compute the covariance of the distribution.
std::unique_ptr< vpdl_distribution< T, n > > distribution
The distribution.
Overloaded functions to allow uniform API access to various field types.
vpdl_mixture()
Default Constructor.
This functor is used by default for sorting with STL.
void normalize_weights()
Normalize the weights of the components to add to 1.
unsigned int num_components() const
Return the number of components in the mixture.
virtual unsigned int dimension() const
Return the run time dimension, which does not equal n when n==0.
void vpdt_fill(vnl_vector< T > &v, const T &val)
Fill a vnl_vector.
bool remove_last()
Remove the last component in the vector.
float outer_product(const float &v1, const float &v2)
vnl defines outer_product for vectors but not scalars.
std::vector< component * > components_
The vector of components.