# Optimizing Portfolios with Modern Portfolio Theory Using Python

Optimizing Portfolios with Modern Portfolio Theory Using PythonMPT and some basic Python implementations for tracking risk, performance, and optimizing your portfolio.

Luke PoseyBlockedUnblockFollowFollowingJun 3Disclaimer: Nothing herein is financial advice, and NOT a recommendation to invest real money with the following methods.

Many platforms exist for simulated investing (paper trading) which can be used for building and developing the methods discussed.

Please use common sense and always first consult a professional before trading or investing.

Modern portfolio theory (MPT) is used by many firms, fund managers, and wealth advisors.

Nobel Laureate Harry Markowitz proposed this idea over six decades ago.

And the theory remains hugely popular today because of its relative simplicity and performance.

MPT is all about assembling a portfolio that maximizes expected return for a set level of risk.

First, let us discuss expected return.

Expected ReturnExpected return is the expected profits of purchasing some asset.

It is easily calculated if probabilities for different ranges of expected prices are established.

This can be found by studying past returns and performance of the asset to find the distribution of prices over established time-frames.

The expected return will be the center of the distribution.

Expected Return…Source: WIKIHere are some implementations of the above methods, written in Python.

The Jupyter Notebook of these methods will be linked at the end of the article.

RiskMPT assumes investors are risk-averse.

That is, it assumes investors will choose a less risky portfolio over a riskier one with the same return.

For example, a portfolio that guarantees \$50,000 profit would be preferable to a portfolio with a 50% chance of returning \$100,000 and a 50% chance of returning \$0.

A risk-neutral person would say those portfolios are equal.

But in MPT, we prefer the risk-averse portfolio.

We can use many different methods for evaluating risk, but standard deviation of an asset’s price to evaluate risk is the most common method.

Here is what a plot of standard deviation might look like.

The percentages can differ depending on the asset or portfolio (combination of assets):The Sharpe Ratio is another popular way for quantifying asset risk.

It simply measures the expected average return in excess of the risk free rate (10-Yr Treasury Bond, currently ~2.

2%).

Sharpe Ratio can help us identify how much additional returns we can make as we add the volatility of riskier assets.

Sharpe Ratio…Source: WIKIWe can use the popular financial functions (ffn) Python library to view all sorts of interesting stats, paying special attention to the ones relating to risk.

We’ll view the six highest companies by market cap (as of June 3rd, 2019).

The current risk free rate (10y treasury) is around 2.

1%.

A Sharpe Ratio of 1 is generally considered “good,” while a Sharpe Ratio of 2 is considered “very good,” and above 3 is considered “excellent.

”Efficient Frontier & Portfolio OptimizationWe can plot all possible combinations of assets as risk vs expected return.

This will show us the optimal portfolio, as our goal is to find the portfolio with the highest ratio of expected return to risk.

For example, a wealth manager might have some formula for determining acceptable client risk.

Someone nearing retirement may have especially low risk, while someone early-mid career may have a higher risk profile.

We could then test all combinations of assets that involve the determined level of risk and pick the portfolio with the highest expected return.

A portfolio with standard deviation of 4% and expected return of 5.

5% is of course superior to a portfolio with standard deviation of 4% and expected return of 5%.

Efficient Frontier Plot…Source: WIKIThere’s a library I recently found that I am especially fond of known as PyPortfolioOpt that has some very useful methods for implementing portfolio optimization techniques.

Here are some examples.

The first example is optimizing a portfolio for maximum Sharpe Ratio.

The output is an array of weights for each stock.

We can take this a step further with a function that given the above input and a cash value will even return the number of shares to buy of each stock to optimize Sharpe Ratio.

And finally, we can find the efficient frontier for a number of desired portfolios.

The following is for a long/short market-neutral portfolio limited to the six highest stocks by market cap, with a set target return.