core/vpdl/vpdl_gaussian.h
Go to the documentation of this file.
00001 // This is core/vpdl/vpdl_gaussian.h
00002 #ifndef vpdl_gaussian_h_
00003 #define vpdl_gaussian_h_
00004 //:
00005 // \file
00006 // \author Matthew Leotta
00007 // \date February 11, 2009
00008 // \brief A Gaussian with variance independent in each dimension
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //   <None yet>
00013 // \endverbatim
00014 
00015 #include <vpdl/vpdl_gaussian_base.h>
00016 #include <vpdl/vpdt/vpdt_gaussian.h>
00017 #include <vpdl/vpdt/vpdt_probability.h>
00018 #include <vpdl/vpdt/vpdt_log_probability.h>
00019 #include <vcl_limits.h>
00020 
00021 //: A Gaussian with variance independent in each dimension
00022 template<class T, unsigned int n=0>
00023 class vpdl_gaussian : public vpdl_gaussian_base<T,n>
00024 {
00025  public:
00026   //: the data type used for vectors
00027   typedef typename vpdt_field_default<T,n>::type vector;
00028   //: the data type used for matrices
00029   typedef typename vpdt_field_traits<vector>::matrix_type matrix;
00030   //: the type used internally for covariance
00031   typedef matrix covar_type;
00032 
00033   //: Constructor
00034   // Optionally initialize the dimension for when n==0.
00035   // Otherwise var_dim is ignored
00036   vpdl_gaussian(unsigned int var_dim = n)
00037   : impl_(var_dim) {}
00038 
00039   //: Constructor - from mean and variance
00040   vpdl_gaussian(const vector& mean_val, const covar_type& covar)
00041   : impl_(mean_val,covar) {}
00042 
00043   //: Destructor
00044   virtual ~vpdl_gaussian() {}
00045 
00046   //: Create a copy on the heap and return base class pointer
00047   virtual vpdl_distribution<T,n>* clone() const
00048   {
00049     return new vpdl_gaussian<T,n>(*this);
00050   }
00051 
00052   //: Return the run time dimension, which does not equal \c n when \c n==0
00053   virtual unsigned int dimension() const { return impl_.dimension();  }
00054 
00055   //: Evaluate the unnormalized density at a point
00056   virtual T density(const vector& pt) const
00057   {
00058     return impl_.density(pt);
00059   }
00060 
00061   //: Evaluate the probability density at a point
00062   virtual T prob_density(const vector& pt) const
00063   {
00064     return vpdt_prob_density(impl_,pt);
00065   }
00066 
00067   //: Evaluate the log probability density at a point
00068   virtual T log_prob_density(const vector& pt) const
00069   {
00070     return vpdt_log_prob_density(impl_,pt);
00071   };
00072 
00073   //: Compute the gradient of the unnormalized density at a point
00074   // \return the density at \a pt since it is usually needed as well, and
00075   //         is often trivial to compute while computing gradient
00076   // \retval g the gradient vector
00077   virtual T gradient_density(const vector& pt, vector& g) const
00078   {
00079     return impl_.gradient_density(pt,g);
00080   }
00081 
00082   //: The normalization constant for the density
00083   // When density() is multiplied by this value it becomes prob_density
00084   // norm_const() is reciprocal of the integral of density over the entire field
00085   virtual T norm_const() const
00086   {
00087     return impl_.norm_const();
00088   }
00089 
00090   //: The squared Mahalanobis distance to this point
00091   // Non-virtual for efficiency
00092   T sqr_mahal_dist(const vector& pt) const
00093   {
00094     return impl_.sqr_mahal_dist(pt);
00095   }
00096 
00097   //: Evaluate the cumulative distribution function at a point
00098   // This is the integral of the density function from negative infinity
00099   // (in all dimensions) to the point in question
00100   virtual T cumulative_prob(const vector& pt) const
00101   {
00102     return impl_.cumulative_prob(pt);
00103   }
00104 
00105   //: Access the mean directly
00106   virtual const vector& mean() const { return impl_.mean; }
00107 
00108   //: Set the mean
00109   virtual void set_mean(const vector& mean_val) { impl_.mean = mean_val; }
00110 
00111   //: Compute the mean of the distribution.
00112   virtual void compute_mean(vector& mean_val) const { mean_val = impl_.mean; }
00113 
00114   //: Access the covariance - requires computation
00115   covar_type covariance() const
00116   {
00117     covar_type M;
00118     impl_.compute_covar(M);
00119     return M;
00120   }
00121 
00122   //: Set the covariance matrix
00123   void set_covariance(const covar_type& covar)
00124   {
00125     impl_.covar.set_matrix(covar);
00126   }
00127 
00128   //: Compute the covariance of the distribution.
00129   virtual void compute_covar(matrix& covar) const
00130   {
00131     impl_.compute_covar(covar);
00132   }
00133 
00134   //: Access the eigenvectors of the covariance matrix
00135   const matrix& covar_eigenvecs() const { return impl_.covar.eigenvectors(); }
00136 
00137   //: Access the eigenvalues of the covariance matrix
00138   const vector& covar_eigenvals() const { return impl_.covar.eigenvalues(); }
00139 
00140   //: Set the eigenvectors of the covariance matrix
00141   void set_covar_eigenvecs(const matrix& m) { impl_.covar.set_eigenvectors(m); }
00142 
00143   //: Set the eigenvalues of the covariance matrix
00144   void set_covar_eigenvals(const vector& v) { impl_.covar.set_eigenvalues(v); }
00145 
00146  protected:
00147   //: the Gaussian implementation from vpdt
00148   vpdt_gaussian<vector> impl_;
00149 };
00150 
00151 #endif // vpdl_gaussian_h_