Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Modern Portfolio Theory with Python

Modern Portfolio Theory

Modern Portfolio Theory (MPT) is a theory that was developed by Harry Markowitz in 1952. It is a mathematical framework for constructing portfolios of financial assets that aims to maximize expected returns for a given level of risk or to minimize risk for a given level of expected returns. The theory has been widely accepted and used by investors, portfolio managers, and financial analysts to manage investment portfolios.

The basic idea behind MPT is that investors should diversify their portfolios across different asset classes such as stocks, bonds, and cash to reduce risk. The theory assumes that investors are rational and risk-averse, meaning they prefer to avoid risk when possible. Therefore, they will require higher returns to compensate for taking on higher levels of risk.

MPT is based on the concept of the efficient frontier, which is the set of portfolios that offer the highest expected return for a given level of risk. The efficient frontier is graphed as a curved line on a risk-return graph, with the x-axis representing the risk (usually measured by standard deviation) and the y-axis representing the expected return.

According to MPT, investors can construct portfolios that lie on the efficient frontier by combining different assets in such a way that the overall portfolio has a lower level of risk than any of the individual assets. This is achieved through diversification, which reduces the overall risk of the portfolio by spreading investments across different asset classes and securities.

The theory also introduced the concept of the capital asset pricing model (CAPM), which is a model that helps investors to determine the expected return on an asset given its risk. The CAPM assumes that the risk of an asset can be measured by its beta, which is a measure of the asset’s sensitivity to market movements. According to the CAPM, the expected return on an asset is equal to the risk-free rate plus a risk premium, where the risk premium is proportional to the asset’s beta.

MPT has several advantages for investors. First, it provides a systematic approach to portfolio construction that is based on sound financial principles. Second, it encourages investors to diversify their portfolios, which helps to reduce risk. Third, it provides a way to measure risk and return, which helps investors to make informed decisions about their investments.

However, MPT has also been criticized for its assumptions, which are often unrealistic. For example, the theory assumes that investors have access to perfect information and that they are rational and risk-averse. In reality, investors may not have access to all the information they need, and they may not always act rationally. Additionally, MPT assumes that market returns are normally distributed, which is not always the case.

Modern Portfolio Theory is a useful framework for constructing investment portfolios that aim to maximize returns for a given level of risk or minimize risk for a given level of returns. It has been widely accepted and used by investors, portfolio managers, and financial analysts for decades. While the theory has its limitations, it provides a systematic approach to portfolio construction that can help investors to achieve their financial goals.

The Python code below implements the Modern Portfolio Theory algorithm:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Load data
data = pd.read_csv('data.csv')
returns = data.pct_change().mean() * 252
covariance = data.pct_change().cov() * 252

# Set up variables
num_assets = len(data.columns)
weights = np.random.random(num_assets)
weights /= np.sum(weights)

# Set up functions
def portfolio_return(weights, returns):
    return np.sum(weights * returns)

def portfolio_volatility(weights, covariance):
    return np.sqrt(np.dot(weights.T, np.dot(covariance, weights)))

# Set up simulation
num_portfolios = 10000
results = np.zeros((num_portfolios, 2))
for i in range(num_portfolios):
    weights = np.random.random(num_assets)
    weights /= np.sum(weights)
    portfolio_return = portfolio_return(weights, returns)
    portfolio_volatility = portfolio_volatility(weights, covariance)
    results[i][0] = portfolio_return
    results[i][1] = portfolio_volatility

# Plot efficient frontier
plt.scatter(results[:, 1], results[:, 0], alpha=0.1)
plt.xlabel('Volatility')
plt.ylabel('Return')
plt.show()

This code imports a dataset of financial asset returns computes the mean returns and covariance matrix and then performs a Monte Carlo simulation to generate a large number of random portfolios with varying weights for each asset. The code then calculates the return and volatility of each portfolio and plots the resulting efficient frontier, which is the set of portfolios with the highest expected return for a given level of risk.