vpdl_gaussian.h
Go to the documentation of this file.
1 // This is core/vpdl/vpdl_gaussian.h
2 #ifndef vpdl_gaussian_h_
3 #define vpdl_gaussian_h_
4 //:
5 // \file
6 // \author Matthew Leotta
7 // \date February 11, 2009
8 // \brief A Gaussian with variance independent in each dimension
9 //
10 // \verbatim
11 // Modifications
12 // <None yet>
13 // \endverbatim
14 
15 #include <limits>
20 #ifdef _MSC_VER
21 # include <vcl_msvc_warnings.h>
22 #endif
23 
24 //: A Gaussian with variance independent in each dimension
25 template<class T, unsigned int n=0>
26 class vpdl_gaussian : public vpdl_gaussian_base<T,n>
27 {
28  public:
29  //: the data type used for vectors
31  //: the data type used for matrices
33  //: the type used internally for covariance
34  typedef matrix covar_type;
35 
36  //: Constructor
37  // Optionally initialize the dimension for when n==0.
38  // Otherwise var_dim is ignored
39  vpdl_gaussian(unsigned int var_dim = n)
40  : impl_(var_dim) {}
41 
42  //: Constructor - from mean and variance
43  vpdl_gaussian(const vector& mean_val, const covar_type& covar)
44  : impl_(mean_val,covar) {}
45 
46  //: Destructor
47  virtual ~vpdl_gaussian() {}
48 
49  //: Create a copy on the heap and return base class pointer
50  virtual vpdl_distribution<T,n>* clone() const
51  {
52  return new vpdl_gaussian<T,n>(*this);
53  }
54 
55  //: Return the run time dimension, which does not equal \c n when \c n==0
56  virtual unsigned int dimension() const { return impl_.dimension(); }
57 
58  //: Evaluate the unnormalized density at a point
59  virtual T density(const vector& pt) const
60  {
61  return impl_.density(pt);
62  }
63 
64  //: Evaluate the probability density at a point
65  virtual T prob_density(const vector& pt) const
66  {
67  return vpdt_prob_density(impl_,pt);
68  }
69 
70  //: Evaluate the log probability density at a point
71  virtual T log_prob_density(const vector& pt) const
72  {
73  return vpdt_log_prob_density(impl_,pt);
74  };
75 
76  //: Compute the gradient of the unnormalized density at a point
77  // \return the density at \a pt since it is usually needed as well, and
78  // is often trivial to compute while computing gradient
79  // \retval g the gradient vector
80  virtual T gradient_density(const vector& pt, vector& g) const
81  {
82  return impl_.gradient_density(pt,g);
83  }
84 
85  //: The normalization constant for the density
86  // When density() is multiplied by this value it becomes prob_density
87  // norm_const() is reciprocal of the integral of density over the entire field
88  virtual T norm_const() const
89  {
90  return impl_.norm_const();
91  }
92 
93  //: The squared Mahalanobis distance to this point
94  // Non-virtual for efficiency
95  T sqr_mahal_dist(const vector& pt) const
96  {
97  return impl_.sqr_mahal_dist(pt);
98  }
99 
100  //: Evaluate the cumulative distribution function at a point
101  // This is the integral of the density function from negative infinity
102  // (in all dimensions) to the point in question
103  virtual T cumulative_prob(const vector& pt) const
104  {
105  return impl_.cumulative_prob(pt);
106  }
107 
108  //: Access the mean directly
109  virtual const vector& mean() const { return impl_.mean; }
110 
111  //: Set the mean
112  virtual void set_mean(const vector& mean_val) { impl_.mean = mean_val; }
113 
114  //: Compute the mean of the distribution.
115  virtual void compute_mean(vector& mean_val) const { mean_val = impl_.mean; }
116 
117  //: Access the covariance - requires computation
119  {
120  covar_type M;
121  impl_.compute_covar(M);
122  return M;
123  }
124 
125  //: Set the covariance matrix
126  void set_covariance(const covar_type& covar)
127  {
128  impl_.covar.set_matrix(covar);
129  }
130 
131  //: Compute the covariance of the distribution.
132  virtual void compute_covar(matrix& covar) const
133  {
134  impl_.compute_covar(covar);
135  }
136 
137  //: Access the eigenvectors of the covariance matrix
138  const matrix& covar_eigenvecs() const { return impl_.covar.eigenvectors(); }
139 
140  //: Access the eigenvalues of the covariance matrix
141  const vector& covar_eigenvals() const { return impl_.covar.eigenvalues(); }
142 
143  //: Set the eigenvectors of the covariance matrix
144  void set_covar_eigenvecs(const matrix& m) { impl_.covar.set_eigenvectors(m); }
145 
146  //: Set the eigenvalues of the covariance matrix
147  void set_covar_eigenvals(const vector& v) { impl_.covar.set_eigenvalues(v); }
148 
149  protected:
150  //: the Gaussian implementation from vpdt
152 };
153 
154 #endif // vpdl_gaussian_h_
vpdt_field_traits< field_type >::matrix_type matrix
the data type used for matrices.
vpdt_field_default< T, n >::type vector
the data type used for vectors.
Definition: vpdl_gaussian.h:30
virtual void compute_mean(vector &mean_val) const
Compute the mean of the distribution.
virtual T gradient_density(const vector &pt, vector &g) const
Compute the gradient of the unnormalized density at a point.
Definition: vpdl_gaussian.h:80
virtual void set_mean(const vector &mean_val)
Set the mean.
virtual T density(const vector &pt) const
Evaluate the unnormalized density at a point.
Definition: vpdl_gaussian.h:59
T sqr_mahal_dist(const vector &pt) const
The squared Mahalanobis distance to this point.
Definition: vpdl_gaussian.h:95
matrix covar_type
the type used internally for covariance.
Definition: vpdl_gaussian.h:34
The basic functions for probability calculations.
virtual void compute_covar(matrix &covar) const
Compute the covariance of the distribution.
#define m
void set_covariance(const covar_type &covar)
Set the covariance matrix.
vpdl_gaussian(unsigned int var_dim=n)
Constructor.
Definition: vpdl_gaussian.h:39
virtual vpdl_distribution< T, n > * clone() const
Create a copy on the heap and return base class pointer.
Definition: vpdl_gaussian.h:50
#define v
virtual T norm_const() const
The normalization constant for the density.
Definition: vpdl_gaussian.h:88
The abstract base class for Gaussian distributions.
const matrix & covar_eigenvecs() const
Access the eigenvectors of the covariance matrix.
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 ~vpdl_gaussian()
Destructor.
Definition: vpdl_gaussian.h:47
vpdt_field_traits< vector >::matrix_type matrix
the data type used for matrices.
Definition: vpdl_gaussian.h:32
A Gaussian with variance independent in each dimension.
Definition: vpdt_gaussian.h:39
vpdl_gaussian(const vector &mean_val, const covar_type &covar)
Constructor - from mean and variance.
Definition: vpdl_gaussian.h:43
The abstract base class for Gaussian distributions.
A generic Gaussian distribution.
T vpdt_prob_density(const vpdl_distribution< T, n > &d, const typename vpdt_field_default< T, n >::type &pt)
probability density wrapper for vpdt.
void set_covar_eigenvecs(const matrix &m)
Set the eigenvectors of the covariance matrix.
virtual unsigned int dimension() const
Return the run time dimension, which does not equal n when n==0.
Definition: vpdl_gaussian.h:56
The field traits class (scalar).
The base class for all probability distributions.
vpdt_gaussian< vector > impl_
the Gaussian implementation from vpdt.
virtual T log_prob_density(const vector &pt) const
Evaluate the log probability density at a point.
Definition: vpdl_gaussian.h:71
virtual T cumulative_prob(const vector &pt) const
Evaluate the cumulative distribution function at a point.
virtual T prob_density(const vector &pt) const
Evaluate the probability density at a point.
Definition: vpdl_gaussian.h:65
A Gaussian with variance independent in each dimension.
Definition: vpdl_gaussian.h:26
The basic functions for log of probability calculation.
void set_covar_eigenvals(const vector &v)
Set the eigenvalues of the covariance matrix.
const vector & covar_eigenvals() const
Access the eigenvalues of the covariance matrix.
virtual const vector & mean() const
Access the mean directly.
covar_type covariance() const
Access the covariance - requires computation.