Skip to content

Instantly share code, notes, and snippets.

@hannesdatta
Created April 11, 2022 13:37
Show Gist options
  • Select an option

  • Save hannesdatta/74bcd0d833a73d13a735a9fee423dca7 to your computer and use it in GitHub Desktop.

Select an option

Save hannesdatta/74bcd0d833a73d13a735a9fee423dca7 to your computer and use it in GitHub Desktop.
From a variance-covariance matrix to a cholesky decomposition (and back)
# From a variance-covariance matrix to a cholesky decomposition (and back)
## Let's first define our var-covar matrix
varcovar = diag(3)
varcovar[1,2] <- .5
varcovar[2,1] <- .5
## Let's get our (upper) cholesky decomposition
decomp = chol(varcovar)
# See - we can go back to the real var-covar by taking the crossproduct
crossprod(decomp)
## From an upper cholesky to the var-covar matrix
uchol = diag(3)
uchol[upper.tri(uchol, diag=T)] <- c(1, .5, .866, 0, 0, 1)
crossprod(uchol)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment