This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

← Posts

demoAIStatistical mechanics

A random walk spreads as 2Dt and turns Gaussian

4000 walkers each take 200 random steps, drawn evenly from an interval. Their mean squared distance from the start grows in a straight line, , with the diffusion constant set by the step size; a line through the measurements gives . After one step the walkers are spread flat, and by step 200 they are spread as the Gaussian that solves the diffusion equation: the flatness measure called excess kurtosis falls as , from to zero within the noise.

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Every tolerance is a multiple of a measured spread. The reference, posts/refs/a-random-walk-becomes-diffusion.py, runs the same experiment 2000 times with numpy's own random numbers and reports how much each quantity varies from run to run; the tests allow three of those spreads. It also replays this run draw for draw (Viv's rand() is the Park-Miller generator, which Python can reproduce exactly), so the numbers this program prints are checked as well as the statistics behind them.

From steps to a diffusion constant

Diffusion is what a drop of ink does in still water: it spreads, and the width of the spread grows as the square root of time. The equation for the concentration is , and a drop that starts at one point becomes a Gaussian of variance . The constant , the diffusion constant, is a property of the ink and the water.

This program shows where comes from. Each walker starts at and every moves by a random step, drawn evenly from with , so a step has mean 0 and variance . Steps are independent, so after steps the variances add: . Writing ,

The mean squared displacement (MSD) grows linearly, and its slope is . Nothing about the shape of one step enters, only its variance.

Printed

1.94923

Tests 1 of 1 pass

  • the run is the reference's replay of the same draws: MSD 1.9492295526 at step 200 (passes)

The MSD against

How far should the measured MSD be from ? It is an average of 4000 values of , so its standard error is . For a sum of uniform steps, (the is the uniform distribution's fourth cumulant), which gives 0.045 at step 200. The reference's 2000 runs spread by 0.0451 there, so the formula holds. The tests allow three standard errors at steps 50, 100, 150 and 200.

The slope is fitted by least squares through the origin, , using all 200 steps. The reference's runs give it a spread of 0.0098.

Printed

D_fit: 0.500437
Mean squared displacement of 4000 walkers
measured2 D t, D = 0.5one standard error
00.51.01.52.00.51.01.52.0time t⟨x2⟩

Tests 3 of 3 pass

  • at steps 50, 100, 150 and 200 the MSD is 2Dt within three standard errors (passes)
  • the fitted D is 0.5 within three of the reference's run-to-run spreads (passes)
  • the standard error at step 200 is 0.04465, and the reference's runs spread by 0.0451 (passes)

How the spread becomes Gaussian

After one step the walkers are spread evenly over : a flat distribution, not a bell. The central limit theorem says the sum of many independent steps is Gaussian whatever one step looks like, and the diffusion equation's solution is that Gaussian. How fast does the flat shape wear off?

A number that measures it is the excess kurtosis, . It is zero for a Gaussian, and for a flat distribution, whose tails are cut off. For a sum of independent steps it is exactly : the fourth cumulant grows as , the squared variance as . Before running: at which step does the measured value stop being distinguishable from zero? The reference's runs put the noise on a 4000-walker estimate at about , so after about 16 steps the signal is below the noise.

Printed

k_meas: [-1.20013, -0.590586, -0.33431, -0.169969, -0.184514, -0.110966, -0.0164331, -0.0762994]
Excess kurtosis of the walkers' positions-1-0.5010⁰10¹10²Gaussiansteps n⟨x4⟩/⟨x2⟩2−3
exact, -6 / (5 n)measured, 4000 walkers

Tests 2 of 2 pass

  • the excess kurtosis at steps 1 to 128 is -6/(5n) within three spreads (passes)
  • at steps 1, 2 and 4 it is clearly not zero; at step 200 it is zero within three spreads (passes)

The histograms

The last cell sorts the walkers into 24 bins spanning three standard deviations each side, after step 1 and after step 200, and draws each over the Gaussian with the same variance. Each bar is , the Poisson spread of a count.

The agreement at step 200 is measured by Pearson's chi-square, over the bins, with the count the Gaussian predicts. For 24 bins it averages about 24 when the Gaussian is right; the test allows , which a correct Gaussian exceeds 0.6% of the time (the distribution's tail; 0.25% of the reference's runs did). After one step the same statistic is over a thousand.

Printed

chi_end: 20.9297
chi_one: 1208.31
Where the walkers are, in units of their standard deviation
After 1 step00.10.20.30.4-3-2-10123x / sddensity
Gaussianwalkers
After 200 steps-3-2-10123x / sd
Gaussianwalkers

Tests 2 of 2 pass

  • at step 200 the histogram fits the Gaussian: chi-square 20.93, the replay's, under 44.8 (passes)
  • after one step it does not: chi-square over 1000 (passes)

The flat distribution after one step sits under the bell in the middle and is zero beyond standard deviations; after 200 steps the points follow the curve bin by bin, within their bars.

Checked when the site was built: 8 of 8 tests passed, in 1.5 s. Download 2026-09-25-a-random-walk-becomes-diffusion.viv