Explanation of the Probability Distribution Simulator — What the Pin Board, the Dice, and the Lamp Are Computing

This page explains what the pile of balls, the scatter of the dice averages, and the spots hit by the light that you saw in the simulator correspond to in the language of probability, and with what formulas they are computed, in the order words → formulas → calculation procedure. The procedure for the detailed view, the conditions of the calculation, and questions for checking are also collected here.

1. About this page

The Probability Distribution Simulator is a device for checking the shapes of the distributions treated in Chapter 3 (probability distributions) and the behavior of the sample mean.

The simulator has two views.

Scene A performs the same kind of calculation as the binomial distribution in ① of the detailed view, and Scenes B and C the same kind as ② of the detailed view (Section 2). We recommend reading in this order: try the simple view, check what it really shows in Section 2, read the formulas in Section 3, and operate the detailed view with the procedure in Section 4.

2. What the simple view really shows

All three scenes of the simple view show that “each single result is decided by chance, but when many are collected, a definite shape appears.” The height of a bar or of a pile of dots in the picture is the number of times (or the fraction of times) that value came up. A higher place is a value that comes up more often, and a wider spread means larger variability.

2.1 Scene A: If you drop balls, what shape do they pile up into?

Each time a ball on the pin board hits a pin, it goes right or left. If we regard “going right or not” at each row as a Bernoulli trial with success (x = 1) and failure (x = 0), then the number of moves to the right m of a ball that has passed through a board of N rows is the number of successes in N Bernoulli trials. The bin number is m itself, so the way the balls pile up follows the binomial distribution Bin(m | N, μ) (Section 3.2 of the text). The number of paths leading to bin m equals the number of ways of choosing which m of the N rows go right (the binomial coefficient), and every path occurs with the same probability μm(1 − μ)N−m. The paths leading to the middle bins are the most numerous, so the middle forms a high peak.

Table 1: Words on the screen in Scene A and the corresponding terms and symbols
Words on the screenTermSymbol or formula
Rows of pins (6 / 12 / 24 rows)number of trialsN
Share going right (Half and half / 70 % right)probability of success (parameter of the Bernoulli distribution)μ = 0.5, 0.7
At each row of pins: right or leftBernoulli trialBern(x | μ)
bin number = moves to the rightnumber of successesm = x1 + ⋯ + xN (written x in the detailed view)
Balls dropped (piled up in the bins)histogram of a sample from the binomial distributionnumber of balls n
○ Calculated height (binomial distribution)expected frequencyn Bin(m | N, μ)
Dashed: smooth peak (Gaussian distribution)Gaussian approximation of the binomial distributionn 𝒩(m | Nμ, Nμ(1 − μ))
Average number of moves to the right (by calculation: rows × share going right)sample mean (the calculated value is the expectation)sample mean of m (𝔼[m] = Nμ)
Width of the spread (standard deviation)sample standard deviation (the calculated value is the standard deviation)square root of the unbiased variance (√(Nμ(1 − μ)))

Numbers for “Try it” (each is the value when balls are dropped onto that board from the start):

Where the analogy fits and where it does not

Where it fits: at each row, one uniform random number u is drawn, and the ball goes right if u < μ. This uses the random numbers in the same way as the binomial random numbers of the shared computing module (the sum of N Bernoulli trials), so the sequence of bin numbers of the balls is the same as the sequence obtained by drawing a sample from the “Binomial” distribution (number of trials N, probability of success μ) in ① of the detailed view with the same seed 20261112. For example, the result of dropping 1000 balls onto the 12-row, half-and-half board is the same as the histogram obtained in ① by setting N = 12 and μ = 0.5 and drawing “1000” samples.

Where it does not fit: on a real pin board, a ball may bounce over two or more rows after hitting a pin, balls may collide with each other, or the pins may be out of place, so the rows are not independent trials with exactly half right and half left. In this scene, the calculation assumes that the probability of going right is the same at every row and that the rows are independent. “70 % right (board tilted right)” simply sets the probability of going right to 0.7; it is not a calculation of how balls move on a tilted board. The ball bouncing off the pins as it falls is a visual effect; only the path (the sequence of right or left) is a result of the calculation.

