vpdt_log_probability.h
Go to the documentation of this file.
1 // This is core/vpdl/vpdt/vpdt_log_probability.h
2 #ifndef vpdt_log_probability_h_
3 #define vpdt_log_probability_h_
4 //:
5 // \file
6 // \author Matthew Leotta
7 // \brief The basic functions for log of probability calculation
8 // \date March 13, 2009
9 //
10 // These functions provide default implementations for various log of
11 // probability calculation functions. They are written in terms of distribution
12 // member functions
13 //
14 // \verbatim
15 // Modifications
16 // None
17 // \endverbatim
18 
19 #include <limits>
21 #include <vnl/vnl_math.h>
22 #ifdef _MSC_VER
23 # include <vcl_msvc_warnings.h>
24 #endif
25 
26 //: Compute the log of the unnormalized density
27 template <class dist>
29 vpdt_log_density(const dist& d,
30  const typename vpdt_dist_traits<dist>::field_type& pt)
31 {
32  typedef typename vpdt_dist_traits<dist>::scalar_type T;
33  T density = d.density(pt);
34  if (density <= T(0))
35  return std::numeric_limits<T>::infinity();
36 
37  return static_cast<T>(std::log(density));
38 }
39 
40 
41 //: Compute the log of the normalized probability density
42 template <class dist>
44 vpdt_log_prob_density(const dist& d,
45  const typename vpdt_dist_traits<dist>::field_type& pt)
46 {
47  typedef typename vpdt_dist_traits<dist>::scalar_type T;
48  T norm = d.norm_const();
49  if (vnl_math::isinf(norm))
50  return -std::numeric_limits<T>::infinity();
51 
52  return static_cast<T>(std::log(norm) + vpdt_log_density(d,pt));
53 }
54 
55 
56 //: Compute the gradient of the log of the unnormalized density
57 template <class dist>
60  const typename vpdt_dist_traits<dist>::field_type& pt,
61  const typename vpdt_dist_traits<dist>::vector_type& g)
62 {
63  typedef typename vpdt_dist_traits<dist>::scalar_type T;
64  T density = d.gradient_density(pt,g);
65  if (density <= T(0)) {
66  vpdt_fill(g,T(0));
67  return std::numeric_limits<T>::infinity();
68  }
69 
70  g /= density;
71  return static_cast<T>(std::log(density));
72 }
73 
74 
75 #endif // vpdt_log_probability_h_
vpdt_field_traits< typename dist::field_type >::field_type field_type
the data type to represent a point in the field.
vpdt_dist_traits< dist >::scalar_type vpdt_log_density(const dist &d, const typename vpdt_dist_traits< dist >::field_type &pt)
Compute the log of the unnormalized density.
vpdt_dist_traits< dist >::scalar_type vpdt_log_prob_density(const dist &d, const typename vpdt_dist_traits< dist >::field_type &pt)
Compute the log of the normalized probability density.
vpdt_field_traits< field_type >::vector_type vector_type
the data type used for vectors (difference between points).
bool isinf(TArg arg)
vpdt_dist_traits< dist >::scalar_type vpdt_gradient_log_density(const dist &d, const typename vpdt_dist_traits< dist >::field_type &pt, const typename vpdt_dist_traits< dist >::vector_type &g)
Compute the gradient of the log of the unnormalized density.
specialized template trait classes for properties of a distribution type
vpdt_field_traits< field_type >::scalar_type scalar_type
The type used for scalar operations.
void vpdt_fill(vnl_vector< T > &v, const T &val)
Fill a vnl_vector.
Definition: vpdt_access.h:78