KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
prob_conditional_distribution.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: prob_conditional_distribution.h 17393 2014-08-23 20:19:14Z predoehl $ */
13 
14 #ifndef PROB_CONDITIONAL_DISTRIBUTION_H_INCLUDED
15 #define PROB_CONDITIONAL_DISTRIBUTION_H_INCLUDED
16 
17 
31 #include "prob_cpp/prob_pdf.h"
32 #include "prob_cpp/prob_sample.h"
33 
34 namespace kjb {
35 
36 /*==================================================================================================
37  Declaration of conditional distribution classes. There could
38  potentially be dozens of these, due to the fact that the
39  dependencies are not predefined.
40  ==================================================================================================*/
41 
61 template<class TargetVariable, class GivenVariable, class DependenceFunc>
63 {
64 private:
65  DependenceFunc depfunc;
66 
67 public:
70 
71  Conditional_distribution(DependenceFunc f) :
72  depfunc(f)
73  {}
74 
76  depfunc(cd.depfunc)
77  {}
78 
80  {
81  if(&cd != this)
82  {
83  depfunc = cd.depfunc;
84  }
85 
86  return *this;
87  }
88 
93  TargetVariable get_distribution(const given_type& y) const
94  {
95  return depfunc(y);
96  }
97 };
98 
103 template<class Target, class Given, class Func>
104 inline double conditional_pdf
105 (
107  const typename Distribution_traits<Target>::type& x,
108  const typename Distribution_traits<Given>::type& y
109 )
110 {
111  return pdf(cd.get_distribution(y), x);
112 }
113 
118 template<class Target, class Given, class Func>
119 inline double conditional_log_pdf
120 (
122  const typename Distribution_traits<Target>::type& x,
123  const typename Distribution_traits<Given>::type& y
124 )
125 {
126  return log_pdf(cd.get_distribution(y), x);
127 }
128 
133 template<class Target, class Given, class Func>
134 inline typename Distribution_traits<Target>::type conditional_sample
135 (
137  const typename Distribution_traits<Given>::type& y
138 )
139 {
140  return sample(cd.get_distribution(y));
141 }
142 
143 
144 /********************************************************
145  * SOME DEPENDENCE FUNCTIONS *
146  ********************************************************/
147 
163 {
164 public:
170  (
171  const Normal_distribution& PX,
172  const Normal_distribution& PY,
173  double covariance_XY
174  ) :
175  X(PX),
176  Y(PY),
177  cov_XY(covariance_XY)
178  {}
179 
186  Normal_on_normal_dependence(double conditional_std_dev) :
187  X(0, sqrt(conditional_std_dev * conditional_std_dev + 1)),
188  Y(0, 1),
189  cov_XY(1)
190  {}
191 
193  X(nond.X),
194  Y(nond.Y),
195  cov_XY(nond.cov_XY)
196  {}
197 
199  {
200  if(&nond != this)
201  {
202  X = nond.X;
203  Y = nond.Y;
204  cov_XY = nond.cov_XY;
205  }
206 
207  return *this;
208  }
209 
211  {
212  double var_X = X.standard_deviation() * X.standard_deviation();
213  double var_Y = Y.standard_deviation() * Y.standard_deviation();
214 
215  double mu = X.mean() + ((cov_XY / var_Y) * (y - Y.mean()));
216  double sigma = var_X - ((cov_XY * cov_XY) / var_Y);
217  return Normal_distribution(mu, sqrt(sigma));
218  }
219 
220 private:
223  double cov_XY;
224 };
225 
227 
229 
231 
247 {
248 public:
254  (
255  const MV_normal_distribution& PX,
256  const MV_normal_distribution& PY,
257  const Matrix& covariance_XY
258  ) :
259  X(PX),
260  Y(PY),
261  cov_XY(covariance_XY)
262  {
263  if(cov_XY.get_num_rows() != X.get_dimension() || cov_XY.get_num_cols() != Y.get_dimension())
264  {
265  KJB_THROW_2(Dimension_mismatch, "Dimension of covariance matrix between X and Y is wrong.");
266  }
267  }
268 
275  MV_normal_on_normal_dependence(const Matrix& conditional_cov, int dim_y) :
276  X(Vector(conditional_cov.get_num_rows(), 0.0),
277  conditional_cov + create_identity_matrix(conditional_cov.get_num_rows())),
278  Y(Vector(dim_y, 0.0), create_identity_matrix(dim_y)),
279  cov_XY(create_identity_matrix(conditional_cov.get_num_rows(), dim_y))
280  {}
281 
283  X(nond.X),
284  Y(nond.Y),
285  cov_XY(nond.cov_XY)
286  {}
287 
289  {
290  if(&nond != this)
291  {
292  X = nond.X;
293  Y = nond.Y;
294  cov_XY = nond.cov_XY;
295  }
296 
297  return *this;
298  }
299 
301  {
302  const Vector& mu_X = X.get_mean();
303  const Vector& mu_Y = Y.get_mean();
304  const Matrix& Sigma_X = X.get_covariance_matrix();
305  const Matrix& Sigma_Y = Y.get_covariance_matrix();
306 
307  Vector mu = mu_X + (cov_XY * matrix_inverse(Sigma_Y) * (y - mu_Y));
308  Matrix Sigma = Sigma_X - (cov_XY * matrix_inverse(Sigma_Y) * matrix_transpose(cov_XY));
309 
310  return MV_normal_distribution(mu, Sigma);
311  }
312 
313 private:
316  Matrix cov_XY;
317 };
318 
320 
322 
324 
325 
326 } //namespace kjb
327 
328 #endif /*PROB_CONDITIONAL_DISTRIBUTION_H_INCLUDED */
329 
Distribution_traits< Target >::type conditional_sample(const Conditional_distribution< Target, Given, Func > &cd, const typename Distribution_traits< Given >::type &y)
Produces a sample from the given conditioanl distro, given a value for the given variable.
Definition: prob_conditional_distribution.h:135
Represents the dependence between X and Y in p(x | y), where (x,y) is a multivariate normal...
Definition: prob_conditional_distribution.h:246
Normal_on_normal_dependence(double conditional_std_dev)
Specify this conditional distributon by specifying the conditional variance, meaning the mean will si...
Definition: prob_conditional_distribution.h:186
const Matrix & get_covariance_matrix() const
Gets the covariance matrix of this distribution.
Definition: prob_distribution.h:689
Conditional_distribution< MV_normal_distribution, MV_normal_distribution, MV_normal_on_normal_dependence > MV_normal_conditional_distribution
Definition: prob_conditional_distribution.h:321
Definition of various standard probability distributions.
double conditional_pdf(const Conditional_distribution< Target, Given, Func > &cd, const typename Distribution_traits< Target >::type &x, const typename Distribution_traits< Given >::type &y)
Gets the conditional pdf p(x | y) of the given ConditionalDistribution.
Definition: prob_conditional_distribution.h:105
MV_gaussian_distribution MV_normal_distribution
Definition: prob_distribution.h:813
Object thrown when an argument is of the wrong size or dimensions.
Definition: l_exception.h:426
Normal_conditional_distribution Gaussian_conditional_distribution
Definition: prob_conditional_distribution.h:230
int get_num_rows() const
Return the number of rows in the matrix.
Definition: m_matrix.h:543
Conditional_distribution & operator=(const Conditional_distribution &cd)
Definition: prob_conditional_distribution.h:79
MV_normal_on_normal_dependence(const Matrix &conditional_cov, int dim_y)
Specify this conditional distributon by specifying the conditional variance, meaning the mean will si...
Definition: prob_conditional_distribution.h:275
MV_normal_on_normal_dependence & operator=(const MV_normal_on_normal_dependence &nond)
Definition: prob_conditional_distribution.h:288
Conditional_distribution(const Conditional_distribution &cd)
Definition: prob_conditional_distribution.h:75
Normal_on_normal_dependence(const Normal_distribution &PX, const Normal_distribution &PY, double covariance_XY)
Specify this conditional distributon by specifying the marginals and the covariance.
Definition: prob_conditional_distribution.h:170
Normal_on_normal_dependence(const Normal_on_normal_dependence &nond)
Definition: prob_conditional_distribution.h:192
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
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
Vector sample(const MV_gaussian_distribution &dist)
Sample from a multivariate normal distribution.
Definition: prob_sample.cpp:42
double conditional_log_pdf(const Conditional_distribution< Target, Given, Func > &cd, const typename Distribution_traits< Target >::type &x, const typename Distribution_traits< Given >::type &y)
Gets the conditional log-pdf log p(x | y) of the given ConditionalDistribution.
Definition: prob_conditional_distribution.h:120
Matrix matrix_inverse(const Matrix &op1)
Invert this matrix.
Definition: m_matrix.cpp:730
int get_num_cols() const
Return the number of columns in the matrix.
Definition: m_matrix.h:554
Int_matrix matrix_transpose(const Int_matrix &op1)
Test for any difference between two matrices.
Definition: l_int_matrix.h:1331
int get_dimension() const
Gets the dimension of this distribution.
Definition: prob_distribution.h:743
MV_normal_conditional_distribution MV_gaussian_conditional_distribution
Definition: prob_conditional_distribution.h:323
PDFs and CDFs for the different distributions defined in "prob_distribution.h".
double type
Definition: prob_distribution.h:111
A conditional distribution.
Definition: prob_conditional_distribution.h:62
x
Definition: APPgetLargeConnectedEdges.m:100
Distribution_traits< GivenVariable >::type given_type
Definition: prob_conditional_distribution.h:69
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
MV_normal_on_normal_dependence(const MV_normal_distribution &PX, const MV_normal_distribution &PY, const Matrix &covariance_XY)
Specify this conditional distributon by specifying the marginals and the covariance.
Definition: prob_conditional_distribution.h:254
Multivariate Gaussian (normal) distribution.
Definition: prob_distribution.h:619
Normal_on_normal_dependence & operator=(const Normal_on_normal_dependence &nond)
Definition: prob_conditional_distribution.h:198
Matrix create_identity_matrix(int rank)
Construct an identity matrix of specified rank.
Definition: m_matrix.h:1795
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
Conditional_distribution(DependenceFunc f)
Definition: prob_conditional_distribution.h:71
Normal_on_normal_dependence Gaussian_on_gaussian_dependence
Definition: prob_conditional_distribution.h:226
boost::math::normal Normal_distribution
Definition: prob_distribution.h:68
Sampling functionality for the different distributions defined in "prob_distributions.h".
MV_normal_on_normal_dependence(const MV_normal_on_normal_dependence &nond)
Definition: prob_conditional_distribution.h:282
Represents the dependence between X and Y in p(x | y), where (x,y) is a bivariate normal...
Definition: prob_conditional_distribution.h:162
MV_normal_distribution operator()(const Vector &y) const
Definition: prob_conditional_distribution.h:300
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
const Vector & get_mean() const
Gets the mean of this distribution.
Definition: prob_distribution.h:681
MV_normal_on_normal_dependence MV_gaussian_on_gaussian_dependence
Definition: prob_conditional_distribution.h:319
Conditional_distribution< Normal_distribution, Normal_distribution, Normal_on_normal_dependence > Normal_conditional_distribution
Definition: prob_conditional_distribution.h:228
TargetVariable get_distribution(const given_type &y) const
Gets the distribution when we fix the given variable; i.e., it applies the dependence function to y...
Definition: prob_conditional_distribution.h:93
Normal_distribution operator()(double y) const
Definition: prob_conditional_distribution.h:210
Distribution_traits< TargetVariable >::type target_type
Definition: prob_conditional_distribution.h:68