# Log determinant of positive definite matrices in Matlab

In Bayesian data analysis, the log determinant of symmetric positive definite matrices often pops up as a normalizing constant in MAP estimates with multivariate Gaussians (ie, chapter 27 of Mackay). Oftentimes, the determinant of A will evaluate as infinite in Matlab although the log det is finite, so one can’t use log(det(A)). However, we know that:

1. $\mathbf{A} = \mathbf{L}\mathbf{L}'$ (Cholesky decomposition)
2. $|\mathbf{L}| = \prod_i L_{ii}$ (determinant of a lower triangular matrix)
3. $\log \prod_i x_i = \sum_i \log x_i$ (log rule)

Thus to calculate the log determinant of a symmetric positive definite matrix:

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


1. james says:

Thank you for this! Was having problems with log(det(K)) for optimizing GP hyperparameters.

2. Victor Hugo says:

Thanks for sharing! Does it work for positive semi-definite matrices too?

3. […] 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 […]

4. […] 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 most be positive definite for […]

5. […] inference in models with Gaussian priors or likelihoods. They can be quite troublesome to compute; done through the Cholesky decomposition, it’s an operation. It’s pretty much infeasible to compute the exact log det for […]