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