Calculate rolling correlation with pandas

前端 未结 3 1988
死守一世寂寞
死守一世寂寞 2021-01-03 16:46

I have a list of 10 stocks differentiated by PERMNO. I would like to group those stocks by PERMNO and calculate the rolling correlation between the stock return (RET) for ea

3条回答
  •  旧时难觅i
    2021-01-03 17:09

    I found an efficient solution. Fairly simple.

    def roll_corr_groupby(x,i):
        x['Z'] = rolling_corr(x['col 1'], x['col 2'],i) 
        return x
    
    x.groupby(['key']).apply(roll_corr_groupby)
    x.head()
    

提交回复
热议问题