0% found this document useful (0 votes)
7 views22 pages

SSRN 4758848

This article presents an interactive framework for teaching modern portfolio theory using Python, aimed at advanced bachelor and entry master-level finance courses. It emphasizes the importance of programming skills in quantitative investment processes and provides a step-by-step guide for applying portfolio theory to market data through a JupyterLab Notebook. The framework addresses the gap between theoretical knowledge and practical application, encouraging students to develop their own programming tools for portfolio optimization.

Uploaded by

宋品岳
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views22 pages

SSRN 4758848

This article presents an interactive framework for teaching modern portfolio theory using Python, aimed at advanced bachelor and entry master-level finance courses. It emphasizes the importance of programming skills in quantitative investment processes and provides a step-by-step guide for applying portfolio theory to market data through a JupyterLab Notebook. The framework addresses the gap between theoretical knowledge and practical application, encouraging students to develop their own programming tools for portfolio optimization.

Uploaded by

宋品岳
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Modernize the Teaching of

Modern Portfolio Theory with Python

Ling Tak Douglas Chung∗

National Chengchi University

ABSTRACT

This article proposes an interactive framework for teaching the modern portfolio theory
with Python. The course material is suitable for advanced bachelor and entry master-level
courses in investments, offering a step-by-step tutorial of the data-driven quantitative
investment process. Students will learn to apply portfolio choice models to market data
and develop essential programming skills through data collection, input estimation, con-
straint imposition, portfolio optimization, back-testing, implementation, and performance
evaluation. By emphasizing the importance of realism, this teaching framework highlights
the potential pitfalls and fallacies when applying financial theories blindly.

JEL: A2; C61; C63; G11


Keywords: portfolio theory; quantitative finance; Python; financial education


E-mail address: cltd@nccu.edu.tw.

Electronic copy available at: https://ssrn.com/abstract=4758848


INTRODUCTION

This article provides a step-by-step guide on using Python to teach the Markowitz
(1952) modern portfolio theory (MPT) interactively. It is imperative to modernize the way
we teach finance. Root, Rozycki, Senteza, and Suh (2007) surveyed the undergraduate
finance programs in 655 institutions, and they found that most finance curricula were
slow to reflect recent trends in the financial industry. Nowadays, standard textbooks
(Bodie, Kane, and Marcus, 2021, 2022) in undergraduate and MBA curricula mainly cover
two risky assets in their expositions of the portfolio theory. By considering only two
risky assets, it greatly simplifies the mathematics required and helps students gain an
intuitive understanding of efficient diversification. However, if the teaching material does
not go beyond two assets, students will feel disconnected as they do not know how to
implement what they learned in the stock market. Therefore, this article fills the gap
between portfolio theory and quantitative investment practices.

The primary goal of teaching with Python is to acquaint students with programming
techniques in portfolio analysis. Programming skills will be valuable assets for finance
graduates in the age of big data. The Economist (2017) argues that data has replaced
oil in becoming the world’s most valuable resource. According to Alexander (2022), data
scientists are highly sought after by Wall Street. Business schools are revamping their
curricula to equip their students with skill sets relevant to the data-driven economy.
Patrick Kandawire, the deputy chief operating officer for capital markets at Morgan
Stanley, said, “I would say for the next generation, take the Python course. Understand
where some of this technology is going,”.

According to the latest TIOBE (2023) Index, Python remains the most popular
programming language based on internet search volume. Python is free to use. Therefore,
many open-source Python resources are readily available to instructors and students
online. Hilpisch (2018) provides an excellent recipe book for people with programming
experience to learn finance applications in Python. However, it can be challenging for
finance majors and instructors without backgrounds in Python to learn and teach. This
article fills the gap by offering a step-by-step guide to teaching portfolio theory with a
companion JupyterLab Notebook. It is also structured to complement the interactive

Electronic copy available at: https://ssrn.com/abstract=4758848


Python applications with finance intuitions and practical insights. Instead of using existing
Python packages such as QuantLib, pyfolio, and PyPortfolioOpt for analyzing portfolios
directly, the goal is to provide a gentle introduction to developing programming tools
for portfolio optimizations to instructors and students with little to no programming
experience. Students will better understand the portfolio theory and quantitative tools by
writing programs from scratch.

Teaching portfolio theory with computer programs has a long history. Before
the prevalence of personal computers, Eckardt (1975, 1978) and Alexander (1978, 1981)
designed proprietary computer programs for teaching portfolio theory and management.
Runyon (1978), Nawrocki (1980), Riley and Montgomery (1980), Pfaff (1981), and
Burns and Burns (1982) advocated using computer simulations in classrooms to promote
realism by bridging portfolio theory with practical applications. Since the 1990s, personal
computers and spreadsheet tools have become readily available. Stephens (1998), Kwan
(2001), Carter, Dare, and Elliott (2002), and Johnson and Liu (2005) illustrated using
Excel spreadsheets to construct mean-variance efficient portfolios and frontier under
different constraints. Repeated experiments conducted by Cagle, Glasgo, and Hyland
(2010) showed that spreadsheet assignments were effective tools in helping students learn
finance.

However, Excel is not suitable for analyzing large data sets in the big data era. Excel
templates typically lack the flexibility for creating mean-variance portfolios with different
numbers of risky assets. Excel users often have to redesign and reconstruct the entire
spreadsheet to make significant changes. In other words, it is difficult to make scalable
programs in Excel. Teaching students Excel VBA is a potential solution (Bauer, 2006), but
it is a less popular language with limited open-source support. Moreover, Excel VBA still
cannot make Excel a practical tool for analyzing a large volume of data. Since teaching
programming to finance students is beneficial, why not choose a general-purpose language
like Python? Apart from the capacity to handle large data sets, there are many open-
source Python packages for downloading financial data through Application Programming
Interfaces (APIs), allowing instructors to demonstrate applications of financial models
with real-world data.

In addition to Python being a better tool for analyzing financial data, the JupyterLab

