首页 > 解决方案 > 投资组合中的自协方差股票收益(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)】

标签: pythoncovarianceportfolio

解决方案


  1. Craft a first-arg for acovf that takes into account the panel structure of your data in a way that df.rets currently doesn't.

  2. 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


推荐阅读