CommencerCommencer gratuitement

Matrix-based calculation of portfolio mean and variance

When \(w\) is the column-matrix of portfolio weights, \(\mu\) the column-matrix of expected returns, and \(\Sigma\) the return covariance matrix. Then the portfolio expected return is \(w'\mu\), and the portfolio variance is \(w'\Sigma w\). Remember that the portfolio's volatility is the square root of its variance.

You will practice matrix multiplication in R using the %*% function, instead of the standard *. In addition, you will transpose the matrices by using the standard function t(). Remember that transposing a matrix is simply changing the rows of the matrix to the columns.

The weights, vector of means, and the covariance matrix are pre-loaded in your workspace as weights, vmeans, and sigma, respectively.

Cet exercice fait partie du cours

Introduction to Portfolio Analysis in R

Afficher le cours

Instructions

  • Convert weights to a matrix called w using as.matrix().
  • Convert the vector of means (vmeans) to a matrix called mu using as.matrix().
  • Calculate portfolio mean monthly return. Remember the function t() transposes a vector.
  • Calculate the portfolio volatility.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Create a weight matrix w


# Create a matrix of returns


# Calculate portfolio mean monthly returns


# Calculate portfolio volatility
sqrt(t(___) %*% ___ %*% ___)
Modifier et exécuter le code