RcppArmadillo icon indicating copy to clipboard operation
RcppArmadillo copied to clipboard

warning: chol(): given matrix is not symmetric

Open emmaleeberry opened this issue 3 years ago • 7 comments

Hi, I'm working on a MCMC estimation algorithm by using "RcppArmadillo", but got the error message "warning: chol(): given matrix is not symmetric" and the algorithm stops due to this issue. I checked the problematic matrix and found that they're symmetric, and the off-diagonal values are very small (close to 0). I tried to enforce the matrix as symmetric by using "symmatu/symmatl" but still doesn't work. I'm wondering if RcppArmadillo has strict rule on symmetric matrix, and what the threshold is. I'm using the cholesky decomposition in generating multivariate normal samples. If there's no way that I can deal with the issue, I'm considering using R package for the sample generation, is there anyway of using other R packages in RcppArmadillo code? Many thanks!

emmaleeberry avatar May 06 '22 14:05 emmaleeberry

Two things:

  • For (Rcpp)Armadillo please check the (excellent, I find) Armadillo documentation: https://arma.sourceforge.net (URL corrected). Numerical tolerance was recently tightened as a default, I am sure you find how to relax it

  • We cannot really do general R trouble shooting here. I find the mailing lists (and/or StackOverflow, in this case maybe even the stats site "CrossValidated" at https://stats.stackexchange.com/) to be of help.

eddelbuettel avatar May 06 '22 15:05 eddelbuettel

Many thanks for your quick response! For some reason the Armadillo documentation link (https://arma.sf.net/) can't be reached...

emmaleeberry avatar May 06 '22 15:05 emmaleeberry

My apologies: it is at http://arma.sourceforge.net/

eddelbuettel avatar May 06 '22 15:05 eddelbuettel

@emmaleeberry The warning from chol() is not an error; it doesn't break anything; it merely tells you there is something not right with the given matrix.

Most forms of the chol() function require that the given matrix is not simply symmetric, but symmetric and positive definite, as stated in the documentation.

More details on definite matrices: https://en.wikipedia.org/wiki/Definite_matrix

The symmatu() / symmatl() functions will generate a symmetric matrix, but this doesn't address the positive definite aspect. As a workaround, it's possible to iteratively add small positive values to the diagonal and keep re-trying chol(). Example:

bool ok = false;
while(ok == false) {
    ok = chol(output_matrix, input_matrix);
    if(ok == false) {
     input_matrix.diag() += 0.01;
    }
}

conradsnicta avatar May 06 '22 15:05 conradsnicta

Yes. Also difficulties with simulated data / estimation by simulation where for 'N' large enough you are likely to be at, or over, some invisible border. I added try/catch logic back in the day of my MC similations.... We can keep it open for a little bit but I agree that this can likely be closed.

eddelbuettel avatar May 06 '22 15:05 eddelbuettel

@emmaleeberry The warning from chol() is not an error; it doesn't break anything; it merely tells you there is something not right with the given matrix.

Most forms of the chol() function require that the given matrix is not simply symmetric, but symmetric and positive definite, as stated in the documentation.

More details on definite matrices: https://en.wikipedia.org/wiki/Definite_matrix

The symmatu() / symmatl() functions will generate a symmetric matrix, but this doesn't address the positive definite aspect. As a workaround, it's possible to iteratively add small positive values to the diagonal and keep re-trying chol(). Example:

bool ok = false;
while(ok == false) {
    ok = chol(output_matrix, input_matrix);
    if(ok == false) {
     input_matrix.diag() += 0.01;
    }
}

Thanks for your response! I don't think the matrix isn't positive definite as I tried to extract the matrix and used it in other package for the same purpose of generating the samples that I want. But I'll definitely double check and try your suggestions.

emmaleeberry avatar May 06 '22 16:05 emmaleeberry

My apologies: it is at http://arma.sourceforge.net/

Thanks!

emmaleeberry avatar May 06 '22 16:05 emmaleeberry