View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Safe computation of logarithm-determinat of large matrix

5.0 | 7 ratings Rate this file 18 Downloads (last 30 days) File Size: 2.25 KB File ID: #22026 Version: 1.0

Safe computation of logarithm-determinat of large matrix


Dahua Lin (view profile)


Computes the logarithm of determinant of a matrix in a safe way that avoids overflow/underflow.

| Watch this File

File Information

Logarithm of determinant of a matrix widely occurs in the context of multivariate statistics. The log-pdf, entropy, and divergence of Gaussian distribution typically comprises a term in form of log-determinant. This function might be useful there, especially in a high-dimensional space.

However, the standard way of computing log-determinant:


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 log-determinant is equal to the sum of their logarithm values. Therefore, we can effectively tackle the problem by computing sum-of-log rather than computing log-of-product. 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 log-determinant 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.

MATLAB release MATLAB 7.6 (R2008a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
24 Oct 2016 Doo-Hyun

07 Aug 2016 ziad alabbasi

10 Feb 2015 Sun Kitty

helps me so much! Many thanks!

07 Aug 2013 Ruben van Bergen

Life saver!

10 Jan 2013 Omar Choudary

24 Feb 2012 John D'Errico

John D'Errico (view profile)

I admit, I simply don't like determinants. They are used in the wrong places, and far too often. However, if you have a valid reason to use this tool then you have chosen well.

Excellent help. Well coded. A nice tool.

24 Feb 2012 Nicolas Beuchat

Nicolas Beuchat (view profile)

Contact us