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 be the price at observation . Let be the price at the previous observation. The simple return is:
Multiply 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:
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, identifies an observation and is the number of observations. Later sections give these symbols a different meaning, and state that meaning.
The mean return is:
Risk is the variability of returns. Variance measures how far returns vary from their mean. The application calculates the sample variance from historical returns:
The divisor is because the returns are a sample from a longer history. The population variance uses the divisor . All variances and covariances on this page use the sample convention when they are calculated from data.
Standard deviation is the square root of variance:
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 and be the returns of asset 1 and asset 2. Let and 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:
The portfolio return is:
Expected return
The expected return 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:
Expectation is linear. Therefore:
The expected portfolio return is the weighted sum of the expected asset returns.
Portfolio variance
Start with the variance of the portfolio return:
Expand the expression:
The covariance 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:
From this section on, is the number of assets. The symbols and identify assets. Let be the weight of asset . Let be the return of asset .
For a fully invested portfolio, the weights add to 1:
Expected return
Each asset contributes to the expected portfolio return according to its weight.
Portfolio variance
The double sum includes every pair of assets. When , the covariance is the variance of asset . 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 of a portfolio is:
In this equation:
- is the expected portfolio return.
- is the risk-free return for the same period.
- 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:
- Change the portfolio weights. Each valid set of weights gives one portfolio.
- Calculate the expected return and the standard deviation of each portfolio.
- Plot each portfolio, with standard deviation on the horizontal axis and expected return on the vertical axis.
- 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 and a standard deviation of zero. A line from 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 through the tangency portfolio is the Capital Market Line (CML). Its gradient is:
The subscript 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:
- Asset-specific risk: Risk that is related to an individual company or asset.
- 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 measures this contribution:
In this equation, is the return of asset and is the market portfolio return.
The CAPM equation is:
In this equation, 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
- The user adds between 2 and 20 assets. The application checks that Yahoo Finance has data for each asset.
- 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.
- The application calculates the log returns. It multiplies the mean and the covariance of the daily returns by 252 to get annual values.
- 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. - 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.
- The application solves for the efficient frontier at 50 target returns. It also calculates 10,000 portfolios with random weights.
- 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
Context