Correlation Matrix

Correlation Matrix in Python: How Correlated are COVID-19 Cases and Different Financial Assets?

Since the sudden emergence of COVID-19, global financial markets have experienced turbulent times. After the markets initially experienced a sharp slump at the beginning of 2020a rapid (V-shaped) recovery led to new record highs at the end of the year. However, there were also sectors and asset classes whose development was precisely the opposite. In this article, we create a correlation matrix in Python that shows how the prices of various asset classes were affected by COVID-19. We will include different asset classes such as stocks, resources, futures, and cryptocurrencies.

The remainder of this blog is structured as follows: Firstly, this article briefly introduces the famous Pearson Correlation Coefficient and shows how to interpret it. Then, we will use this Correlation to create a stock market correlation matrix in Python. The matrix will measure the correlation between COVID-19 cases and several financial assets such as gold, Bitcoin, and different stocks.

A correlation matrix, as we will create it in this article
A correlation matrix, as we will create it in this article.

About Pearson Correlation

The Pearson correlation coefficient r is a standard measure for quantifying a linear relationship between two variables. In other words, r is a measure of how strongly two continuous variables (for example, price or volume) tend to make similar changes. For the Pearson correlation coefficient to return a meaningful value, the following conditions must be met:

  • Both variables x and y are metrically scaled and continuous.
  • The relationship between the two variables is approximately linear.
  • The two samples of the variables x and y are independent of each other.

Correlation measures how much two variables are associated. The Pearson correlation is calculated by dividing the covariance of two variables (x, y) by their standard deviations.

The standard Formula for the Pearson Correlation Coefficient
The formula for the Pearson Correlation Coefficient r

Interpreting the Pearson Correlation Coefficient

The value of r is restricted to the range between 1 and -1. Interpreting r requires us to differentiate the following cases:

  • The closer r is to 1, the stronger is the relationship and the closer are points (xi / yi) on the regression line together.
  • The closer r is to 0, the weaker is the correlation and the more widely spread are the points around the regression line.
  • The extreme cases r = 1 or r = -1 result from a functional relation, which is defined by a linear equation of the form y = a + bx can be described exactly. In this case, all points (xi / yi) are located on the regression line.
correlation matrix
Graphical representation of different correlation coefficients

Be aware that the correlation coefficient is often subject to misinterpretation. For example, an empirical correlation coefficient whose value is > 0 merely states that we can prove a relation based on a sample. However, it does not explain why this relationship exists. In addition, if r ~ 0 does not mean that two variables are not related in general. Instead, it only means that we cannot prove a linear relation.

Implementing a Correlation Matrix in Python

In the following, we will develop a correlation matrix using Python that shows the correlation between COVID-19 cases and different financial assets. We will include data on the spread of cases and COVID-19 casualties in our analysis. I have chosen this example because, when writing this article, the whole topic about visualizing COVID statistics is highly relevant.

Prerequisites

Before we start the coding part, make sure that you have set up your Python 3 environment and required packages. If you don’t have an environment set up yet, you can follow this tutorial to set up the Anaconda environment.

Also, make sure you install all required packages. In this tutorial, we will be working with the following standard packages: 

In addition, we will be using the pandas-DataReader package and Seaborn for visualization.

You can install packages using console commands:

  • pip install <package name>
  • conda install <package name> (if you are using the anaconda packet manager)

Step #1 Load Data

We begin by loading data about historic COVID-19 cases and price Information on different financial assets.

1.1 Load Historic COVID-19 Data

We begin by downloading the COVID-19 data. For this purpose, we will use the Statworx API. It provides historical time series data on the number of COVID-19 cases in different countries. In addition, the data contains the number of casualties. If you are not yet familiar with APIs, consider my recent tutorial on working with APIs in Python.

# Setup Pakages
import pandas as pd
import pandas_datareader as web
import numpy as np
from datetime import datetime
import seaborn as sns
from matplotlib import pyplot as plt
import matplotlib as matplot
import matplotlib.dates as mdates
import matplotlib.cbook as cbook
import requests
import json
from pandas.plotting import register_matplotlib_converters

# Load second Dataset with Corona Cases
payload = {"code": "ALL"}
URL = "https://api.statworx.com/covid"
response = requests.post(url=URL, data=json.dumps(payload))
df_covid = pd.DataFrame.from_dict(json.loads(response.text))
# df_covid = df_covid[df_covid['code'] == 'US']

