Loading [MathJax]/extensions/MathMenu.js

Tuesday, 27 January 2015

How to build a variance-covariance matrix in Python

Recently I wrote a script to calculate the VaR of a portfolio of stocks given historical prices and returns and, in order to do that, I had to study the basics of a variance-covariance matrix.

First of all, what is it?
The formal definition should sound something like this: a variance-covariance matrix is a matrix whose element in the i,j position is the covariance between the ith and jth elements of a vector of random variables. Informally, we may say that a variance-covariance matrix is the matrix of the covariances and since the covariance of a random variable with itself is its variance, the main diagonal of the matrix is filled with the variances of the random variables (hence the fancy name).

clip_image002[17]

What is it useful for?
When calculating VaR, say for a single stock, you need to gather the standard deviation of the returns of your stock. However, when calculating the VaR of a portfolio, things get pretty messy pretty quick, since you cannot simply add or subtract variances. In a more intuitive way, we may say that the variance-covariance matrix generalizes the notion of variance to multiple dimensions.

So how can we build it in Python?
Here is a simple template of how I built mine. I used only two stocks, but in the script I talked about earlier I used 500 stocks, you can easily imagine what a mess it can be if you miss some numbers.

Before showing the code, let’s take a quick look at relationships between variance, standard deviation and covariance:

Standard deviation is the square root of the variance

clip_image002[15]

Covariance can be obtained given correlation (check how to build a correlation matrix) and standard deviations.

clip_image002[13]

Now we can look at the script:

import numpy as np
import math
stdv = {"ABC":0.3,"XYZ":0.2}
tickersCorr = ["ABC","XYZ"]
# Assuming a 0.5 correlation here is the correlation matrix
c = [[1,0.5],[0.5,1]]
def varCovarMatrix(stocksInPortfolio):
cm = np.array(c)
vcv = []
for eachStock in stocksInPortfolio:
row = []
for ticker in stocksInPortfolio:
if eachStock == ticker:
variance = math.pow(stdv[ticker],2)
row.append(variance)
else:
cov = stdv[ticker]*stdv[eachStock]* cm[tickersCorr.index(ticker)][tickersCorr.index(eachStock)]
row.append(cov)
vcv.append(row)
vcvmat = np.mat(vcv)
return vcvmat
print(varCovarMatrix(["ABC","XYZ"]))
view raw varCovar.py hosted with ❤ by GitHub

And here is the output:

# Output
# >>> ================================ RESTART ================================
# >>>
# [[ 0.09 0.03]
# [ 0.03 0.04]]
# >>>
view raw varCovar_out.py hosted with ❤ by GitHub

Hope this was interesting.

No comments:

Post a Comment