Electronic copy available at: https://ssrn.com/abstract=4758848


Notebook Integrated Development Environment (IDE) offers code and markdown cells.
Unlike making comments in computer programs, markdown is a word processor that
allows users to format texts, make tables, and write mathematical equations. Lecturers
can create pre-formatted JupyterLab that resembles traditional lecture notes by adding
sections with prologues in markdowns. Students can then fill up each section in their
own words and codes. By encouraging students to write detailed documentation and
summaries in the JupyterLab Notebook, instructors can develop students’ abilities in
conducting transparent and reproducible investment analysis.

MODERN PORTFOLIO THEORY

This section reviews important concepts and optimization techniques in portfolio


theory. Some students may find the Markowitz (1952) Nobel-winning MPT intimidating
at first. To promote their learning incentives, instructors can begin by illustrating the
groundbreaking insights of Harry M. Markowitz and how his thoughts influence subsequent
developments in financial theories and investment industry. By developing the theoretical
foundation for making portfolio choices under uncertainty, he formalized the conventional
wisdom of not putting all the eggs in the same basket by demonstrating the effect of
diversification through portfolio statistics. His mean-variance analysis offered a systematic
way to achieve the best tradeoff between the expected return and risk of a portfolio subject
to one’s risk aversion. The insight of the Markowitz optimal portfolio choice eventually led
to the development of another Nobel-winning economic theory: the Sharpe (1964) Capital
Asset Pricing Model (CAPM). Apart from its profound academic value, the MPT has
profound practical implications and is well-received by the financial industry. Bernstein
(1992), the founding editor of the Journal of Portfolio Management, dubbed Markowitz the
“grandfather of financial theories” and praised his role in revolutionizing modern-day Wall
Street. According to Scaggs (2023), Robert D. Arnott, the founder of Research Affiliates,
said “Before Harry, investing was a bunch of rules of thumb...”. After the sad passing
of Markowitz, CFA Institute and GARP, the two leading professional organizations in
investment and risk management, issued in memoriams to commemorate his significant
contributions to the field of finance (Goetzmann, 2023; Ginnis, 2023).

As pointed out by Markowitz (1990) in his Nobel Prize Lecture, the intuition

Electronic copy available at: https://ssrn.com/abstract=4758848


of portfolio selections follows the utility maximization framework in microeconomics.
Investors make their optimal investment choices under uncertainty by maximizing the
expected mean-variance utility of

1
E[Up,A ] = E[rp ] − Aσp2 . (1)
2

For an investor with a risk aversion of A, the expected utility (E[Up,A ]) of a portfolio p
equals the ratio between the portfolio expected return (E[rp ]) and the product of the
risk aversion and the portfolio variance (σp2 ) divided by two. In reality, it does not
mean investors have quadratic utility functions. Instead, Markowitz views mean-variance
analysis as a practical solution to approximate the maximum expected utility (Kritzman
and Markowitz, 2017).

Following Arnold (2002), this article emphasizes the necessity of using linear
algebra in teaching portfolio theory. With linear algebra, instructors can demonstrate
real-world applications of portfolio choices beyond two risky assets. Moreover, numerical
optimizations of portfolio problems in Python require array (matrix) operations in NumPy
and SciPy. Using matrix notations, the portfolio variance for N risky assets is

σp2 = w⊺ Σw, (2)

where w is a N × 1 vector of portfolio weights on risky assets and Σ is the N × N sample


variance-covariance matrix. To locate the Global Minimum Variance Portfolio (GMVP),
the objective function is the portfolio variance (σp2 ), and we minimize it by changing
weights on risky assets (w). Similarly, the portfolio Sharpe Ratio for N risky assets follows

w⊺ E[R]
SRp = √ , (3)
w⊺ Σw

where E[R] is a N × 1 vector of risky assets’ expected excess returns. Therefore, the
numerator is the portfolio expected excess return and the denominator is the portfolio
standard deviation. To find the Maximum Sharpe Ratio Portfolio (MSRP), the objective
function becomes the portfolio Sharpe Ratio (SRp ), and we maximize it by choosing w.
Portfolio weights must sum up to one. The equality investment constraint is thus w⊺ 1 = 1,
with 1 being a N × 1 vector of ones. By imposing the equality investment constraint, the

Electronic copy available at: https://ssrn.com/abstract=4758848


analytical matrix solutions of the GMVP (g) and the MSRP (m) are

Σ−1 1
wg = (4)
1⊺ Σ−1 1

and
Σ−1 E[R]
wm = , (5)
1⊺ Σ−1 E[R]
respectively. Both portfolios are independent of investors’ risk aversions and on the
efficient minimum variance frontier without the risk-free asset. If investors can borrow or
lend at the common risk-free rate, the theoretical efficient frontier becomes the capital
allocation line linking between MSRP and the risk-free asset. If we maximize the expected
mean-variance utility function in Equation (3), we obtain

Σ−1 1 −1 −1
h 1⊺ Σ−1 E[R] i
wa = + A Σ E[R] − 1 . (6)
1⊺ Σ−1 1 1⊺ Σ−1 1

The optimal risky portfolio for an investor is thus the sum of the GMVP and a zero-
sum portfolio that depends on the investor-specific risk aversion. For advanced-level
courses, instructors can derive these closed-form solutions with vector calculus and explain
their implications. However, analytical solutions for portfolio choice problems are no
longer available when we introduce inequality or nonlinear constraints. To find admissible
solutions for the quadratic programming problem subject to realistic investment constraints,
we will numerically solve the system of equations and inequalities corresponding to the
Karush–Kuhn–Tucker (KKT) conditions with Python.

JUPYTERLAB NOTEBOOK

This section provides a step-by-step Python programming guide for applying


portfolio theory to financial market data. In the companion JupyterLab Notebook, each
code cell is executable by pressing Shift ↑ + Enter ←- on one’s keyboard simultaneously or
the ▶ button on the interface’s menu bar. The notebook also gives explanations of model
specifications and their associated syntax in the markdown environment. Instructors
and students can freely modify the coding environments to add new features and the
markdown environment to make comments or take notes.

