A beginner's guide to gauging portfolio risk

October 12, 2024
Facebook logo.
Twitter logo.
LinkedIn logo.
Get this code in Google Colab

A beginner's guide to gauging portfolio risk

New investors often struggle with managing risk.

But that’s what all the best investors spend their time thinking about!

The most basic tool to manage portfolio risk is through a portfolio’s variance. Although calculating it is not straightforward.

Portfolio variance is a measurement of risk. It measures how the actual returns of a set of securities making up a portfolio fluctuate over time.

Portfolio variance is calculated using the standard deviations of each security in the portfolio and the correlations of each security pair.

It might sound complicated, but Python makes it easy.

Let’s go!

A beginner's guide to gauging portfolio risk

Modern Portfolio Theory (MPT) remains important for managing investment risk. At its core is portfolio variance, which measures risk by assessing return deviations. MPT identifies optimal portfolios on the "efficient frontier," balancing risk and return.

In practice, traditional mean-variance optimization has shown to underperform and is rarely used. However, the concepts of calculating portfolio risk remain.

Calculating portfolio variance involves assessing each asset's variance and their covariances. Covariance measures how assets move relative to each other. Investors can reduce portfolio risk by choosing assets with low or negative correlations. Adjusting asset weights in a portfolio modifies its overall risk and return profile.

Professionals use these calculations to build diversified portfolios that manage risk. By selecting assets with complementary movements, they achieve lower portfolio variance.

Let’s see how it works with Python.

Imports and set up

We’ll start the usual way, by importing the libraries we need for the analysis.

1import numpy as np
2import yfinance as yf
3import warnings
4warnings.filterwarnings("ignore")

Then start collecting historical stock prices for a set of specified assets. We will use the yfinance library to download this data.

1prices = yf.download(
2    ["FDN", "FTXL", "FXD", "FXR", "HDEF"], 
3    start="2020-01-01"
4)
5
6returns = (
7    prices["Adj Close"]
8    .pct_change()
9    .dropna()
10)

After downloading historic data, we calculate the daily returns for each asset by taking the percentage change of the adjusted close prices from one day to the next. The pct_change() function is used to compute these changes. We drop any missing values that may arise from calculating percentage changes, particularly at the start of the dataset.

Determine expected returns and covariance matrix

We now compute the expected returns for each asset and the covariance matrix of the returns. The expected return gives us an average sense of how much we can expect each asset to return. The covariance matrix helps us understand how the returns of the different assets move in relation to each other.

1expected_returns = np.mean(returns, axis=0)
2cov_matrix = np.cov(returns, rowvar=False)

We calculate the expected returns using the mean of the daily returns for each asset. This gives us a single number for each asset representing its average daily return over the entire period.

The covariance matrix is computed using np.cov, which takes the returns data and calculates how the returns of different assets change together. The result is a matrix where each element represents the covariance between a pair of assets, helping us understand their return correlations.

Evaluate the portfolio's risk through variance and standard deviation

Finally, we define a portfolio with equal weights for each asset and calculate its variance and standard deviation. These metrics will help us assess the risk associated with the portfolio.

1weights = np.array([0.2] * 5)
2
3portfolio_variance = np.dot(weights.T, np.dot(cov_matrix, weights))
4portfolio_std_dev = np.sqrt(portfolio_variance) * np.sqrt(252)

We define equal weights of 20% for each asset, assuming we want to allocate the same proportion of our investment to each. The portfolio variance is calculated using a mathematical formula that combines the weights and the covariance matrix.

This calculation results in a single value representing the total risk of the portfolio based on the variance of its assets. To get the annualized standard deviation, we take the square root of the portfolio variance and multiply it by the square root of 252, the typical number of trading days in a year.

The standard deviation provides a measure of how much the portfolio's return might fluctuate annually.

Your next steps

Try modifying the weight distribution among the assets to see how it affects the portfolio's risk. You can adjust the weights in the weights array, making some assets more dominant than others.

Observe how changes in asset allocation impact the portfolio's variance and standard deviation, allowing you to explore different risk-return profiles. Experimenting with different weights helps in understanding the dynamics of portfolio optimization.

Man with glasses and a wristwatch, wearing a white shirt, looking thoughtfully at a laptop with a data screen in the background.