Skip to content

Commit

Permalink
Warn users on non-PSD kernel matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
kdm9 committed Jan 10, 2016
1 parent 28763d5 commit 92ff36b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ calculate_pairwise(std::vector<std::string> &hash_fnames)
if (verbosity > 0) {
*outstream << "Done all!" << std::endl;
}

if (!matrix_is_pos_semidef(_kernel_m)) {
*outstream << "WARNING: The kernel matrix is not positive semidefinite."
<< std::endl;
} else {
*outstream << "The kernel matrix is positive semidefinite."
<< std::endl;
}
}

void
Expand Down
9 changes: 9 additions & 0 deletions src/kwip-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@


#include "kwip-utils.hh"
#include <Eigen/Eigenvalues>


namespace kwip
{
Expand Down Expand Up @@ -120,4 +122,11 @@ kernel_to_distance(MatrixXd &dist, MatrixXd &kernel, bool normalise)
}
}

bool
matrix_is_pos_semidef(MatrixXd &mat)
{
VectorXd eigenvalues = mat.eigenvalues().real();
return eigenvalues.minCoeff() > -1e-5;
}

} // end namespace kwip
5 changes: 5 additions & 0 deletions src/kwip-utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <Eigen/Core>

using Eigen::MatrixXd;
using Eigen::VectorXd;

#include <kwip-config.hh>

Expand All @@ -37,6 +38,10 @@ void print_lsmat(MatrixXd &mat, std::ostream &outstream,
void normalise_matrix(MatrixXd &norm, MatrixXd &input);
void kernel_to_distance(MatrixXd &dist, MatrixXd &kernel, bool normalise=true);

// Checks if a matrix is postitive semi-definite. Specifically, that all
// eigenvalues are > -1e-5.
bool matrix_is_pos_semidef(MatrixXd &mat);

template <typename eltype>
eltype
vec_min(std::vector<eltype> &vec)
Expand Down

0 comments on commit 92ff36b

Please sign in to comment.