# add the date column as variable
df_covid["Date"] = pd.to_datetime(df_covid["date"])

# delete some columns that we won't use
df_covid.drop(
    ["day", "month", "year", "country", "code", "population", "date"],
    axis=1,
    inplace=True,
)

# Summarize cases over all countries
df_covid = df_covid.groupby(["Date"]).sum()
df_covid

1.2 Loading Data on Selected Financial Assets

We continue by downloading historical price data on different financial assets. For this purpose, we use the Yahoo Finance API. We limit the period to the time after the first documented COVID-19 cases. When you execute the code of this tutorial as it is, you will receive price information for the following financial assets:

Stock Market Indexes

  • S&P500
  • DAX
  • Niki
  • N225
  • S&P500 Futures

Stocks: Online Services

  • Amazon
  • Netflix
  • Apple
  • Google
  • Microsoft

Stocks: Airlines

  • Lufthansa Stock
  • American Airlines

Resource Futures

  • Crude Oil Price
  • Gold
  • Soybean Price

Treasury Bonds Futures

  • US Treasury Bonds

Exchange Rates

  • EUR-USD
  • CHF-EUR
  • GBP-USD
  • GBP-EUR

Crypto Currencies

  • BTC-USD
  • ETH-USD

Be aware that stock symbols can change from time to time. If the API does not find a specific stock symbol, you have to look up the current Symbol on Yahoo Finance.

df_covid_new = df_covid.copy()

# Read the data for different assets
today_date = datetime.today().strftime("%Y-%m-%d")
start_date = "2020-01-01"
asset_dict = {
    "^GSPC": "SP500",
    "DAX": "DAX",
    "^N225": "N225",
    "ES=F": "SP500FutJune20",
    "LHA.DE": "Lufthansa",
    "AAL": "AmericanAirlines",
    "NFLX": "Netflix",
    "AMZN": "Amazon",
    "AAPL": "Apple",
    "MSFT": "Microsoft",
    "GOOG": "Google",
    "BTC-USD": "BTCUSD",
    "ETH-USD": "ETHUSD",
    "CL=F": "Oil",
    "GC=F": "Gold",
    #"SM=F": "Soybean",
    "ZB=F": "UsTreasuryBond",
    "GBPEUR=X": "GBPEUR",
    "EURUSD=X": "EURUSD",
    "CHFEUR=X": "CHFEUR",
    "GBPUSD=X": "GBPUSD"}

col_list = []
# Join the dataframes
for key, value in asset_dict.items():
    print(key, value)    
    try:
        df_temp = web.DataReader(
            key, start=start_date, end=today_date, data_source="yahoo")
    except ValueError: 
        print(f' {key} symbol not found')
    # convert index to Date Format
    df_temp.index = pd.to_datetime(df_temp.index) 
    df_temp.rename(columns={"Close": value}, inplace=True) # Rename Close Column       
    df_covid_new = pd.merge(
        left=df_covid_new,
        right=df_temp[value],
        how="inner",
        left_index=True, right_index=True)     

df_covid_new.head()
Data on COVID-19 and financial assets
Data on COVID-19 and financial assets

If you want, you can add assets of your choice to the asset list. You can find the respective symbols on finance.yahoo.com.

Step #2 Plotting the Data

Next, we will visualize the historical data using line charts.

# Plot line charts
df_plot = df_covid_new.copy()

list_length = df_plot.shape[1]
ncols = 4
nrows = int(round(list_length / ncols, 0))

fig, ax = plt.subplots(nrows=nrows, ncols=ncols, sharex=True, figsize=(14, 18))
fig.subplots_adjust(hspace=0.5, wspace=0.5)
for i in range(0, list_length):
        ax = plt.subplot(nrows,ncols,i+1)
        sns.lineplot(data = df_plot.iloc[:, i], ax=ax)
        ax.set_title(df_plot.columns[i])
        ax.tick_params(axis="x", rotation=30, labelsize=10, length=0)
        ax.xaxis.set_major_locator(mdates.AutoDateLocator())
fig.tight_layout()
plt.show()
line plots on the price of different financial assets

We can easily spot pairs that seem to have experienced similar price developments. This does not mean, however, that these pairs are correlated.

Step #3 Correlation Matrix

Next, we will calculate the correlation matrix. Various Python libraries make this an easy task that only requires a few lines of code. We will use the standard math package for this purpose.

# Plotting a diagonal correlation matrix
sns.set(style="white")

