I am trying to simulate the following random walk equation:
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))
At the last line in the loop, it's supposed to be just
results.append(result)
and notresults = results.append(result)
.results
is a list object and theappend
function modifies that object, returningNone