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

This article shows how you can create a correlation matrix in Python to vizualize the correlation between various asset classes and COVID-19. Correlation is a stastical measure that describes the linear relationship between two independent variables. In financial market analysis, correlation plays an important role, as it measures how assets develop in relation to each other. We can use it to analzye how individual assets move in dependence of the overall market, or how they develop in relation to other assets.

The remainder of this tutorial proceeds as follows: We begin by introducing Pearson Correlation Coefficient and you will learn how to interpret it. Then, we will 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 various other financial assets.

## 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.

## 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 the relationship is, and the better the points (Xi / Yi) fit on the regression line.
• The closer r is to 0, the weaker the correlation is, 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, defined by a linear equation of y = a + b*x can be described exactly. In this case, all points (xi / Yi) is located on the regression line.

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 independent. 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 of visualizing COVID statistics is highly relevant.

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 2020. However, there were also sectors and asset classes whose development was precisely the opposite. We can use correlation to vizualize the relation between COVID-19 cases and the performance of financial assets.

The code is available on the GitHub repository.

### Prerequisites

Before starting 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.

```# A tutorial for this file is available at www.relataly.com

# Imports
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.dates as mdates
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 = 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()

#### 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
• 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",
"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:
key, start=start_date, end=today_date, data_source="yahoo")
except ValueError:
# 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)

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

### Step #2 Exploring the Data

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

```# Create lineplots
list_length = df_covid_new.shape
ncols = 6
nrows = int(round(list_length / ncols, 0))
height = list_length/3 if list_length > 30 else 16

fig, axs = plt.subplots(nrows=nrows, ncols=ncols, sharex=True, figsize=(20, height))

for i, ax in enumerate(fig.axes):
if i < list_length:
sns.lineplot(data=df_covid_new, x=df_covid_new.index, y=df_covid_new.iloc[:, i], ax=ax)
ax.set_title(df_covid_new.columns[i])
ax.tick_params(labelrotation=45)

plt.show()```

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```

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,
cmap=cmap,
center=0,
square=True,
linewidths=0.5,
cbar_kws={"shrink": 0.5},
)``` 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. We can distinguish between correlated pairs, not correlated pairs, and negatively correlated pairs. We will compare the different asset classes step by step in the following.

#### 5.1 Stock Market Indices / COVID-19

Let us start with the pairs of Stock market indices and COVID-19 data. 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.

#### 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. There is a positive correlation between the shares of Microsoft and Google and 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. However, the correlation is only slightly positive for ETH-USD and the markets.

#### 5.5 COVID-19 / Currency Exchange Rates

The correlation between exchange rates and COVID-19 cases is relatively weak. Only GBP/EUR, EUR/USD, and GBP/USD show a slightly negative correlation. An exception is CHF/EUR, which positively correlates 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. Finally, we have visualized the matrix in a heatmap and concluded the correlation of different asset pairs. However, we need to keep in mind that we may still be unaware of potential non-linear correlations.

## 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.

### 2 thoughts on “Correlation Matrix in Python: How Correlated are COVID-19 Cases and Different Financial Assets?”

1. amazing blog thank you this elaborating 🥰

2. 