I have a dataframe where one column contains dates, one column contains the price of a stock and one column contains the dividend. I want to add another column that calculates compounded return with this data. Here is the formula I want to follow.
下面是一个数据帧示例,以及我想对其执行的操作:
price dividend
2020-07-31 83.08 0.7125
2020-08-31 73.35 0.7225
2020-09-30 74.55 0.7325
2020-10-31 81.57 0.8400
2020-11-30 81.85 0.8500
2020-12-31 79.95 0.8600
假设n=2,那么我想使用当前行和前两行来计算每行的回报.例如,2020-12-31年世界其他地区的计算结果为:
复合旋转=(79.95/81.57)*((1+0.84/81.57)*(1+0.85/81.85)*(1+0.86/79.95))=1.0113
当n=2时,新列将如下所示:
price dividend return
2020-07-31 83.08 0.7125 NA
2020-08-31 73.35 0.7225 NA
2020-09-30 74.55 0.7325 0.9229
2020-10-31 81.57 0.8400 1.1457
2020-11-30 81.85 0.8500 1.1318
2020-12-31 79.95 0.8600 1.0113
有什么内置函数可以在python/numpy上帮助我做到这一点吗?