Electronic copy available at: https://ssrn.com/abstract=4758848


In step one, we import four Python libraries: yfinance, pandas, NumPy, and
matplotlib. First, yfinance is an open-source third-party tool for downloading financial
market data through Yahoo! Finance’s public APIs. The Anaconda distribution includes
all the required libraries except for yfinance. We can install yfinance by running the first
line of Cell [1] in a new cell by removing the # symbol (comment out). Second, pandas
and NumPy provide functions and methods for data manipulations and matrix operations,
respectively. Last, matplotlib allows its users to visualize data and their analytics. In
addition, we need two functions from the optimize module of the SciPy library for solving
constrained optimization problems.

[1]: #pip install yfinance --upgrade --no-cache-dir


import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize, Bounds

In step two, we define our investment universe by enclosing stock tickers in a list:
[‘PG’, ‘DIS’, ‘IBM’, ‘NKE’, ‘BA’, ‘KO’, ‘CVX’] with square brackets. These seven
stocks are BA (Boeing), CVX (Chevron), Coca-Cola (KO), DIS (Disney), IBM (IBM),
NKE (Nike), and PG (Procter & Gamble). Instructors should encourage students to
experiment with different stocks across global stock markets. We use the download
function from yfinance to download daily stock data from Yahoo! Finance. The first input
to the function is the list containing tickers, followed by data frequency in interval, start
date in start, and end date in end. The resulting output is a table (pandas data frame)
named Ast, which contains multiple items for seven stocks from 1 December 2017 to 29
September 2023. We subset the Ast table with the column name ‘Adj Close’ to get the
adjusted closing prices, which are the closing prices after all applicable adjustments for
stock splits and dividend payments. Before going through the code further, instructors
should discuss the dichotomy between the in-sample period for constructing the optimal
portfolio and the out-of-sample period for back-testing the investment strategy. Figure
1 presents a timeline for using five data periods to estimate the portfolio and one data
period to test the strategy. It is essential to emphasize that the IS period must not
overlap with the out-of-sample period to avoid look-ahead bias in back-testing investment
performance. In other words, we cannot use future information in making investment

Electronic copy available at: https://ssrn.com/abstract=4758848


decisions today. Our in-sample period is from 1 December 2017 to 29 December 2022,
whereas our out-of-sample sample starts on 30 December 2022 and ends on 29 September
2023. Like the investment universe, instructors can tell students to try different settings
by specifying in-sample and out-of-sample periods. The pandas library allows us to
manipulate the Ast data frame with different methods.

Figure 1: Back-testing
Estimate Test
Period
−5 −4 −3 −2 −1 0 1

When estimating inputs for the portfolio model, we use the convention sampling
window of 60 monthly returns (5 years of data). Therefore, we apply the loc method
to subset the data by its row index labels. The input of [:‘2022-12-29’] means our
in-sample period starts with the first row until the row of 29 December 2022. Next, we
use resample(‘M’).last() to convert the in-sample daily adjusted closing prices into the
month-end prices. We then convert these adjusted closing prices into percentage returns
with the pct_change() method. Instructors should remind students that these computed
returns only reflect capital gain or loss because percentage changes based on the adjusted
closing prices do not account for dividend yields. The percentage change calculation
requires two consecutive prices, so the first row of the resulting Ri table contains missing
values. We remove them using the dropna() method. We can apply multiple methods to
transform the original object into the desired output with a single line of code. If students
are new to programming, instructors can consider breaking the chain into multiple steps
to explain the inner workings of the code better. For instance, one can create the subset
of Ast by AstIS = Ast[‘Adj Close’].loc[:‘2022-12-29’] and convert it into month-end
data by AstIS = AstIS.resample(‘M’).last().

[2]: Ast = yf.download(['PG', 'DIS', 'IBM', 'NKE', 'BA', 'KO', 'CVX'],


interval="1d", start="2017-12-01", end="2023-09-30")
Ri = (Ast['Adj Close'].loc[:'2022-12-29'].resample('M').last()
.pct_change().dropna())
CRo = Ast['Adj Close'].loc['2022-12-30':'2023']
CRo = CRo/CRo.iloc[0,:]

As for the out-of-sample back-testing period, we again subset Ast with loc to
create the CRo table. We compute the cumulative gross returns of all seven stocks at a

Electronic copy available at: https://ssrn.com/abstract=4758848


daily frequency over 2023 by dividing each column of data by its initial value. We can use
iloc[0,:] to extract data from the first row of every column in CRo. In contrast to loc(),
the iloc() method follows position-based indexing. For Excel or Matlab users, please note
that Python starts its index counter with 0. This procedure is equivalent to standardizing
the initial values of all seven return series to one. Instructors can explain this idea with a
rhetorical question: “Suppose we invested $1 in IBM on the last trading day of 2022; how
much is it worth today in 2023?” and show the plot of cumulative returns for stocks over
the out-of-sample period.

In step three, we obtain historical estimates of expected returns and variance-


covariance matrix from the table of in-sample monthly returns Ri. We apply the mean()
method to compute time-series average values ER (expected returns) and the cov()
method to estimate the sample covariance-variance matrix S. It is a Pythonic way to
write multiple assignments to objects by adding commas between expressions, writing ER
= Ri.mean() and S = Ri.cov() yield identical outputs. We can add axis=1 inside
the mean() method to compute cross-sectional average. The default axis is 0 for indices,
and the alternative is 1 for columns. When commas separate expressions, they become
elements of a tuple. After estimating inputs, we define objective functions using the
keyword def to specify the function name and arguments. The first PV function takes
the weighting vector w and the sample covariance-variance matrix S as inputs, and it
returns the portfolio variance in Equation (2). In addition to the two inputs in PV, the
second NPSR function also requires expected returns ER to compute the negative of
portfolio Sharpe Ratio in Equation (3). We negate the portfolio Sharpe Ratio because
the SciPy library only offers the minimize function. It is typically to have minimize
functions instead of maximize functions in numerical optimization packages across different
programming languages. In effect, we convert the maximization of the portfolio Sharpe
Ratio into the minimization of NPSR. In this simplified example, the risk-free rate is
assumed to be zero. When the risk-free rate is available, we subtract it from the ER array
to get an array of expected excess returns. Instructors can discuss the choice of risk-free
rate with students. For example, it is a common practice to use the one-month Treasury
bill rate as the US risk-free rate.

Electronic copy available at: https://ssrn.com/abstract=4758848


[3]: ER, S = Ri.mean(), Ri.cov()

def PV(w, S):


return (w.T @ S @ w)

def NPSR(w, ER, S):


return -(w.T @ ER)/ ((w.T @ S @ w) ** 0.5)

In step four, we use NumPy arrays and SciPy functions to estimate four portfolios:
i). G: Global Minimum Variance Portfolio (GMVP); ii). M: Maximum Sharpe Ratio
Portfolio (MSRP); iii). Gn: GMVP with no short selling; and iv). Mn: MSRP with no
short selling. Before using the minimize function, we must specify the initial guess for
portfolio weights and investment constraints. We use the len function to count the number
of elements in ER to get the number of assets N. Alternatively, we can use Ri.shape to
check the dimensions of Ri, which returns a tuple containing the number of rows in the
first element and the number of columns in the second element. Please note that shape is
an attribute of the data frame, not a pandas method. If one writes Ri.shape(), an error
will arise. Given our initial guess x0 of an equal-weighted portfolio, we divide an array
of N ones by N. The first investment constraint is an equality constraint ‘type’: ’eq’
with the sum of portfolio weights minus one equals zero ‘fun’: lambda x: np.sum(x) -
1. The lambda function takes an array x as input and returns the sum of elements in
x minus one. We have to specify this constraint inside a dictionary { }. Similar to the
aforementioned list [ ] and tuple ( ,), the dictionary is a built-in data structure of Python.
After preparing all the ingredients for constrained optimizations, we can go through the
procedure of solving the four portfolios individually.

[4]: N = len(ER)
x0 = np.ones(N)/N # initial guess with w = 1/N
cons = ({'type': 'eq', 'fun' : lambda x: np.sum(x) - 1})

G = minimize(PV, x0, method='SLSQP', args=S, constraints=cons).x


M = minimize(NPSR, x0, method='SLSQP', args=(ER, S), constraints=cons).x

bounds = Bounds(0, 1) # define no short selling constraint

Gn = minimize(PV, x0, method='SLSQP', constraints=cons, args=S,


bounds=bounds).x
Mn = minimize(NPSR, x0, method='SLSQP', args=(ER, S), constraints=cons,
bounds=bounds).x

10

Electronic copy available at: https://ssrn.com/abstract=4758848


G: The minimize function for estimating G takes five compulsory arguments: i) the
objective function to be minimized PV; ii) the initial guess x0; iii) the optimiza-
tion algorithm method=‘SLSQP’; iv) argument inputs to the objective function
args=S; and v) optimization constraints constraints=cons. After executing the
minimize function, the array of optimized portfolio weights x is an attribute of the
output and is callable by .x.

M: The inputs for estimating M are similar to G, except for changing the objective
function to NPSR and supplying additional arguments args=(ER, S) to the objec-
tive function. In other words, the optimization problem becomes the minimization
of the portfolio Sharpe Ratio.

Gn: The Bounds function allows us to define the possible range for individual asset
weights wi . It precludes short selling by restricting the lower and upper bounds
to 0 and 1, respectively. These bounds are equivalent to having the inequality
constraint of 0 ≤ wi ≤ 1. Together with the inputs in estimating G, we further add
bounds=bounds to the minimize function to estimate Gn.

Mn: We estimate Mn by adding bounds=bounds to the minimize function of M,


which is identical to maximizing the portfolio Sharpe Ratio with no short selling.

In the final step, we visualize our estimated portfolio weights with a bar plot. P is
an empty pandas data frame with stock tickers as row names index=Ri.columns and
the abbreviations of five portfolios as column names columns=[‘G’, ‘M’, ‘Gn’, ‘Mn’,
‘EW’]. We assign the four sets of portfolio weights estimated in the previous step into P
and plot them using P.plot(kind=‘bar’). All the plots in this cell use the ‘seaborn’
style. The additional Portfolio EW is an equal-weighted strategy that allocates 14.29%
into each stock (wi = 1/7).

[5]: plt.style.use('seaborn')

P = pd.DataFrame(index=Ri.columns, columns=['G', 'M', 'Gn', 'Mn', 'EW'])


P['G'], P['M'], P['Gn'], P['Mn'], P['EW']= G, M, Gn, Mn, 1/N

fig2a = P.plot(kind='bar')
fig2b = (CRo @ P).plot()

11

Electronic copy available at: https://ssrn.com/abstract=4758848


Previously, in step two, CRo contains the out-of-sample cumulative gross returns
for our investment universe. Since portfolio weights sum up to one, the matrix product
of (CRo @ P) yields a data frame containing four strategies’ cumulative daily returns
in 2023. For instance, if we invest 50% in Stock A and 50% in Stock B on day 0, it is
equivalent to investing $0.5 in each stock for every dollar invested. Therefore, the portfolio
net asset value (NAV) on day 5 is just the sum of $0.5 multiplied by the two cumulative
gross returns from day 0 to day 5. We visualize these returns with the plot method of
the pandas data frame.

