mlpack: a scalable c++ machine learning library
mlpack  2.0.2
eigenvalue_ratio_constraint.hpp
Go to the documentation of this file.
1 
14 #ifndef mlpack_METHODS_GMM_EIGENVALUE_RATIO_CONSTRAINT_HPP
15 #define mlpack_METHODS_GMM_EIGENVALUE_RATIO_CONSTRAINT_HPP
16 
17 #include <mlpack/core.hpp>
18 
19 namespace mlpack {
20 namespace gmm {
21 
30 {
31  public:
38  EigenvalueRatioConstraint(const arma::vec& ratios) :
39  // Make an alias of the ratios vector. It will never be modified here.
40  ratios(const_cast<double*>(ratios.memptr()), ratios.n_elem, false)
41  {
42  // Check validity of ratios.
43  if (std::abs(ratios[0] - 1.0) > 1e-20)
44  Log::Fatal << "EigenvalueRatioConstraint::EigenvalueRatioConstraint(): "
45  << "first element of ratio vector is not 1.0!" << std::endl;
46 
47  for (size_t i = 1; i < ratios.n_elem; ++i)
48  {
49  if (ratios[i] > 1.0)
50  Log::Fatal << "EigenvalueRatioConstraint::EigenvalueRatioConstraint(): "
51  << "element " << i << " of ratio vector is greater than 1.0!"
52  << std::endl;
53  if (ratios[i] < 0.0)
54  Log::Warn << "EigenvalueRatioConstraint::EigenvalueRatioConstraint(): "
55  << "element " << i << " of ratio vectors is negative and will "
56  << "probably cause the covariance to be non-invertible..."
57  << std::endl;
58  }
59  }
60 
64  void ApplyConstraint(arma::mat& covariance) const
65  {
66  // Eigendecompose the matrix.
67  arma::vec eigenvalues;
68  arma::mat eigenvectors;
69  arma::eig_sym(eigenvalues, eigenvectors, covariance);
70 
71  // Change the eigenvalues to what we are forcing them to be. There
72  // shouldn't be any negative eigenvalues anyway, so it doesn't matter if we
73  // are suddenly forcing them to be positive. If the first eigenvalue is
74  // negative, well, there are going to be some problems later...
75  eigenvalues = (eigenvalues[0] * ratios);
76 
77  // Reassemble the matrix.
78  covariance = eigenvectors * arma::diagmat(eigenvalues) * eigenvectors.t();
79  }
80 
82  template<typename Archive>
83  void Serialize(Archive& ar, const unsigned int /* version */)
84  {
85  // Strip the const for the sake of loading/saving. This is the only time it
86  // is modified (other than the constructor).
87  ar & data::CreateNVP(const_cast<arma::vec&>(ratios), "ratios");
88  }
89 
90  private:
92  const arma::vec ratios;
93 };
94 
95 } // namespace gmm
96 } // namespace mlpack
97 
98 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
FirstShim< T > CreateNVP(T &t, const std::string &name, typename boost::enable_if< HasSerialize< T >>::type *=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.
const arma::vec ratios
Ratios for eigenvalues.
static mlpack_EXPORT util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:89
static mlpack_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:92
EigenvalueRatioConstraint(const arma::vec &ratios)
Create the EigenvalueRatioConstraint object with the given vector of eigenvalue ratios.
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
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...
void ApplyConstraint(arma::mat &covariance) const
Apply the eigenvalue ratio constraint to the given covariance matrix.