positive_definite_constraint.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_GMM_POSITIVE_DEFINITE_CONSTRAINT_HPP
13 #define MLPACK_METHODS_GMM_POSITIVE_DEFINITE_CONSTRAINT_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace gmm {
19 
28 {
29  public:
36  static void ApplyConstraint(arma::mat& covariance)
37  {
38  // What we want to do is make sure that the matrix is positive definite and
39  // that the condition number isn't too large. We also need to ensure that
40  // the covariance matrix is not too close to zero (hence, we ensure that all
41  // eigenvalues are at least 1e-50).
42  arma::vec eigval;
43  arma::mat eigvec;
44  arma::eig_sym(eigval, eigvec, covariance);
45 
46  // If the matrix is not positive definite or if the condition number is
47  // large, we must project it back onto the cone of positive definite
48  // matrices with reasonable condition number (I'm picking 1e5 here, not for
49  // any particular reason).
50  if ((eigval[0] < 0.0) || ((eigval[eigval.n_elem - 1] / eigval[0]) > 1e5) ||
51  (eigval[eigval.n_elem - 1] < 1e-50))
52  {
53  // Project any negative eigenvalues back to non-negative, and project any
54  // too-small eigenvalues to a large enough value. Make them as small as
55  // possible to satisfy our constraint on the condition number.
56  const double minEigval = std::max(eigval[eigval.n_elem - 1] / 1e5, 1e-50);
57  for (size_t i = 0; i < eigval.n_elem; ++i)
58  eigval[i] = std::max(eigval[i], minEigval);
59 
60  // Now reassemble the covariance matrix.
61  covariance = eigvec * arma::diagmat(eigval) * eigvec.t();
62  }
63  }
64 
70  static void ApplyConstraint(arma::vec& diagCovariance)
71  {
72  // If the matrix is not positive definite or if the condition number is
73  // large, we must project it back onto the cone of positive definite
74  // matrices with reasonable condition number (I'm picking 1e5 here, not for
75  // any particular reason).
76  double maxEigval = -DBL_MAX;
77  for (size_t i = 0; i < diagCovariance.n_elem; ++i)
78  {
79  if (diagCovariance[i] > maxEigval)
80  maxEigval = diagCovariance[i];
81  }
82 
83  for (size_t i = 0; i < diagCovariance.n_elem; ++i)
84  {
85  if ((diagCovariance[i] < 0.0) || ((maxEigval / diagCovariance[i]) > 1e5)
86  || (maxEigval < 1e-50))
87  {
88  diagCovariance[i] = std::max(maxEigval / 1e5, 1e-50);
89  }
90  }
91  }
92 
94  template<typename Archive>
95  static void serialize(Archive& /* ar */, const unsigned int /* version */) { }
96 };
97 
98 } // namespace gmm
99 } // namespace mlpack
100 
101 #endif
102 
.hpp
Definition: add_to_po.hpp:21
static void ApplyConstraint(arma::mat &covariance)
Apply the positive definiteness constraint to the given covariance matrix, and ensure each value on t...
static void ApplyConstraint(arma::vec &diagCovariance)
Apply the positive definiteness constraint to the given diagonal covariance matrix (which is represen...
The core includes that mlpack expects; standard C++ includes and Armadillo.
static void serialize(Archive &, const unsigned int)
Serialize the constraint (which stores nothing, so, nothing to do).
Given a covariance matrix, force the matrix to be positive definite.