0
$\begingroup$

So I'm simulating a 1-dimensional random walk, with 1000 walks that each take 1000 steps.

How do I calculate the average number of times that a walker returns to the origin and then the standard deviation of the number of times that a walker returns to the origin?

I feel like I'm not understanding the logic of how to manipulate this to get the answers I need, so if anyone could help, that'd be really helpful! The code I have for now is:

def StepsFunc() :
    step = np.random.randint( 2 )

    #step is 0 or 1, want -1 or +1
    if step == 0 :
        step = -1
    return step

#defining another function, which will actually take N number of steps
def Move( N ) : 
    x = 0 # starting from origin position
    i = 0 # count
  
    while i <= N-1 :
        step = StepsFunc()
        x += step
        i += 1
    return x

#number of steps
N = 1000

#number of random walks
walks = 1000

StepsList=[]

num=0
for i in range(walks):
    x = Move( N )
    if x == 0:
        num += 1    
    StepsList.append( x )
    

print("The random walk returns to zero {} times".format(num))
print(np.mean(StepsList)) #average of all walks
print(num/walks) #getting the average number of walks that returned to zero?
#plt.hist(StepsList, bins='auto')
$\endgroup$
4
  • $\begingroup$ You are making N moves without checking for visits to the origin during the steps, and without recording the trajectory (only the final state). Thus you are discarding the information that is needed to reconstruct the statistics you want to compute. (The problem here is really about programming, though.) $\endgroup$
    – Ian
    Commented May 9, 2021 at 18:01
  • $\begingroup$ @Ian ok so I see that, but also, the way I understand the given question is that I need to check the statistics for the final state? If it was that, could you please help explain how I'd get the required statistics? Because as far as I understand, the mean for the final state would just be the number of times it returned to 0 divided by the number of walks. But then I don't understand how I'd get the standard deviation by doing this... $\endgroup$ Commented May 9, 2021 at 18:22
  • 2
    $\begingroup$ Normally you'd be interested in the number of times each trajectory returns to 0, not the number of times that the trajectory stopped at 0 at the end of the simulation. So to do that you would need to increment some counter each time the trajectory visited zero. If you're just interested in the endpoint then you don't need to do all these dynamics, you can just sample a binomial(n,1/2) and subtract n/2. $\endgroup$
    – Ian
    Commented May 9, 2021 at 18:25
  • $\begingroup$ @Ian ah okay, I'll try implementing this and see if I get what I need, thank you! $\endgroup$ Commented May 9, 2021 at 19:51

1 Answer 1

1
$\begingroup$

I think this one does what you're looking for:

import numpy

Nstep = 1000
Nsim = 1000

step_cases = [1,-1]
p = [0.5,0.5]

steps = numpy.random.choice(a=step_cases,size=(Nsim,Nstep),p=p)

sim = numpy.zeros((Nsim,Nstep))
for k in range(1,Nstep):
    sim[:,k]=sim[:,k-1]+steps[:,k]

zeros_sim = numpy.zeros(Nsim)
for k in range(0,Nsim):
    zeros_sim[k] = numpy.sum(numpy.where(sim[k,:]==0,1,0))-1 # do not count the start
    
print(numpy.average(zeros_sim))
print(numpy.std(zeros_sim))
$\endgroup$
2
  • $\begingroup$ Thank you, this seems really helpful! Could you just let me know what is happening in the for loop with the zeros_sim. Specifically numpy.where? Also, if we compare this to my code, I'm assuming Nsim would be the number of walks, right? $\endgroup$ Commented May 9, 2021 at 19:55
  • 1
    $\begingroup$ Nsim is the number of random walks and the numpy.where checks returns 1 at every step where the random walk is 0 (i.e. returns to the origin) and 0 otherwise so the sum (-1) is the number of times the singular RW returned to the origin during the 1000 steps realization $\endgroup$
    – Snoop
    Commented May 9, 2021 at 19:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .