Last active
August 29, 2015 14:07
-
-
Save cwidmer/37f9879adc6ab6359c57 to your computer and use it in GitHub Desktop.
different methods for log determinants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import time | |
# generate data | |
X = np.random.randn(1000, 10000) | |
t0 = time.time() | |
K = X.dot(X.T) + 1e-9*np.eye(X.shape[0]) | |
tK = time.time() - t0 | |
print "tK:", tK | |
# np function | |
t0 = time.time() | |
np.linalg.slogdet(K) | |
tnp = time.time() - t0 + tK | |
print "tslogdet:", tnp | |
# np function | |
t0 = time.time() | |
np.log(np.linalg.det(K)) | |
tnp = time.time() - t0 + tK | |
print "tdet:", tnp | |
# using eigh | |
t0 = time.time() | |
S1, U1 = np.linalg.eigh(K) | |
np.log(S1[S1 > 1e-9]).sum() | |
teig = time.time() - t0 + tK | |
print "teig:", teig | |
# using svd | |
t0 = time.time() | |
U,S,V = np.linalg.svd(X, full_matrices=False) | |
S2 = S*S | |
np.log(S2[S2 > 1e-9]).sum() | |
tsvd = time.time() - t0 | |
print "tsvd:", tsvd | |
# using cholesky (creates lower-triagular matrix, for which eigenvalues can be read off the diagonal) | |
t0 = time.time() | |
L = np.linalg.cholesky(K) | |
log_det_chol = np.log(np.diag(L)**2).sum() | |
tcho = time.time() - t0 + tK | |
print "tcho:", tcho | |
""" | |
output: | |
tK: 0.680999994278 | |
tnp: 0.746999979019 | |
teig: 1.02300000191 | |
tsvd: 4.94499993324 | |
tcho: 0.770999908447 | |
verdict: | |
use cholesky if other operations are done on matrices | |
if only log det is needed, slogdet is fine (uses LU factorzation internally) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment