Random Walk diffusion

 

 

Numerical Simulation of a Random Walk as a Stochastic Model of Diffusion

Papadopol Lucian Ioan
l.i.papadopol@gmail.com

1. Introduction

In this notebook we study a simple stochastic system: the one-dimensional random walk.

Unlike the deterministic models studied previously, in a stochastic system the evolution is not completely determined by the initial conditions. Even starting from the same initial state, two different simulations may produce different trajectories, because the model contains a random component.

The 1D random walk describes a particle moving along a line. At each time step, the particle can move to the right or to the left with a certain probability.

2. Libraries and parameters

import numpy as np
import matplotlib.pyplot as plt

# Simulation parameters

n_steps = 500          # number of time steps
p_right = 0.5          # probability of a step to the right
x0 = 0                 # initial position
step_scale = 1.0       # spatial step size

3. Random walk of a single particle

The following function simulates a one-dimensional random walk. At each step, the particle moves by either +step_scale or -step_scale. The final trajectory is obtained by taking the cumulative sum of all random steps.

def random_walk(n_steps, step_scale=1.0, seed=None):
    rng = np.random.default_rng(seed)

    steps = step_scale * rng.choice(
        [-1, 1],
        size=n_steps
    )

    positions = np.zeros(n_steps + 1)
    positions[1:] = np.cumsum(steps)

    return positions

4. Two-dimensional representation of a single particle

To visualize the motion in a plane, we use two independent one-dimensional random walks: one for the horizontal coordinate and one for the vertical coordinate.

# 2D random walk of a single particle

x = random_walk(n_steps, step_scale=step_scale, seed=1)
y = random_walk(n_steps, step_scale=step_scale, seed=2)

plt.figure(figsize=(7, 7))

plt.plot(x, y, linewidth=1.5)

# Starting point
plt.scatter(x[0], y[0], s=50, label="Start")

# Final point
plt.scatter(x[-1], y[-1], s=70, marker="x", label="End")

plt.xlabel("Position x")
plt.ylabel("Position y")
plt.title("2D random walk of a single particle")
plt.grid(True)
plt.axis("equal")
plt.legend()
plt.show()
2D random walk of a single particle
Two-dimensional random walk of a single particle. The initial point and final point are marked separately.

5. Test with many particles

We now repeat the same stochastic process for many particles. Each particle starts from the same origin, but follows a different trajectory because the random choices are different.

n_particles = 20
step_scale = 1.0

time = np.arange(n_steps + 1)
plt.figure(figsize=(7, 7))

for i in range(n_particles):
    x = random_walk(n_steps, step_scale=step_scale, seed=2*i)
    y = random_walk(n_steps, step_scale=step_scale, seed=2*i + 1)

    plt.plot(x, y, linewidth=1.2, alpha=0.9)

    # Starting point
    plt.scatter(x[0], y[0], s=20)

    # Final point
    plt.scatter(x[-1], y[-1], s=45, marker="x")

plt.xlabel("Position x")
plt.ylabel("Position y")
plt.title("2D random walk of many particles")
plt.grid(True)
plt.axis("equal")
plt.show()
2D random walk of many particles
Two-dimensional random walks of many particles. Each curve represents one independent realization of the same stochastic process.

A single trajectory is irregular and cannot be predicted deterministically. However, when many independent particles are simulated, a collective statistical behavior emerges