contrib/brl/bbas/bsta/bsta_gaussian_full.h
Go to the documentation of this file.
00001 // This is brl/bbas/bsta/bsta_gaussian_full.h
00002 #ifndef bsta_gaussian_full_h_
00003 #define bsta_gaussian_full_h_
00004 //:
00005 // \file
00006 // \brief A Gaussian distribution with a full covariance matrix
00007 // \author Matt Leotta (mleotta@lems.brown.edu)
00008 // \date January 25, 2006
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //    Jan 21 2008  -  Matt Leotta  -  Rename probability to prob_density and
00013 //                                    add probability integration over a box
00014 // \endverbatim
00015 
00016 #include "bsta_gaussian.h"
00017 #include <vnl/vnl_vector_fixed.h>
00018 #include <vnl/vnl_matrix_fixed.h>
00019 #include <vcl_iostream.h>
00020 
00021 //: A Gaussian distribution with a full covariance matrix
00022 template <class T, unsigned n>
00023 class bsta_gaussian_full : public bsta_gaussian<T,n>
00024 {
00025  public:
00026   //: the covariance type
00027   typedef vnl_matrix_fixed<T,n,n> covar_type;
00028 
00029   //: Constructor
00030   bsta_gaussian_full()
00031   : bsta_gaussian<T,n>(), covar_(T(0)), det_covar_(T(0)), inv_covar_(NULL) {}
00032 
00033   //: Constructor
00034   bsta_gaussian_full(const vnl_vector_fixed<T,n>& mean,
00035                      const vnl_matrix_fixed<T,n,n>& covar)
00036   : bsta_gaussian<T,n>(mean), covar_(covar), det_covar_(T(-1)), inv_covar_(NULL)
00037   { compute_det(); }
00038 
00039   //: Destructor
00040   ~bsta_gaussian_full() { delete inv_covar_; }
00041 
00042   //: The covariance matrix of the distribution
00043   const covar_type& covar() const { return covar_; }
00044 
00045   //: Set the covariance matrix of the distribution
00046   void set_covar(const covar_type& covar);
00047 
00048   //: The probability density at this sample given square mahalanobis distance
00049   T dist_prob_density(const T& sqr_mahal_dist) const
00050   {
00051     if (det_covar_ <= 0)
00052       return T(0);
00053     return static_cast<T>(vcl_sqrt(1/(det_covar_*two_pi_power<n>::value()))
00054            * vcl_exp(-sqr_mahal_dist/2));
00055   }
00056 
00057   //: The probability density at this sample
00058   T prob_density(const vnl_vector_fixed<T,n>& pt) const
00059   {
00060     return dist_prob_density(sqr_mahalanobis_dist(pt));
00061   }
00062 
00063   //: The probability integrated over a box
00064   T probability(const vnl_vector_fixed<T,n>& /*min_pt*/,
00065                 const vnl_vector_fixed<T,n>& /*max_pt*/) const
00066   {
00067     // TODO - This stub needs implementation
00068     return -1;
00069   }
00070 
00071   //: The squared Mahalanobis distance to this point
00072   T sqr_mahalanobis_dist(const vnl_vector_fixed<T,n>& pt) const;
00073 
00074   //: Compute the determinant of the covariance matrix
00075   T det_covar() const { return det_covar_; }
00076 
00077   //: The inverse covariance matrix of the distribution
00078   const covar_type& inv_covar() const;
00079 
00080  protected:
00081   //: The covariance matrix
00082   covar_type covar_;
00083 
00084   //: The cached covariance determinant
00085   T det_covar_;
00086 
00087  private:
00088   //: compute the determinant of the covariance
00089   void compute_det();
00090 
00091   //: cache for inverse of the covariance matrix
00092   mutable vnl_matrix_fixed<T,n,n>* inv_covar_;
00093 };
00094 
00095 template <class T , unsigned n>
00096 inline vcl_ostream& operator<< (vcl_ostream& os,
00097                                 bsta_gaussian_full<T, n> const& g)
00098 {
00099   return
00100   os << "gauss_full:mean(" << g.mean() << ")\n"
00101      << "gauss_full:covar(\n" << g.covar() << ")\n";
00102 }
00103 
00104 #endif // bsta_gaussian_full_h_