The log of a determinant in Matlab

If you’re calculating log likelihoods (for example) it may be that you have a large covariance matrix for which you want to calculate the log of its determinant. In many cases the actual value of the determinant of the matrix may be to large for computational precision to handle, and in e.g. Matlab det(A) might return an Inf – this in turn would give an Inf for the log of the determinant. The solution, if you’re just wanting the log likelihood anyway, is given by this post, but I repeat the main Matlab commands (Note: the covariance matrix must be positive definite for this to work), for a covariance matrix A:

L = chol(A);
logdetA = 2*sum(log(diag(L)));

Leave a Reply