3
$\begingroup$

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.

$\endgroup$
9
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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
}
$\endgroup$
  • $\begingroup$ OP is asking about portfolio covariance, not just the covariance between two independent assets. $\endgroup$ – chrisaycock Mar 25 '11 at 15:36
  • $\begingroup$ @ 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? $\endgroup$ – Owe Jessen Mar 25 '11 at 19:27
  • 1
    $\begingroup$ 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. $\endgroup$ – chrisaycock Mar 25 '11 at 19:48
  • 4
    $\begingroup$ 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. $\endgroup$ – Richard Herron Mar 25 '11 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.