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