简体   繁体   English

Python数据帧中的滚动回归估计

[英]Rolling Regression Estimation in Python dataframe

I have a dataframe like this:我有一个这样的数据框:

           Date         Y         X1         X2          X3
22   2004-05-12  9.348158e-09  0.000081  0.000028     0.000036   
23   2004-05-13  9.285989e-09  0.000073  0.000081     0.000097   
24   2004-05-14  9.732308e-09  0.000085  0.000073     0.000096   
25   2004-05-17  2.235977e-08  0.000089  0.000085     0.000099   
26   2004-05-18  2.792661e-09  0.000034  0.000089     0.000150   
27   2004-05-19  9.745323e-09  0.000048  0.000034     0.000053 

......

1000   2004-05-20  1.835462e-09  0.000034  0.000048     0.000099   
1001   2004-05-21  3.529089e-09  0.000037  0.000034     0.000043   
1002   2004-05-24  3.453047e-09  0.000043  0.000037     0.000059   
1003   2004-05-25  2.963131e-09  0.000038  0.000043     0.000059   
1004   2004-05-26  1.390032e-09  0.000029  0.000038     0.000054   

I want to run a rolling 100-day window OLS regression estimation, which is:我想运行一个滚动的 100 天窗口 OLS 回归估计,即:

First for the 101st row, I run a regression of Y-X1,X2,X3 using the 1st to 100th rows, and estimate Y for the 101st row;首先对于第 101 行,我使用第 1 到第 100 行运行 Y-X1,X2,X3 的回归,并估计第 101 行的 Y;

Then for the 102nd row, I run a regression of Y-X1,X2,X3 using the 2nd to 101st rows, and estimate Y for the 102nd row;然后对于第 102 行,我使用第 2 行到第 101 行运行 Y-X1,X2,X3 的回归,并估计第 102 行的 Y;

Then for the 103rd row, I run a regression of Y-X1,X2,X3 using the 2nd to 101st rows, and estimate Y for the 103rd row;然后对于第 103 行,我使用第 2 行到第 101 行运行 Y-X1,X2,X3 的回归,并估计第 103 行的 Y;

...... ......

Until the last row.直到最后一行。

How to do this?如何做到这一点?

model = pd.stats.ols.MovingOLS(y=df.Y, x=df[['X1', 'X2', 'X3']], 
                               window_type='rolling', window=100, intercept=True)
df['Y_hat'] = model.y_predict

I also needed to do some rolling regression, and encountered the issue of pandas depreciated function in the pandas.ols.我还需要做一些滚动回归,在pandas.ols中遇到了pandas depreciated function的问题。 Below, is my work-around下面是我的解决方法

Basically, I use create an empty numpy array first, then use numpy polyfit to generate the regression values in a for-loop.基本上,我首先使用创建一个空的 numpy 数组,然后使用 numpy polyfit 在 for 循环中生成回归值。 Then I add the numpy arrays into the panda dataframe.然后我将 numpy 数组添加到熊猫数据框中。 Hope that helps the community!希望对社会有所帮助!

data = pd.DataFrame(x_data, y_data)

regression = np.zeros((len(data.index),2)) #set the regression numpy array empty first
for row in range(0, len(data.index), 1):
    y = data.y_data[row: row + 300]
    x = data.x_data[row: row + 300]
    regression[row] = np.polyfit(x, y, 1)

data['beta'] = regression[:,0]
data['alpha'] = regression[:,1]

statsmodels 0.11.0 added RollingOLS (Jan2020) statsmodels 0.11.0 添加了 RollingOLS(2020 年 1 月)

from statsmodels.regression.rolling import RollingOLS

#add constant column to regress with intercept
df['const'] = 1

#fit
model = RollingOLS(endog =df['Y'].values , exog=df[['const','X1','X2','X3']],window=20)
rres = model.fit()
rres.params.tail() #look at last few intercept and coef

Or use R-style regression formula或者使用 R 风格的回归公式

model = RollingOLS.from_formula('Y ~ X1 + X2 + X3' , data = df, window=20)
rres = model.fit()
rres.params.tail()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM