How to calculate Variance Covariance Matrix?

realtylerdurdon

New Member
Joined
Jan 20, 2004
Messages
44
Hi board,

I want to calculate the yearly standard deviation of a portfolio with a few hundred stocks. I plan to first calculate the variance covariance matrix for the portfolio's stocks and then use matrix algebra (like here http://www.fenews.com/fen39/back_to_basics/Back to Basics Column-formulas.htm) to arrive at the porfolio's standard deviation.

I have a large array with stock returns that looks like this:

StockID;Year;Month;Return;Portfolio
123456;1990;1;0.05;1
234567;1990;1;0.06;1
345678;1990;1;0.02;2
...

If there would be 100 different stock IDs, I would like to calculate a 100x100 matrix that contains the covariances of the returns of the stocks of that portfolio for a specific year.

I did some experiments with array formulas like {=cov(if(stockID=...);if(stockID=...))} but that didn't work. I couldn't find any plugins that would help. Any ideas?

Thanks alot in advance!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
This looks straightforward enough.

But before doing anything on it, I see you number your portfolio(s) 1 and 2, whereas you refer elsewhere only to "the portfolio".

Which portfolio? All together? Just 1? Just 2? Each separately? Other?
 
Upvote 0
There are 10 portfolios and I would like to do it for each portfolio. For each combination of year and portfolio there would be one var cov matrix. BTW, there are 20 Years, thats why i can't do it manually...

Thanks alot!
 
Upvote 0
Hi,

If you’re happy with VBA code you might try the following.

It supposes that (as I took from your post) that the StockID are in column A, year in column B, month in col C and return in col D. Also suppose you have your indicated headings in row 1 at the top of each column.

Try this out on some data where you know the result, and see if it is the sort of thing you want.

If it is, I can make a few more comments which may be of use to you.

Prosit.
Code:
Sub covars_etc2()
n = [a2].End(xlDown).Row
a = Range([a2], Cells(n, 5))
Set yr = CreateObject("Scripting.Dictionary")
Set mn = CreateObject("Scripting.Dictionary")
Set stk = CreateObject("Scripting.Dictionary")
For i = 1 To n - 1
     If Not stk.exists(a(i, 1)) Then stk.Add a(i, 1), Empty
    If Not yr.exists(a(i, 2)) Then yr.Add a(i, 2), Empty
    If Not mn.exists(a(i, 3)) Then mn.Add a(i, 3), Empty
Next i

stke = stk.keys:     stc = stk.Count
yrke = yr.keys:  yrc = yr.Count
mnke = mn.keys:  mnc = mn.Count

ReDim x(1 To yrc, 1 To mnc, 1 To stc)
For p = 1 To n - 1
    For q = 1 To yrc
        For r = 1 To mnc
            For s = 1 To stc
                If yrke(q - 1) = a(p, 2) And _
                    mnke(r - 1) = a(p, 3) And _
                        stke(s - 1) = a(p, 1) Then
                    x(q, r, s) = a(p, 4)
                End If
            Next s
        Next r
    Next q
Next p

For q = 1 To yrc
ReDim c(1 To stc, 1 To stc)
    For s1 = 1 To stc
        For s2 = 1 To stc
            ssq = 0
            sa = 0
            sb = 0
            For r = 1 To mnc
                ssq = ssq + x(q, r, s1) * x(q, r, s2)
                sa = sa + x(q, r, s1)
                sb = sb + x(q, r, s2)
            Next r
            c(s1, s2) = (ssq - sa * sb / mnc) / (mnc)
        Next s2
    Next s1

Cells(q * stc + q, 8) = yrke(q - 1)
Cells(q * stc + q, 9).Resize(stc, stc) = c
Next q
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,622
Messages
6,179,947
Members
452,950
Latest member
bwilliknits

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