Skip to content

Instantly share code, notes, and snippets.

@kcarnold
Created April 22, 2014 00:24
Show Gist options
  • Save kcarnold/11161244 to your computer and use it in GitHub Desktop.
Save kcarnold/11161244 to your computer and use it in GitHub Desktop.
Reconstruct the actual Cholesky factor of a covariance matrix parameterized using the LKJ "onion method"
def reconstruct_lkj_cholesky(ell, R2):
num_items = len(R2) + 1
L = np.zeros((num_items, num_items))
L[0, 0] = 1.
L[1, 0] = 2. * R2[0] - 1.0
L[1, 1] = np.sqrt(1.0 - L[1, 0])
start = 0
for i in range(2, num_items):
ell_row = ell[start:start+i]
assert len(ell_row) == i
scale = np.sqrt(R2[i-1] / np.dot(ell_row, ell_row))
L[i, :i] = scale * ell_row
L[i, i] = np.sqrt(1.0 - R2[i-1])
start += i
return L
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment