Python 随机行走熊猫
我正试图在熊猫身上快速创建一个模拟的随机行走系列Python 随机行走熊猫,python,pandas,random-walk,Python,Pandas,Random Walk,我正试图在熊猫身上快速创建一个模拟的随机行走系列 import pandas as pd import numpy as np dates = pd.date_range('2012-01-01', '2013-02-22') y2 = np.random.randn(len(dates))/365 Y2 = pd.Series(y2, index=dates) start_price = 100 希望构建另一个日期序列,从开始日期的起始价格开始,并以随机增长率增长。 伪代码: P0 = 10
import pandas as pd
import numpy as np
dates = pd.date_range('2012-01-01', '2013-02-22')
y2 = np.random.randn(len(dates))/365
Y2 = pd.Series(y2, index=dates)
start_price = 100
希望构建另一个日期序列,从开始日期的起始价格开始,并以随机增长率增长。
伪代码:
P0 = 100
P1 = 100 * exp(Y2)
P2 = P1 * exp(Y2)
在excel中很容易做到,但我想不出不使用pandas迭代数据帧/系列的方法,我也会用头撞一下
已尝试:
p = Y2.apply(np.exp)-1
y = p.cumsum(p)
y.plot()
这应该给出自启动以来的累积复合回报
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
def geometric_brownian_motion(T = 1, N = 100, mu = 0.1, sigma = 0.01, S0 = 20):
dt = float(T)/N
t = np.linspace(0, T, N)
W = np.random.standard_normal(size = N)
W = np.cumsum(W)*np.sqrt(dt) ### standard brownian motion ###
X = (mu-0.5*sigma**2)*t + sigma*W
S = S0*np.exp(X) ### geometric brownian motion ###
return S
dates = pd.date_range('2012-01-01', '2013-02-22')
T = (dates.max()-dates.min()).days / 365
N = dates.size
start_price = 100
y = pd.Series(
geometric_brownian_motion(T, N, sigma=0.1, S0=start_price), index=dates)
y.plot()
plt.show()