Value at Risk: Matricies

We define a number of methods for producing matrices in VolatilityAbstract.java.

  • getCorrelationMatrix(double[][] matrix)
  • getCovarianceMatrix(double[][] matrix)
  • getCholeskyDecompositionMatrix(double[][] matrix)

All of these methods returns an matrix for a portfolio of stocks.

Percentage Changes

Suppose our portfolio contains multiple stocks, say GOOG, MSFT, and AAPL.

We will need to compute a vector of historical percentage changes for each of these. We can combine each of these vectors to produce a matrix of price changes.

In Analytical.java, HistoricalSimulation.java, and MonteCarlo.java we instantiate this matrix as a two dimensional array:

double[][] matrix = double[countAsset][size];

where countAsset is the number of stock symbols in our portfolio and size is the number of percentage changes in our data.

Variance Covariance Matrix

The value of each element is simply the variance between two vectors of percentage changes.

In our implementation, we simply step through each element of our matrix and invoke getVariance().

public double[][] getCovarianceMatrix(double[][] matrix) {
    int numCol = matrix.length;
    double[][] covarianceMatrix = new double[numCol][numCol];

    for (int i = 0; i < numCol; i++) {
        for (int j = 0; j < numCol; j++)
            covarianceMatrix[i][j] = getVariance(matrix[i], matrix[j]);
    }
    return covarianceMatrix;
}

The covariance matrix for a portfolio of GOOG, MSFT, and AAPL stocks looks something like:

Note that getVariance() is an abstract class and could be instantiated in a number of ways. The above output was computed from an equal weighted variance estimate.

Correlation Matrix

In the below figure we illustrate the correlation of two stocks, and . Moving from left to right, we see examples of:

  • full positive correlation
  • independence
  • full negative correlation

Each length is the volatility of the historical percentage change of each stock.

The correlation coefficient is calculated as:

In our implementation of getCorrelationMatrix(), we iterate through each matrix element and assign correlation by invoking both getVariance() and getVolatility() at each step.

public double[][] getCorrelationMatrix(double[][] matrix) {
    int numCol = matrix.length;
    double[][] correlationMatrix = new double[numCol][numCol];

    for (int i = 0; i < numCol; i++) {
        for (int j = 0; j < numCol; j++) {
            double covXY = getVariance(matrix[i], matrix[j]);
            double sigmaX = getVolatility(matrix[i], matrix[i]);
            double sigmaY = getVolatility(matrix[j], matrix[j]);
            correlationMatrix[i][j] = covXY / (sigmaX * (sigmaY));
        }        
    }
    return correlationMatrix;
}

The correlation matrix for a portfolio of GOOG, MSFT, and AAPL stocks looks something like:

The unit diagonal makes sense - any random variable is fully correlated with itself.

Cholesky Decomposition

In all our scalar volatility estimates, we have been computing volatility as the square root of variance. But what if we need to return a multivariate (i.e. a matrix) estimate of volatility? We do in fact need this when simulating multiple stock price changes in MonteCarlo.java.

We need a way of finding the square root of a matrix . But there is no direct way of doing this.

Our approach is to take the Cholesky decomposition to approximate . This gives us a lower triangular matrix in which all elements above the diagonal are zero. The product of with its transpose is .

Consider the following matrix , which is symmetric and positive definite as an example:

We need to find such that . Writing this out looks like:

Then we obtain the following formulas for :

above the diagonal

below the diagonal

Our implementation is as follows:

public double[][] getCholeskyDecomposition(double[][] matrix) {
    double[][] covarianceMatrix = getCovarianceMatrix(matrix);
    int numCol = matrix.length;
    double[][] choleskyMatrix = new double[numCol][numCol];

    for (int i = 0; i < covarianceMatrix.length; i++)
        for (int j = 0; j <= i; j++) {
            Double sum = 0.0;
            for (int k = 0; k < j; k++)
                sum += choleskyMatrix[i][k] * choleskyMatrix[j][k];
            if (i == j)
                choleskyMatrix[i][j] = Math.sqrt(covarianceMatrix[i][j] - sum);
            else
                choleskyMatrix[i][j] = (covarianceMatrix[i][j] - sum) / choleskyMatrix[j][j];
        }
    return choleskyMatrix;
}

The Cholesky decomposition for a portfolio of GOOG, MSFT, and AAPL stocks looks something like: