KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
prob_estimation.h
Go to the documentation of this file.
1 /* ======================================================================== *
2  | |
3  | Copyright (c) 2007-2010, by members of University of Arizona Computer |
4  | Vision group (the authors) including |
5  | Kobus Barnard, Kyle Simek, Ernesto Brau. |
6  | |
7  | For use outside the University of Arizona Computer Vision group please |
8  | contact Kobus Barnard. |
9  | |
10  * ======================================================================== */
11 
12 /* $Id$ */
13 
14 #ifndef PROB_ESTIMATION_H_INCLUDED
15 #define PROB_ESTIMATION_H_INCLUDED
16 
30 #include <l_cpp/l_exception.h>
31 #include <algorithm>
32 #include <cmath>
33 #include <functional>
34 #include <boost/accumulators/accumulators.hpp>
35 #include <boost/accumulators/statistics/stats.hpp>
36 #include <boost/accumulators/statistics/mean.hpp>
37 #include <boost/accumulators/statistics/variance.hpp>
38 #include <boost/bind.hpp>
39 #include <boost/ref.hpp>
40 
41 namespace kjb {
42 
43 /* @brief Returns the MLE of the parameters of a normal. */
44 template<class InIter>
45 inline
46 Normal_distribution mle_normal(InIter first, InIter last)
47 {
48  namespace ba = boost::accumulators;
49 
50  ba::accumulator_set<double, ba::stats<ba::tag::variance> > acc;
51  std::for_each(first, last, boost::bind<void>(boost::ref(acc), _1));
52 
53  return Normal_distribution(ba::mean(acc), std::sqrt(ba::variance(acc)));
54 }
55 
56 /* @brief Returns the MLE of the parameters of a Beta. */
57 template<class InIter>
58 Beta_distribution mle_beta(InIter first, InIter last)
59 {
60  namespace ba = boost::accumulators;
61 
62  ba::accumulator_set<double, ba::stats<ba::tag::variance> > acc;
63  std::for_each(first, last, boost::bind<void>(boost::ref(acc), _1));
64 
65  double m = ba::mean(acc);
66  double v = ba::variance(acc);
67 
68  // compute parameters, if possible
69  IFT(v < m*(1 - m), Runtime_error,
70  "Cannot compute Beta distribution parameters with this method.");
71 
72  // boost computes alpha and beta this way
73  //double alpha = m * ((m*(1 - m))/v - 1);
74  //double beta = (1 - m) * ((m*(1 - m))/v - 1);
75  double alpha = Beta_distribution::find_alpha(m, v);
76  double beta = Beta_distribution::find_beta(m, v);
77 
78  return Beta_distribution(alpha, beta);
79 }
80 
81 /* @brief Returns the MLE of the parameters of a Gamma. */
82 template<class InIter>
83 Gamma_distribution mle_gamma(InIter first, InIter last)
84 {
85  double N = std::distance(first, last);
86  double sum = std::accumulate(first, last, 0.0);
87  double lsum = std::accumulate(
88  first, last, 0.0,
89  boost::bind(
90  std::plus<double>(), _1,
91  boost::bind(
92  static_cast<double(*)(double)>(std::log),
93  _2)));
94 
95  double s = std::log(sum/N) - lsum/N;
96  double k = (3 - s + std::sqrt((s - 3)*(s - 3) + 24*s)) / (12*s);
97  double t = sum/(N*k);
98 
99  return Gamma_distribution(k, t);
100 }
101 
102 /* @brief Returns the MLE of the parameters of a V-M-F. */
103 template<size_t D, class InIter>
104 Von_mises_fisher_distribution<D> mle_vmf(InIter first, InIter last)
105 {
106  const size_t N = std::distance(first, last);
107  Vector_d<D> sum = accumulate(first, last, Vector_d<D>(0.0));
108  double mag = sum.magnitude();
109 
110  // mu
111  Vector_d<D> mu = sum / mag;
112 
113  // kappa
114  double R = mag / N;
115  double kappa = (R*(3 - R*R)) / (1 - R*R);
116 
117  return Von_mises_fisher_distribution<D>(mu, kappa);
118 }
119 
120 } //namespace kjb
121 
122 
123 #endif /* PROB_ESTIMATION_H_INCLUDED */
124 
double magnitude() const
return the l2-norm of this vector
Definition: m_vector_d.impl.h:369
Definition of various standard probability distributions.
double accumulate(const Matrix_d< R, C, T > &mat, double init)
Definition: m_matrix_d.impl.h:432
for k
Definition: APPgetLargeConnectedEdges.m:61
Definition: prob_distribution.h:984
Gamma_distribution mle_gamma(InIter first, InIter last)
Definition: prob_estimation.h:83
Value_type mean(Iterator begin, Iterator end, const Value_type &)
Definition: prob_stat.h:56
#define IFT(a, ex, msg)
Definition: l_exception.h:101
Von_mises_fisher_distribution< D > mle_vmf(InIter first, InIter last)
Definition: prob_estimation.h:104
boost::math::beta_distribution Beta_distribution
Definition: prob_distribution.h:61
sum(zmx.*zmy) sum(zmy.^2)]
boost::math::normal Normal_distribution
Definition: prob_distribution.h:68
Definition: g_quaternion.h:37
for m
Definition: APPgetLargeConnectedEdges.m:64
Support for error handling exception classes in libKJB.
boost::math::gamma_distribution Gamma_distribution
Definition: prob_distribution.h:65
Normal_distribution mle_normal(InIter first, InIter last)
Definition: prob_estimation.h:46
Object thrown when computation fails somehow during execution.
Definition: l_exception.h:321
Beta_distribution mle_beta(InIter first, InIter last)
Definition: prob_estimation.h:58