Wednesday, June 1, 2016

Sample covariance matrix






Function xlCovSMtx(rng As Range)
   
    Dim i, j
    Dim nCls As Integer
    Dim mtx()
   
    With WorksheetFunction
   
        nCls = rng.Columns.Count
        'MsgBox nCls
        ReDim mtx(1 To nCls, 1 To nCls)
   
        For i = 1 To nCls
            For j = 1 To nCls
                mtx(i, j) = .Covariance_S(rng.Columns(i), rng.Columns(j))
            Next j
        Next i
   
        xlCovSMtx = mtx
   
    End With

End Function

Function xlCovSMtx_U(rng As Range)
   
    Dim i, j
    Dim nCls As Integer
    Dim mtx()
   
    With WorksheetFunction
   
        nCls = rng.Columns.Count
        'MsgBox nCls
        ReDim mtx(1 To nCls, 1 To nCls)
   
        For i = 1 To nCls
            For j = 1 To nCls
                If i <= j Then
                    mtx(i, j) = .Covariance_S(rng.Columns(i), rng.Columns(j))
                Else
                    mtx(i, j) = 0
                End If
            Next j
        Next i
   
        xlCovSMtx_U = mtx
   
    End With

End Function

Function xlCovSMtx_L(rng As Range)
   
    Dim i, j
    Dim nCls As Integer
    Dim mtx()
   
    With WorksheetFunction
   
        nCls = rng.Columns.Count
        'MsgBox nCls
        ReDim mtx(1 To nCls, 1 To nCls)
   
        For i = 1 To nCls
            For j = 1 To nCls
                If i >= j Then
                    mtx(i, j) = .Covariance_S(rng.Columns(i), rng.Columns(j))
                Else
                    mtx(i, j) = 0
                End If
            Next j
        Next i
   
        xlCovSMtx_L = mtx
   
    End With

End Function


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.