Skip to content

[sol upstream candidate] cov_matrix_free accepts indefinite matrices when LLT fails #27

Description

@sims1253

cov_matrix_free() checks squareness, nonzero size, and a positive diagonal,
then consumes Eigen::LLT::matrixL() without checking llt.info(). A positive
diagonal does not make a matrix positive definite.

Minimal repro against develop at 344d7167a:

Eigen::MatrixXd bad(2, 2);
bad << 1.0, 2.0,
       2.0, 1.0;
auto z = stan::math::cov_matrix_free(bad);
auto roundtrip = stan::math::cov_matrix_constrain(z, 2);
std::cout << z.transpose() << "\n"
          << (roundtrip - bad).cwiseAbs().maxCoeff() << "\n";

Actual result:

0 2 0
4

The inverse transform accepts an indefinite matrix and returns a finite vector
that reconstructs a different covariance matrix. The adjacent
cov_matrix_free_lkj() implementation already checks factorization success.

Check llt.info() == Eigen::Success before reading the factor, or use the
existing positive-definite validation seam. Add an indefinite positive-diagonal
case, a near-singular positive-definite case, and a valid round-trip case.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions