Skip to content

Instantly share code, notes, and snippets.

@cwidmer
Last active August 29, 2015 14:07

Revisions

  1. cwidmer revised this gist Oct 17, 2014. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion log_det.py
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,14 @@
    t0 = time.time()
    np.linalg.slogdet(K)
    tnp = time.time() - t0 + tK
    print "tnp:", tnp
    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()
  2. cwidmer revised this gist Oct 17, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions log_det.py
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    X = np.random.randn(1000, 10000)

    t0 = time.time()
    K = X.dot(X.T)
    K = X.dot(X.T) + 1e-9*np.eye(X.shape[0])
    tK = time.time() - t0
    print "tK:", tK

    @@ -32,7 +32,7 @@

    # using cholesky (creates lower-triagular matrix, for which eigenvalues can be read off the diagonal)
    t0 = time.time()
    L = np.linalg.cholesky(K+1e-9*np.eye(K.shape[0]))
    L = np.linalg.cholesky(K)
    log_det_chol = np.log(np.diag(L)**2).sum()
    tcho = time.time() - t0 + tK
    print "tcho:", tcho
  3. cwidmer renamed this gist Oct 17, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. cwidmer revised this gist Oct 17, 2014. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions log_det
    Original file line number Diff line number Diff line change
    @@ -36,3 +36,17 @@ L = np.linalg.cholesky(K+1e-9*np.eye(K.shape[0]))
    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)
    """
  5. cwidmer revised this gist Oct 17, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion log_det
    Original file line number Diff line number Diff line change
    @@ -33,6 +33,6 @@ 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+1e-9*np.eye(K.shape[0]))
    log_det_chol = np.log(np.diag(meh)**2).sum()
    log_det_chol = np.log(np.diag(L)**2).sum()
    tcho = time.time() - t0 + tK
    print "tcho:", tcho
  6. cwidmer revised this gist Oct 17, 2014. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions log_det
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,38 @@
    import numpy as np
    import time

    # generate data
    X = np.random.randn(1000, 10000)

    t0 = time.time()
    K = X.dot(X.T)
    tK = time.time() - t0
    print "tK:", tK

    # np function
    t0 = time.time()
    np.linalg.slogdet(K)
    tnp = time.time() - t0 + tK
    print "tnp:", 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+1e-9*np.eye(K.shape[0]))
    log_det_chol = np.log(np.diag(meh)**2).sum()
    tcho = time.time() - t0 + tK
    print "tcho:", tcho
  7. cwidmer revised this gist Oct 17, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion log_det
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    import numpy as np

    # generate data
    X = ...
    X = np.random.randn(1000, 10000)
    K = X.dot(X.T)

    # np function
  8. cwidmer created this gist Oct 17, 2014.
    17 changes: 17 additions & 0 deletions log_det
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    import numpy as np

    # generate data
    X = ...
    K = X.dot(X.T)

    # np function
    np.linalg.slogdet(K)

    # using eigh
    S1, U1 = np.linalg.eigh(K)
    np.log(S1[S1 > 1e-9]).sum()

    # using svd
    U,S,V = np.linalg.svd(X, full_matrices=False)
    S2 = S*S
    np.log(S2[S2 > 1e-9]).sum()