mlpack  master
eigenvalue_ratio_constraint.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_GMM_EIGENVALUE_RATIO_CONSTRAINT_HPP
13 #define MLPACK_METHODS_GMM_EIGENVALUE_RATIO_CONSTRAINT_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace gmm {
19 
28 {
29  public:
36  EigenvalueRatioConstraint(const arma::vec& ratios) :
37  // Make an alias of the ratios vector. It will never be modified here.
38  ratios(const_cast<double*>(ratios.memptr()), ratios.n_elem, false)
39  {
40  // Check validity of ratios.
41  if (std::abs(ratios[0] - 1.0) > 1e-20)
42  Log::Fatal << "EigenvalueRatioConstraint::EigenvalueRatioConstraint(): "
43  << "first element of ratio vector is not 1.0!" << std::endl;
44 
45  for (size_t i = 1; i < ratios.n_elem; ++i)
46  {
47  if (ratios[i] > 1.0)
48  Log::Fatal << "EigenvalueRatioConstraint::EigenvalueRatioConstraint(): "
49  << "element " << i << " of ratio vector is greater than 1.0!"
50  << std::endl;
51  if (ratios[i] < 0.0)
52  Log::Warn << "EigenvalueRatioConstraint::EigenvalueRatioConstraint(): "
53  << "element " << i << " of ratio vectors is negative and will "
54  << "probably cause the covariance to be non-invertible..."
55  << std::endl;
56  }
57  }
58 
62  void ApplyConstraint(arma::mat& covariance) const
63  {
64  // Eigendecompose the matrix.
65  arma::vec eigenvalues;
66  arma::mat eigenvectors;
67  arma::eig_sym(eigenvalues, eigenvectors, covariance);
68 
69  // Change the eigenvalues to what we are forcing them to be. There
70  // shouldn't be any negative eigenvalues anyway, so it doesn't matter if we
71  // are suddenly forcing them to be positive. If the first eigenvalue is
72  // negative, well, there are going to be some problems later...
73  eigenvalues = (eigenvalues[0] * ratios);
74 
75  // Reassemble the matrix.
76  covariance = eigenvectors * arma::diagmat(eigenvalues) * eigenvectors.t();
77  }
78 
80  template<typename Archive>
81  void Serialize(Archive& ar, const unsigned int /* version */)
82  {
83  // Strip the const for the sake of loading/saving. This is the only time it
84  // is modified (other than the constructor).
85  ar & data::CreateNVP(const_cast<arma::vec&>(ratios), "ratios");
86  }
87 
88  private:
90  const arma::vec ratios;
91 };
92 
93 } // namespace gmm
94 } // namespace mlpack
95 
96 #endif
void ApplyConstraint(arma::mat &covariance) const
Apply the eigenvalue ratio constraint to the given covariance matrix.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
const arma::vec ratios
Ratios for eigenvalues.
The core includes that mlpack expects; standard C++ includes and Armadillo.
FirstShim< T > CreateNVP(T &t, const std::string &name, typename std::enable_if_t< HasSerialize< T >::value > *=0)
Call this function to produce a name-value pair; this is similar to BOOST_SERIALIZATION_NVP(), but should be used for types that have a Serialize() function (or contain a type that has a Serialize() function) instead of a serialize() function.
static MLPACK_EXPORT util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:87
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
EigenvalueRatioConstraint(const arma::vec &ratios)
Create the EigenvalueRatioConstraint object with the given vector of eigenvalue ratios.
void Serialize(Archive &ar, const unsigned int)
Serialize the constraint.
Given a vector of eigenvalue ratios, ensure that the covariance matrix always has those eigenvalue ra...