Cholesky decomposition in multilanguage

This article is contributed by: QuantMinds

summary.jpg

Summary:

Cholesky decomposition is a normal way to re-stucture matrix /vector when we need to simulate the way in which a portfolio of assets behaves, since essentially we want to simulate a vector of returns. e.g., notably in working on value-at-risk (VaR), but also when valuing some exotic options.  This could be achieved by splitting the matrix into the product [...]

Read the rest of this entry »

Cholesky decomposition is a normal way to re-stucture matrix /vector when we need to simulate the way in which a portfolio of assets behaves, since essentially we want to simulate a vector of returns. e.g., notably in working on value-at-risk (VaR), but also when valuing some exotic options.  This could be achieved by splitting the matrix into the product of lower triangular matrix L and an upper trianglar U, if it is the positive definiteness. A reference and its computation in Mathematica could be found from here

http://reference.wolfram.com/mathematica/ref/CholeskyDecomposition.html

The Cholesky decomposition can be done in multiple natural languages. Here is the script for VB, please refer to the snippet below. Another way to do this is in Java, you can refer to Stotastic.com , it has a wonderful post on this.

The java scripts for the purpose of Cholesky decomposition is attached below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// returns a cholesky decomposed matrix
// A is the square matrix to decompose
function cholesky(A) {
  var n = A.length;
  // create square matrix L
  var L = new Array(n)
  for(var i=0; i<n; i++){
    L[i] = new Array(n)
    for(var j=0; j<n; j++){
      L[i][j] = 0;
    }
  }
 
  for(var i=0; i<n; i++){
    for(var j=0; j<=i; j++){
      var ss = 0;
      for(var k=0; k<j; k++){
        ss = ss + L[i][k] * L[j][k];
      }
      if(i==j){
        L[i][i] = Math.sqrt(A[i][i] - ss);
      } else{
        L[i][j] = (A[i][j] - ss) / L[j][j];
      }
    }
  }
  return(L);  
}

Attribute VB_Name = "Module2"

'Given an input positive definite matrix A, this function
'returns a lower tridiagonal matrix L such that A=L*L'
Public Function GetCholeskyDecomposition(x As Variant) As Variant
  Dim n As Single
  Dim k As Single
  Dim j As Single
  n = UBound(x, 1)
  ReDim TempMat(1 To n, 1 To n)
  For j = 1 To n
    For k = 1 To n
      TempMat(j, k) = 0
    Next k
  Next j
  For k = 1 To n
    If (x(k, k) <= 0) Then
      GetCholeskyDecomposition = TempMat
      MsgBox "Matrix is not positive definite"
      Exit Function
    End If
    x(k, k) = x(k, k) ^ 0.5
    Dim k2 As Single
    For k2 = k + 1 To n
      x(k2, k) = x(k2, k) / x(k, k)
    Next k2
    For j = k + 1 To n
      For k2 = j To n
        x(k2, j) = x(k2, j) - x(k2, k) * x(j, k)
      Next k2
    Next j
  Next k
  For j = 1 To n
    For k2 = j To n


Note: You can republish this post by referencing the original author name and QuantMinds.com using the shortlink. 分享家:Addthis中文版

One Response to “Cholesky decomposition in multilanguage”

  1. [...] Cholesky decomposition in multilanguage | QuantMinds.com [...]

Leave a Reply

Click to Insert Smiley

LOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepy