[Python] Brownian motion keeps returning np.complex128(0j)

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    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 np.complex128(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

    Continue reading...

Compartilhe esta Página