An Introduction to Portfolio Optimization in Python – Built In

In investing, portfolio optimization is the task of selecting assets such that the return on investment is maximized while the risk is minimized. For example, an investor may be interested in selecting five stocks from a list of 20 to ensure they make the most money possible. Portfolio optimization methods, applied to private equity, can also help manage and diversify investments in private companies. More recently, with the rise in cryptocurrency, portfolio optimization techniques have been applied to investments in Bitcoin and Ethereum, among others.

In each of these cases, the task of optimizing assets involves balancing the trade-offs between risk and return, where return on a stock is the profits realized after a period of time and risk is the standard deviation in an asset's value.Many of the available methods of portfolio optimization are essentially extensions of diversification methods for assets in investing. The idea here is that having a portfolio of different types of assets is less risky than having ones that are similar.

Finding the right methods for portfolio optimization is an important part of the work done by investment banks and asset management firms. One of the early methods is called mean variance optimization, which was developed by Harry Markowitz and, consequently, is also called the Markowitz Method or the HM method. The method works by assuming investors are risk-averse. Specifically, it selects a set of assets that are least correlated (i.e., different from each other) and that generate the highest returns. This approach means that, given a set of portfolios with the same returns, you will select the portfolio with assets that have the least statistical relationship to one another.

For example, instead of selecting a portfolio of tech company stocks, you should pick a portfolio with stocks across disparate industries. In practice, the mean variance optimization algorithm may select a portfolio containing assets in tech, retail, healthcare and real estate instead of a single industry like tech.Althoughthis is a fundamental approach in modern portfolio theory, it has many limitations such as assuming that historical returns completely reflect future returns.

Additional methods like hierarchical risk parity (HRP) and mean conditional value at risk (mCVAR) address some of the limitations of the mean variance optimization method. Specifically, HRP does not require inverting of a covariance matrix, which is a measure of how stock returns move in the same direction. The mean variance optimization method requires finding the inverse of the covariance matrix, however, which is not always computationally feasible.

Further, the mCVAR method does not make the assumption that mean variance optimization makes, which happens when returns are normally distributed. Since mCVAR doesnt assume normally distributed returns, it is not as sensitive to extreme values like mean variance optimization. This means that if a stock has an anomalous increase in price, mCVAR will be more robust than mean variance optimization and will be better suited for asset allocation. Conversely, mean variance optimization may naively suggest we disproportionately invest most of our resources in an asset that has an anomalous increase in price.

The Python package PyPortfolioOpt provides a wide variety of features that make implementing all these methods straightforward.Here, we will look at how to apply these methods to construct a portfolio of stocks across industries.

More From Sadrach PierreNeed to Perform Financial Data Analysis? Why Python Is Your Best Tool.

We will pull stock price data using the Pandas-Datareader library. You can easily install the library using pip in a terminal command line:

Next, lets import the data reading in a new Python script:

We should pull stocks from a few different industries, so well gather price data in healthcare, tech, retail and finance. We will pull three stocks for each industry.Lets start by pulling a few stocks in healthcare. We will pull two years of stock price data for Moderna, Pfizer and Johnson & Johnson.

First, lets import Pandas and relax the display limits on rows and columns:

Next, lets import the datetime module and define start and end dates:

Now we have everything we need to pull stock prices. Lets get data for Moderna (MRNA):

Lets wrap this logic in a function that we can easily reuse since we will be pulling several stocks:

Now, lets pull for Pfizer (PFE) and Johnson & Johnson (JNJ):

Lets define another function that takes a list of stocks and generate a single data frame of stock prices for each stock:

Now, lets pull stocks for the remaining industries:

Healthcare: Moderna (MRNA), Pfizer (PFE), Johnson & Johnson (JNJ)

Tech: Google (GOOGL), Facebook (FB), Apple (AAPL)

Retail: Costco (COST), Walmart (WMT), Kroger Co (KR)

Finance: JPMorgan Chase & Co (JPM), Bank of America (BAC), HSBC Holding (HSBC)

We now have a single dataframe of returns for our stocks. Lets write this dataframe to a csv so we can easily read in the data without repeatedly having to pull it using the Pandas-Datareader.

