In this post, we are going to learn quite a few things. First, we will see how to calculate stocks returns using Python. Then, we will calculate and plot the stock returns correlation matrix. This will let us see how stock returns are correlated to each other. Finally, we will use linear regression to test which stocks do have an statistically significant correlation.

Contents

## Stocks daily returns with Python

Let’s start with getting the daily returns for a list of stocks. We will use yfinance library to retrieve financial data. I will not go into details because I already have a post covering how to calculate stock returns using yfinance. The outcome will be a pandas DataFrame containing stock daily returns for the last year.

import pandas as pd

import matplotlib.pyplot as plt

import yfinance as yf

from statsmodels import api as sm

stocks =[‘AAPL’,’MSFT’,’FB’,’GME’,’TSLA’,’AMZN’]

stock_list = []

for stock in stocks:

returns = yf.Ticker(stock)

returns = returns.history(period=”1y”)

returns[‘returns’] = returns[‘Close’].pct_change()

returns.rename(columns={‘returns’: stock}, inplace=True)

returns = returns[stock]

stock_list.append(returns)

all_stock_returns =pd.DataFrame(stock_list).T

print(all_stock_returns)

stock daily returns for the last year

## Daily stock returns – descriptive statistics

We can use the pandas describe method to have a better understanding of our data:

#statistics

print(all_stock_returns.describe())

Daily Stock Returns Main Statistics

## Calculate and plot stock correlation matrix

Next, we can calculate and plot the stocks matrix correlation using Python. As we see below, it is super easy to do and the outcome matrix after running the code is beautiful.

#calculate correlation

correlation = all_stock_returns.corr()

#plot correlation

sm.graphics.plot_corr(correlation,xnames=list(correlation.columns))

plt.show()

Below we see the correlation matrix and the nice looking graph:

Stocks Correlation Matrix

## How to interpret the correlation between stock prices?

The results of the correlation between two stocks can range from -1 to 1. Below it is a short summary on how to interpret the results:

**A positive correlation** means that a pair of stock returns **move closely together and in the same direction**. Where +1 means that the stock returns are perfectly correlated.A **correlation of 0** between a pair of stocks means that there is** no relationship** between their returns. **A negative correlation indicates that the stock returns of the pair of stocks move in different directions.** The closer to -1 the stronger the inverse relationship.

For more details on what is correlation, I would advise you to have a look at the investopedia site.Looking to the correlation matrix for our stocks, we can extract two interesting findings:

Game Stop (GME) returns seem to **not have a strong relationship** with the other stocks. The correlation score ranges from -0.10 to 0.1.All technological stocks seem to be having a strong correlation. The correlation score is higher than 0.65 for all of them (AAPL, FB, MSFT and AMZN).

A positive thing about finding stocks having low correlation between them is that if we add any of them to a portfolio it will increase the benefit of diversification by reducing the total risk of the portfolio. Learn more about portfolio optimisation with Python in one of my previous post. Please keep in mind that all stocks keep some certain degree of correlation. Therefore, it may make sense to look into other asset classes to really benefit from diversificaiton.

In the next section, we will analyse if the two findings listed above are statistically significant.

## Are stock return correlations statistically significant?

We will use a linear regression to find out if below two statements can be rejected or not:

**Ho: GME does not have a strong correlation with other stocks in our sample (e.g. Apple)**

**Ho: Technological companies do not have a strong correlation between them (e.g. Apple and Microsoft)**

We will use the library statsmodel to test our hypothesis. If the *p-value* of the regression is lower than 0.05 we reject our H0 hypothesis, and therefore, we can conclude statistically speaking that the selected stock prices are moving together.

from scipy.stats import linregress

all_stock_returns = all_stock_returns.iloc[1:]

print(linregress(all_stock_returns[‘AAPL’],all_stock_returns[‘MSFT’]))

print(linregress(all_stock_returns[‘AAPL’],all_stock_returns[‘GME’]))

We can see below the outcome of the code:

LinregressResult(slope=0.5912264151809, intercept=8.07240085732013e-05, rvalue=0.7291931304903984, pvalue=6.556872029750241e-43, stderr=0.03516115854855423)

LinregressResult(slope=0.34037677473398503, intercept=0.027569291927221833, rvalue=0.045437241346593805, pvalue=0.4735973053513729, stderr=0.4742414567943584)

Based on our results, we see that for the correlation between AAPL and MSFT, the p-value is lower than 0.05. Therefore, we can reject our H0 and conclude that AAPL and MSFT stocks are positively correlated.

We see that the p-value of the correlation between GME and AAPLE is bigger than 0.05. Therefore, we cannot reject the HO. Therefore our statement on GME not having a strong correlation with other stock returns included in our list seem to be true from a statistical point of view.

## Wrapping Up

We have covered quite a bit on this post studying the correlation between different stocks using Python. Now, we are well equipped to perform further analysis on stock correlation. We have only covered a few stocks but it would be interested to perform a similar analysis grouping stocks by industry and looking into industry correlations.

Another good idea would be to look at the correlation between different asset classes. For instance, what is the correlation between fixed income and equity stocks? That I will leave it for you.

See the whole code below for your reference. Hope you find the content on Python for FInance useful!

Thanks for reading, check out Coding and Fun for more awesome stuff.

The post How to calculate stock returns and stock correlations using Python appeared first on Coding and Fun.