mlpack  2.0.1
positive_definite_constraint.hpp
Go to the documentation of this file.
1 
14 #ifndef __MLPACK_METHODS_GMM_POSITIVE_DEFINITE_CONSTRAINT_HPP
15 #define __MLPACK_METHODS_GMM_POSITIVE_DEFINITE_CONSTRAINT_HPP
16 
17 #include <mlpack/core.hpp>
18 
19 namespace mlpack {
20 namespace gmm {
21 
30 {
31  public:
38  static void ApplyConstraint(arma::mat& covariance)
39  {
40  // What we want to do is make sure that the matrix is positive definite and
41  // that the condition number isn't too large. We also need to ensure that
42  // the covariance matrix is not too close to zero (hence, we ensure that all
43  // eigenvalues are at least 1e-50).
44  arma::vec eigval;
45  arma::mat eigvec;
46  arma::eig_sym(eigval, eigvec, covariance);
47 
48  // If the matrix is not positive definite or if the condition number is
49  // large, we must project it back onto the cone of positive definite
50  // matrices with reasonable condition number (I'm picking 1e5 here, not for
51  // any particular reason).
52  if ((eigval[0] < 0.0) || ((eigval[eigval.n_elem - 1] / eigval[0]) > 1e5) ||
53  (eigval[eigval.n_elem - 1] < 1e-50))
54  {
55  // Project any negative eigenvalues back to non-negative, and project any
56  // too-small eigenvalues to a large enough value. Make them as small as
57  // possible to satisfy our constraint on the condition number.
58  const double minEigval = std::max(eigval[eigval.n_elem - 1] / 1e5, 1e-50);
59  for (size_t i = 0; i < eigval.n_elem; ++i)
60  eigval[i] = std::max(eigval[i], minEigval);
61 
62  // Now reassemble the covariance matrix.
63  covariance = eigvec * arma::diagmat(eigval) * eigvec.t();
64  }
65  }
66 
68  template<typename Archive>
69  static void Serialize(Archive& /* ar */, const unsigned int /* version */) { }
70 };
71 
72 } // namespace gmm
73 } // namespace mlpack
74 
75 #endif
76 
Linear algebra utility functions, generally performed on matrices or vectors.
static void ApplyConstraint(arma::mat &covariance)
Apply the positive definiteness constraint to the given covariance matrix, and ensure each value on t...
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
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.