vpdl_gaussian_indep.h
Go to the documentation of this file.
1 // This is core/vpdl/vpdl_gaussian_indep.h
2 #ifndef vpdl_gaussian_indep_h_
3 #define vpdl_gaussian_indep_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>
17 #ifdef _MSC_VER
18 # include <vcl_msvc_warnings.h>
19 #endif
20 #include <cassert>
21 
25 
26 //: A Gaussian with variance independent in each dimension
27 template<class T, unsigned int n=0>
29 {
30  public:
31  //: the data type used for vectors
33  //: the data type used for matrices
35  //: the type used internally for covariance
36  typedef vector covar_type;
37 
38  //: Constructor
39  // Optionally initialize the dimension for when n==0.
40  // Otherwise var_dim is ignored
41  vpdl_gaussian_indep(unsigned int var_dim = n)
42  : impl_(var_dim) {}
43 
44  //: Constructor - from mean and variance
45  vpdl_gaussian_indep(const vector& mean_val, const covar_type& var)
46  : impl_(mean_val,var) {}
47 
48  //: Destructor
49  virtual ~vpdl_gaussian_indep() {}
50 
51  //: Create a copy on the heap and return base class pointer
52  virtual vpdl_distribution<T,n>* clone() const
53  {
54  return new vpdl_gaussian_indep<T,n>(*this);
55  }
56 
57  //: Return the run time dimension, which does not equal \c n when \c n==0
58  virtual unsigned int dimension() const { return impl_.dimension(); }
59 
60  //: Evaluate the unnormalized density at a point
61  virtual T density(const vector& pt) const
62  {
63  return impl_.density(pt);
64  }
65 
66  //: Evaluate the probability density at a point
67  virtual T prob_density(const vector& pt) const
68  {
69  return vpdt_prob_density(impl_,pt);
70  }
71 
72  //: Evaluate the log probability density at a point
73  virtual T log_prob_density(const vector& pt) const
74  {
75  return vpdt_log_prob_density(impl_,pt);
76  };
77 
78  //: Compute the gradient of the unnormalized density at a point
79  // \return the density at \a pt since it is usually needed as well, and
80  // is often trivial to compute while computing gradient
81  // \retval g the gradient vector
82  virtual T gradient_density(const vector& pt, vector& g) const
83  {
84  return impl_.gradient_density(pt,g);
85  }
86 
87  //: The normalization constant for the density
88  // When density() is multiplied by this value it becomes prob_density
89  // norm_const() is reciprocal of the integral of density over the entire field
90  virtual T norm_const() const
91  {
92  return impl_.norm_const();
93  }
94 
95  //: The squared Mahalanobis distance to this point
96  // Non-virtual for efficiency
97  T sqr_mahal_dist(const vector& pt) const
98  {
99  return impl_.sqr_mahal_dist(pt);
100  }
101 
102  //: Evaluate the cumulative distribution function at a point
103  // This is the integral of the density function from negative infinity
104  // (in all dimensions) to the point in question
105  virtual T cumulative_prob(const vector& pt) const
106  {
107  return impl_.cumulative_prob(pt);
108  }
109 
110  //: Access the mean directly
111  virtual const vector& mean() const { return impl_.mean; }
112 
113  //: Set the mean
114  virtual void set_mean(const vector& mean_val) { impl_.mean = mean_val; }
115 
116  //: Compute the mean of the distribution.
117  virtual void compute_mean(vector& mean_val) const { mean_val = impl_.mean; }
118 
119  //: Access the vector of variance
120  const covar_type& covariance() const { return impl_.covar; }
121 
122  //: Set the vector of variance
123  void set_covariance(const covar_type& var) { impl_.covar = var; }
124 
125  //: Compute the covariance of the distribution.
126  // Should be the diagonal matrix of var_
127  virtual void compute_covar(matrix& covar) const
128  {
129  impl_.compute_covar(covar);
130  }
131 
132  protected:
133  //: the Gaussian implementation from vpdt
135 };
136 
137 #endif // vpdl_gaussian_indep_h_
vpdt_field_traits< field_type >::matrix_type matrix
the data type used for matrices.
vpdl_gaussian_indep(const vector &mean_val, const covar_type &var)
Constructor - from mean and variance.
virtual T prob_density(const vector &pt) const
Evaluate the probability density at a point.
virtual T density(const vector &pt) const
Evaluate the unnormalized density at a point.
virtual T log_prob_density(const vector &pt) const
Evaluate the log probability density at a point.
vpdl_gaussian_indep(unsigned int var_dim=n)
Constructor.
vpdt_gaussian< vector, covar_type > impl_
the Gaussian implementation from vpdt.
The basic functions for probability calculations.
virtual T gradient_density(const vector &pt, vector &g) const
Compute the gradient of the unnormalized density at a point.
vpdt_field_traits< vector >::matrix_type matrix
the data type used for matrices.
virtual void compute_mean(vector &mean_val) const
Compute the mean of the distribution.
virtual T norm_const() const
The normalization constant for the density.
virtual unsigned int dimension() const
Return the run time dimension, which does not equal n when n==0.
virtual vpdl_distribution< T, n > * clone() const
Create a copy on the heap and return base class pointer.
virtual T cumulative_prob(const vector &pt) const
Evaluate the cumulative distribution function at a point.
The abstract base class for Gaussian distributions.
T sqr_mahal_dist(const vector &pt) const
The squared Mahalanobis distance to this point.
virtual ~vpdl_gaussian_indep()
Destructor.
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_covar(matrix &covar) const
Compute the covariance of the distribution.
A Gaussian with variance independent in each dimension.
Definition: vpdt_gaussian.h:39
vpdt_field_default< T, n >::type vector
the data type used for vectors.
const covar_type & covariance() const
Access the vector of variance.
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.
vector covar_type
the type used internally for covariance.
void set_covariance(const covar_type &var)
Set the vector of variance.
The field traits class (scalar).
The base class for all probability distributions.
The basic functions for log of probability calculation.
virtual void set_mean(const vector &mean_val)
Set the mean.
virtual const vector & mean() const
Access the mean directly.
A Gaussian with variance independent in each dimension.