Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Rolling OLS Model interface in Pandas?

Tags:

python

pandas

I want to calibrate a MovingOLS, but keep receiving error message

IndexError: index -1 is out of bounds for axis 0 with size 0

The data frame I used to train the MovingOLS is as below:

   x1  y  x2
0  1   1   1
1  2   2   2
2  3   3   3

with code

model = pandas.stats.ols.MovingOLS(y = df['y'], x=df[['x1', 'x2']], window_type='rolling', window=2, min_periods=2)

The model is very simple, since I just want to familiarize myself the MovingOLS API, and I expect to get 2 OLS models if I understand the Moving and rolling parts correctly. I wonder whether any good materials on it, because I cannot find documents/tutorials online giving enough details about the Rolling OLS/MovingOLS API.

The library version is 0.11.0, by the way.

Thank you!

like image 745
Summer_More_More_Tea Avatar asked Feb 03 '26 02:02

Summer_More_More_Tea


1 Answers

I would think it is caused by the fact that K (parameters) > N (observations). movingOLS in pandas adds an intercept. You are trying to estimate 3 parameters with a rolling window of 2 observations.

It should work if you add some rows and make your rolling window bigger. For example, window = 4:

df=pd.DataFrame({'y': [1,2,3,4,5,6],
                 'x1' : [3,4,6,7,10,12],
                 'x2' : [2,7,4,9,3,15]})
print df

   x1  x2  y
0   3   2  1
1   4   7  2
2   6   4  3
3   7   9  4
4  10   3  5
5  12  15  6

print pd.stats.ols.MovingOLS(y = df['y'], x=df[['x1', 'x2']], 
                             window_type='rolling', 
                             window=4, min_periods=4)

-------------------------Summary of Regression Analysis-------------------------

Formula: Y ~ <x1> + <x2> + <intercept>

Number of Observations:         4
Number of Degrees of Freedom:   3

R-squared:         0.9777
Adj R-squared:     0.9331

Rmse:              0.3339

F-stat (2, 1):    21.9240, p-value:     0.1493

Degrees of Freedom: model 2, resid 1

-----------------------Summary of Estimated Coefficients------------------------
      Variable       Coef    Std Err     t-stat    p-value    CI 2.5%   CI 97.5%
--------------------------------------------------------------------------------
            x1     0.4319     0.0850       5.08     0.1237     0.2653     0.5984
            x2     0.0262     0.0425       0.62     0.6483    -0.0572     0.1096
     intercept     0.5180     0.6415       0.81     0.5675    -0.7392     1.7753
---------------------------------End of Summary---------------------------------

When I set the window = 2, I also get an index out of bounds error.

like image 84
Karl D. Avatar answered Feb 05 '26 16:02

Karl D.