Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11631

Excel vba module issue(?)

$
0
0

Hi I was trying to compute choleskyDecomposition in vba.

I made a module for testing and I made another module for final assignment.

here is my wondering point.when I execute the testing module, I successfully computed Cholesky Decomposition matrix but when I execute the module which is for final assignment, I got error message that I made invalid procedures or argument.

I bloody can't figure it out why there is an issue in the assignment module even though I used exactly same code with testing module...

If you guys give me any input, I would be truly grateful for you guys' help...

Here is my comprehensive code...

Option ExplicitFunction ComputingVolatility(s() As Double, n As Integer) As Double    Dim R() As Double    ReDim R(n)    Dim Sum_R As Double    Dim i As Integer    Dim Avg_R As Double    Dim Dev_R As Double    Dim Stdev_R As Double'Computing Average of Return    For i = 0 To n - 1        R(i) = Log(s(i) / s(i + 1))        Sum_R = Sum_R + R(i)    Next    Avg_R = Sum_R / n    For i = 0 To n - 1        Dev_R = Dev_R + (R(i) - Avg_R) ^ 2    Next    Stdev_R = Sqr(Dev_R / n)    ComputingVolatility = Stdev_R * Sqr(n)End FunctionFunction ComputingCorrelation(s1() As Double, s2() As Double, n As Integer) As Double    Dim R1() As Double, R2() As Double    ReDim R1(n)    ReDim R2(n)    Dim Sum_R1 As Double, Sum_R2 As Double    Dim Avg_R1 As Double, Avg_R2 As Double    Dim Cov As Double    Dim Multiple As Double    Dim i As Integer    For i = 0 To n - 1        R1(i) = Log(s1(i) / s1(i + 1))        Sum_R1 = Sum_R1 + R1(i)        R2(i) = Log(s2(i) / s2(i + 1))        Sum_R2 = Sum_R2 + R2(i)    Next    Avg_R1 = Sum_R1 / n    Avg_R2 = Sum_R2 / n    For i = 0 To n - 1        Cov = Cov + (R1(i) - Avg_R1) * (R2(i) - Avg_R2)    Next        Cov = Cov / (n - 1)        Multiple = ComputingVolatility(s1(), n) * ComputingVolatility(s2(), n) / n    ComputingCorrelation = Cov / MultipleEnd FunctionFunction CholeskyDecompositionF(mat() As Double, size As Long) As Double()    Dim a() As Double, L() As Double, s As Double, i As Long, j As Long, k As Long, sum As Double    a = mat    ReDim L(1 To size, 1 To size)    For i = 1 To size        For j = 1 To size                If i = j Then                    For k = 1 To j - 1                        sum = sum + L(j, k) ^ 2                    Next                    L(j, j) = Sqr(a(j, j) - sum)                    sum = 0                ElseIf i > j Then                    For k = 1 To j - 1                        sum = sum + L(i, k) * L(j, k)                    Next                    L(i, j) = (a(i, j) - sum) / L(j, j)                    sum = 0                ElseIf i < j Then                    L(i, j) = 0                End If            Next    Next    CholeskyDecompositionF = LEnd FunctionSub Assignment()'For vol and corr    Dim i As Integer, j As Integer    Dim days As Integer'For Cholesky    Dim mat() As Double    ReDim mat(1 To 3, 1 To 3)    Dim Chole() As Double    ReDim Chole(1 To 3, 1 To 3)    days = Cells(6, 1)'Bring data    Dim IndexData As Worksheet    Set IndexData = ThisWorkbook.Sheets("Adj Index")    Dim KOSPI200() As Double    ReDim KOSPI200(days)    Dim SNP500() As Double    ReDim SNP500(days)    Dim STOXX50E() As Double    ReDim STOXX50E(days)    Dim NIKKEI225() As Double    ReDim NIKKEI225(days)    For i = 0 To days        KOSPI200(i) = IndexData.Cells(i + 4, 2)        SNP500(i) = IndexData.Cells(i + 4, 5)        STOXX50E(i) = IndexData.Cells(i + 4, 8)        NIKKEI225(i) = IndexData.Cells(i + 4, 11)    Next'Releasing Vol    Cells(8, 3) = ComputingVolatility(KOSPI200(), days)    Cells(8, 4) = ComputingVolatility(SNP500(), days)    Cells(8, 5) = ComputingVolatility(STOXX50E(), days)    Cells(8, 6) = ComputingVolatility(NIKKEI225(), days)'Releasing Corr    Cells(11, 3) = ComputingCorrelation(KOSPI200(), SNP500(), days)    Cells(12, 3) = ComputingCorrelation(KOSPI200(), STOXX50E(), days)    Cells(13, 3) = ComputingCorrelation(KOSPI200(), NIKKEI225(), days)    Cells(11, 6) = ComputingCorrelation(SNP500(), STOXX50E(), days)    Cells(12, 6) = ComputingCorrelation(SNP500(), NIKKEI225(), days)    Cells(13, 6) = ComputingCorrelation(STOXX50E(), NIKKEI225(), days)'CholeskyDecomposition    For i = 1 To 3        For j = 1 To 3            mat(i, j) = Cells(i + 10, 3 * j)        Next j    Next i' Apply Cholesky decomposition    Chole = CholeskyDecompositionF(mat, 3)' Output the result to the worksheet    For i = 1 To 3        For j = 1 To 3            Cells(i + 5, j + 10) = Chole(i, j)        Next j    Next iEnd Sub

Viewing all articles
Browse latest Browse all 11631

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>