1
votes

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

1
Welcome to Stackoverflow, I understand this is your first question, but it doesn't meet the standard quality expected, please read stackoverflow.com/help/how-to-ask to have some guidance. For instance you don't provide the error you actually get, and the language you use, so edit the question (don't add comments) and fix that.Baptiste Mille-Mathias
@baptistemm thank you for letting me know, I fixed itkirill zakharov
I have formatted the question a bit, but I have the impression it misses some relevant information, you forgot the language and the libraries version, and perhaps a bit of explanation (I don't understand "columns with returns" for instance); thanks for editing. I can't help on the technical part but trying to help you to get people help you.Baptiste Mille-Mathias
@baptistemm I added more details. Should be understandable nowkirill zakharov

1 Answers

0
votes

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()