Logarithm of determinant of a matrix widely occurs in the context of multivariate statistics. The logpdf, entropy, and divergence of Gaussian distribution typically comprises a term in form of logdeterminant. This function might be useful there, especially in a highdimensional space.
However, the standard way of computing logdeterminant:
log(det(A));
is likely to run into underflow or overflow, especially for large matrices, such as 1000 x 1000 or larger ones. The main cause of the problem is that the value of determinant is too large to be represented by a single or double number. However, what is needed is the logarithm of this value rather than the determinant itself. So an approach to circumvent this difficulty is to keep the computation at logarithm scale.
The key idea of the implementation is based on the mathematical fact that the determinant of a triangular matrix equals the product of its diagonal elements. Hence, the matrix's logdeterminant is equal to the sum of their logarithm values. Therefore, we can effectively tackle the problem by computing sumoflog rather than computing logofproduct. For generic matrix, it uses LU factorization, while for positive definite matrices, cholesky factorization can be used, which is typically more efficient.
The core of the implementation contains only several lines. Actually, this function is not to solve kind of complicated problems, but rather it is aimed at offering convenient help for you when you want to compute logdeterminant of large matrices. The usage is very simple:
For generic matrix:
v = logdet(A);
for positive definite matrix (such as covariance matrix), it is faster to write
v = logdet(A, 'chol');
Hope it helps.