2.2 Scene B: What if you roll many dice and take the average?

The face x of one die follows the distribution in which 1 to 6 each come up with the same probability 1/6. The mean is 3.5 and the variance is σ2 = 35/12 (standard deviation 1.71). When n dice are rolled independently, the average of their faces x̄n is the sample mean; its expectation stays at 3.5, and its standard deviation shrinks to σ/√n (Chapter 2, Section 2.8 of the text; the text writes the sample size as N). For n = 1, 4, 16 it is 1.71, 0.85, 0.43, halving each time the number of dice is multiplied by 4. Moreover, by the central limit theorem, for large n the distribution of x̄n approaches the bell shape of the Gaussian distribution regardless of the original distribution (a flat shape).

Table 2: Words on the screen in Scene B and the corresponding terms and symbols
Words on the screenTermSymbol or formula
Face of one dierandom variable (uniform discrete distribution on 1–6)x, 𝔼[x] = 3.5, Var[x] = 35/12
Dice rolled each time (1 / 4 / 16 at a time)sample sizen
Average of the faces (such as “Average 3.25”)sample meanx̄n = (x1 + ⋯ + xn)/n
Averages takennumber of times the sample mean is made anew (corresponds to the number of paths)R
Stacked barshistogram of x̄n (normalized to a density)—
Average of the averagesmean of the R values of x̄napproaches 𝔼[x̄n] = 3.5
Width of the spread (standard deviation); “By calculation: ±…”sample standard deviation of the R values of x̄n; standard errorσ/√n = 1.708/√n

Numbers for “Try it” (200 rounds each; the random sequence starts from the beginning):

The bars for the average of 16 dice combine five neighboring values (five values in steps of 1/16; width 5/16) into one bar. For 4 dice and for 1 die, there is one bar for each possible value (steps of 1/4 and of 1). The height of a bar is a density, the count divided by “number of rounds × bar width,” so the areas can be compared even when the number of dice differs.

Where the analogy fits and where it does not

Where it fits: each face is made from a uniform random number u as ⌊6u⌋ + 1, and the calculation treats the die as an ideal one in which 1 to 6 each come up with exactly 1/6. The average of the averages and the standard deviation are computed with the same sample-summary function as in the detailed view (mean and unbiased variance).

Where it does not fit: a real die may have a slight bias. The dice in this scene are ideal, with no bias. Because x̄n of dice faces is a discrete quantity that takes only values in steps of 1/n, it only “approaches” the bell shape of the Gaussian distribution and never becomes the same. The faces changing while the dice are rolling is a visual effect.

2.3 Scene C: Can an average fail to settle down?

A lamp is placed at distance γ from a wall, and it lights the wall in a direction toward the wall chosen at random (every direction equally likely) between −90° and 90°. When the direction is θ from straight ahead, the light hits the wall at a spot shifted from the point straight ahead, x0, by γ tan θ (Figure 1). This spot x follows the Cauchy distribution (Lorentz form) (derived in Section 3.4). The Cauchy distribution has no mean (Section 3.7 of the text), so no matter how many times the lamp shines, the average of the spots hit does not settle at a single value. On the other hand, the middle value (median) of the spots hit approaches the point straight ahead, x0. Scene C uses x0 = 0 and γ = 1, and one unit is the distance from the lamp to the wall.

Wall Lamp θ γ Straight ahead x0 Spot hit x γ tan θ
Figure 1: The wall and the lamp. When the distance from the lamp to the wall is γ (1 unit in Scene C) and the direction of the light is θ from straight ahead, the light hits the wall at a spot shifted from the point straight ahead, x0, by γ tan θ.
Table 3: Words on the screen in Scene C and the corresponding terms and symbols
Words on the screenTermSymbol or formula
Straight ahead of the lamp (0)center (median)x0 = 0
1 unit = distance from the lamp to the wallhalf width at half maximum (distance to the quartiles)γ = 1
random directionangle following a uniform distributionθ = π(u − 1/2)
Spots hit on the wall (1 dot per shot)sample from the Cauchy distributionxj = x0 + γ tan θj
Number of shotssample sizen
Average of the spots hit (solid line)sample meanx̄n (regardless of n, follows Cauchy(x0, γ))
Middle value of the spots hit (dashed line)sample medianwith standard deviation about πγ/(2√n), approaches x0
Day 1, day 2 (Try another day)independent pathspath j = 0, 1, …

Numbers for “Try it”:

Where the analogy fits and where it does not

Where it fits: the Cauchy random numbers of the shared computing module are made from a uniform random number u as tan(π(u − 1/2)). This is exactly “choose the direction θ = π(u − 1/2) uniformly and hit the spot tan θ,” so the direction of the light in the picture is recovered from the random number used in the calculation as θ = arctan((x − x0)/γ). The sequences of day 1 and day 2 are the same as Cauchy paths 1 and 2 with the defaults of ② in the detailed view (seed 20261112, scale 1), and the 1000-shot averages 5.33 and −0.96 agree with the readout when “Upper limit of sample size n” in ② is set to 103 (n = 103). The 1000 shots of day 1 are also the same as the values obtained by drawing “1000” samples from the Cauchy distribution (x0 = 0, γ = 1) in ① (step 3 in Section 4.1).

Where it does not fit: a real lamp does not choose its direction toward the wall uniformly at random. Also, the calculation assumes an infinitely long wall, so light pointing almost parallel to the wall is counted as hitting thousands of units away. Only part of the wall (±4 or ±6 units) can be drawn in the picture, so the number of shots that landed outside it is shown as a count, such as “← 87 out.” The lamp turning and stopping is a visual effect.

3. How it works

3.1 The sum of small chance events becomes a bell shape (Scene A)

The bin number m of the pin board is the sum of N independent Bernoulli variables x1, …, xN (1 for right, 0 for left). By independence, the probability that a particular path occurs is the product of the probabilities at each row, μm(1 − μ)N−m, and there are as many paths leading to bin m as there are ways of choosing the m rows that go right out of N rows. Adding these up gives the binomial distribution, and its mean and variance are N times the mean μ and the variance μ(1 − μ) of a Bernoulli variable (Section 3.2 of the text, Theorem 3.1).

Bin(m∣N,μ)=(Nm)μm(1−μ)N−m,𝔼[m]=Nμ,Var[m]=Nμ(1−μ)

Because m is a sum of quantities that are independent and follow the same distribution, by the central limit theorem (Chapter 2, Section 2.8 of the text) the distribution of m approaches, for large N, the Gaussian distribution with mean Nμ and variance Nμ(1 − μ). The dashed line in Scene A is the density of this Gaussian distribution multiplied by the number of balls n (the number of balls per bin).

Bin(m∣N,μ)≈𝒩(m∣Nμ,Nμ(1−μ))(for large N)

The explanation in Section 3.5 of the text is that measurement errors tend to follow a Gaussian distribution because many small independent factors add up. When μ = 1/2 the binomial distribution is symmetric, and when μ ≠ 1/2 the peak moves toward Nμ and the distribution is no longer symmetric (Figure 3.2 of the text).

3.2 Distribution shape and interval probability (detailed view ①)

The interval probability is the integral of the probability density function p(x), computed as a difference of the cumulative distribution function F. For a discrete distribution it is a sum that includes the end integers.

P(x1≤x≤x2)=∫x1x2p(x)dx=F(x2)−F(x1),discrete: ∑k=⌈x1⌉⌊x2⌋p(k)

The density (mass) function of each distribution, and its mean and variance (only when they exist):

Binomial: p(x)=(Nx)μx(1−μ)N−x,𝔼[x]=Nμ,Var[x]=Nμ(1−μ) Poisson: p(x)=λxe−λx!,𝔼[x]=Var[x]=λ Gaussian: 𝒩(x∣μ,σ2)=12πσ2exp(−(x−μ)22σ2),F(x)=Φ(x−μσ),FWHM22ln2σ Cauchy: p(x)=1πγ(x−x0)2+γ2,F(x)=12+1πarctanx−x0γ,FWHM2γ Gamma: Gam(x∣a,b)=baxa−1e−bxΓ(a),𝔼[x]=ab,Var[x]=ab2(b is the rate) Beta: Beta(x∣a,b)=Γ(a+b)Γ(a)Γ(b)xa−1(1−x)b−1,𝔼[x]=aa+b,Var[x]=ab(a+b)2(a+b+1) Student’s t: p(x)=Γ((ν+1)/2)Γ(ν/2)νπs[1+(x−μ)2νs2]−(ν+1)/2,𝔼[x]=μ(ν>1),Var[x]=s2νν−2(ν>2) Weibull: F(s)=1−exp[−(s/s0)m],𝔼[s]=s0Γ(1+1/m)

3.3 Why the sample mean clusters together (Scene B)

For n values that are independent and follow the same distribution, the average x̄n has the same expectation as the mean μ of the original distribution, and its variance becomes σ2/n (Chapter 2, Section 2.8 of the text, Theorem 2.9). This is because the variance of a sum of independent values is the sum of the variances, nσ2, and dividing by n multiplies the variance by 1/n2. For dice it is as follows.

𝔼[x]=1+2+⋯+66=3.5,Var[x]=12+⋯+626−3.52=916−494=3512,𝔼[x¯n]=3.5,Var[x¯n]=σ2n=3512n,standard deviationσn≈1.708n

The standard deviation σ/√n (the standard error) becomes half only when n is multiplied by 4. For this shrinking to hold, the variance σ2 of the original distribution must be finite (an assumption of the law of large numbers and the central limit theorem; Section 2.8 of the text).

3.4 The spot hit by the lamp’s light follows the Cauchy distribution (Scene C)

When the direction θ follows the uniform distribution from −π/2 to π/2, the spot hit can be written as follows.

x=x0+γtanθ,θ=π(u−12)(u is a uniform random number between 0 and 1)

Because tan is an increasing function between −π/2 and π/2, “the spot hit is at most x” is the same as “the direction is at most arctan((x − x0)/γ).” Because θ is uniform on an interval of length π,

P(spot hit≤x)=P(θ≤arctanx−x0γ)=arctan((x−x0)/γ)+π/2π=12+1πarctanx−x0γ

This is exactly the cumulative distribution function F(x) of the Cauchy distribution in Section 3.7.1 of the text. Differentiating with respect to x gives the density (1/π) γ/[(x − x0)2 + γ2]. For directions almost parallel to the wall (θ close to ±π/2), tan θ becomes arbitrarily large, so the light can hit very far away, and because of this the mean does not exist. The following formulas show the reason with integrals.

The mean of the Cauchy distribution does not exist because the integral truncated on both sides of x0 at distance R diverges as R → ∞ (the principal value is x0, but the definition of the expectation, ∫|x| p(x) dx < ∞, is not satisfied).

∫x0−Rx0+R|x−x0|p(x)dx=γπln(1+R2γ2),∫x0−Rx0+R(x−x0)2p(x)dx=2γπ[R−γarctanRγ]

For the sample mean in ②, x̄n = (x1 + ⋯ + xn)/n, if the variance σ2 is finite, then Var[x̄n] = σ2/n, and the probability of falling in the band ±σ/√n is about 68 % for large n. For the Cauchy distribution, from the characteristic function 𝔼[eikx] = eix0k − γ|k|,

𝔼[eikx¯n]=(eix0k/n−γ|k|/n)n=eix0k−γ|k|⇒x¯n∼Cauchy(x0,γ)

so, regardless of n, the probability that |x̄n − x0| > γ remains 1/2. The median, on the other hand (if the density at the median p(m) > 0), shrinks approximately with standard deviation 1/(2p(m)√n): σ√(π/2)/√n for the Gaussian and πγ/(2√n) for the Cauchy.

