Introduction to Financial Portfolios— Correlation & Diversification with Python

This articles demonstrates how to measure the correlation of financial portfolios to build diversified portfolios.

Amir Nejad
QuantJam
Published in
6 min readFeb 9, 2021

--

Photo by mostafa meraji on Unsplash

Related Read

If you find this article helpful please consider following both author and publication site.

Source code of this article can be downloaded from Github: Link

In the context of finance, a portfolio is a collection of different financial assets (securities) such as stocks, bonds, exchange traded funds (ETFs), mutual funds, and/or cash. According to Markowitz (1952), the process of selecting a portfolio may be divided into two stages:

  • Forming beliefs and forecasts about future performance of securities
  • Selecting appropriate securities based on risk and return metrics to create a portfolio

Uncertainty about the future performance of securities is the main contributor to portfolio deterioration. Because we do not possess a crystal ball to observe the future performance of the securities, we have to account for the worst case scenario of a broad stock market crash. Thanks to the 2020 coronavirus stock market crash, it is easier to envision the risk of future market crashes and importance of portfolio diversification.

Different forces constantly affect financial markets such as trade wars, international tensions, cyberattacks, etc. The magnitude and effect of each force is not known ahead of time, but the future existence of economic forces is almost certain. The following figure demonstrate extreme events (categorized by events greater than 4 standard deviation) affecting the return of the stock market:

Standardized histogram of S&P 500 returns (30-day rolling returns).

Since 1993, four standard deviation events happened in five distinct time periods:

  1. 1998: Stock market crash caused by the Russian government devalues the ruble, defaults on domestic debt
  2. 2001: The September 11 attacks caused global stock markets to drop sharply.
  3. 2002: Stock market downturn of 2002
  4. 2008: Financial crisis of 2007–2008
  5. 2020: Global pandemic stock market crash

As it can be seen, stock market crashes are caused by different forces that before hand cannot be predicted. Such systematic risks can affect the majority of stocks in the market. Non-systematic risk, also called firm-specific risk, refers to risk that is unique to a particular asset; in the case of equity securities, non-systematic risk may include the possibility that the issuer would underperform compared to its peers [Kwon-Yong, 2019].

Therefore, it is sensible to somehow diversify financial portfolios and construct risk resilient portfolios. One way of thinking about diversification is using correlation. The returns of assets tend to move up and down together. Diversification may be the only free lunch in finance. To reduce risk it is necessary to avoid portfolios whose securities are all highly correlated with each other. One hundred securities whose returns rise and fall in near unison afford little more protection than the uncertain return of a single security [Markowitz 1952].

Correlation

Correlation is a mathematical relationship whos value is between +1 and -1. The value of 1 means two assets move in tandem and correlation of -1 means that prices move in opposite directions. Correlation value of 0 means there’s no correlation between the assets. One can think of correlation as a measure of identifying redundancy in the portfolio. If two assets in a portfolio have perfect correlation, then dropping one asset from the portfolio should not diminish performance of the portfolio while reducing the risk of correlated market events (assuming future performance of assets follows historical trends).

S&P 500 ETF Correlation Analysis

Let’s assume a portfolio containing all components of S&P 500 index. Measuring correlations between assets in such portfolio is easy using Pandas and corr functionalities. After downloading the historical data, we can measure the rolling correlations between each stock (window of 30 days used in this work) and find out the median correlation of S&P 500 stocks. The code snippet for correlation calculations can be seen in the following block (Please note that in this context, I am focusing only on the magnitude of the correlation coefficient, hence abs function is used to calculate absolute value of coefficients):

The following figure demonstrates cumulative return of the S&P 500 index along with rolling 30-day median correlation coefficient between all 500 companies of the S&P 500 index. The function median_corr (introduced above) takes the time series of stock price data, and calculates correlations between the stocks using pandas.DataFrame.corr function. The results demonstrates 5 periods of high correlations spikes (correlation greater than 0.75) and during such periods market experienced drawdown and decline in returns. For example, during March 2020 market decline of COVID-19 pandemic, the median correlation coefficient spiked to the highest level during the 2014–2020 period. In contrast, the median correlation of the market stayed under 0.55 during the year 2017 when the market demonstrated significant gain without any visible periods of extreme drawdown. One can conclude from this exercise that during market crashes most stocks behave similarly (hence correlation coefficient spikes) and during the market gain, stocks follow different paths.

Industrial ETF Correlation Analysis

Now let’s repeat the same exercise but this time we assume holding only XLI, the industrial sector focused ETF that contains the basket of 75 industrial companies (refer here for more info). Applying the median_corr function on the constituents of this ETF yields the following figure. Looking at the plot, one can notice far more frequent periods that correlation spiked larger than 0.75 (more than 13 times!). As a result of building a portfolio with highly correlated assets, one can observe such a portfolio experiences more correlated events that potentially diminish the performance of the portfolio.

Key Takeaways

Correlation can play an import role in financial portfolios. Looking at the constituents of S&P 500, we discovered stocks behave similarly during the market crashes and selloffs. However, it seems during the market rally, stocks behave differently and correlation coefficient stays lower than 0.5 (absolute correlation coefficient). Focusing on the sector specific ETFs reveals constituents of sectors experience high correlation events more frequently. This leads to the importance of diversification. A properly diversified portfolio has low (or negative) correlation so during market selloffs some parts of portfolio rise while other parts fall. This suggests numerous portfolio building schemes to build a properly diversified portfolios such as 60/40, 90/10, three-fund etc. In the next parts of this series we will discuss correlation in more detail and attempt to introduce diversified portfolios. Stay tuned and follow us to get notified on the future episodes.

Thanks for reading! My name is Amir Nejad,PhD. I’m a data scientist and editor of QuantJam , and I love share my ideas and to collaborate with other fellow data scientists. You can connect with me on Github, Twitter, and LinkedIn.

QuantJam:

You can see my other writings at:

About QuantJam

Quantjam is a medium publishing company aims at sharing reproducible research focused on finance and stock market data.

Follow us to stay up-to-date on all articles and activities: https://medium.com/quantjam

Disclaimer

QuantJam writings do not include any investment advice. Past performance is no guarantee of future results. Please consult with your financial advisor before making any investment decision. Investing involves risk and you may incur a profit or loss regardless of strategy selected, including diversification and asset allocation. Investments mentioned may not be suitable for all investors.

All images have been produced by the author, except where stated otherwise.

References

  • Markowitz, H. (1952). “Portfolio Selection”. The Journal of Finance, 7(1), 77–91. doi:10.2307/2975974
  • Jin, Kwon-Yong. “The Myth of Diversification: The Destabilizing Impact of Diversification on Financial Institutions.” Journal of Financial Regulation 5.2 (2019): 179–219.

--

--

Amir Nejad
QuantJam

PhD. Engineer | Data Scientist | Problem Solver | Solution Oriented (twitter: @Dr_Nejad)