0

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))
pjs
  • 18,696
  • 4
  • 27
  • 56
  • 1
    [Debuggers](https://docs.python.org/3/library/pdb.html) are a wonderful thing. The lack of coverage of them in intro courses is a major failure IMHO. – pjs Nov 21 '20 at 17:29

1 Answers1

1

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

droptop
  • 1,372
  • 13
  • 24