I am having trouble trying to figure out how to do this in Python. I have created it in Excel, but I would like to automate this for any sector or grouping of sub-industries.
I first start with creating a correlation matrix of the individual securities by sub-industry.
I then get the mean of the correlations from Building Products vs Casinos & Gaming, and then Building Products vs Construction Materials, and so on. The end result looks like this:
In this matrix, I set the diagonal as 1 since I am only worried about the average correlations between sub-industry.
I have done this for a few quarters but I am interested in automating this through Python. Data is from my Bloomberg Terminal.