Now, lets read in our csv:

Now we are ready to implement the mean variance optimization method to construct our portfolio. Lets start by installing the PyPortfolioOpt library:

Now, lets calculate the covariance matrix and store the calculated returns in variables S and mu, respectively:

Next, lets import the EfficientFrontier module and calculate the weights. Here, we will use the max Sharpe statistic. The Sharpe ratio is the ratio between returns and risk. The lower the risk and the higher the returns, the higher the Sharpe ratio. The algorithm looks for the maximum Sharpe ratio, which translates to the portfolio with the highest return and lowest risk. Ultimately, the higher the Sharpe ratio, the better the performance of the portfolio.

We can also display portfolio performance:

Finally, lets convert the weights into actual allocations values (i.e., how many of each stock to buy). For our allocation, lets consider an investment amount of $100,000:

Our algorithm says we should invest in 112 shares of MRNA, 10 shares of GOOGL, 113 shares of AAPL and 114 shares of KR.

We see that our portfolio performs with an expected annual return of 225 percent. This performance is due to the rapid growth of Moderna during the pandemic. Further, the Sharpe ratio value of 5.02 indicates that the portfolio optimization algorithm performs well with our current data. Of course, this return is inflated and is not likely to hold up in the future.

Mean variance optimization doesnt perform very well since it makes many simplifying assumptions, such as returns being normally distributed and the need for an invertible covariance matrix. Fortunately, methods like HRP and mCVAR address these limitations.

The HRP method works by finding subclusters of similar assets based on returns and constructing a hierarchy from these clusters to generate weights for each asset.

Lets start by importing the HRPOpt method from Pypfopt:

We then need to calculate the returns:

Then run the optimization algorithm to get the weights:

We can now print the performance of the portfolio and the weights:

We see that we have an expected annual return of 24.5 percent, which is significantly less than the inflated 225 percent we achieved with mean variance optimization. We also see a diminished Sharpe ratio of 1.12. This result is much more reasonable and more likely to hold up in the future since HRP is not as sensitive to outliers as mean variance optimization is.

Finally, lets calculate the discrete allocation using our weights:

We see that our algorithm suggests we invest heavily into Kroger (KR), HSBC, Johnson & Johnson (JNJ) and Pfizer (PFE) and not, as the previous model did, so much into Moderna (MRNA). Further, while the performance decreased, we can be more confident that this model will perform just as well when we refresh our data. This is because HRP is more robust to theanomalous increase in Moderna stock prices.

The mCVAR is another popular alternative to mean variance optimization. It works by measuring the worst-case scenarios for each asset in the portfolio, which is represented here by losing the most money. The worst-case loss for each asset is then used to calculate weights to be used for allocation for each asset.

Lets import the EEfficicientCVAR method:

Calculate the weights and get the performance:

Next, get the discrete allocation:

We see that this algorithm suggests we invest heavily into JP Morgan Chase (JPM) and also buy a single share each of Moderna (MRNA) and Johnson & Johnson (JNJ). Also we see that the expected return is 15.5 percent. As with HRP, this result is much more reasonable than the inflated 225 percentreturns given by mean variance optimizationsince it is not as sensitive to the anomalous behaviour of the Moderna stock price.

The code from this post is available on GitHub.

More in FinanceIs Your Startup Fundraising in 2021? This Is Your Time.

Although we only considered healthcare, tech, retail and finance, the methods we discussed can easily be modified to consider additional industries. For example, maybe you are more interested in constructing a portfolio of companies in the energy, real estate and materials industry. An example of this sort of portfolio could be made up of stocks such as Exxonmobil (XOM), DuPont (DD), and American Tower (AMT). I encourage you to play around with different sectors in constructing your portfolio.

What we discussed provides a solid foundation for those interested in portfolio optimization methods in Python. Having a knowledge of both the methods and the tools available for portfolio optimization can allow quants and data scientists to run quicker experiments for optimizing investment portfolio.

View post:

An Introduction to Portfolio Optimization in Python - Built In

Related Post

Comments are closed.