我想通过使用模拟收益模拟一系列股票价格。例如,我将初始股价设置为70美元,并创建了一个具有10个回报期的熊猫系列:
returns = pd.Series([1.01,1.02,1.03,1.01,0.99,0.98,1.07,0.99,1.03,1.03])
如何获得10个周期的一系列股票价格?
(第一个价格数据应为初始股价(70) * 1.01
;第二个数据应为70 * 1.01 * 1.02
;第三个数据应为...直到20。)
initial stock price = 70
returns = pd.Series([1.01,1.02,1.03,1.01,0.99,0.98,1.07,0.99,1.03,1.03])
答案 0 :(得分:5)
IIUC,使用cumprod
:
initial_stock_price = 70
returns = pd.Series([1.01,1.02,1.03,1.01,0.99,0.98,1.07,0.99,1.03,1.03])
returns.cumprod() * initial_stock_price
输出:
0 70.700000
1 72.114000
2 74.277420
3 75.020194
4 74.269992
5 72.784592
6 77.879514
7 77.100719
8 79.413740
9 81.796153
dtype: float64
答案 1 :(得分:4)
您可以使用reduce将列表中的所有值相乘,然后乘以价格:
from functools import reduce
import pandas as pd
inital_price = 70
returns = pd.Series([1.01,1.02,1.03,1.01,0.99,0.98,1.07,0.99,1.03,1.03])
[inital_price * reduce(lambda x, y: x*y, returns[0:i+1]) for i in range(len(returns))]
输出:
[70.7,
72.114,
74.27742,
75.0201942,
74.269992258,
72.78459241284,
77.8795138817388,
77.10071874292142,
79.41374030520907,
81.79615251436535]