3.5 Calculation procedure

  1. Scene A: at each row, draw a uniform random number u; go right (1) if u < μ, and left (0) otherwise. Add one ball to bin m, the sum over the N rows. The calculated height is curve in distributions.model.js (the probability mass function of the binomial distribution) multiplied by the number of balls n, and the smooth peak is the Gaussian density 𝒩(m | Nμ, Nμ(1 − μ)) multiplied by n.
  2. Scene B: in each round, draw n faces ⌊6u⌋ + 1, take their average, and add it to the record. The average of the averages and the standard deviation are computed with sampleStats (mean and unbiased variance).
  3. Scene C: draw random numbers from the Cauchy distribution (x0 = 0, γ = 1) one at a time. The average uses the same compensated (Kahan) summation as ② of the detailed view, and the middle value uses the same running median with two heaps (RunningMedian); both are updated each time one value is added.
  4. Detailed view: as in the next paragraph.

Numerical computation: the cumulative distribution functions are computed with the regularized incomplete beta and gamma functions (continued fractions), the error function, and arctan (shared library MLCore). The “numerical-integration check” on the screen is a value obtained by tanh–sinh (double-exponential) numerical integration of the density over small intervals separated by quantiles, and is meant to be compared with the difference of the cumulative distribution function. The sample mean in ② uses compensated (Kahan) summation, and the median is a running median using two heaps.

The calculations of the simple view use the same functions as the detailed view (curve, sampleStats, make, drawSample, pathSeed, and RunningMedian in distributions.model.js) and the shared random numbers. The only calculations added for the simple view are the paths of the balls (right or left at each row), the dice faces and their average, the direction of the light (the inverse of tan), and how the bars of the picture are grouped (distributions.easy.js).

4. How to use the detailed view

The detailed view has two tabs: ① (distribution shape and interval probability) and ② (behavior of the sample mean). In both, the figure and the “Putting the current numbers into the formula” box are on the left, and the controls and the readout are on the right.

4.1 Procedure and what to observe

  1. Open ① with its defaults (Gaussian distribution 𝒩(x | 0, 12), interval [−1, 1]). What to see: the shaded area P(−1 ≤ x ≤ 1) = Φ(1) − Φ(−1) = 0.8413 − 0.1587 = 0.6827. Press “± 2 SD” in the interval controls to get 0.9545, and check the mean, the variance, and the FWHM 2√(2 ln 2) σ = 2.355 in the readout.
  2. Press “Gaussian and Cauchy (match the FWHM)” under “Comparison examples,” and turn on “Logarithmic vertical axis” and “Wide horizontal axis (see the tails).” What to see: even with the same peak width, the tail of the Cauchy distribution decreases only as 1/x2, while the Gaussian falls off steeply. P(−1 ≤ x ≤ 1) is 0.6827 for the Gaussian and 0.4482 for the Cauchy. The readout says, with a reason, that the mean and variance of the Cauchy distribution “do not exist.”
  3. Set the distribution to the Cauchy distribution and draw “1000” samples. What to see: the histogram matches the shape of the density, yet the sample mean is 5.33 (seed 20261112), far from the center 0, and the sample variance is as large as 1.1×105. How many points fall outside the range. The median is −0.048, close to the center.
  4. Press “t (ν) and Gaussian” under “Comparison examples” and move the degrees-of-freedom ν slider from 1 to 1000. What to see: at ν = 1 the shape is the same as the Cauchy distribution, and for large ν it approaches the Gaussian distribution. The mean exists only for ν > 1 and the variance only for ν > 2 (variance 3 at ν = 3, and 1.071 at ν = 30).
  5. Open ② (default: n up to 104, 5 paths, seed 20261112). What to see: the sample mean of the Gaussian distribution on the left shrinks into the band ±σ/√n, whereas the sample mean of the Cauchy distribution on the right occasionally jumps by a large amount, and even at n = 104 the five values are scattered: 0.769, −1.83, 0.118, 1.35, −0.910 (2 of the 5 paths have |x̄ − x0| > γ).
  6. In ②, set “Quantity to draw” to “Median.” What to see: even for the Cauchy distribution the median settles down and enters the band ±πγ/(2√n) (±0.0157 at n = 104). Pressing “Rerun with same seed” does not change the curves.

With the keyboard, Tab moves to buttons and sliders, Enter (or Space) presses a button, and the arrow keys move a slider (Page Up / Page Down move it in larger steps). The tabs can be switched with ← and →.

5. Conditions of the calculation

