I'm trying to write code for a geometric brownian motion using data instead of just randomly generated numbers based on 0. The data comes in as either a pandas series, or numpy array. No matter what data set I feed the code I keep getting back npplex128(0j) as the last couple thousand variables in the array. It's supposed to be randomized, so it shouldn't keep returning to 0. Where is the mistake in my code, or math?
def geometric_brownian_motion(data):
mu = data.mean()
sigma = data.std()
S0 = data[len(data) - 1]
T = 1
dt = T / len(data)
n = len(data)
t = np.linspace(0, T, n)
W = np.random.standard_normal(size=n)
S = []
S.append(S0)
for i in range(1, n):
S_temp = S[0] * np.exp((mu - 0.5 * sigma ** 2) * dt + sigma * np.sqrt(dt) * W[i - 1])
S.append(S_temp)
S = np.array(S)
return S