Skip to content

Instantly share code, notes, and snippets.

@tbates
Last active December 10, 2015 17:58
# Above are some results from the a matrix of a
# trivariate cholesky with the above diag cells filled in
A = matrix(nrow = 3, byrow = T, c(.71, -.28, .15, -.28, .61, -.38, .15, -.38, .000001))
[,1] [,2] [,3]
[1,] 0.71 -0.28 1.5e-01
[2,] -0.28 0.61 -3.8e-01
[3,] 0.15 -0.38 1.0e-06
# Yeah... so that's wrong. that's the lower a matrix.
# So let's get back to that lower and start again:
a <- A # make a copy
a[upper.tri(a)] <- 0 # empty the upper triangle, and have a look at what we've got
round(a,2)
[,1] [,2] [,3]
[1,] 0.71 0.00 0
[2,] -0.28 0.61 0
[3,] 0.15 -0.38 0
# Now big A is:
A = a %*% t(a) # see how var three now has some variance...
[,1] [,2] [,3]
[1,] 0.5041 -0.1988 0.1065
[2,] -0.1988 0.4505 -0.2738
[3,] 0.1065 -0.2738 0.1669
# And then:
I = diag(3)
rg = solve(sqrt(I * A)) %&% A
round(rg,2)
[,1] [,2] [,3]
[1,] 1.00 -0.42 0.37
[2,] -0.42 1.00 -1.00
[3,] 0.37 -1.00 1.00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment