Random walks and diffusion · Python Programming Class 11–12 | StemPy Lab
Course 3.1·Lesson 3 of 5
Random walks and diffusion
In a random walk on a line, every step is one to the left or one to the right with equal chance, and the position is the running total of the steps, made with np.cumsum. Many walkers are simulated at once as the rows of a two-dimensional array, with cumsum along axis 1. The average position stays at 0, but the walkers spread out: the mean of the squared distance after n steps is about n, so the typical distance is the square root of n and not n. The final positions of many walkers form a bell curve, which is the central limit theorem. This is how particles diffuse: the mean squared distance grows in proportion to the time, and the slope of the straight line of the mean squared distance against the steps is 1 for steps of size 1.
about 40 min1 simulation
Lesson 3 of 5Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
After this lesson you can
simulate a random walk with np.cumsum,
simulate thousands of walkers at once with a two-dimensional array, and
show that the typical distance grows as the square root of the number of steps.
A drunk on a line
A walker stands on a line. At every step, the walker goes one place to the left or one place to the right, with the same chance. After many steps, where is the walker? This is a random walk. It is a model for many things: a molecule of perfume moving through the air, a particle jostled by the molecules of water (Brownian motion), the price of a share, and the path of a photon in the Sun.
Predict first. After 20 steps, will the walker be near 0 or far from 0?
Many walkers at once
Predict first. On average, will the walkers drift to the right, or stay near 0? How far from 0 will a typical walker be after 100 steps: about 100, about 10, or about 1?
The output says:
The mean final position is −0.06, about 0. There is no push to either side, so on average the walkers have not gone anywhere.
The mean of the squared distance is 101.3. It is about 100, the number of steps.
The typical distance is 10.07, which is the square root of 100.
So after 100 steps, the walker is typically 10 places from the start, not 100. The walker takes 100 steps, but most of them cancel: half to the left and half to the right.
The law of the mean squared distance
⟨x2⟩=n
For steps of length ℓ and a number of steps n=t/τ in time t, this becomes ⟨x2⟩=ℓ2t/τ: the mean squared distance grows in proportion to the time. This is the law of diffusion, found by Einstein in 1905. Perfume spreads in a room, and ink spreads in water, in just this way. It is also why diffusion is very effective over a short distance and very slow over a long one: to go 10 times farther, a particle needs 100 times as much time.
The mean position stays near 0, because left and right steps cancel. The squares do not cancel, which is why the spread grows.
The bell curve
A histogram of the final positions of 20000 walkers after 100 steps, in green bars from minus 40 to 40. The bars rise to a peak near 0 and fall away on both sides in a bell shape. A black bell curve with the spread 10 follows the tops of the bars closely.
This is a case of a deep fact, the central limit theorem: when many small random pieces are added, the sum has a bell-shaped distribution, whatever the shape of the pieces. Here each piece is a step of ±1. This is why the bell curve is everywhere in nature: the height of people, the errors of measurements, and the final positions of walkers.
A walk in a plane
Three random walks of 500 steps in a plane, drawn as thin wandering lines in blue, orange and green. All start at a black square at the origin and wander in different directions, ending at round dots a few tens of units from the start. The scale is equal in both directions and there is a grid.
Common mistakes
Thinking that the walker drifts away at one step for each unit of time. The typical distance is n.
Taking the mean of the position and concluding that nothing moved. Use the mean of the squared position.
Using cumsum along the wrong axis. For one running total for each walker, use axis=1.
Looking at one path only. The law is for the average.
Mixing up size=(walkers, steps) and size=(steps, walkers).
Practice
Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
Warm-up. Open the lab Random walk. You write a walk, and the mean squared distance of many walkers.
Build. Open the problem Drunk walker and the problem Diffusion coefficient.
Remember these
A random walk is the running total (np.cumsum) of random steps.
For many walkers, make a two-dimensional array with a row for each walker, and cumsum(axis=1).
The mean position is 0. The mean squared distance after n steps is n. The typical distance is n.
The final positions form a bell curve: this is diffusion.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
What is the typical distance of a walker after 400 steps of length 1?
How many times more steps are needed to go 3 times as far?
What is np.cumsum([1, -1, 1, 1])?
Why is the mean position zero, but the mean squared distance is not?
Enroll free in Simulation and Randomness
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.