python - 如何将代码从 PandasRollingOLS 更改为 RollingOLS?
问题描述
看起来它在这行代码中使用了已弃用的 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
- 获取要保留的 beta 列表。我们不想要
const
哪个被添加add_constant
- 仅使用 调用同一组
RollingOLS
。重命名y
为endog
和x
。exog
- 您需要显式调用
fit()
onRollingOLS
。 - 使用 访问系数
params
,并使用keep
保留相关系数。
推荐阅读
- python - 运行pytest时如何让pycharm输出颜色?
- php - 我如何知道并检查{用户是否从真正的 android 手机打开我的应用程序而不是浏览器检查(手机心情)}?
- swift - 是否有可以检测当前前台应用程序何时更改的 Swift macOS/Cocoa API?
- mysql - MySQL 逆序排列前 6 个月
- swift - 从 UIKit 到 SwitUI 的 SwiftUI 绑定/订阅,但在 SwiftUI 视图中不改变观察者
- asp.net-core - 是否可以在 cPanel 中托管 ASP.Net Core MVC 网站?
- javascript - 您能以多快的速度自动增加 Firebase 实时数据库上的值?
- javascript - 变量等于元素内的文本
- reactjs - Jest 模拟从实用程序文件导入
- linux - 未找到 Linux NSwag NPM CLI 框架 Microsoft.NETCore.App 版本 2.1.0