Created
October 13, 2015 08:32
-
-
Save RichPollock/04091c29789dc6e5c55c to your computer and use it in GitHub Desktop.
VBA function for Cholesky decomposition
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
Function CholeskyDecompose(matrix As Range) | |
Dim A, LTM() As Double, S As Double | |
Dim j As Long, K As Long, i As Long, N As Long, M As Long | |
A = matrix | |
N = matrix.Rows.Count | |
M = matrix.Columns.Count | |
If N <> M Then | |
CholeskyDecompose = "Non-invertible matrix" | |
Exit Function | |
End If | |
ReDim LTM(1 To N, 1 To N) | |
For j = 1 To N | |
S = 0 | |
For K = 1 To j - 1 | |
S = S + LTM(j, K) ^ 2 | |
Next K | |
LTM(j, j) = A(j, j) - S | |
If LTM(j, j) <= 0 Then Exit For | |
LTM(j, j) = Sqr(LTM(j, j)) | |
For i = j + 1 To N | |
S = 0 | |
For K = 1 To j - 1 | |
S = S + LTM(i, K) * LTM(j, K) | |
Next K | |
LTM(i, j) = (A(i, j) - S) / LTM(j, j) | |
Next i | |
Next j | |
CholeskyDecompose = LTM | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!