contrib/brl/bbas/bsta/bsta_gaussian_indep.h
Go to the documentation of this file.
00001 // This is brl/bbas/bsta/bsta_gaussian_indep.h
00002 #ifndef bsta_gaussian_indep_h_
00003 #define bsta_gaussian_indep_h_
00004 //:
00005 // \file
00006 // \brief A Gaussian distribution, independent in each dimension
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 <vcl_iostream.h>
00019 #include <vnl/vnl_random.h>
00020 
00021 //: A Gaussian distribution, independent in each dimension
00022 // Thus, the covariance matrix is diagonal
00023 template <class T, unsigned n>
00024 class bsta_gaussian_indep : public bsta_gaussian<T,n>
00025 {
00026  public:
00027   typedef vnl_vector_fixed<T,n> covar_type;
00028   typedef typename bsta_gaussian<T,n>::vector_type vector_;
00029 
00030   //: Constructor
00031   bsta_gaussian_indep()
00032   : bsta_gaussian<T,n>(), diag_covar_(T(0)), det_covar_(T(0)) {}
00033 
00034   //: Constructor
00035   bsta_gaussian_indep(const vnl_vector_fixed<T,n>& mean,
00036                       const covar_type& covar)
00037   : bsta_gaussian<T,n>(mean), diag_covar_(covar), det_covar_(T(-1))
00038   { compute_det(); }
00039 
00040   //: The diagonal covariance of the distribution
00041   const covar_type& diag_covar() const
00042   { return diag_covar_; }
00043 
00044   //: Generic access to covariance or variance across Gaussian subtypes
00045   const covar_type& covar() const
00046   { return diag_covar_; }
00047 
00048   //: generic set covariance across Gaussian subtypes
00049   void set_covar(const covar_type& covar)
00050   { diag_covar_ = covar; compute_det(); }
00051 
00052   //: The probability density at this sample given square mahalanobis distance
00053   T dist_prob_density(const T& sqr_mahal_dist) const
00054   {
00055     if (det_covar_ <= 0)
00056       return T(0);
00057     return static_cast<T>(vcl_sqrt(1/(det_covar_*two_pi_power<n>::value()))
00058          * vcl_exp(-sqr_mahal_dist/2));
00059   }
00060 
00061   //: The probability density at this sample
00062   T prob_density(const vnl_vector_fixed<T,n>& pt) const
00063   {
00064     return dist_prob_density(sqr_mahalanobis_dist(pt));
00065   }
00066 
00067   //: The probability that a sample lies inside a n-d box
00068   T probability(const vnl_vector_fixed<T,n>& min_pt,
00069                 const vnl_vector_fixed<T,n>& max_pt) const;
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   //: sample from the distribution
00078   vector_ sample(vnl_random& ran_gen) const
00079   {
00080     vector_ d = bsta_gaussian<T,n>::mean_;
00081     covar_type v = diag_covar_;
00082     for (unsigned j = 0; j < n; j++) {
00083       v[j] = (T)(vcl_sqrt(v[j])*ran_gen.normal());
00084     }
00085     vector_ sum = d+v;
00086     return sum;
00087   }
00088 
00089   //: Gradient vector at this point
00090   covar_type gradient(const vnl_vector_fixed<T,n>& pt) const;
00091   
00092  protected:
00093   //: The diagonal covariance matrix stored as a vector
00094   covar_type diag_covar_;
00095 
00096   //: The cached covariance determinant
00097   T det_covar_;
00098 
00099  private:
00100   //: compute the determinant of the covariance
00101   void compute_det();
00102 };
00103 
00104 template <class T , unsigned n>
00105 inline vcl_ostream& operator<< (vcl_ostream& os,
00106                                 bsta_gaussian_indep<T, n> const& g)
00107 {
00108   os << "gauss_indep:mean(" << g.mean() << ")\n"
00109      << "gauss_indep:covar(" << g.diag_covar() << ")\n";
00110   return os;
00111 }
00112 
00113 #endif // bsta_gaussian_indep_h_