As shown in Figure 2a, both the global minimum variance portfolio G and the
maximum Sharpe Ratio portfolio M have short selling positions. Portfolio G shorts
6.1% only in Boeing while Portfolio M shorts 14.7% in Boeing, 33.8% in Disney, and
21.2% in IBM. Instructors should explain to students that the short positions in M are
unrealistic as they make the portfolio highly leveraged and very risky. In reality, Portfolio
M is also not implementable because its percentage margin of 41.8% violates the Federal
Reserve Board Regulation T’s minimum 50% initial margin. The percentage margin
is defined as equity divided by the sum of absolute markets values of long and short
positions in stocks. In our example, we compute the percentage margin of Portfolio M
by dividing the total invested cash of $1 by the total absolute weights in stocks. Luckett
(1988) explains the use of margin credit in investment and how the Federal Reserve can
mitigate market speculations by imposing a minimum margin requirement. After imposing
short-sale constraints, Portfolio Gn and Portfolio Mn have only positive weights but no
longer invest in every stock because negative weights in Portfolio G and Portfolio M are
essentially set to zero. For instance, Portfolio Mn invests 67.9% in Procter & Gamble
(SR = 0.23), 16.4% in Nike (SR = 0.18), and 15.7% in Chevron (SR = 0.15). This result
is hardly surprising as the optimizer aims to maximize the portfolio’s in-sample Sharpe
Ratio (SR = 0.27). Assuming zero risk-free rate, we can compute in-sample Sharpe Ratios
of assets by dividing their average returns by their standard deviations. The Python code
is Ri.mean()/Ri.std(). As for portfolio in-sample Sharpe Ratio, we can compute it with
the negative of the pre-specified NPSR function. For example, the code for calculating the
in-sample Sharpe Ratio for Portfolio G is -NPSR(G, ER, S). In contrast, Portfolio Gn
is more diversified, with positive weights in six out of seven stocks. Therefore, instructors
should highlight the potential concentration risk of choosing Portfolio Mn over Portfolio

12

Electronic copy available at: https://ssrn.com/abstract=4758848


Gn in practice.

To assess the performance of the four optimal portfolios, we compare the buy-and-
hold out-of-sample cumulative daily returns from January 2023 until October 2023 in
Figure 2b. Students should now become clear that maximizing the in-sample Sharpe
Ratio may not guarantee a good out-of-sample performance. Portfolio M had the highest
in-sample Sharpe Ratio (SR = 0.32), but it performed the worst out-of-sample. Because
of the high leverage ratio in Portfolio M, the path of its out-of-sample portfolio values
was more bumpy than the rest. In other words, investors of Portfolio M would have
experienced large drawdowns and volatile returns. On the contrary, Portfolios G, Gn,
and Mn held at least 40% in Procter & Gamble and 10% in Nike, and they had tiny or no
short-selling positions. Therefore, they exhibited similar patterns in cumulative returns
over the out-of-sample period.

In Figure 2b, the value of Portfolio EW was on top of others over most of the
out-of-sample period, revealing that more sophisticated portfolios from portfolio theory
might not always perform better than an agnostic strategy. This result might seem
surprising to students, who might question the necessity of learning portfolio theory. To
resolve the potential confusion for students, instructors should consider the following steps.
First, remind students that the back-testing result is always sample-specific. Given only
seven stocks in a short out-of-sample period, we need to include more stocks and use
longer samples to evaluate the empirical performance of these strategies.

Second, introduce students to practitioner and academic journal articles on applying


portfolio theory. For example, Michaud (1989) points out the tendency of mean-variance
optimization in maximizing estimation errors in inputs, also commonly known as the
“garbage in, garbage out” problem. If we cannot reliably estimate expected returns and
the variance-covariance matrix with historical data, the resulting “optimal” portfolios
will likely be sub-optimal out-of-sample. That said, Michaud (1989) argues that mean-
variance optimization is still superior to ad hoc investment practices for using information
efficiently and integrating portfolio objectives with investment constraints. In an interview
(Kritzman and Markowitz, 2017), Markowitz explained the importance of constraints: “We
use constraints for multiple reasons. One reason is that our estimates are imperfect. But
even if our estimates were perfect, we wouldn’t want to expose the investor to too much of

13

Electronic copy available at: https://ssrn.com/abstract=4758848


Figure 2: Visualizing Portfolio Results

(a) Strategy portfolio weights (b) Strategy out-of-sample cumulative returns

highly volatile asset classes like small cap or emerging markets.” By imposing investment
constraints, we avoid extreme allocations and excessive leverages in our portfolios. There
is ample evidence showing that statistical adjustment of inputs and realistic investment
constraints enhance the out-of-sample performance of mean-variance-efficient portfolios.

Regarding input estimations, Jobson and Korkie (1981) consider the James-Stein
approach to estimate the expected returns of individual stocks by the cross-sectional
average of their historical average returns. Using the same estimate for individual stock
returns in portfolio optimization enhances the out-of-sample Sharpe Ratio of the MSRP.
Frost and Savarino (1986) and Jorion (1986) also apply Bayesian shrinkage estimators
on the sample mean. By shrinking sample average returns towards their proposed priors,
they select portfolios superior to those based on sample estimates. Similarly, Ledoit
and Wolf (2004, 2017) show that variance-covariance matrices estimated from shrinkage
estimators outperform their sample counterparts in portfolio optimizations. Since the goal
of the MSRP is to select stocks that maximize the portfolio Sharpe Ratio, the resulting
portfolio weights are sensitive to estimation error in expected returns. In line with this
view, Haugen and Baker (1991) find that the low-volatility portfolio generates superior
out-of-sample performance over the Wilshire 5000 index, whereas Green and Hollifield
(1992) argue using the GMVP to circumvent extreme portfolio allocations arising from
imprecise input estimates. Instructors can follow up by discussing the growing popularity
of low volatility investing (Blitz and van Vliet, 2007) in the asset management industry.

14

Electronic copy available at: https://ssrn.com/abstract=4758848


