-
-
Notifications
You must be signed in to change notification settings - Fork 190
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
check generalized inverse for full rank symmetric mat #2577
Conversation
…4.1 (tags/RELEASE_600/final)
…/math into generalized_inverse_fix
…4.1 (tags/RELEASE_600/final)
This reverts commit cf36885.
…4.1 (tags/RELEASE_600/final)
…/math into generalized_inverse_fix
…4.1 (tags/RELEASE_600/final)
…4.1 (tags/RELEASE_600/final)
Instead of creating two identity matrices, create two vectors and add diag
This reverts commit 27c2647.
…4.1 (tags/RELEASE_600/final)
…/math into generalized_inverse_fix
…4.1 (tags/RELEASE_600/final)
…4.1 (tags/RELEASE_600/final)
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
if (G_ref.rows() == G_ref.cols()) { | ||
Eigen::CompleteOrthogonalDecomposition< | ||
Eigen::Matrix<value_type_t<EigMat>, EigMat::RowsAtCompileTime, | ||
EigMat::ColsAtCompileTime>> | ||
complete_ortho_decomp_G = G_ref.completeOrthogonalDecomposition(); | ||
if (!(complete_ortho_decomp_G.rank() < G_ref.rows())) | ||
return inverse(G_ref); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So my only Q is how much this costs / accuracy of computing the completeOrthogonalDecomposition()
and then using it for the check and then sometimes using it for the pseudoinverse. If someone passes an NxM matrix here i sort of feels like they either
- Would know ahead of time that they will or will not have a square matrix
- Know their matrix is low rank and they can't use
inverse()
Are those not good assumptions? If they are then should we always just do the pseudoinverse()? Is the accuracy of pseudoinverse pretty low compared to inverse()? I almost feel like even if so someone using this would expect a pseudoinverse instead of the inverse
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are great questions!
On
If someone passes an NxM matrix here i sort of feels like they either
- Would know ahead of time that they will or will not have a square matrix
- Know their matrix is low rank and they can't use
inverse()
I'm interested in the case where you'd specifically pass a square matrix but not know ahead of time if the matrix is low rank or not. It's probably low-rank but it may not be (say in a factor model).
This got me to revisit the paper that the code is based on. The author does something that I missed because he notates L
as a cholesky factor when it is actually a modified cholesky factor that works even for low rank matrices. This is how he is able to not do a rank find (which one usually does either as I've done here or using SVD). I've coded up the prim version at https://github.com/spinkney/math/tree/generalized_inverse_low_rank_chol. The point is that some rank issue must be dealt with or there's numerical instability issues.
The code now follows the paper explicitly. The major change involves adding a cholesky_low_rank_decomposition
lambda. I've coded it using Eigen block notation. I believe there's further optimizations that could be done, if someone with a background in high performance computing helps. It could be added to Stan as a separate function, which is cool in it's own right.
The prim tests pass. I haven't benchmarked though to see how it compares to the completeOrthogonalDecomposition()
version here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've put together an example how low rank chol can transform a low rank matrix of "factors" into a valid correlation matrix. I'm not sure if this works all the time but it's promising. low_rank_chol_eg.R.zip
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice! So with the low rank chol version should we close this PR and open one up based on the low rank chol? Or should we merge this then make a separate PR for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking that the low rank chol should be the final version. Though I think it may be worth just getting this update to fix the symmetric low-rank case (for this release cycle) and then in the next release having 2 PRs: 1) add low rank chol as a function and 2) update gen inverse using it in a new PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
@rok-cesnovar @SteveBronder this passed before and that commit shouldn't have failed. I'm not sure what that strange CI error is, there's no info, is this a server issue? |
Yeah seems like a machine went down. Restarted only the upstream tests, should be done in an hour: https://jenkins.mc-stan.org/blue/organizations/jenkins/Math%20Pipeline/detail/PR-2577/26/pipeline |
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
@SteveBronder this is good to go! |
@spinkney go ahead and merge. You should be able to. |
Summary
Fixes #2576. Add a check on symmetric matrices for full rank. Uses the
FullPivLUcompleteOrthogonalDecomposition to check the rank and returns Eigen's pseudoInverse from the complete ortho-decomp if rank < full rank (otherwise just do the regular inverse).Tests
Added a test for a less than full rank symmetric matrix.
Side Effects
No.
Release notes
Replace this text with a short note on what will change if this pull request is merged in which case this will be included in the release notes.
Checklist
Math issue Generalized inverse should work for less than full rank symmetric matrices #2576
Copyright holder: Sean Pinkney
the basic tests are passing
the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested