Skip to Main Content
Is-what-Is

Finance

Modern Portfolio Management

This project explains Modern Portfolio Theory and the Capital Asset Pricing Model (CAPM). A web application applies these concepts to portfolio construction.

Context

Summary

Harry Markowitz developed Modern Portfolio Theory (MPT) in the 1950s. He published his foundational paper on portfolio selection in 1952. He developed the theory further during the 1950s.

MPT studies the relationship between investment return and risk. Return measures the gain or loss from an investment. Variance and standard deviation measure the variability of returns. Investors normally expect a higher return when they accept higher risk.

Portfolio management selects asset weights that give an efficient balance between expected return and risk. Diversification can decrease asset-specific risk. Portfolio optimisation identifies the portfolios that give the best expected return for a defined level of risk.

Portfolio management aims to maximise expected return for a given level of risk, or minimise risk for a given expected return.

An optimal portfolio does not guarantee a high return. It does not prevent losses.

Implementation

Data

The application retrieves market data from Yahoo Finance through the yfinance Python API. The application retrieves the data when it needs the data. The application can keep retrieved data for up to one hour, so this is not a real-time data feed.

The data gives the daily historical prices of each asset. These prices are inputs to the portfolio model. The model uses them to calculate asset returns and portfolio statistics.

The application uses the data that is available from Yahoo Finance. Prices can contain errors or gaps. The application does not verify the accuracy or completeness of the data.

Theory

Theory

Return

The return of an asset measures the percentage change in its price between two observations.

Let PiP_i be the price at observation ii. Let Pi−1P_{i-1} be the price at the previous observation. The simple return rir_i is:

ri=Pi−Pi−1Pi−1r_i = \frac{P_i-P_{i-1}}{P_{i-1}}

Multiply rir_i by 100 to show it as a percentage.

The application uses adjusted prices. Adjusted prices include the effect of dividends and stock splits. The application also uses the log return, not the simple return:

ri=ln⁡(PiPi−1)r_i = \ln\left(\frac{P_i}{P_{i-1}}\right)

The two values are almost equal for small price changes. The two values have a larger difference for large price changes.

Risk

In this section, ii identifies an observation and NN is the number of observations. Later sections give these symbols a different meaning, and state that meaning.

The mean return rˉ\bar r is:

rˉ=1N∑i=1Nri\bar r = \frac{1}{N} \sum_{i=1}^{N}r_i

Risk is the variability of returns. Variance measures how far returns vary from their mean. The application calculates the sample variance from historical returns:

Var⁡(r)=1N−1∑i=1N(ri−rˉ)2\operatorname{Var}(r) = \frac{1}{N-1} \sum_{i=1}^{N}(r_i-\bar r)^2

The divisor is N−1N-1 because the returns are a sample from a longer history. The population variance uses the divisor NN. All variances and covariances on this page use the sample convention when they are calculated from data.

Standard deviation σr\sigma_r is the square root of variance:

σr=Var⁡(r)\sigma_r=\sqrt{\operatorname{Var}(r)}

Standard deviation expresses variability in the same units as returns.

Variance and standard deviation are common measures of financial risk. They do not measure every type of investment risk.

A portfolio with two assets

Consider a portfolio with two assets. Let r1r_1 and r2r_2 be the returns of asset 1 and asset 2. Let w1w_1 and w2w_2 be the portfolio weights of asset 1 and asset 2. A weight is the fraction of the portfolio that the investor puts in an asset.

For a fully invested portfolio without leverage, the weights add to 1. Each weight is between 0 and 1:

w1+w2=1w_1+w_2=1

The portfolio return rpr_p is:

rp=w1r1+w2r2r_p=w_1r_1+w_2r_2

Expected return

The expected return E[r]\mathbb{E}[r] is the mean of the distribution of possible returns. The true value is not known. The application uses the mean historical return as an estimate.

Start with the expected value of the portfolio return:

E[rp]=E[w1r1+w2r2]\mathbb{E}[r_p] = \mathbb{E}[w_1r_1+w_2r_2]

Expectation is linear. Therefore:

E[rp]=w1E[r1]+w2E[r2]\boxed{ \mathbb{E}[r_p] = w_1\mathbb{E}[r_1] + w_2\mathbb{E}[r_2] }

The expected portfolio return is the weighted sum of the expected asset returns.

Portfolio variance

Start with the variance of the portfolio return:

Var⁡(rp)=Var⁡(w1r1+w2r2)\operatorname{Var}(r_p) = \operatorname{Var}(w_1r_1+w_2r_2)

Expand the expression:

Var⁡(rp)=w12Var⁡(r1)+w22Var⁡(r2)+2w1w2Cov⁡(r1,r2)\boxed{ \begin{aligned} \operatorname{Var}(r_p) ={}&w_1^2\operatorname{Var}(r_1)\\ &+w_2^2\operatorname{Var}(r_2)\\ &+2w_1w_2\operatorname{Cov}(r_1,r_2) \end{aligned} }

The covariance Cov⁡(r1,r2)\operatorname{Cov}(r_1,r_2) measures how the two returns move together. It is positive when the two returns tend to be on the same side of their means. It is negative when they tend to be on opposite sides.

Each term has one function:

  • The first term measures the weighted variance of asset 1.
  • The second term measures the weighted variance of asset 2.
  • The third term measures how the two asset returns move together.

The covariance term is important. It can increase or decrease the portfolio variance. Two assets do not need to move in the same direction at the same time. Their relationship affects the risk of the combined portfolio.

If both weights are positive and the two returns do not move in exact proportion, the portfolio standard deviation is less than the weighted sum of the two standard deviations. This is the mathematical basis of diversification.

A portfolio with N assets

The two-asset model extends to a portfolio with many assets:

Asset 1Asset 2⋮Asset N\begin{aligned} &\text{Asset 1}\\ &\text{Asset 2}\\ &\vdots\\ &\text{Asset }N \end{aligned}

From this section on, NN is the number of assets. The symbols ii and jj identify assets. Let wiw_i be the weight of asset ii. Let rir_i be the return of asset ii.

For a fully invested portfolio, the weights add to 1:

∑i=1Nwi=1\sum_{i=1}^{N}w_i=1

Expected return

E[rp]=∑i=1NwiE[ri]\boxed{ \mathbb{E}[r_p] = \sum_{i=1}^{N}w_i\mathbb{E}[r_i] }

Each asset contributes to the expected portfolio return according to its weight.

Portfolio variance

Var⁡(rp)=∑i=1N∑j=1NwiwjCov⁡(ri,rj)\boxed{ \operatorname{Var}(r_p) = \sum_{i=1}^{N}\sum_{j=1}^{N} w_iw_j\operatorname{Cov}(r_i,r_j) }

The double sum includes every pair of assets. When i=ji=j, the covariance is the variance of asset ii. Therefore, portfolio variance depends on the individual asset variances and on the covariances between assets.

Portfolio risk does not depend only on the risk of each individual asset. It also depends on how the assets move in relation to one another.

The Sharpe ratio

The Sharpe ratio SpS_p of a portfolio is:

Sp=E[rp]−rfσp\boxed{ S_p= \frac{\mathbb{E}[r_p]-r_f}{\sigma_p} }

In this equation:

  • E[rp]\mathbb{E}[r_p] is the expected portfolio return.
  • rfr_f is the risk-free return for the same period.
  • σp\sigma_p is the portfolio return standard deviation for that period.

The Sharpe ratio measures expected excess return per unit of return standard deviation. Excess return is the return above the risk-free return. A higher Sharpe ratio shows a higher expected excess return per unit of measured risk.

The return and the risk must use the same period. The application uses annual values for both.

Portfolio optimisation and the efficient frontier

Portfolio optimisation compares portfolios that have different weights:

  1. Change the portfolio weights. Each valid set of weights gives one portfolio.
  2. Calculate the expected return and the standard deviation of each portfolio.
  3. Plot each portfolio, with standard deviation on the horizontal axis and expected return on the vertical axis.
  4. Find the portfolios that give the highest expected return at each level of standard deviation.

The plot shows the set of possible risk-return combinations.

The efficient frontier contains portfolios that offer the highest expected return for a given level of risk, or the lowest risk for a given expected return.

A portfolio below the efficient frontier is inefficient. Another portfolio can give a higher expected return for the same risk.

Now add a risk-free asset. This asset has the return rfr_f and a standard deviation of zero. A line from rfr_f to a portfolio on the plot has a gradient equal to the Sharpe ratio of that portfolio. The line with the largest gradient touches the efficient frontier at one point.

Under the standard mean-variance assumptions, this point identifies the tangency portfolio. The line from rfr_f through the tangency portfolio is the Capital Market Line (CML). Its gradient is:

CML gradient=E[rT]−rfσT\text{CML gradient} = \frac{\mathbb{E}[r_T]-r_f}{\sigma_T}

