最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Brownian motion keeps returning np.complex128(0j) - Stack Overflow

programmeradmin5浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论