How to calculate rolling cumulative product on Pandas DataFrame(如何在 Pandas DataFrame 上计算滚动累积乘积)
问题描述
我在 pandas DataFrame 中有一个时间序列的回报、滚动 beta 和滚动 alpha.如何计算 DataFrame 的 alpha 列的滚动年化 alpha?(我想做相当于=PRODUCT(1+[trailing 12 months])-1 in excel)
I have a time series of returns, rolling beta, and rolling alpha in a pandas DataFrame. How can I calculate a rolling annualized alpha for the alpha column of the DataFrame? (I want to do the equivalent to =PRODUCT(1+[trailing 12 months])-1 in excel)
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'] 列.
I was surprised to see that there was no "rolling" function built into pandas for this, but I was hoping somebody could help with a function that I can then apply to the df['Alpha'] column using pd.rolling_apply.
提前感谢您提供的任何帮助.
Thanks in advance for any help you have to offer.
推荐答案
这样可以吗?
import pandas as pd
import numpy as np
# your DataFrame; df = ...
pd.rolling_apply(df, 12, lambda x: np.prod(1 + x) - 1)
这篇关于如何在 Pandas DataFrame 上计算滚动累积乘积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Pandas DataFrame 上计算滚动累积乘积


- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 沿轴计算直方图 2022-01-01