research_public icon indicating copy to clipboard operation
research_public copied to clipboard

Fixed a mistake in the pairs trading lecture

Open quantopiancal opened this issue 6 years ago • 4 comments

An error was pointed out by community member Emilio via helpscout:

Hi, I found a mistake in this lecture. In the moving average section at the end, there is this piece of code:

# Calculate rolling beta coefficient
rolling_beta = pd.ols(y=S1, x=S2, window_type='rolling', window=30)
spread = S2 - rolling_beta.beta['x'] * S1

I noticed that in the rolling beta calculation y=S1 and x=S2. However, when calculating the spread S2 now becomes the dependent variable Y and S1 is the independent variable X Thanks, Emilio

Rene confirmed it was a mistake and offered a fix via slack: image

That fix is reflected in the code change.

quantopiancal avatar Mar 11 '19 17:03 quantopiancal

@richafrank Jamie asked me to test the change for truthiness. Will re-run to change the cell output and test for truthiness.

quantopiancal avatar Mar 11 '19 18:03 quantopiancal

Nice. Ok, new request for you: Would it be possible to run this in an environment where we aren't generating all the FutureWarnings or saving them in the output for everyone?

richafrank avatar Mar 12 '19 20:03 richafrank

Hey Rich, I did my best to cut down on those error messages - there is one error left.

I did my best to alter the code while keeping the results exactly the same, but don't have the know how to convert pd.ols to whatever the statsmodels equivalent is.

I would have just run the code in a notebook with the original pandas version (0.17.1), but the notebook requires using get_pricing(), which is unavailable locally.

quantopiancal avatar Apr 29 '19 18:04 quantopiancal

Ah, I didn't realize the warnings were from 0.18 - I had assumed you were running this locally with a newer version! I wouldn't worry about the remaining warnings then. Definitely stick with what's on the platform.

Thanks for updating to Series.rolling.

  1. Does std_30 = spread.rolling(window=30).std() need center=False, like the others?
  2. Since this will be copied by lots of folks, could you add a space before the center=False args that you added, so we conform to PEP8?

richafrank avatar Apr 29 '19 21:04 richafrank