Fixed a mistake in the pairs trading lecture
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:

That fix is reflected in the code change.
@richafrank Jamie asked me to test the change for truthiness. Will re-run to change the cell output and test for truthiness.
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?
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.
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.
- Does
std_30 = spread.rolling(window=30).std()needcenter=False, like the others? - Since this will be copied by lots of folks, could you add a space before the
center=Falseargs that you added, so we conform to PEP8?