问题描述
看起来它在这行代码中使用了弃用的 PandasRollingOLS 版本 -
from statsmodels.regression.rolling import PandasRollingOLS
后来在这里引用-
T = 24 betas = (factor_data .groupby(level='ticker',group_keys=False) .apply(lambda x: RollingOLS(window=min(T,x.shape[0]-1),y=x.return_1m,x=x.drop('return_1m',axis=1)).beta))
我希望有人能告诉我如何将这行代码转换为使用。 ——
statsmodels.regression.rolling.RollingOLS
解决方法
不需要太多更改。您可以使用它代替原始笔记本中的相应单元格:
from statsmodels.api import add_constant
from statsmodels.regression.rolling import RollingOLS
T = 24
# Variables excluding "const"
keep=["Mkt-RF","SMB","HML","RMW","CMA"]
betas = (add_constant(factor_data) # Add the constant
.groupby(level='ticker',group_keys=False)
.apply(lambda x: RollingOLS(window=min(T,x.shape[0]-1),endog=x.return_1m,exog=x.drop('return_1m',axis=1)).fit().params[keep]))
变化:
- 导入
RollingOLS
和add_constant
- 获取要保留的测试版列表。我们不想要由
const
添加的 - 仅使用
RollingOLS
调用同一组。将y
重命名为endog
,将x
重命名为exog
。 - 您需要在
fit()
上显式调用RollingOLS
。 - 使用
params
访问系数,并使用keep
保留相关系数。
add_constant