Search code examples
pythonpandasdataframecorrelationelementwise-operations

Calculating the differences between series of correlation matrixes (as DataFrames, pandas) in python


From a series of DataFrames that contain daily log returns of a large number of stocks, eg:

data_list = [data_2015, data_2016, data_2017, data_2018, data_2019, data_2020]

The task at hand is to compute the change in the correlations between each successive year, eg:

data_2015.corr() - data_2016.corr()

It is the element-wise differences / changes that is needed. A simple for loop gives really bad answers and I am stuck

for i in data_list:
    j = i +1
        
    a = (i).corr()
    b = (j).corr()
    
    print(a-b)

An stylized example would work something as follows:

#import pandas and numpy
import numpy as np
import pandas as pd


#create four symmetric matrices with 1 on the diagonal as correlation matrix
np.random.seed(39)

b = np.random.randint(-100,100,size=(4,4))/100
b_symm = (b + b.T)/2
b = np.fill_diagonal(b_symm, 1)

c = np.random.randint(-100,100,size=(4,4))/100
c_symm = (c + c.T)/2
c = np.fill_diagonal(c_symm, 1)

d = np.random.randint(-100,100,size=(4,4))/100
d_symm = (d + d.T)/2
d = np.fill_diagonal(d_symm, 1)

e = np.random.randint(-100,100,size=(4,4))/100
e_symm = (e + e.T)/2
e = np.fill_diagonal(e_symm, 1)

#convert to DataFrame
data_2015 = pd.DataFrame(b_symm)
data_2016 = pd.DataFrame(c_symm)
data_2017 = pd.DataFrame(d_symm)
data_2018 = pd.DataFrame(e_symm)

#print DataFrames
print(data_2015)
print(data_2016)
print(data_2017)
print(data_2018)

#print intended result(s)
print("Change in correlations 2015-16",'\n',data_2015-data_2016,'\n')
print("Change in correlations 2016-17",'\n',data_2016-data_2017,'\n')
print("Change in correlations 2017-18",'\n',data_2017-data_2018,'\n')

Solution

  • If I understand correctly you want to access consecutive pairs of elements of data_list so you can calculate the differences in their correlations? There are many ways to do it, a somewhat ugly but at least transparent way is as follows

    for i in range(len(data_list)-1):
            
        a = data_list[i+1].corr()
        b = data_list[i].corr()
        
        print(a-b)
    

    a more Pythonian way to write this would be

    for a,b in zip(data_list[1:],data_list[:-1]):
        print(a.corr()-b.corr())
    

    or even simply

    [print(a.corr() - b.corr()) for a,b in zip(data_list[1:],data_list[:-1])]