I return daily from three markets (GLD, SPY and USO). My goal is to calculate the average pairwise correlation from the correlation matrix based on the rental of 130 days.
My starting point:
import numpy as np
import pandas as pd
import os as os
import pandas.io.data as web
import datetime as datetime
from pandas.io.data import DataReader
stocks = ['spy', 'gld', 'uso']
start = datetime.datetime(2010,1,1)
end = datetime.datetime(2016,1,1)
df = web.DataReader(stocks, 'yahoo', start, end)
adj_close_df = df['Adj Close']
returns = adj_close_df.pct_change(1).dropna()
returns = returns.dropna()
rollingcor = returns.rolling(130).corr()
This creates a panel of correlation matrices. However, extracting the lower (or upper) triangles, removing the diagonals, and then calculating the average for each observation is where I drew the space. Ideally, I would like the result for each date to be in a series, where I can then index it by date.
Maybe I started from the wrong place, but any help would be appreciated.
source
share