Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function/is correlation #315

Merged
merged 5 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions examples/correlation_matrices/sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,11 @@ void correlation_matrix_uniform_sampling_MT(const unsigned int n, const unsigned
std::cout << "Elapsed time : " << time << " (ms)" << std::endl;

int valid_points = 0;
EigenvaluesProblems<NT, MT, Eigen::Matrix<NT, Eigen::Dynamic, 1>> solver;
for(const auto& points : randPoints){
if(is_correlation_matrix(points.mat)){
valid_points++;
}
if(solver.is_correlation_matrix(points.mat)){
valid_points++;
}
}
std::cout << "Number of valid points = " << valid_points << std::endl;

Expand Down
20 changes: 20 additions & 0 deletions include/matrix_operations/EigenvaluesProblems.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,26 @@ class EigenvaluesProblems<NT, Eigen::Matrix<NT,Eigen::Dynamic,Eigen::Dynamic>, E
return false;
}

/// Check if a matrix is indeed a correlation matrix
/// return true if input matrix is found to be a correlation matrix
/// |param[in] matrix
bool is_correlation_matrix(const MT& matrix){

const double tol = 1e-8;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a function parameter with default value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


//check if all the diagonal elements are ones
for(int i=0 ; i<matrix.rows() ; i++){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please leave one empty space between for and (, similar to if etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

if(std::abs(matrix(i, i)-1.0) > tol){
return false;
}
}

//check if the matrix is positive definite
if(isPositiveSemidefinite(matrix)) return true;

return false;
}

/// Minimum positive eigenvalue of the generalized eigenvalue problem A - lB
/// Use Eigen::GeneralizedSelfAdjointEigenSolver<MT> ges(B,A) (faster)
/// \param[in] A: symmetric positive definite matrix
Expand Down
Loading