contrib/brl/bbas/bsta/bsta_gaussian_sphere.h
Go to the documentation of this file.
00001 // This is brl/bbas/bsta/bsta_gaussian_sphere.h
00002 #ifndef bsta_gaussian_sphere_h_
00003 #define bsta_gaussian_sphere_h_
00004 //:
00005 // \file
00006 // \brief A (hyper-)spherical Gaussian distribution (i.e. single variance)
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 <vcl_iostream.h>
00018 #include <vnl/vnl_random.h>
00019 
00020 //: A (hyper-)spherical Gaussian distribution
00021 // Thus, the covariance matrix is the identity times a scalar variance
00022 template <class T, unsigned n>
00023 class bsta_gaussian_sphere : public bsta_gaussian<T,n>
00024 {
00025  public:
00026   typedef typename bsta_gaussian<T,n>::vector_type vector_;
00027   typedef  T covar_type;
00028   //: Constructor
00029   bsta_gaussian_sphere()
00030   : bsta_gaussian<T,n>(), var_(T(0)), det_covar_(T(0)) {}
00031 
00032   //: Constructor
00033   bsta_gaussian_sphere(const vector_& mean,
00034                        const covar_type& var)
00035   : bsta_gaussian<T,n>(mean), var_(var), det_covar_(T(-1))
00036   { compute_det(); }
00037 
00038   //: The variance of the distribution
00039   const covar_type& var() const { return var_; }
00040 
00041   //: Set the variance of the distribution
00042   void set_var(const covar_type& var) { var_ = var; compute_det(); }
00043 
00044   //: generic access to covariance or variance across Gaussians
00045   const covar_type& covar() const {return var_;}
00046 
00047   //: generic set covariance across Gaussian subtypes
00048   void set_covar(const covar_type& covar) {this->set_var(covar);}
00049 
00050   //: The probability density at this sample given square mahalanobis distance
00051   T dist_prob_density(const T& sqr_mahal_dist) const
00052   {
00053     if (det_covar_ <= 0)
00054       return T(0);
00055     return static_cast<T>(vcl_sqrt(1/(det_covar_*two_pi_power<n>::value()))
00056          * vcl_exp(-sqr_mahal_dist/2));
00057   }
00058 
00059   //: The probability density at this sample
00060   T prob_density(const vector_& pt) const
00061   {
00062     return dist_prob_density(sqr_mahalanobis_dist(pt));
00063   }
00064 
00065   //: The probability integrated over a box
00066   T probability(const vector_& min_pt,
00067                 const vector_& max_pt) const;
00068 
00069   //: The squared Mahalanobis distance to this point
00070   T sqr_mahalanobis_dist(const vector_& pt) const;
00071 
00072   //: Compute the determinant of the covariance matrix
00073   T det_covar() const { return det_covar_; }
00074 
00075   //: sample from the distribution
00076   vector_ sample(vnl_random& rng) const;
00077 
00078  protected:
00079   //: The variance
00080   covar_type var_;
00081 
00082   //: The cached covariance determinant
00083   T det_covar_;
00084 
00085  private:
00086   //: compute the determinant of the covariance
00087   void compute_det();
00088 };
00089 
00090 template <class T , unsigned n>
00091 inline vcl_ostream& operator<< (vcl_ostream& os,
00092                                 bsta_gaussian_sphere<T, n> const& g)
00093 {
00094   os << "gauss_sphere:mean(" << g.mean() << ")\n"
00095      << "gauss_sphere:var(" << g.var() << ")\n";
00096   return os;
00097 }
00098 
00099 #endif // bsta_gaussian_sphere_h_