user2426361
user2426361

Reputation: 387

variable size rolling window regression

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.

Upvotes: 3

Views: 3855

Answers (1)

Joop
Joop

Reputation: 8108

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

Upvotes: 3

Related Questions