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