KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
prob_util.h
Go to the documentation of this file.
1 /* $Id: prob_util.h 18931 2015-04-22 21:50:53Z cdawson $ */
2 /* ======================================================================== *
3  | |
4  | Copyright (c) 2007-2010, by members of University of Arizona Computer |
5  | Vision group (the authors) including |
6  | Kobus Barnard, Kyle Simek, Ernesto Brau. |
7  | |
8  | For use outside the University of Arizona Computer Vision group please |
9  | contact Kobus Barnard. |
10  | |
11  * ======================================================================== */
12 
13 
14 #ifndef PROB_UTIL_H_INCLUDED
15 #define PROB_UTIL_H_INCLUDED
16 
24 #include <iterator>
25 #include <algorithm>
26 #include <cmath>
27 
28 namespace kjb{
29 
30  //forward declarations
31  class Vector;
32  class Matrix;
33 
34 #if 0
35 NOTE: I moved this to sample_cpp to resolve circular dependency between prob_util.h and prob_distribution.h. Besides, this is basically sampling, even
36  though its name isnt sample()...
37  Kyle May 19, 2012
44 template<class Iterator, class distance_type>
45 inline
46 Iterator element_uar(Iterator first, distance_type size)
47 {
49  Iterator p = first;
50  std::advance(p, dist);
51 
52  return p;
53 }
54 #endif
55 
56 
66 inline double log_sum(double n1, double n2)
67 {
68 /*
69  * the implementation below relies on this identity:
70  *
71  * log{ x1 + x2 + x3 + ...} =
72  * pi + log(sum_i exp( log xi - pi))
73  *
74  * where pi = max_i (log(xi))
75  */
76  if(n1 < n2)
77  std::swap(n1, n2);
78 
79  return n1 + log(1.0 + exp(n2-n1));
80 }
81 
94 inline double log_sum(double n1, double n2, double n3)
95 {
96 /*
97  * the implementation below relies on this identity:
98  *
99  * log{ x1 + x2 + x3 + ...} =
100  * pi + log(sum_i exp( log xi - pi))
101  *
102  * where pi = max_i (log(xi))
103  */
104  if(n1 < n2)
105  std::swap(n1, n2);
106  if(n1 < n3)
107  std::swap(n1, n3);
108 
109  return n1 + log(1.0 + exp(n2-n1) + exp(n3 - n1));
110 }
111 
123 template<class Iterator>
124 double log_sum(Iterator first, Iterator last)
125 {
126 /*
127  * the implementation below relies on this identity:
128  *
129  * log{ x1 + x2 + x3 + ...} =
130  * pi + log(sum_i exp( log xi - pi))
131  *
132  * where pi = max_i (log(xi))
133  *
134  */
135  double pi = *std::max_element(first, last);
136 
137  double accum = 0;
138 
139  for(; first != last; first++)
140  {
141  accum += exp(*first - pi);
142  }
143  return pi + log(accum);
144 }
145 
155 inline double log_diff(double n1, double n2)
156 {
157 /*
158  * the implementation below relies on this identity:
159  *
160  * log{ x1 - x2} = log(x2) + log(exp(log(x2/x1) - 1))
161  *
162  */
163  if(n1 < n2)
164  std::swap(n1, n2);
165 
166  return n2 + log(exp(n2-n1) - 1);
167 }
168 
174 Vector log_normalize(const Vector& vec);
175 
181 Vector exponentiate(const Vector& vec);
182 
188 Vector log_normalize_and_exponentiate(const Vector& vec);
189 
195 Matrix log_normalize_rows(const Matrix& mat);
196 
202 Matrix log_normalize_and_exponentiate(const Matrix& mat);
203 
209 Vector log_marginalize_over_cols(const Matrix& mat);
210 
216 Vector log_marginalize_over_rows(const Matrix& mat);
217 
218 }; // namespace kjb
219 
220 #endif /*PROB_UTIL_H_INCLUDED */
221 
Matrix log_normalize_rows(const Matrix &mat)
normalize the rows of a stochastic matrix in log space
Definition: prob_util.cpp:60
Vector log_normalize(const Vector &vec)
normalize a probability vector in log space
Definition: prob_util.cpp:15
double log_diff(double n1, double n2)
Definition: prob_util.h:155
Vector sample(const MV_gaussian_distribution &dist)
Sample from a multivariate normal distribution.
Definition: prob_sample.cpp:42
double dist(const pt a, const pt b)
compute approx. Great Circle distance between two UTM points
Definition: layer.cpp:45
Vector log_marginalize_over_rows(const Matrix &mat)
marginalize across the rows of a matrix of log probabilities
Definition: prob_util.cpp:73
for I
Definition: APPgetLargeConnectedEdges.m:141
Vector exponentiate(const Vector &vec)
elementwise exponentiate a vector without normalizing
Definition: prob_util.cpp:26
Vector log_normalize_and_exponentiate(const Vector &vec)
normalize and exponentiate a vector of unnormalized log probabilities
Definition: prob_util.cpp:36
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
Iterator element_uar(Iterator first, distance_type size)
Pick an element uniformly at random (UAR) from a sequence, represented by a beginning iterator and a ...
Definition: prob_sample.h:228
double log_sum(double n1, double n2)
Definition: prob_util.h:66
Vector log_marginalize_over_cols(const Matrix &mat)
marginalize down the columns of a matrix of log probabilities
Definition: prob_util.cpp:85