The subscript TT identifies the tangency portfolio. This gradient is equal to the Sharpe ratio of the tangency portfolio. Under the standard assumptions, this is the maximum Sharpe ratio that the feasible risky portfolios can give.

Three terms have different meanings:

  • Efficient frontier: A curve of portfolios that contain only risky assets.
  • Capital Market Line: A line that shows the relationship between expected return and total risk for efficient portfolios. These portfolios combine the risk-free asset and the tangency portfolio.
  • Security Market Line (SML): A line that shows the relationship between expected return and systematic risk. Beta measures this risk. The CAPM section defines beta.

From diversification to the Capital Asset Pricing Model

An asset has two types of risk:

  1. Asset-specific risk: Risk that is related to an individual company or asset.
  2. Systematic risk: Risk from factors that affect many assets, for example changes in interest rates or in broad economic conditions.

When you add assets to a portfolio, their asset-specific risks tend to offset one another. If a portfolio holds all publicly traded assets, the effect of the risk of each individual stock becomes small. The risk that remains comes from the covariances between the assets. Diversification cannot remove this risk.

This portfolio is an approximation of the market portfolio. The theoretical market portfolio holds all risky assets in proportion to their market values.

The Capital Asset Pricing Model (CAPM) gives the expected return of an asset under a set of theoretical assumptions. Investors can diversify away asset-specific risk, so the model gives no additional expected return for this risk. The expected return depends only on the contribution of the asset to market risk. Beta βi\beta_i measures this contribution:

βi=Cov⁡(ri,rm)Var⁡(rm)\boxed{ \beta_i = \frac{\operatorname{Cov}(r_i,r_m)} {\operatorname{Var}(r_m)} }

In this equation, rir_i is the return of asset ii and rmr_m is the market portfolio return.

The CAPM equation is:

E[ri]=rf+βi(E[rm]−rf)\boxed{ \mathbb{E}[r_i] = r_f+ \beta_i \left( \mathbb{E}[r_m]-r_f \right) }

In this equation, E[rm]−rf\mathbb{E}[r_m]-r_f is the expected market risk premium.

The expected return of an asset is equal to the risk-free return plus a payment for market risk. An asset with a beta of 1 moves with the market, so it has the expected return of the market. When the market risk premium is positive, an asset with a higher beta has a higher expected return.

CAPM is a theoretical model. Its expected returns are not guaranteed returns, and they do not always agree with observed market returns.

Modern Portfolio Theory explains how to combine assets to manage risk and return. CAPM explains how the market prices systematic risk under a set of theoretical assumptions.

Interactive

Web Application

The web application is a practical implementation of the theory. Users can construct and analyse a portfolio of up to 20 assets. They can select equities and exchange-traded funds (ETFs).

The application retrieves available historical market data from Yahoo Finance through the yfinance API. It uses the selected assets and their historical returns to evaluate portfolio combinations. The objective is to help users explore portfolio optimisation and understand the relationship between expected return and risk.

How the application calculates a portfolio

  1. The user adds between 2 and 20 assets. The application checks that Yahoo Finance has data for each asset.
  2. The application retrieves daily adjusted closing prices from 1 January 2015, or from the first available date. It keeps only the days on which all selected assets have a price. It stops if there are fewer than 60 daily returns.
  3. The application calculates the log returns. It multiplies the mean and the covariance of the daily returns by 252 to get annual values.
  4. The application uses the latest yield of the US 5-year Treasury note as the risk-free return. The Yahoo Finance ticker for this yield is ^FVX.
  5. The application uses the Sequential Least Squares Programming (SLSQP) solver from SciPy to find the weights with the highest Sharpe ratio. Each weight is between 0 and 1, and the weights add to 1.
  6. The application solves for the efficient frontier at 50 target returns. It also calculates 10,000 portfolios with random weights.
  7. The application shows the optimal weights, the expected return, the standard deviation and the Sharpe ratio. A chart shows the random portfolios, the efficient frontier and the Capital Market Line.

Limitations of the application

The results describe the historical period that the application uses. They are not forecasts and they are not investment advice. The application does not guarantee an optimal investment outcome.

The application calculates the portfolio return as a weighted sum of log returns. This value is an approximation, because log returns do not add exactly across assets. The application does not convert currencies. It does not include trading costs or taxes. It does not calculate beta.

Open the application

Open the Modern Portfolio Management application (opens in a new tab)

Source code

View the source code on GitHub (opens in a new tab)

Context

All Projects