1

While trying to write code for a Dirichlet process Gibbs Sampler, using a Normal-Wishart model, I got an error that the matrix I want to use as scale matrix for the posterior is not symmetric. I also tried it by myslef and got the following

is.symmetric.matrix(solve(cov(data)))

resulted FALSE as output. Also, the cov(data) is a symmetric matrix, which should make solve(cov(data)) a symmetric matrix too. While I tried the base packaage equivalent function for checking whether a matrix is symmetric

isSymmetric.matrix(solve(cov(data)))

I got a TRUE as an answer

Any ideas why this happens?

1 Answers1

3

The difference is how to the two functions measure equality.

LaplacesDemon::is.symmetric.matrix uses strict equality

 return(sum(x == t(x)) == (nrow(x)^2))

This can be problematic when comparing float numbers: see Why are these numbers not equal? To use this function, one approach, you could round your covariance matrix.

isSymmetric.matrix uses all.equal: if the numbers are equal up to a tolerance

all.equal(object, t(object), tolerance = tol, ...)
Community
  • 1
  • 1
user20650
  • 24,654
  • 5
  • 56
  • 91