# Covariance for arbitrarily large portfolios

I am implementing a method in Java to calculate the variance, covariance, and value at risk for a portfolio, which should be flexible for use with any number of assets in a portfolio. I am struggling with how to calculate the covariance of the assets as I can only find formulae to do so for two or three sets of values.

Java has a built-in library to calculate the covariance of two assets and also to calculate the covariance matrix. However, I am not sure how to find the covariance for a portfolio that can contain any number of assets.

I am implementing a method in Java to calculate the variance, covariance, and value at risk for a portfolio, which should be flexible for use with any number of assets in a portfolio. I am struggling with how to calculate the covariance of the assets as I can only find formulae to do so for two or three sets of values.

Are you sure you are up to the task? Do you have access to R (hey, it's free and open source) or Matlab (hey, Octave is free and open source) or something similar (hint: no, not Excel) to prototype this?

Otherwise, I don't even know where to start as there is so much more to this:

• non-synchronocity of returns (as your assets may not all trade at the same time),
• missing observations (leading to non-positive definite matrices),
• roundoff error,
• modeling issues,
• factor-models for dimension reduction as you do not want N x N for really large N.

There have literally been shelves full of dissertations and practitioner books been written on this. Read some---fifteen years ago we all read the first RiskMetrics (now part of MSCI) manual which was pretty novel and path-breaking then. It has answers to your questions too.

A decade ago, I did something like this for a universe of 200 assets in Perl (don't ask) and it can be done that way. That doesn't mean it should be done that way. Besides learning about the underlying (financial econometrics) math, you should also learn about some numerical libraries for Java. No need to reinvent the wheel.

Have a look at http://en.wikipedia.org/wiki/Covariance_matrix - especially the properties part. According to http://www.aiaccess.net/English/Glossaries/GlosMod/e_gm_covariance_matrix.htm#Animation_covariance%20matrix, if you have a matrix $X$ of assets (assets in columnes, returns in rows), you can calculate the covariance matrix as $\Sigma=[XX^T]/n$, where $n$ is the size of the sample. This should be rather simple in Java, in R it would look something like

Sigma <- function(X){
mu <- apply(X,1,mean)
n <- ncol(X)
Sigma <- X%*%t(X)/n
}

• OP is asking about portfolio covariance, not just the covariance between two independent assets. – chrisaycock Mar 25 '11 at 15:36
• @ chris: Dirk pointed out some of the pitfalls of calculating the covariance for a large number of assets, but I'm not shure I understand your distinction between portfolio covariance and the covariance between the assets of a portfolio, would you please elaborate a bit on that point? – Owe Jessen Mar 25 '11 at 19:27
• The portfolio variance is more complicated than a regular variance calculation; it requires the assets' weights and pair-wise correlations. I suspect the portfolio covariance may likely be more difficult than just the covariance matrix, which the OP claims to already have anyway. – chrisaycock Mar 25 '11 at 19:48
• FWIW, portfolio variance is $\omega' V \omega$, where $\omega$ is a vector of portfolio weights such that $\sum_i \omega_i = 1$ and $V$ is the variance-covariance matrix for the assets in the portfolio. – Richard Herron Mar 25 '11 at 20:31