calculating covariance matrix from array in vba

centauri

New Member
Joined
Jul 18, 2010
Messages
6
I am trying to calculate a covariance matrix in VBA. I have managed to get my data into an array "datarng" which has 10 columns and 100 rows. I want to calculate the covariance matrix of my dataset so that I will have a new array 10x10 by calculating the covariance between each column in the original array. can anyone help?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi centauri

You mean the autocovariance matrix?

So, for ex., if your values are in A1:J100, the element (2,5) in the autocovariance matrix would be

=COVAR(B1:B100,E1:E100)

Is this what you want to do in vba?
 
Upvote 0
Yes, this is what I'm trying to do, but I have an array of data that also varies in size so I need to set up a new array which will be a square matrix of size equal to the number of columns in the first array. How do I define the size of the array and the elements in the new array?
 
Upvote 0
Hi

This is an example that you can adjust to your needs.

In this example I used an array 100x10 that I filled with random data.
I added some writing to the worksheet just so that you can easily check the result.

Select an empty worksheet and try:

Code:
Sub Test()
Dim dArrData(1 To 100, 1 To 10) As Double
Dim dAutoCoVar() As Double
Dim j As Long, k As Long
 
' fill the array with some bogus data
Randomize
For j = 1 To 100
    For k = 1 To 10
        dArrData(j, k) = Rnd
    Next k
Next j
 
' write to the worksheet, for debug
Range("A1:J100").Value = dArrData
 
' calculate the autocovariance matrix
dAutoCoVar = Autocovar(dArrData)
 
' write to the worksheet, for debug
Range("L1:U10").Value = dAutoCoVar
End Sub
 
Function Autocovar(dArrData() As Double) As Double()
Dim dArrResult() As Double
Dim j As Long, k As Long
 
' redim the result array as a square array.
ReDim dArrResult(1 To UBound(dArrData, 2), 1 To UBound(dArrData, 2))
 
' calculate the autocovariance matrix
For j = 1 To UBound(dArrData, 2)
    For k = 1 To UBound(dArrData, 2)
        With Application.WorksheetFunction
            dArrResult(j, k) = .Covar(.Index(dArrData, 0, j), .Index(dArrData, 0, k))
        End With
    Next k
Next j
Autocovar = dArrResult
End Function
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,674
Members
448,977
Latest member
moonlight6

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top