Simulating for loop

34 Views Asked by At

I am trying to simulate the following random walk equation:

xt = xt-1 + σεt

However, when I run my code I can't append the results. I get the following error:

first_term = results[i] TypeError: 'NoneType' object is not subscriptable

Is there an alternative approach to this?

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))
1

There are 1 best solutions below

0
On

At the last line in the loop, it's supposed to be just results.append(result) and not results = results.append(result). results is a list object and the append function modifies that object, returning None