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.
- Coatue Data Science Head Alex Izydorczyk Is Leaving the Company - Business Insider - November 25th, 2021
- Data Science Is a Key Weapon in the Fight Against Fraud - Built In - November 25th, 2021
- Grads from MIT, IITs, IIMs and other Renowned Varsities Working on Building LIVEY, a World-Class Data Science and AI-powered Healthcare Solution -... - November 25th, 2021
- Tim Kao: Infusing Data Science to Revolutionize the Functionalities of Navy and Marine Industries - Analytics Insight - November 25th, 2021
- COVID-19: Compliance to household mixing restrictions in England decreased with each lockdown - EurekAlert - November 25th, 2021
- Data science approaches to confronting the COVID-19 pandemic: a narrative review - DocWire News - November 25th, 2021
- Bayer and Microsoft partner to build new cloud-based digital tools - IT Brief New Zealand - November 25th, 2021
- Elizabeth Holmes testifies in Theranos trial & Athenahealth acquisition 2.0 - STAT - November 25th, 2021
- Who are the current generation of Chief Data Officers? - ComputerWeekly.com - November 25th, 2021
- Analysis in Government Awards 2021 Shortlist - GOV.UK - November 25th, 2021
- Pace receives NSF grant to expand data science instruction nationwide - Westfair Online - November 17th, 2021
- Snowflake Shapes the Future of Data Science with Python Support - Business Wire - November 17th, 2021
- UK Railroads Invest in Data Science, AI, and Machine Learning - Cities of the Future - November 17th, 2021
- Top Upcoming Data Science Webinars to Attend in 2021 - Analytics Insight - November 17th, 2021
- The Iconic CEO Erica Berchtold wants to use the science of data to convert you to online shopping - The Australian Financial Review - November 17th, 2021
- Immunosuppressants linked to severe reactions in people with common genetic profile - Stanford Medical Center Report - November 17th, 2021
- Why Genpact 'Dare in Reality' Is A Hackathon Not To Be Missed - Analytics India Magazine - November 17th, 2021
- TigerGraph expands its graph data library with 20 new algorithms - VentureBeat - November 17th, 2021
- Improving Your Odds of ML Success with MLOps - insideBIGDATA - November 17th, 2021
- Smarter Production and Data-Driven Insight | - Advanced Television - November 17th, 2021
- The Role of Women in Scalping up AI and Data Science - Analytics Insight - November 10th, 2021
- Debunking The Four Most Common Data Science Myths - Influencive - November 10th, 2021
- Olive Partners with ClosedLoop to Improve Care and Reduce Financial Risk for Patients - Yahoo Finance - November 10th, 2021
- Knowland Releases First-of-Its-Kind Future Event Activity Forecast - PRNewswire - November 10th, 2021
- The Question Weve Stopped Asking About Teen-Agers and Social Media - The New Yorker - November 10th, 2021
- Do You Want To Deploy Responsible AI In Your Organization? Join This Session To Operationalize Responsible AI - Analytics India Magazine - November 10th, 2021
- In-Demand skills research finds the US is one of the most competitive markets for skilled tech workers, but talent scarcity is a global issue -... - November 10th, 2021
- Data Science Master's Degree | 100% Online | University of ... - November 6th, 2021
- MSE in Data Science - November 6th, 2021
- Variable Names: Why They're a Mess and How to Clean Them Up - Built In - November 6th, 2021
- UVA Announces New Research Partnership at Intersection of Business and Data Science - UVA Today - November 6th, 2021
- Succeeding in Data Science Projects Inputs that Could Help You - Analytics Insight - November 6th, 2021
- Inspiring Innovation; New Short Talks Features Karl Schubert and Data Science Program - University of Arkansas Newswire - November 6th, 2021
- How to succeed around data science projects - Information Age - November 6th, 2021
- Training students at the intersection of power engineering and computer science WSU Insider - WSU News - November 6th, 2021
- California Tries to Close the Gap in Math, but Sets Off a Backlash - The New York Times - November 6th, 2021
- A look at some of the AI and ML expert speakers at the iMerit ML DataOps Summit - TechCrunch - November 6th, 2021
- Exploring, Monitoring and Modeling the Deep Ocean Are Goals of New Research - UT News - UT News | The University of Texas at Austin - November 6th, 2021
- UVA Science and Engineering Faculty Win 12 NSF Career Awards - University of Virginia - November 6th, 2021
- Microsoft Excel is still the data analytics gold standard. The pre-Black Friday sale can teach you fast. - The Next Web - November 6th, 2021
- Heard on the Street 10/28/2021 - insideBIGDATA - October 28th, 2021
- Insights on the Data Science Platform Global Market to 2027 - Featuring Microsoft, IBM and Google Among Others - Yahoo Finance - October 28th, 2021
- Ericsson helps Giga to map connectivity in more than a million schools - Ericsson - October 28th, 2021
- NIH awards nearly $75M to catalyze data science research in Africa - National Institutes of Health - October 26th, 2021
- Worldwide Data Science Platform Industry to 2027 - Increasing Adoption of Data-Driven Technologies by Enterprises Presents Opportunities - PRNewswire - October 26th, 2021
- Multi-institution project to train Kenyan experts to bring social determinants to bear on modeling health outcomes - Newswise - October 26th, 2021
- Takeda's Krista McKee on Shifting the Data Ecosystem - Bio-IT World - October 26th, 2021
- Stitch Fix CEO: 'Data science and algorithms are at the core' of the company - Oakland News Now - October 26th, 2021
- Miami Dade College to Host 10th Anniversary School of Science White Coat Ceremony and STEM Research Symposium - The Reporter - October 26th, 2021
- FDA takes hands-on approach to upskill workforce under data modernization action plan - Federal News Network - October 26th, 2021
- Global Data Science Platform Market (2021 to 2027) - by Component, Deployment, Organization Size, Function, Industry Vertical and Geography -... - October 26th, 2021
- Planning to study Data Science after Plus-Two? Heres what is on offer - Telegraph India - October 26th, 2021
- Consumer-facing Companies Still Have Few Incentives to Stop Data Breaches, and Thats a National Security Concern. - Council on Foreign Relations - October 26th, 2021
- Skills and online courses to become a Data Scientist, the top job role in the world by 2025 - India Today - October 26th, 2021
- Which degree is right for me: data science or digital health? - News - The University of Sydney - October 26th, 2021
- Data Science Leaders are Ruling the Corporate Industry for their Technical Talent as well as Management Expertise - Analytics Insight - October 26th, 2021
- Will Data Science be in Demand in the Future? - Entrepreneur - September 30th, 2021
- Promoting the Public Good | UVA Today - UVA Today - September 30th, 2021
- MetaCell launches innovative Cloud Hosting for life science and healthcare - Yahoo Finance - September 30th, 2021
- KDD 2021 Honors Recipients of the SIGKDD Best Paper Awards - Yahoo Finance - September 30th, 2021
- Analytics Insight Announces Big Data Analytics Companies of the Year - Yahoo Finance - September 30th, 2021
- R is better than Python. Try telling that to banks - eFinancialCareers - September 30th, 2021
- World AI & Data Science Conference to be held on October 13th, 2021 - Analytics Insight - September 27th, 2021
- How Data Science and Big Data are Shaping the Indian Food Industry in 2021? - Analytics Insight - September 27th, 2021
- mRNA Could Fight Diseases Such as Alzheimer's and Cancer, With Help of UVA Scientist - University of Virginia - September 27th, 2021
- Media advisory: Kevin Leicht to testify before congressional subcommittee about disinformation - University of Illinois News - September 27th, 2021
- Heard on the Street 9/27/2021 - insideBIGDATA - September 27th, 2021
- New Business Institute at UT Austin Will Specialize in Sports Analytics - Diverse: Issues in Higher Education - September 27th, 2021
- How AI is Transforming The Race Strategy Of Electric Vehicles - Analytics India Magazine - September 27th, 2021
- Argentine project analyzing how data science and artificial intelligence can help prevent the outbreak of Covid-19 | Chosen from more than 150... - September 25th, 2021
- Metropolitan Chicago Data-science Corps to partner with area organizations on projects - Northwestern University NewsCenter - September 25th, 2021
- Business of Sports Institute at UT McCombs School Founded by Gift from Accenture - UT News - UT News | The University of Texas at Austin - September 25th, 2021
- 'I Want The Folks in Our Society to Be Data Literate So That We Are Making Good Decisions Together for the Good of the World,' Says Professor... - September 25th, 2021
- Pandemic oversight board to preserve data analytics tools beyond its sunset date - Federal News Network - September 25th, 2021
- Increase the Readability of Your Python Script With 1 Simple Tool - Built In - September 25th, 2021
- On World Cancer Research Day, Illumina Highlights the Transformative Power of Genomics - Yahoo Finance - September 25th, 2021
- Life sciences use of digital twins mirrors its application in other industries - MedCity News - September 25th, 2021
- The Top 3 Tools Every Data Scientist Needs - Built In - September 21st, 2021
- OpsRamp Introduces The Future of Incident Response: Harnessing Machine Learning and Data Science to Predict and Prevent IT Outages - Yahoo Finance - September 21st, 2021
- Health Data Science Symposium: Smartphones, Wearables, and Health 11/5 Reduced Registration by 10/5 - HSPH News - September 21st, 2021