vpdl_distribution.h
Go to the documentation of this file.
1 // This is core/vpdl/vpdl_distribution.h
2 #ifndef vpdl_distribution_h_
3 #define vpdl_distribution_h_
4 //:
5 // \file
6 // \author Matthew Leotta
7 // \date February 5, 2009
8 // \brief The templated base class for all distributions
9 //
10 // \verbatim
11 // Modifications
12 // None
13 // \endverbatim
14 
15 #include <cmath>
18 #ifdef _MSC_VER
19 # include <vcl_msvc_warnings.h>
20 #endif
21 
22 //: The base class for all probability distributions.
23 // There is a distinct polymorphic class hierarchy for each choice of
24 // template parameters. The vector and matrix data types vary with both \c T and \c n.
25 // \tparam T is the scalar type use for numerical calculations (generally double or float)
26 // \tparam n is the fixed dimension of the space with special case 0 (the default)
27 // indicating dynamic dimension set at run time.
28 // - For n > 1 the data types are vnl_vector_fixed<T,n> and vnl_matrix_fixed<T,n,n>
29 // - For n == 1 the data types are T and T
30 // - For n == 0 the data types are vnl_vector<T> and vnl_matrix<T>
31 //
32 template<class T, unsigned int n=0>
34 {
35  public:
36  virtual ~vpdl_distribution() {}
37 
38  //: the data type used for vectors
40 
41  //: the data type used for vectors
43  //: the data type used for matrices
45 
46  //: Return the run time dimension, which does not equal \c n when \c n==0
47  virtual unsigned int dimension() const = 0;
48 
49  //: Create a copy on the heap and return base class pointer
50  virtual vpdl_distribution<T,n>* clone() const = 0;
51 
52  //: Evaluate the unnormalized density at a point
53  // \note This is not a probability density.
54  // To make this a probability multiply by norm_const()
55  // \sa prob_density
56  virtual T density(const vector& pt) const = 0;
57 
58  //: Evaluate the probability density at a point
59  virtual T prob_density(const vector& pt) const
60  {
61  return density(pt) * norm_const();
62  }
63 
64  //: Evaluate the log probability density at a point
65  virtual T log_prob_density(const vector& pt) const
66  {
67  return std::log(prob_density(pt));
68  };
69 
70  //: Compute the gradient of the unnormalized density at a point
71  // \return the density at \a pt since it is usually needed as well, and
72  // is often trivial to compute while computing gradient
73  // \retval g the gradient vector
74  virtual T gradient_density(const vector& pt, vector& g) const = 0;
75 
76  //: The normalization constant for the density
77  // When density() is multiplied by this value it becomes prob_density
78  // norm_const() is reciprocal of the integral of density over the entire field
79  virtual T norm_const() const = 0;
80 
81  //: Evaluate the cumulative distribution function at a point
82  // This is the integral of the density function from negative infinity
83  // (in all dimensions) to the point in question
84  // \note It is not possible to compute this value for all functions in
85  // closed form. In some cases, numerical integration may be used.
86  // If no good solutions exists the function should return a quiet NaN.
87  virtual T cumulative_prob(const vector& pt) const = 0;
88 
89  //: Compute the inverse of the cumulative_prob() function
90  // The value of x: P(x'<x) = P for x' drawn from the distribution.
91  // \note This is only valid for univariate distributions
92  // multivariate distributions will return a quiet NaN
93  virtual vector inverse_cdf(const T& p) const;
94 
95  //: The probability of being in an axis-aligned box
96  // The box is defined by two points, the minimum and maximum.
97  // Implemented in terms of \c cumulative_prob() by default.
98  virtual T box_prob(const vector& min_pt, const vector& max_pt) const;
99 
100  //: Compute the mean of the distribution.
101  // This may be trivial for distributions like Gaussians,
102  // but actually involves computation for others.
103  virtual void compute_mean(vector& mean) const = 0;
104 
105  //: Compute the covariance of the distribution.
106  // This may be trivial for distributions like Gaussians,
107  // but actually involves computation for others.
108  virtual void compute_covar(matrix& covar) const = 0;
109 };
110 
111 
112 //: Default implementation of numerical CDF inverse computation.
113 // This function is called by the virtual function inverse_cdf() by default
114 // in the univariate case.
115 template <class T>
116 T vpdl_compute_inverse_cdf(const vpdl_distribution<T,1>& dist, double p);
117 
118 
119 //=============================================================================
120 // These global functions allow vpdl_distributions be used efficiently as
121 // components in vpdt distributions like vpdt_mixture_of
122 
123 //: probability density wrapper for vpdt
124 template<class T, unsigned int n>
126  const typename vpdt_field_default<T,n>::type& pt)
127 {
128  return d.prob_density(pt);
129 }
130 
131 //: The box probability wrapper for vpdt
132 template<class T, unsigned int n>
134  const typename vpdt_field_default<T,n>::type& min_pt,
135  const typename vpdt_field_default<T,n>::type& max_pt)
136 {
137  return d.box_prob(min_pt,max_pt);
138 }
139 
140 //: The log density wrapper for vpdt
141 template<class T, unsigned int n>
143  const typename vpdt_field_default<T,n>::type& pt)
144 {
145  return d.log_density(pt);
146 }
147 
148 //: The log probability density wrapper for vpdt
149 template<class T, unsigned int n>
151  const typename vpdt_field_default<T,n>::type& pt)
152 {
153  return d.log_prob_density(pt);
154 }
155 
156 
157 #endif // vpdl_distribution_h_
vpdt_field_traits< field_type >::matrix_type matrix
the data type used for matrices.
virtual T prob_density(const vector &pt) const
Evaluate the probability density at a point.
virtual T norm_const() const =0
The normalization constant for the density.
virtual T log_prob_density(const vector &pt) const
Evaluate the log probability density at a point.
virtual T box_prob(const vector &min_pt, const vector &max_pt) const
The probability of being in an axis-aligned box.
virtual void compute_covar(matrix &covar) const =0
Compute the covariance of the distribution.
A type generator for the default types (those used in vpdl)
vpdt_field_default< T, n >::type field_type
the data type used for vectors.
virtual vector inverse_cdf(const T &p) const
Compute the inverse of the cumulative_prob() function.
virtual T density(const vector &pt) const =0
Evaluate the unnormalized density at a point.
vpdt_field_default< T, n >::type vector
the data type used for vectors.
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.
virtual vpdl_distribution< T, n > * clone() const =0
Create a copy on the heap and return base class pointer.
virtual unsigned int dimension() const =0
Return the run time dimension, which does not equal n when n==0.
T vpdt_log_prob_density(const vpdl_distribution< T, n > &d, const typename vpdt_field_default< T, n >::type &pt)
The log probability density wrapper for vpdt.
virtual void compute_mean(vector &mean) const =0
Compute the mean of the distribution.
virtual T cumulative_prob(const vector &pt) const =0
Evaluate the cumulative distribution function at a point.
T vpdt_prob_density(const vpdl_distribution< T, n > &d, const typename vpdt_field_default< T, n >::type &pt)
probability density wrapper for vpdt.
specialized template trait classes for properties of a field type
The field traits class (scalar).
The base class for all probability distributions.
T vpdt_log_density(const vpdl_distribution< T, n > &d, const typename vpdt_field_default< T, n >::type &pt)
The log density wrapper for vpdt.
virtual ~vpdl_distribution()
T vpdl_compute_inverse_cdf(const vpdl_distribution< T, 1 > &dist, double p)
Default implementation of numerical CDF inverse computation.
virtual T gradient_density(const vector &pt, vector &g) const =0
Compute the gradient of the unnormalized density at a point.