Search code examples
pythonpandascovariance

How to reduce dimensionality of matrix when calculating rolling covariance?


I have 2 columns with returns dataframe (one is Bitcoin returns over time and one is one crypto asset's returns). I want to calculate rolling cov between them, then variance and then calculate rolling beta coefficient. By the end beta should look like beta and I want to make chart like this rolling beta

cov = df[['Return Market','Return Asset']].rolling(window=3).cov()
cov

var = pd.rolling_var(df['Return Market'], window=3)
var

df['Beta'] = cov / var

When I run cov = df[['Return Market','Return Asset']].rolling(window=3).cov()

I am getting this output and I it prevents me from finishing the rest of the code. I do not need full covariance matrix, I only need cov between 'Return Market' and 'Return Asset'. I want to drop the rest. The problem is that every index has 2 rows. How to fix it?

This error: TypeError: incompatible index of inserted column with frame index must be caused by incorrect cov output

Please help me to figure it out. I use Python 3.7 and pandas version is 0.22.0


Solution

  • First of all, you still (similar like your previous question) use pandas functions from version 0.17 and you have 0.22 installed. All rolling_* functions were removed by version 0.18. Please make sure you read the documentation for your pandas version. The version is displayed at the top left of the documentation "pandas 0.23.3 documentation » API Reference »". Here is the function you need to use with pandas version 0.22: https://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.core.window.Rolling.var.html#pandas.core.window.Rolling.var

    In order to get the covariance values (and not the complete covariance matrix) you need to pass in the second column as a rolling parameter:

    cov = df[['Return Market']].rolling(window=3).cov(other=df['Return Asset'].rolling(window=3))
    var = df['Return Market'].rolling(window=3).var()
    beta = (cov['Return Market'] / var).dropna()