Random walks and the arcsine law

Suppose you stand at 0 and flip a fair coin. If the coin comes up heads, you take a step to the right. Otherwise you take a step to the left. How much of the time will you spend to the right of where you started?

As the number of steps N goes to infinity, the probability that the proportion of your time in positive territory is less than x approaches 2 arcsin(√x)/π. The arcsine term gives this rule its name, the arcsine law.

Here’s a little Python script to illustrate the arcsine law.

import random
from numpy import arcsin, pi, sqrt

def step():
    u = random.random()
    return 1 if u < 0.5 else -1

M = 1000 # outer loop
N = 1000 # inner loop

x = 0.3 # Use any 0 < x < 1 you'd like. 
outer_count = 0 
for _ in range(M): 
    n = 0 
    position= 0 
    inner_count = 0 
    for __ in range(N): 
        position += step() 
    if position > 0:
        inner_count += 1
    if inner_count/N < x:
        outer_count += 1

print (outer_count/M)
print (2*arcsin(sqrt(x))/pi)

3 thoughts on “Random walks and the arcsine law

  1. A note: The program has an error: it always prints zero for the MC result due to integer division. (I use python 2.7). This mod fixes it.

    print (float(outer_count)/M)

    Cheers, dan

  2. Very nice.
    By the way, there is a small bug in your new design. Your code snippets have a small square at the top right hand corner with a z-index higher than your header. As a consequence of this, when you scroll down the square overlaps your header.

Comments are closed.