As for investment constraints, Jagannathan and Ma (2003) show that imposing
the no-short-sales constraints on the minimum variance portfolio has the same effect of
reducing estimated covariances. In other words, these non-negativity constraints mitigate
the effect of sampling error in estimating the variance-covariance matrix, resulting in
better out-of-sample portfolio performance. However, when imposing constraints, there
is a trade-off in reducing estimation error versus increasing model specification error.
Therefore, Jagannathan and Ma (2003) suggest that adding non-negativity constraints is
likely detrimental to portfolio optimizations involving expected returns estimated from
factor models, shrinkage methods, and large portfolios. That said, instructors should
emphasize the practical values of an upper bound on portfolio weight to avoid over-
exposure to one asset and the no-short-sales constraints to reduce investing on margin. In
response to significant estimation errors, we can assume equal expected returns, equal
variances, and correlations of zero across assets. The resulting optimal portfolio will be
an equal-weighted (1/N ) strategy. DeMiguel, Garlappi, and Uppal (2009) document that
the naive 1/N portfolio has better out-of-sample performance than the sample-estimate-
based mean-variance efficient portfolio and its extensions utilizing statistical adjustments
and investment constraints. Krirzman, Page, and Turkington (2010) dispute this view
by arguing that the relatively superior performance of the 1/N portfolio hinges on the
sampling window of input estimations. Optimized portfolios constructed from longer
historical samples can perform better than 1/N portfolio out-of-sample. Importantly,
instructors should emphasize that the “best” practice may depend on many factors, such
as asset classes, markets, and sampling periods. There is no clear-cut answer in selecting
the “true” optimal portfolio.

Third, as stated in Box (1979), “Models, of course, are never true, but fortunately it
is only necessary that they be useful”. There will always be a chasm between theory and
practice. However, models are essential in guiding us to analyze the problem systematically.
In our example, portfolios with low volatility, non-negativity constraints, or 1/N allocations
all perform better than the canonical maximum Sharpe Ratio portfolio. Therefore,
instructors should make students aware of new developments in applying portfolio theory
from both the practitioner and academic sides.

Fourth, instructors should highlight the shortcomings of the simplified analysis.

15

Electronic copy available at: https://ssrn.com/abstract=4758848


For instance, the current analysis assumes a zero risk-free rate, uses closing price returns
without dividend yields, and ignores transaction costs. Interest rates on government debt
securities and total returns of individual stocks are available on commercial databases such
as Bloomberg and Datastream. Regarding transaction cost analysis, Nisbet and O’Brien
(1991) provide a tutorial on incorporating block trading costs into investment analysis.
For real-life applications, a survivorship bias-free database is essential to avoid look-ahead,
over-fitting, and sample selection biases that inflate the out-of-sample performance. To
account for liquidity and implementability issues, practitioners should consider intra-day
transaction data.

Finally, the standard caveat of “past performance does not guarantee future results”
will always apply to investment strategies based on historical data because of estimation
errors and non-stationary parameters. Since the future is hard to predict, the investment
policy statement must include risk warnings and disclaimers.

BRIDGING THEORY AND PRACTICE

The primary objective of teaching portfolio theory interactively with Python is


to promote realism. Figure 3 presents a road map for making investment decisions
quantitatively. The process starts with gathering market data and estimating statistical
inputs. Next, investors impose investment constraints to exclude unrealistic allocations.
Together with inputs and constraints, the mean-variance optimizer selects the optimal
portfolio based on the pre-specified objective function. The back-testing procedure then
examines the investment strategy’s performance in the historical sample. If the back-
testing result is satisfactory, investors can make actual investments according to the
optimal portfolio weights. The final step is to evaluate the actual strategy performance
after implementation. There are backward actions in blocks four and six, reflecting the
possibility of revising and redesigning the investment strategy given poor back-testing
results or trading losses.

Regarding actual applications, some universities have the resources to allow their
students to manage real money portfolios. For example, Belt (1975) and Bear and Boyd
(1984) describe student-managed portfolio programs at their institutions. Yerkes (2018)
highlights key benefits and challenges in letting students manage actual funds. Moreover,

16

Electronic copy available at: https://ssrn.com/abstract=4758848


Figure 3: A Quantitative Investment Flow Chart

Collect Data & Estimate Inputs

Impose Constraints

Portfolio Optimization

Back-testing

Implementation

Performance Evaluation

Lin (2022) explains why these funds face unique challenges and recommends ways of
tackling them. As a low-cost alternative, Wood, O’Hare, and Andrews (1992) discuss the
costs and benefits of using a stock market game with hypothetical money as an educational
tool in the classroom. Kagan, Mayo, and Stout (1995) point out that participants in
stock market games tend to take excessive risks in under-diversified portfolios. Jankowski
and Shank (2010) also argue that free online trading simulators lack the flexibility for
higher-level courses. That said, Ali, Derina, and Zurbruegg (2009) find that students
become more motivated to study and eager to pursue a future career in finance after
participating in an online portfolio trading game.

Therefore, when designing the portfolio theory course, instructors should adopt
the problem-based learning approach to offer a realistic experience to students (Loviscek,
Crowley, and Anderson, 2003). On the one hand, homework assignments should focus
on developing students’ technical skills in statistics and optimizations. Instructors can
specify a sample data set and provide expected outcomes for students to replicate. By
doing so, students can check their understanding of the course material and ensure
they are proficient with Python applications in portfolio selections. On the other hand,
group projects and presentations should focus on encouraging students’ creativity, critical
thinking, and collaboration. Table 1 gives a sample description of the group project.

Students can freely choose the investment universe, the statistical tools for estimating
inputs, and the portfolio construction methodology. The only constraints students face are

17

Electronic copy available at: https://ssrn.com/abstract=4758848


Table 1: Group Project

In December 2022, you are working with a group of fund managers managing a
quantitative fund. Your group wants to design and implement a new investment strategy
starting January 2023. Your investment decisions should be based on statistical
analysis and back-testing. The fund mandate is to explore profitable equity investment
opportunities in a stock market. The fund’s investors have moderate risk tolerance so
that the fund can take leveraged or short-selling positions. However, the fund’s initial
percentage margin must be higher than 50%. The fund’s model validation quant will
inspect your team’s source Python codes in constructing the strategy.

The tentative work plan is as follows:


