Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable size rolling window regression

Tags:

pandas

In Pandas OLS the window size is fix length. How can I achieve set the window size based on index instead of number of rows?

I have a series where it has variable number of observations per day and I have 10 years history of data, so I want to run rolling OLS on 1 year rolling window. loop through each date is a bit too slow, anyway to make it faster? Here is the example of the data.

  Date     x      y
2008-1-2  10.0    2
2008-1-2  5.0     1
2008-1-3  7.0   1.5
2008-1-5  9.0   3.0
...
2013-5-30 11.0  2.5

I would like something simple like pandas.ols(df.y, df.x, window='1y'), rather than looping each row since it will be slow to do the loop.

like image 334
user2426361 Avatar asked Feb 05 '26 09:02

user2426361


1 Answers

There is method for doing this in pandas see documentation http://pandas.pydata.org/pandas-docs/dev/computation.html#computing-rolling-pairwise-correlations:

model = pandas.ols(y=df.y, x=df.x, window=250)

you will just have to provide your period is a number of intervals on frame instead of '1y'. There are also many additional options that you might find useful on your data.

all the rolling ols statistics are in model

model.beta.plot()

to show rolling beta

like image 138
Joop Avatar answered Feb 12 '26 17:02

Joop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!