【问题标题】:Simulating for loop模拟 for 循环
【发布时间】:2021-03-04 09:03:36
【问题描述】:

我正在尝试模拟以下随机游走方程:

但是,当我运行我的代码时,我无法附加结果。我收到以下错误:

first_term = results[i] TypeError: 'NoneType' 对象不是 可下标

有没有其他方法可以解决这个问题?

import numpy as np
import pandas as pd

def simulation(x0, T, sigma, p):
    results = [x0]
    prob_e = [p, (1-p)]
    values_e = [1, -1]
    for i in range(T):
        first_term = results[i]
        error = np.random.choice(values_e, 1, prob_e)
        second_term = sigma * error
        result = first_term + second_term
        results = results.append(result)
    return results

print(simulation(10, 120, 0.6, 0.5))

【问题讨论】:

  • Debuggers 是一件很棒的事情。在介绍课程中缺乏对它们的报道是恕我直言的重大失败。

标签: for-loop statistics time-series simulation random-walk


【解决方案1】:

在循环的最后一行,它应该只是results.append(result) 而不是results = results.append(result)results 是一个列表对象,append 函数修改该对象,返回 None

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2021-08-30
    • 2019-09-07
    相关资源
    最近更新 更多