
Check and repair a matrix to be positive definite and symmetric
Source:R/func_MNIW_sampler.R
check_pds.RdVerifies that a matrix is approximately symmetric (within a relative and absolute tolerance). If so, symmetrises it and, if still not positive definite, adds a small diagonal ridge. Raises an error if the asymmetry exceeds the tolerance.
Examples
# A covariance matrix with a tiny rounding asymmetry
A <- matrix(c(2, 1, 1 + 1e-8, 2), 2)
isSymmetric(A)
#> [1] FALSE
A_fixed <- check_pds(A)
isSymmetric(A_fixed)
#> [1] TRUE
A_fixed
#> [,1] [,2]
#> [1,] 2 1
#> [2,] 1 2