1. Choose a specific stock market around the world as your investment universe.
2. Identify the appropriate index for benchmarking your fund’s performance.
3. Create a proprietary trading strategy that aims to beat the benchmark index.
4. Draft an investment policy statement.

investors’ risk tolerance and the leverage limit, thus restricting excessive risk-taking and
under-diversification. By analyzing international stock markets, instructors can discuss
the equity home bias puzzle (French and Poterba, 1991) and explain the potential benefit
of diversifying investment globally. To promote data and knowledge sharing, students
must upload their Python codes and investment policy statements of their investment
strategies onto the class website before their project presentations. Students will learn
to work in a team and write professionally. The development of technical writing skill is
essential as Kish and Hogan (2001) suggest that write-ups in asset allocation exercises
help students prepare for job interviews. In the investment policy statement, students
must state their fund’s investment objectives to potential clients and explain why they
believe their strategy can meet their goals. Furthermore, they should discuss their fund’s
asset allocation, risk tolerance, and other practical concerns. Like academic conferences,
project groups will present their strategies and critique other groups’ findings. The peer
review process motivates students to think critically about each other’s work. At the end
of each presentation and discussion session, instructors should give their comments and
suggestions to student presenters and discussants. In between sessions, instructors should
open the floor for Q&As.

18

Electronic copy available at: https://ssrn.com/abstract=4758848


CONCLUSION

This article describes an interactive framework for modernizing the teaching of


the Markowitz (1952)’s MPT with Python. After reviewing the use of programming
languages in teaching finance and current practices of teaching portfolio theory, we provide
a comprehensive guide to creating a JupyterLab Notebook that implements the MPT with
real-world data. Under the quantitative investment roadmap, students will learn to gather
financial market data, estimate parameters, construct portfolios, and back-test investment
strategies. The course design encourages students to share and review each other’s ideas in
class. Correspondingly, students will develop practical skills in programming and critical
thinking in solving real-life problems.

19

Electronic copy available at: https://ssrn.com/abstract=4758848


References
Alexander, Gordon J., 1978, PORTFO: A Computer Program Involving the Application
of Portfolio Theory, Journal of Financial Education 7, 72–75.
Alexander, Gordon J., 1981, PORTID: A Computer Program Involving the Interaction of
Utility Theory and Portfolio Theory, Journal of Financial Education 10, 88–90.
Alexander, Reed, 2022, Wall Street’s love affair with data science has business schools like
Wharton and Columbia scrambling to train the next generation of bankers and deal-
makers, Business Insider, Februrary 24, 2022, https://www.businessinsider.com/wall–
street–data–science–business–schools–mba–wharton–columbia–northwestern–2022–2.
Ali, Akbar Zamin, Ratna Derina, and Ralf Zurbruegg, 2009, Adaptation of Online Material
for Teaching Financial Economics Courses, Journal of Financial Education 35, 96–113.
Arnold, Tom, 2002, Advanced Portfolio Theory: Why Understanding The Math Matters,
Journal of Financial Education 28, 79–96.
Bauer, Richard J. Jr., 2006, Teaching Excel VBA to Finance Students, Journal of Financial
Education 32, 43–63.
Bear, Thomas, and G. Michael Boyd, 1984, An Applied Course in Investment Analysis
and Portfolio Management, Journal of Financial Education 13, 68–71.
Belt, Brian, 1975, A Securities Portfolio Managed by Graduate Students, Journal of
Financial Education 4, 77–81.
Bernstein, Peter L., 1992, How Improbable Are the Origins of Modern Wall Street? Or the
Miraculous Importance of Three Little Words, Financial Analysts Journal 48, 15–18.
Blitz, David C., and Pim van Vliet, 2007, The Volatility Effect, The Journal of Portfolio
Management 34, 102–113.
Bodie, Zvi, Alex Kane, and Alan Marcus, 2021, Investments, 12th edition (McGraw Hill).
Bodie, Zvi, Alex Kane, and Alan Marcus, 2022, Essentials of Investments, 12th edition
(McGraw Hill).
Box, George E. P., 1979, Some problems of statistics and everyday life, Journal of the
American Statistical Association 74, 1–4.
Burns, William, and Michele Burns, 1982, The Use of a Portfolio Management Simulation
as a Learning Device, Journal of Financial Education 11, 79–82.
Cagle, Julie A. B., Philip W. Glasgo, and David C. Hyland, 2010, Spreadsheets: Do They
Improve Student Learning in the Introductory Finance Course?, Journal of Financial
Education 36, 35–52.
Carter, David A., William H. Dare, and William B. Elliott, 2002, Determination Of Mean-
Variance Efficient Portfolios Using An Electronic Spreadsheet, Journal of Financial
Education 28, 63–78.
DeMiguel, Victor, Lorenzo Garlappi, and Raman Uppal, 2009, Optimal Versus Naive
Diversification: How Inefficient is the 1/N Portfolio Strategy?, The Review of Financial
Studies 22, 1915–1953.
Eckardt, Walter L. Jr., 1975, PERPORT: A Portfolio Management Game, Journal of
Financial Education 4, 91–98.
Eckardt, Walter L. Jr., 1978, PERS: The Evolution of a Computer-Based Portfolio Game,
Journal of Financial Education 7, 72–75.

20

Electronic copy available at: https://ssrn.com/abstract=4758848