5.1 Data and random-number conditions

5.2 Conditions fixed in the simple view

5.3 Simplifications

5.4 Conditions under which results become unstable

6. Questions for checking

Questions 1 and 2 can be answered with the simple view alone. Questions 3–6 use the detailed view.

  1. In Scene B of the simple view, when the number of dice rolled each time is changed from 4 to 16, by about what factor does the width of the spread change? If the number were 64, about how large would you expect the width to be?
    Sample answer

    It becomes about 1/2 (0.85 to 0.43 by calculation). The standard deviation of the sample mean is 1.708/√n, so multiplying n by 4 makes it 1/2. For 64 dice, it is expected to be about 1.708/8 ≈ 0.21.

  2. In Scene C of the simple view, why does the average not settle near 0 even after the lamp shines 1000 times? What is different from the dice (B)?
    Sample answer

    When the lamp points almost parallel to the wall, the light hits thousands of units away (9771 units in the 1000 shots of day 1). When such a value comes up at the n-th shot, the average moves a lot, and even with more shots, hits that are even farther away keep happening. The spots hit follow the Cauchy distribution, which has neither a mean nor a variance, so the reasoning of B, shrinking as σ/√n, cannot be used. Dice faces come up only in the range 1–6, and their variance is finite.

  1. For the Gaussian distribution 𝒩(x | 0, 1) and the Cauchy distribution with matched FWHM (γ = √(2 ln 2) ≈ 1.177), what are P(−1 ≤ x ≤ 1) and P(|x| > 3), respectively?
    Sample answer

    P(−1 ≤ x ≤ 1) is 0.6827 for the Gaussian and (2/π) arctan(1/1.177) = 0.4482 for the Cauchy. P(|x| > 3) is 0.0027 for the Gaussian against 0.2381 for the Cauchy, about 88 times larger (for |x| > 5, 5.7×10−7 and 0.147). Even with the same peak width, the Cauchy distribution keeps much of its probability in the tails. Fitting a spectral line shape with a Gaussian misses the intensity in these tails.

  2. Explain, using the partial-integral values in the readout, why the mean of the Cauchy distribution (γ = 1) does not exist.
    Sample answer

    Truncating ∫|x − x0| p(x) dx at ±R gives (γ/π) ln(1 + R2/γ2), which is 1.47, 2.93, 4.40, and 8.80 for R = 10, 100, 1000, and 106; it keeps growing by about 1.47 each time R is multiplied by 10 (logarithmic divergence). Because p(x) decreases only as 1/x2, the integral of x p(x) ~ 1/x does not converge.

  3. With the defaults of ② (n ≤ 104, 5 paths, seed 20261112, scale 1), how widely are the sample means at n = 104 scattered for the Gaussian and for the Cauchy? How does this correspond to theory?
    Sample answer

    The Gaussian values are −0.0035, 0.0006, −0.0087, 0.0075, 0.0137, which stay within about the theoretical standard deviation σ/√n = 0.01. The Cauchy values are 0.769, −1.83, 0.118, 1.35, −0.910, and 2 of the 5 paths have |x̄| > γ = 1 (around n = 103 the mean of path 1 exceeds 5; it becomes visible when the vertical-axis range is set to ± 10). The distribution of x̄n does not depend on n; it is Cauchy(x0, γ), and the probability of |x̄n − x0| > γ is always 1/2. The medians of the same samples are 0.0057, 0.0022, 0.0052, 0.0005, 0.0036, which lie within the range πγ/(2√n) = 0.0157.

  4. For the t distribution with ν = 1, 2, 3, 30, what happens to the mean and the variance?
    Sample answer

    ν = 1: neither the mean nor the variance exists (same as the Cauchy distribution). ν = 2: the mean μ exists but the variance diverges. ν = 3: variance s2ν/(ν − 2) = 3. ν = 30: 30/28 = 1.071, approaching the Gaussian distribution (variance 1).

7. Corresponding sections of the text and references

References (from the end of Chapter 3):

Back to the Probability Distribution Simulator | Machine Learning — From Probability to GPT (Contents) | Equations are typeset with the browser’s built-in MathML (no external library).