Back to Community
Pandas Rolling Linear Regression

Question to those that are proficient with Pandas data frames:

The attached notebook shows my atrocious way of creating a rolling linear regression of SPY. I know there has to be a better and more efficient way as looping through rows is rarely the best solution. I would really appreciate if anyone could map a function to data['lr'] that would create the same data frame (or another method). I'll continue to try and answer my own question and will post if I find a way.

Thank you!

Loading notebook preview...
Notebook previews are currently unavailable.
2 responses

I don't know if you ever found an answer. I couldn't really find a great solution either. I basically manually calculated the regression coefficients. It is not any prettier, but it is quite a bit faster.

(Note: I also changed the line where you assigned data['lr'] = '' to data['lr']=np.nan)

Loading notebook preview...
Notebook previews are currently unavailable.

Unless you need to use minutely data, Pipeline is meant to do exactly those kind of rolling computation. Once you get familiar with that you will enjoy the simplicity of Pipeline. Have a look here and here for rolling linear regression with pipeline