French, Kenneth R., and James M. Poterba, 1991, Investor Diversification and International
Equity Markets, The American Economic Review 81, 222–226.
Frost, Peter A., and James E. Savarino, 1986, An Empirical Bayes Approach to Efficient
Portfolio Selection, The Journal of Financial and Quantitative Analysis 21, 293–305.
Ginnis, Tod, 2023, Remembering Harry Markowitz: A Risk Management Pioneer and
Beloved Mentor, Risk Career Blog, GARP, July 31, 2023, https://www.garp.org/risk–
intelligence/risk–career/remembering–harry–markowitz–073123.
Goetzmann, William N., 2023, Harry Markowitz in Memoriam, Financial Analysts Journal
79, 5–7.
Green, Richard C., and Burton Hollifield, 1992, When will mean-variance efficient portfolios
be well diversified?, The Journal of Finance 47, 1785–1809.
Haugen, Robert A., and Nardin L. Baker, 1991, The efficient market inefficiency of
capitalization–weighted stock portfolios, The Journal of Portfolio Management 17,
35–40.
Hilpisch, Yves J., 2018, Python for Finance, second edition (O’REILLY).
Jagannathan, Ravi, and Tongshu Ma, 2003, Risk Reduction in Large Portfolios: Why
Imposing the Wrong Constraints Helps, The Journal of Finance 58, 1651–1683.
Jankowski, Joel, and Todd Shank, 2010, A Comparison of Online Stock Trading Simulators
for Teaching Investments, Journal of Financial Education 36, 105–119.
Jobson, J. D., and Robert M Korkie, 1981, Putting Markowitz theory to work, The
Journal of Portfolio Management 7, 70–74.
Johnson, Larry J., and Yaru G. Liu, 2005, An Excel-Based Method to Determine Investible
Mean-Variance Efficient Portfolios with Short Sales, Journal of Financial Education 31,
89–99.
Jorion, Philippe, 1986, Bayes-Stein Estimation for Portfolio Analysis, The Journal of
Financial and Quantitative Analysis 21, 279–292.
Kagan, Gary, Herbert Mayo, and Robert Stout, 1995, Risk-Adjusted Returns and Stock
Market Games, The Journal of Economic Education 26, 39–50.
Kish, Richard J., and Karen M. Hogan, 2001, Linking Theory And Practice: An Asset
Allocation Assignment, Journal of Financial Education 27, 76–87.
Krirzman, Mark, Sébastien Page, and David Turkington, 2010, In Defense of Optimization:
The Fallacy of 1/N, Financial Analysts Journal 66, 31–39.
Kritzman, Mark, and Harry M. Markowitz, 2017, An Interview with Nobel Laureate Harry
M. Markowitz, Financial Analysts Journal 79, 16–21.
Kwan, Clarence C. Y., 2001, Portfolio Analysis Using Spreadsheet Tools, Journal of
Applied Finance 11, 70–81.
Ledoit, Olivier, and Michael Wolf, 2004, Honey, I Shrunk the Sample Covariance Matrix,
The Journal of Portfolio Management 30, 110–119.
Ledoit, Olivier, and Michael Wolf, 2017, Nonlinear Shrinkage of the Covariance Matrix
for Portfolio Selection: Markowitz Meets Goldilocks, The Review of Financial Studies
30, 4349–4388.
Lin, Crystal Yan, 2022, Student-managed investment funds: seven mismatches and three
recommendations, Managerial Finance 48, 334–347.

21

Electronic copy available at: https://ssrn.com/abstract=4758848


Loviscek, Anthony L., Frederick D. Crowley, and Randy I. Anderson, 2003, Problem-
Based Learning In Finance: An Application To Portfolio Analysis, Journal of Financial
Education 29, 85–103.
Luckett, Dudley G., 1988, A Pedagogical Note on the Use of Margin Credit, The Journal
of Economic Education 19, 337–340.
Markowitz, Harry M., 1952, Portfolio Selection, The Journal of Finance 7, 77–91.
Markowitz, Harry M., 1990, Foundations of Portfolio Theory, No-
bel Prize Lecture in Economic Sciences December 7, 1990,
https://www.nobelprize.org/uploads/2018/06/markowitz–lecture.pdf.
Michaud, Richard O., 1989, The Markowitz Optimization Enigma: Is “Optimized” Opti-
mal?, Financial Analysts Journal 45, 31–42.
Nawrocki, David N., 1980, Development of Computer Programs for Application of Portfolio
Theory in the Classroom, Journal of Financial Education 9, 93–97.
Nisbet, James D., and Thomas J. O’Brien, 1991, An Example of Blending Block Trading
Costs into Investment Analysis, Financial Practice and Education 1, 35–38.
Pfaff, Philip, 1981, Modern Portfolio Theory on a Microcomputer, Journal of Financial
Education 10, 85–87.
Riley, William B., and Austin H. Jr. Montgomery, 1980, The Use of Interactive Com-
puter Programs in Security Analysis and Portfolio Management, Journal of Financial
Education 9, 89–92.
Root, Thomas, John Rozycki, Jimmy Senteza, and Inchul Suh, 2007, The Undergraduate
Finance Curriculum in the New Millennium: A Comprehensive Survey, Journal of
Financial Education 33, 1–27.
Runyon, Richard, 1978, The Use of a Computer Simulation Game in the Investments
Course, Journal of Financial Education 7, 76–79.
Scaggs, Alexandara, 2023, Harry Markowitz, economist, 1927–2023, Financial Times, July
1, 2023, https://www.ft.com/content/2ebd7d96–c3e3–4b69–92ac–e39bc90e8068.
Sharpe, William F., 1964, Capital Asset Prices: A Theory Of Market Equilibrium Under
Conditions Of Risk, The Journal of Finance 19, 425–442.
Stephens, Alan A., 1998, Markowitz and the Spreadsheet, Journal of Financial Education
24, 35–43.
The Economist, 2017, The world’s most valuable resource is no longer oil, but data, May
6, 2017, https://www.economist.com/leaders/2017/05/06/the–worlds–most–valuable–
resource–is–no–longer–oil–but–data.
TIOBE, 2023, TIOBE Index for December 2023, https://www.tiobe.com/tiobe–index/.
Wood, William C., Sharon L. O’Hare, and Robert L. Andrews, 1992, The Stock Market
Game: Classroom Use and Strategy, The Journal of Economic Education 23, 236–246.
Yerkes, Rustin T., 2018, The Art and Science of Student-Managed Investment Portfolios,
The Journal of Trading 13, 39–51.

22

Electronic copy available at: https://ssrn.com/abstract=4758848

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy