vpdl_mixture_of.h
Go to the documentation of this file.
1 // This is core/vpdl/vpdl_mixture_of.h
2 #ifndef vpdl_mixture_of_h_
3 #define vpdl_mixture_of_h_
4 //:
5 // \file
6 // \author Matthew Leotta
7 // \date February 24, 2009
8 // \brief A mixture of a fixed type of distributions
9 //
10 // \verbatim
11 // Modifications
12 // None
13 // \endverbatim
14 
16 #include <vpdl/vpdt/vpdt_access.h>
20 
21 //: A mixture of a fixed type of distributions
22 // A mixture is a weighted linear combination of other mixtures.
23 // This class represents a mixture of a specific type of distribution.
24 // Each component in the mixture has its own weight and parameters,
25 // but each must be of the same type.
26 // \tparam dist_t is the type of a component distribution
27 // \sa vpdl_mixture
28 template<class dist_t>
30  : public vpdl_multi_cmp_dist<typename vpdt_dist_traits<dist_t>::scalar_type,
31  vpdt_dist_traits<dist_t>::dimension>
32 {
34 
35  public:
36  //: the data type to represent a point in the field
37  typedef typename dist_t::field_type field_type;
38  //: define the component type
39  typedef dist_t component_type;
40 
41  //: define the fixed dimension (normally specified by template parameter n)
42  static const unsigned int n = vpdt_field_traits<field_type>::dimension;
43  //: the data type to represent a point in the field
44  typedef typename dist_t::field_type F;
45  //: define the scalar type (normally specified by template parameter T)
47  //: define the vector type
49  //: the data type used for matrices
51 
52  // Default Constructor
54 
55  // Destructor
56  virtual ~vpdl_mixture_of() {}
57 
58  //: Create a copy on the heap and return base class pointer
59  virtual vpdl_distribution<T,n>* clone() const
60  {
61  return new vpdl_mixture_of<dist_t>(*this);
62  }
63 
64  //: Return the run time dimension
65  virtual unsigned int dimension() const { return impl_.dimension(); }
66 
67  //: Return the number of components in the mixture
68  unsigned int num_components() const { return impl_.num_components(); }
69 
70  //: Access (const) a component distribution of the mixture
71  const dist_t& distribution(unsigned int index) const
72  { return impl_.distribution(index); }
73 
74  //: Access a component distribution of the mixture
75  dist_t& distribution(unsigned int index) { return impl_.distribution(index); }
76 
77  //: Return the weight of a component in the mixture
78  T weight(unsigned int index) const { return impl_.weight(index); }
79 
80  //: Set the weight of a component in the mixture
81  void set_weight(unsigned int index, const T& w) { impl_.set_weight(index,w); }
82 
83  //: Insert a new component at the end of the vector
84  bool insert(const dist_t& d, const T& wght = T(0))
85  { return impl_.insert(d,wght); }
86 
87  //: Remove the last component in the vector
88  bool remove_last() { return impl_.remove_last(); }
89 
90  //: Compute the unnormalized density at this point
91  T density(const vector& pt) const { return impl_.density(pt); }
92 
93  //: Compute the probability density at this point
94  T prob_density(const vector& pt) const { return vpdt_prob_density(impl_,pt); }
95 
96  //: Compute the gradient of the unnormalized density at a point
97  // \return the density at \a pt since it is usually needed as well, and
98  // is often trivial to compute while computing gradient
99  // \retval g the gradient vector
100  virtual T gradient_density(const vector& pt, vector& g) const
101  {
102  return impl_.gradient_density(pt,g);
103  }
104 
105  //: The probability integrated over a box
106  T box_prob(const vector& min_pt, const vector& max_pt) const
107  { return vpdt_box_prob(impl_,min_pt,max_pt); }
108 
109  //: Evaluate the cumulative distribution function at a point
110  // This is the integral of the density function from negative infinity
111  // (in all dimensions) to the point in question
112  virtual T cumulative_prob(const vector& pt) const
113  { return impl_.cumulative_prob(pt); }
114 
115  //: Compute the mean of the distribution.
116  // weighted average of the component means
117  virtual void compute_mean(vector& mean) const { impl_.compute_mean(mean); }
118 
119  //: Compute the covariance of the distribution.
120  virtual void compute_covar(matrix& covar) const { impl_.compute_covar(covar); }
121 
122  //: The normalization constant for the density
123  // When density() is multiplied by this value it becomes prob_density
124  // norm_const() is reciprocal of the integral of density over the entire field
125  virtual T norm_const() const { return impl_.norm_const(); }
126 
127  //: Normalize the weights of the components to add to 1.
128  void normalize_weights() { impl_.normalize_weights(); }
129 
130  //: Sort the components in order of decreasing weight
131  void sort() { impl_.sort(); }
132 
133  //: Sort the components in the range \a idx1 to \a idx2 in order of decreasing weight
134  void sort(unsigned int idx1, unsigned int idx2) { impl_.sort(idx1, idx2); }
135 
136  //: Sort the components using any StrictWeakOrdering function
137  // The prototype should be
138  // \code
139  // template <class dist_t>
140  // bool functor(const dist_t& d1, const vpdt_dist_traits<dist_t>::scalar_type& w1,
141  // const dist_t& d2, const vpdt_dist_traits<dist_t>::scalar_type& w2);
142  // \endcode
143  template <class comp_type_>
144  void sort(comp_type_ comp) { impl_.sort(comp); }
145 
146  //: Sort the components in the range \a idx1 to \a idx2 using any StrictWeakOrdering function
147  template <class comp_type_>
148  void sort(comp_type_ comp, unsigned int idx1, unsigned int idx2) { impl_.sort(comp,idx1,idx2); }
149 };
150 
151 
152 #endif // vpdl_mixture_of_h_
virtual void compute_covar(matrix &covar) const
Compute the covariance of the distribution.
T box_prob(const vector &min_pt, const vector &max_pt) const
The probability integrated over a box.
virtual T cumulative_prob(const vector &pt) const
Evaluate the cumulative distribution function at a point.
virtual unsigned int dimension() const
Return the run time dimension.
A mixture of a fixed type of distributions.
base class for multiple component distributions
The basic functions for probability calculations.
dist_t component_type
define the component type.
void sort(comp_type_ comp, unsigned int idx1, unsigned int idx2)
Sort the components in the range idx1 to idx2 using any StrictWeakOrdering function.
void sort(unsigned int idx1, unsigned int idx2)
Sort the components in the range idx1 to idx2 in order of decreasing weight.
T weight(unsigned int index) const
Return the weight of a component in the mixture.
vpdt_mixture_of< dist_t > impl_
virtual vpdl_distribution< T, n > * clone() const
Create a copy on the heap and return base class pointer.
dist_t::field_type F
the data type to represent a point in the field.
T vpdt_box_prob(const vpdl_distribution< T, n > &d, const typename vpdt_field_default< T, n >::type &min_pt, const typename vpdt_field_default< T, n >::type &max_pt)
The box probability wrapper for vpdt.
T density(const vector &pt) const
Compute the unnormalized density at this point.
static const unsigned int n
define the fixed dimension (normally specified by template parameter n).
vpdt_field_traits< field_type >::vector_type vector
define the vector type.
void set_weight(unsigned int index, const T &w)
Set the weight of a component in the mixture.
virtual void compute_mean(vector &mean) const
Compute the mean of the distribution.
const dist_t & distribution(unsigned int index) const
Access (const) a component distribution of the mixture.
void normalize_weights()
Normalize the weights of the components to add to 1.
void sort(comp_type_ comp)
Sort the components using any StrictWeakOrdering function.
void sort()
Sort the components in order of decreasing weight.
bool insert(const dist_t &d, const T &wght=T(0))
Insert a new component at the end of the vector.
T vpdt_prob_density(const vpdl_distribution< T, n > &d, const typename vpdt_field_default< T, n >::type &pt)
probability density wrapper for vpdt.
T prob_density(const vector &pt) const
Compute the probability density at this point.
virtual T norm_const() const
The normalization constant for the density.
The field traits class (scalar).
The base class for all probability distributions.
virtual T gradient_density(const vector &pt, vector &g) const
Compute the gradient of the unnormalized density at a point.
A mixture of a fixed type of distributions.
A mixture of a fixed type of distributions.
The base class for all multiple component probability distributions.
vpdt_field_traits< field_type >::matrix_type matrix
the data type used for matrices.
dist_t & distribution(unsigned int index)
Access a component distribution of the mixture.
Overloaded functions to allow uniform API access to various field types.
specialized template trait classes for properties of a distribution type
virtual ~vpdl_mixture_of()
dist_t::field_type field_type
the data type to represent a point in the field.
bool remove_last()
Remove the last component in the vector.
unsigned int num_components() const
Return the number of components in the mixture.
vpdt_field_traits< field_type >::scalar_type T
define the scalar type (normally specified by template parameter T).