2 #ifndef vpdt_mixture_of_h_ 3 #define vpdt_mixture_of_h_ 20 # include <vcl_msvc_warnings.h> 50 template<
class dist_t>
62 typedef typename dist_t::field_type
F;
103 template <
class comp_type_>
125 for (
unsigned int i=0; i<
components_.size(); ++i) {
133 for (
unsigned int i=0; i<
components_.size(); ++i) {
142 for (
unsigned int i=0; i<
components_.size(); ++i) {
146 for (
unsigned int i=0; i<rhs.
components_.size(); ++i) {
197 components_.push_back(
new component(d, wght));
214 typedef typename std::vector<component*>::const_iterator comp_itr;
218 prob += (*i)->weight * (*i)->distribution.prob_density(pt);
229 const unsigned int d = this->
dimension();
232 typedef typename std::vector<component*>::const_iterator comp_itr;
236 T w_i = (*i)->distribution.norm_const() * (*i)->weight;
237 dens += w_i * (*i)->distribution.gradient_density(pt,g_i);
249 typedef typename std::vector<component*>::const_iterator comp_itr;
253 prob += (*i)->weight * (*i)->distribution.cumulative_prob(pt);
254 sum_w += (*i)->weight;
256 assert(sum_w >
T(0));
264 const unsigned int d = this->
dimension();
268 typedef typename std::vector<component*>::const_iterator comp_itr;
272 (*i)->distribution.compute_mean(cmp_mean);
273 cmp_mean *= (*i)->weight;
274 sum_w += (*i)->weight;
284 const unsigned int d = this->
dimension();
291 typedef typename std::vector<component*>::const_iterator comp_itr;
296 const T& wgt = (*i)->weight;
297 (*i)->distribution.compute_covar(cmp_covar);
298 (*i)->distribution.compute_mean(cmp_mean);
316 typedef typename std::vector<component*>::const_iterator comp_itr;
327 typedef typename std::vector<component*>::iterator comp_itr;
330 (*i)->weight *= norm;
337 void sort(
unsigned int idx1,
unsigned int idx2)
347 template <
class comp_type_>
352 template <
class comp_type_>
353 void sort(comp_type_ comp,
unsigned int idx1,
unsigned int idx2)
363 template <
class dist>
370 typedef typename std::vector<typename vpdt_mixture_of<dist>::component*>::const_iterator comp_itr;
374 prob += (*i)->weight *
vpdt_box_prob((*i)->distribution,min_pt,max_pt);
375 sum_w += (*i)->weight;
377 assert(sum_w > T(0));
382 #endif // vpdt_mixture_of_h_ dist_t::field_type F
the data type to represent a point in the field.
bool insert(const dist_t &d, const T &wght=T(0))
Insert a new component at the end of the vector.
unsigned int dimension() const
Return the run time dimension, which does not equal n when n==0.
T norm_const() const
The normalization constant for the density.
The distribution traits class.
void set_weight(unsigned int index, const T &w)
Set the weight of a component in the mixture.
T gradient_density(const F &pt, vector &g) const
Compute the gradient of the unnormalized density at a point.
bool operator<(const component &rhs) const
Used to sort by decreasing weight.
A mixture of a fixed type of distributions.
T weight(unsigned int index) const
Return the weight of a component in the mixture.
This adaptor allows users to define ordering functors on the components without accessing the compone...
T cumulative_prob(const F &pt) const
Evaluate the cumulative distribution function at a point.
vpdt_field_traits< field_type >::scalar_type T
define the scalar type (normally specified by template parameter T).
The basic functions for probability calculations.
void normalize_weights()
Normalize the weights of the components to add to 1.
bool remove_last()
Remove the last component in the vector.
sort_adaptor(comp_type_ c)
void compute_mean(F &mean) const
Compute the mean of the distribution.
void compute_covar(matrix &covar) const
Compute the covariance of the distribution.
void sort(comp_type_ comp)
Sort the components using any StrictWeakOrdering function.
component(const component_type &d, const T &w=T(0))
Constructor.
dist_t & distribution(unsigned int index)
Access a component distribution of the mixture.
vpdt_field_traits< field_type >::vector_type vector
define the vector type.
dist_t component_type
define the component type.
void sort()
Sort the components in order of decreasing weight.
vpdt_mixture_of< dist_t > & operator=(const vpdt_mixture_of< dist_t > &rhs)
Assignment operator.
void vpdt_set_size(vnl_vector< T > &v, unsigned int s)
Set the size of a vnl_vector.
The field traits class (scalar).
bool operator()(const component *const c1, const component *const c2) const
vpdt_field_traits< field_type >::matrix_type matrix
the data type used for matrices.
This functor is used by default for sorting with STL.
static const unsigned int n
define the fixed dimension (normally specified by template parameter n).
void sort(unsigned int idx1, unsigned int idx2)
Sort the components in the range idx1 to idx2 in order of decreasing weight.
vpdt_mixture_of(const vpdt_mixture_of< dist_t > &other)
specialized template trait classes for properties of a distribution type
dist_t::field_type field_type
the data type to represent a point in the field.
void sort(comp_type_ comp, unsigned int idx1, unsigned int idx2)
Sort the components in the range idx1 to idx2 using any StrictWeakOrdering function.
A struct to hold the component distributions and weights.
component_type distribution
The distribution.
void vpdt_fill(vnl_vector< T > &v, const T &val)
Fill a vnl_vector.
const dist_t & distribution(unsigned int index) const
Access (const) a component distribution of the mixture.
bool operator()(const component *c1, const component *c2) const
T density(const F &pt) const
Compute the unnormalized density at this point.
vpdt_dist_traits< vpdt_mixture_of< dist > >::scalar_type vpdt_box_prob(const vpdt_mixture_of< dist > &d, const typename vpdt_dist_traits< vpdt_mixture_of< dist > >::field_type &min_pt, const typename vpdt_dist_traits< vpdt_mixture_of< dist > >::field_type &max_pt)
The probability of being in an axis-aligned box.
std::vector< component * > components_
The vector of components.
unsigned int num_components() const
Return the number of components in the mixture.
float outer_product(const float &v1, const float &v2)
vnl defines outer_product for vectors but not scalars.