# Compute the correlation matrix
df = pd.DataFrame(df_covid_new, columns=col_list)
corr = df_covid_new.corr()
corr
correlation matrix
Correlation Matrix

The matrix shows the Pearson correlation coefficients of all the pairs (X, Y) in our dataset.

Step #4 Visualizing the Correlation Matrix in a Heatmap

Heatmaps are an excellent choice for visualizing a correlation matrix. The heatmap applies a color palette to represent numeric values on a scale in different colors. This makes it easier to capture differences and similarities among the correlation coefficients. In Python, we can create heatmaps using the Seaborn package.

# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=np.bool))

# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))

# Generate a custom diverging colormap
cmap = "RdBu"

# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(
    corr,
    mask=mask,
    cmap=cmap,
    center=0,
    square=True,
    linewidths=0.5,
    cbar_kws={"shrink": 0.5},
)
Visualization of the Correlation Matrix in Form of a Heatmap
Visualization of the Correlation Matrix in the form of a Heatmap

The correlation matrix is symmetric. This is because the correlation between a pair of variables X and Y is the same as between Y and X.

Step #5 Interpretation

The heatmap uses a color palette that ranges from blue (positive correlation) over white (no correlation) to red (negative correlation). The different shades of the three colors visualize the extent of the correlation. In this way, we can distinguish between correlated pairs, not correlated pairs, and negatively correlated pairs. In the following, we will compare the different asset classes step by step.

5.1 Stock Market Indices / COVID-19

Let us start with the pairs of Stock market indices and COVID-19 data. As expected, the heatmap signals a negative correlation between the indices (DAX, S&P500, NIKI) and COVID-19. In other words, when the number of cases rises, stock market indices tend to fall in value. If we look precisely, the total number of new cases seems to be more correlated than the number of total cases (cases_cum) or deaths (deaths_cum). In addition, one can observe that the stock market indices are correlated with each other.

5.2 Stock Market Indices / Online Service Provider Stocks

When we compare the stock markets with the shares of online service providers, the situation is heterogeneous. The shares of Microsoft and Google are positively correlated with the overall development of the markets. On the other hand, the shares of Netflix, Amazon, and Apple are hardly correlated with the market development.

5.3 Stock Market Indices / Airline Stocks

Airlines are heavily affected by the pandemic. Thus it is plausible that we observe a strong positive correlation between airline stocks and the general stock market indices.

5.4 Stock Market Indices / Crypto-Currencies

Next, we compare Cryptocurrencies with the stock market indices. The results are surprising. BTC-USD correlates surprisingly strong positive with the general development of the stock markets. For ETH-USD and the markets, however, the correlation is only slightly positive.

5.5 COVID-19 / Currency Exchange Rates

The correlation between exchange rates and COVID-19 cases is rather weak. Only GBP/EUR, EUR/USD, and GBP/USD show a slightly negative correlation. An exception is CHF/EUR, which is positively correlated to the number of COVID-19 cases.

5.6 Treasury Bonds / Resources

When we look at the coefficients of resources and US Treasury Bonds, we can observe a strong negative correlation of COVID-19 cases with the oil price and a strong positive correlation with the gold price.

5.7 Crypto-Currencies / Resources

Finally, let us consider the coefficients of resources and cryptocurrencies. It is noticeable that BTCUSD correlates with the oil price. Based on the absence of a correlation with gold, one might conclude that BTC-USD is not a comparable crisis currency. However, the correlation between market indices and cryptocurrencies such as ETH-USD is relatively low. Thus, they were less affected by the recent market slump.

Summary

Congratulation, you have reached the end of this tutorial! In this article, we have load data on COVID-19 and financial assets via an API. We have created a correlation matrix in Python that shows the linear correlation between financial assets and COVID-19 cases. However, we need to keep in mind that we may still be unaware of potential non-linear correlations. Finally, we have visualized the matrix in a heatmap and concluded the correlation of different asset pairs.

If you found this article helpful, please show your appreciation by leaving a like or comment.

Sources and further Information on Correlation

YouTube tutorial that explains the math behind the correlation

Author

  • Hi, I am Florian, a Zurich-based consultant for AI and Data. Since the completion of my Ph.D. in 2017, I have been working on the design and implementation of ML use cases in the Swiss financial sector. I started this blog in 2020 with the goal in mind to share my experiences and create a place where you can find key concepts of machine learning and materials that will allow you to kick-start your own Python projects.

Leave a Reply