KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mcmcda_likelihood.h
Go to the documentation of this file.
1 /* =========================================================================== *
2  |
3  | Copyright (c) 1994-2011 by Kobus Barnard (author)
4  |
5  | Personal and educational use of this code is granted, provided that this
6  | header is kept intact, and that the authorship is not misrepresented, that
7  | its use is acknowledged in publications, and relevant papers are cited.
8  |
9  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
10  |
11  | Please note that the code in this file has not necessarily been adequately
12  | tested. Naturally, there is no guarantee of performance, support, or fitness
13  | for any particular task. Nonetheless, I am interested in hearing about
14  | problems that you encounter.
15  |
16  | Author: Ernesto Brau
17  * =========================================================================== */
18 
19 #ifndef MCMCDA_LIKELIHOOD_H_INCLUDED
20 #define MCMCDA_LIKELIHOOD_H_INCLUDED
21 
24 #include <gp_cpp/gp_multi_output.h>
25 #include <gp_cpp/gp_mean.h>
26 #include <gp_cpp/gp_covariance.h>
27 #include <m_cpp/m_vector.h>
28 #include <cmath>
29 #include <boost/function.hpp>
30 
31 namespace kjb {
32 namespace mcmcda {
33 
43 template<class Track>
45 {
46 private:
47  typedef typename Track::Element Element;
48  typedef typename Association<Track>::Available_data Available_data;
49  typedef Independent_mo_gaussian_process<Zero, Squared_exponential> Gp;
50 
51 private:
52  Gp gp;
53  double m_noise_sigma;
54  typename Data<Element>::Convert m_convert;
55  mutable Imogp_distribution mll;
56  mutable Gp_inputs X;
57  double noise_prob;
58  mutable bool fixed_inputs;
59  mutable bool limits_set;
60  mutable int low_lim;
61  mutable int up_lim;
62 
63 public:
71  (
72  double scale,
73  double signal_sigma,
74  double noise_sigma,
75  const typename Data<Element>::Convert& to_vector
76  ) :
77  gp(std::vector<Zero>(2, Zero()),
78  std::vector<Squared_exponential>(2,
79  Squared_exponential(scale, signal_sigma))
80  ),
81  m_noise_sigma(noise_sigma),
82  m_convert(to_vector),
83  mll(Imogp_distribution::Mvn_vector(1, MV_normal_distribution(2))),
84  fixed_inputs(false),
85  limits_set(false)
86  {
87  double ssd = sqrt(signal_sigma);
88  noise_prob = pdf(Normal_distribution(0, ssd), 1.3 * ssd);
89  }
90 
95  double operator()(const Association<Track>& w) const;
96 
100  double at_noise_point(const Element& pt) const
101  {
102  return m_convert(pt).get_length() * std::log(noise_prob);
103  }
104 
108  double at_noise(const Available_data& false_alarms) const;
109 
113  double at_track(const Track& track) const;
114 
118  double get_noise_sigma() const
119  {
120  return m_noise_sigma;
121  }
122 
126  const Gp& get_gp() const
127  {
128  return gp;
129  }
130 
134  const typename Data<Element>::Convert& get_convert() const
135  {
136  return m_convert;
137  }
138 
140  void fix_inputs(const Gp_inputs& ins, size_t dim) const
141  {
142  X = ins;
143  std::vector<double> noise_sigmas(dim, m_noise_sigma);
144  mll = gp.get_ml_distribution(X, noise_sigmas);
145  fixed_inputs = true;
146  }
147 
149  void unfix_inputs() const
150  {
151  fixed_inputs = false;
152  }
153 
155  void set_limits(int low, int up) const
156  {
157  low_lim = low;
158  up_lim = up;
159  limits_set = true;
160  }
161 
163  void reset_limits() const
164  {
165  limits_set = false;
166  }
167 };
168 
169 /*============================================================================*
170  * MEMBER FUNCTION DEFINITIONS *
171  *----------------------------------------------------------------------------*/
172 
173 template<class Track>
175 {
176  double ll = 0.0;
177 
178  for(typename Association<Track>::const_iterator track_p = w.begin();
179  track_p != w.end();
180  track_p++)
181  {
182  ll += at_track(*track_p);
183  }
184 
185  ll += at_noise(w.get_available_data());
186 
187  return ll;
188 }
189 
190 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
191 
192 template<class Track>
193 double Likelihood<Track>::at_noise(const Available_data& false_alarms) const
194 {
195  double ll = 0;
196  size_t t = 0;
197  for(typename Available_data::const_iterator set_p = false_alarms.begin();
198  set_p != false_alarms.end();
199  set_p++)
200  {
201  if(limits_set && (++t < low_lim || t > up_lim)) continue;
202 
203  for(typename std::set<const Element*>::const_iterator
204  elem_pp = set_p->begin();
205  elem_pp != set_p->end();
206  elem_pp++)
207  {
208  ll += at_noise_point(**elem_pp);
209 
210  /*if(t % 2 != 0)
211  {
212  ll += at_noise_point(**elem_pp);
213  }*/
214  }
215  }
216 
217  return ll;
218 }
219 
220 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
221 
222 template<class Track>
223 double Likelihood<Track>::at_track(const Track& track) const
224 {
225  bool recompute_inputs = !fixed_inputs || limits_set
226  || track.size() != X.size();
227 
228  typename Track::const_iterator b_pair_p;
229  typename Track::const_iterator e_pair_p;
230  size_t track_size;
231 
232  if(limits_set)
233  {
234  b_pair_p = track.lower_bound(low_lim);
235  e_pair_p = track.upper_bound(up_lim);
236  track_size = std::distance(b_pair_p, e_pair_p);
237 
238  if(track_size == 0) return 0.0;
239  }
240  else
241  {
242  b_pair_p = track.begin();
243  e_pair_p = track.end();
244  track_size = track.size();
245  }
246 
247  if(recompute_inputs)
248  {
249  //X.resize(track_size);
250  X.clear();
251  X.reserve(2*track_size);
252  }
253 
254  Mogp_outputs Y;
255  Y.reserve(2*track_size);
256 
257  int i = 0;
258  for(typename Track::const_iterator pair_p = b_pair_p;
259  pair_p != e_pair_p;
260  pair_p++, i++)
261  {
262  int t = pair_p->first;
263  //if(t % 2 != 0){}
264  Vector avg(m_convert(*pair_p->second).size(), 0.0);
265  size_t k = 0;
266  while(pair_p != e_pair_p && pair_p->first == t)
267  {
268  if(recompute_inputs)
269  {
270  //X[i] = Vector(static_cast<double>(pair_p->first));
271  X.push_back(Vector().set(t + k*0.001));
272  avg += m_convert(*pair_p->second);
273  k++;
274  pair_p++;
275  }
276  }
277 
278  avg /= k;
279 
280  for(size_t j = 0; j < k; j++)
281  {
282  //Y[i] = m_convert(*pair_p->second);
283  Y.push_back(avg);
284  }
285 
286  pair_p--;
287  }
288 
289  if(recompute_inputs)
290  {
291  std::vector<double> noise_sigmas(Y[0].get_length(), m_noise_sigma);
292  mll = gp.get_ml_distribution(X, noise_sigmas);
293  }
294 
295  return log_pdf(mll, Y);
296 }
297 
298 }} //namespace kjb::mcmcda
299 
300 #endif /*MCMCDA_LIKELIHOOD_H_INCLUDED */
301 
Available_data get_available_data() const
Computes "available data".
Definition: mcmcda_association.h:143
Parent::const_iterator const_iterator
Definition: mcmcda_association.h:122
MV_gaussian_distribution MV_normal_distribution
Definition: prob_distribution.h:813
void reset_limits() const
Unser limits on evaluation.
Definition: mcmcda_likelihood.h:163
for k
Definition: APPgetLargeConnectedEdges.m:61
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
A class that represents a MCMCDA association.
Definition: mcmcda_association.h:66
double pdf(const MV_gaussian_distribution &P, const Vector &x)
Computes the joint PDF of a multivariate Gaussian at x.
Definition: prob_pdf.cpp:32
double at_noise_point(const Element &pt) const
Returns the likelihood of the noise track.
Definition: mcmcda_likelihood.h:100
double get_noise_sigma() const
Return the noise sigma of this model.
Definition: mcmcda_likelihood.h:118
const Gp & get_gp() const
Return the smoothness scale of this model.
Definition: mcmcda_likelihood.h:126
boost::function1< Vector, const Element & > Convert
Definition: mcmcda_data.h:53
double log_pdf(const MV_gaussian_distribution &P, const Vector &x)
Computes the log PDF a multivariate normal distribution at x.
Definition: prob_pdf.cpp:64
Likelihood(double scale, double signal_sigma, double noise_sigma, const typename Data< Element >::Convert &to_vector)
Constructor.
Definition: mcmcda_likelihood.h:71
double operator()(const Association< Track > &w) const
Applies this functor to the given association.
Definition: mcmcda_likelihood.h:174
void set_limits(int low, int up) const
Set limits on evaluation.
Definition: mcmcda_likelihood.h:155
void unfix_inputs() const
Fixes the inputs for faster likelihood computation.
Definition: mcmcda_likelihood.h:149
boost::math::normal Normal_distribution
Definition: prob_distribution.h:68
double at_track(const Track &track) const
Computes the GP log-likelihood of a track.
Definition: mcmcda_likelihood.h:223
const Data< Element >::Convert & get_convert() const
Return the convert function.
Definition: mcmcda_likelihood.h:134
double at_noise(const Available_data &false_alarms) const
Returns the likelihood of the noise track.
Definition: mcmcda_likelihood.h:193
void fix_inputs(const Gp_inputs &ins, size_t dim) const
Fixes the inputs for faster likelihood computation.
Definition: mcmcda_likelihood.h:140
Computes the GP-based likelihood of an association.
Definition: mcmcda_likelihood.h:44
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
void scale(kjb::Axis_aligned_rectangle_2d &box, const kjb::Vector &s)
Definition: gr_2D_bounding_box.cpp:108
std::vector< Elementp_set > Available_data
Definition: mcmcda_association.h:79
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...