The covariance matrix
The covariance matrix is crucial in determining the portfolio variance in the general case of \(N\) assets. Remember that an element on row \(i\) and column \(j\) correspond to the covariance of the \(i\) th and \(j\) th return. Recall also that the covariance of two return series is the product between their volatilities and their correlation, and that the covariance of an asset return with itself is its variance.
In this exercise, you will compute and analyze the covariance and correlation matrix on the monthly returns of the four asset classes from the previous exercise. To refresh, these asset classes are equities, bonds, real estate, and commodities. To create these matrices, you will use the standard functions cov()
and cor()
.
In your workspace are the monthly investments as returns
, and the vector of standard deviations sds
that you created previously.
This exercise is part of the course
Introduction to Portfolio Analysis in R
Exercise instructions
- Create a diagonal matrix that contains the variances on the diagonal. You can use the function diag() to do this, using a squared
sds^2
as the only argument. Call thisdiag_cov
. - Compute the covariance matrix of returns. Call this
cov_matrix
. - Compute the correlation matrix of returns. Call this
cor_matrix
. - Verify that the covariance between bond returns and equity returns equals the product of their standard deviations and correlation, by running the pre-loaded code. Do not alter this code.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a matrix with variances on the diagonal
# Create a covariance matrix of returns
# Create a correlation matrix of returns
# Verify covariances equal the product of standard deviations and correlation
all.equal(cov_matrix[1,2], cor_matrix[1,2] * sds[1] * sds[2])