python - 投资组合中的自协方差股票收益(Python)(流动性度量)
问题描述
我正在尝试计算每家公司股票收益的每月自协方差。(从 2020 年 1 月 1 日到 2020 年 12 月 31 日的 2549 件仪器)。DataFrame
如果它太大,我可以拆分它。
日期 | 乐器 | rets |
---|---|---|
2020-01-01 | 1 | 0.0285516 |
2020-01-02 | 1 | 0.0285516 |
... | ... | ... |
2020-12-30 | 2549 | 0.0024105 |
2020-12-31 | 2549 | -0.0762408 |
我按照 Bao、Pan 和 Wang (2011) 构建了股票层面的非流动性指标 ILLIQ。
具体来说,让
是股票 i 在 t 月 d 日的对数价格变化。然后,ILLIQ 定义为
我想用 Python 实现。到目前为止,我已经找到了这个statsmodels.tsa.stattools acovf
功能。
df['ILLIQ_1']= acovf(df.rets, adjusted=False, demean=True, fft=False, missing='drop',nlag=30)
但是这段代码没有考虑到我的df(股票i,时间t)的面板结构。
理想情况下,我想这样做。
df['ILLIQ_2']=df.sort_values('date').groupby('instrument')['rets'].acovf(df.rets, adjusted=False, demean=True, fft=None, missing='drop',nlag=30)
但我得到这个错误:
AttributeError: 'SeriesGroupBy' object has no attribute 'acovf'.
有什么建议吗?我试图在没有运气的情况下创建一个 for 循环..
参考:【http://www.mit.edu/~junpan/bond_liquidity.pdf】【包潘文(2021)】
解决方案
Craft a first-arg for
acovf
that takes into account the panel structure of your data in a way thatdf.rets
currently doesn't.Feed that into
acovf
, e.g.,df['ILLIQ'] = acovf(my_rets, ...)
Without seeing the exact structure of the dataframe, it's hard to give a more detailed answer
推荐阅读
- google-chrome - http://reload.extensions 未找到 IP 地址
- assembly - 将汇编转换为十六进制。如何一步一步做到这一点?
- bash - Bash IF 检查确切的数字是否在列表中
- python - 2D 随机游走,python
- python - 文件流 - ValueError:嵌入的空字节
- javascript - 为什么上传的文件不会通过 post 请求发送到数据库?
- linux - 如何列出目录中以字符开头但文件名后跟 8 个小写字符的文件?
- php - 如何解决 SQLSTATE[42S22]:Laravel 5.8 中找不到列
- c# - 代码 GetAsyncKeyState(VK_SHIFT) & 0x8000 中的这些数字是什么?它们是必不可少的吗?
- python - 忽略语句体?