Random Walk in One Dimension, Part 1

As I continue reading the Feynman Lectures on Physics, I’m intrigued by the discussion of the random walk in Chapter 6: “Probability,” Section 6–3: “The random walk.” Feynman discusses a random walk in one dimension, where at each step an object moves one unit either forward or backward at random. In Figure 6–5, he draws a graph tracing the distance moved over 30 steps, for 3 trials. I remember seeing graphs like this in my textbooks when I was in school, studying them and trying to appreciate them. And I realize that now technology has advanced so much that I can explore these further on my home computer.

Problem: Graph a random walk in one dimension, following more trials over longer periods.

I was able to code this in Python/Numpy/Matplotlib. First, I tried running it with 10 trials, again for 30 steps. The walks are in blue, partially transparent so that it is easier to see when multiple walkers are in the same place. The yellow line traces the mean, and the red lines trace one standard deviation from the mean.

Graph of Random Walk: 10 trials, 30 steps

The dashed yellow line is y=0 (the expected mean), and the dashed red lines are ±√N. There is reasonable agreement, with the final mean -0.6 (expected 0) and standard deviation 5.80 (expected 5.48).

Let’s try this again, with a lot more trials — say, 1000.

Graph of Random Walk: 1000 trials, 30 steps

Note that the vertical axis is compressed a bit to fit in the outliers. The calculated mean and standard deviation (0.21 and 5.38) closely match the predicted ones. You can also see that the chance that a walker will continue moving away from the origin for each step drops over time (the chance that it will move +1 each time for n steps is 1/2n, and so is the chance that it will move −n). By the time 11 steps have been taken, there is less than one chance in a thousand that a walker would be at the maximum distance (1/211,multiplied by 2 since it could go either always forward or always backward).

Finally, let’s let this go for a lot longer: 2000 steps.

Graph of Random Walk: 1000 trials, 2000 steps

Now this looks really cool, and the density distribution is readily apparent. The mean is 2.078, (predicted=0), and standard deviation is 46.23 (predicted=44.72).

Here’s another run, with the predicted mean ± 1, 2, and 3 standard deviations, to illustrate the “68–95–99.7 rule” — that for normally distributed data, 68.3% are within one standard deviation, 95.5% are within two, and 99.7% are within 3 standard deviations of the mean.

Graph of Random Walk: 1000 trials, 2000 steps

To round this out, here are two more graphs. First is a run without any statistical curves added.

Graph of Random Walk: 1000 trials, 2000 steps

And finally, here is a version with equal scales for the x– and y-axes. The gray lines represent the theoretical maximum and minimum (if the random walker moved in the same direction for every step).

Graph of Random Walk: 1000 trials, 2000 steps

It’s neat to see that even though the positions spread a bit over time, they still stay pretty close to a net distance of 0. The probability that a walker could follow one of the gray lines all the way out to 2000 steps is 2/22000, about 1.74×10−602, or 1 chance in 5.74×10601. There are only an estimated 1080 atoms in the universe, so even if each atom participated in this random walk, by the time we got to 266 steps or so, chances are that no atom would still be on the gray line, taking every step in the same direction.

Read more in Part 2.

One thought on “Random Walk in One Dimension, Part 1

Comments are closed.