【问题标题】:How to change code from PandasRollingOLS to just RollingOLS?如何将代码从 PandasRollingOLS 更改为 RollingOLS?
【发布时间】:2021-05-15 18:51:09
【问题描述】:

我正在阅读这本书 - https://github.com/stefan-jansen/machine-learning-for-trading/blob/c7e508dbe98e064887faeca65a45966e85af1c96/04_alpha_factor_research/01_feature_engineering.ipynb

看起来它在这行代码中使用了已弃用的 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

【问题讨论】:

    标签: python pandas regression statsmodels


    【解决方案1】:

    不需要太多更改。您可以使用它代替原始笔记本中的相应单元格:

    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]))
    

    变化:

    1. 导入RollingOLSadd_constant
    2. 获取要保留的 beta 列表。我们不想要由add_constant 添加的const
    3. 仅使用RollingOLS 呼叫同一组。将y 重命名为endog,将x 重命名为exog
    4. 您需要在RollingOLS 上显式调用fit()
    5. 使用params访问系数,使用keep保留相关系数。

    【讨论】:

    猜你喜欢
    • 2020-09-13
    • 2019-09-04
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    相关资源
    最近更新 更多