我在 pandas DataFrame 中有一个时间序列的返回、滚动 beta 和滚动 alpha。如何计算 DataFrame 的 alpha 列的滚动年化 alpha? (我想在 excel 中做相当于 =PRODUCT(1+[trailing 12 months])-1 的操作)
SPX Index BBOEGEUS Index Beta Alpha
2006-07-31 0.005086 0.001910 1.177977 -0.004081
2006-08-31 0.021274 0.028854 1.167670 0.004012
2006-09-30 0.024566 0.009769 1.101618 -0.017293
2006-10-31 0.031508 0.030692 1.060355 -0.002717
2006-11-30 0.016467 0.031720 1.127585 0.013153
我很惊讶地看到 pandas 中没有为此内置“滚动”功能,但我希望有人可以帮助我提供一个功能,然后我可以使用 pd.rolling_apply 将其应用于 df['Alpha'] 列.
提前感谢您提供的任何帮助。
最佳答案
rolling_apply
已在 pandas 中被删除,取而代之的是更通用的
window methods (例如 rolling()
等)
# Both agg and apply will give you the same answer
(1+df).rolling(window=12).agg(np.prod) - 1
# BUT apply(raw=True) will be much FASTER!
(1+df).rolling(window=12).apply(np.prod, raw=True) - 1
关于python - 如何计算 Pandas DataFrame 上的滚动累积乘积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15295434/