pythonMovingOLS的替换
(2018-09-21 16:30:16)
标签:
ols |
在python3.X中,pandas已经没有了MovingOLS。因此,若想求解滚动窗口的OLS,只能自己来写。
原来的方法如下:
原来的方法如下:
model = pd.stats.ols.MovingOLS(y=df.y, x=df.x, window_type='rolling', window=1000, intercept=True)
替换的代码如下:window = 1000
a = np.array([np.nan] * len(df))
b = [np.nan] * len(df) # If betas required.
y_ = df.y.values
x_ = df[['x']].assign(constant=1).values
for n in range(window, len(df)):
= y_[(n - window):n] y
= x_[(n - window):n] X
# betas = Inverse(X'.X).X'.y
= np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y) betas
= betas.dot(x_[n, :]) y_hat
[n] = y_hat a
[n] = betas.tolist() # If betas required. b
或者:
df=df.dropna() #uncomment this line to drop nans
window = 5
df['a']=None #constant
df['b1']=None #beta1
df['b2']=None #beta2
for i in range(window,len(df)):
=df.iloc[i-window:i,:] temp
=sm.OLS(temp.loc[:,'Y'],sm.add_constant(temp.loc[:,['time','X']])).fit() RollOLS .iloc[i,df.columns.get_loc('a')]=RollOLS.params[0]
df .iloc[i,df.columns.get_loc('b1')]=RollOLS.params[1]
df
.iloc[i,df.columns.get_loc('b2')]=RollOLS.params[2] df
当然,也有人自己写了一个模型解决这个问题,如下:
# Rolling regressions
https://www.e-learn.cn/content/wangluowenzhang/754972
from pyfinance.ols import OLS, RollingOLS, PandasRollingOLS
y = data.usd
x = data.drop('usd', axis=1)
window = 12 # months
model = PandasRollingOLS(y=y, x=x, window=window) print(model.beta.head())
参考:主要是stackoverflow里面的2个网址
/questions/44380068/pandas-rolling-regression-alternatives-to-looping
/questions/44707384/python-pandas-has-no-attribute-ols-error-rolling-ols
https://e-learn.cn/content/wangluowenzhang/192368