A1119 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

A11 · Lesson 19 · Lab: distributions in code

Sampling, histograms, the central limit theorem and extremes

Lessons 9 to 18 introduced the standard distributions and what happens when random values are transformed, summed and maximized. In this lab you'll generate them yourself: build a Gaussian from twelve uniforms, as early computers did, make exponential samples from uniform ones, compare the binomial with its Poisson approximation, estimate a density from a histogram, and measure the largest of many values against the formula.

  • The curve is the density of the sum of n independent uniform values on [−12,12], scaled to unit variance: a box for n=1, a triangle for 2, and for 3 a smooth hump made of parabolas.
  • Already at n=3 it's close to the Gaussian (dashed). That's the central limit theorem at work.
  • The readout is the fraction outside ±2 for the Gaussian, which the sum approaches.

Predict

Twelve independent uniform values on [0,1) are added and 6 is subtracted. What are the result's mean and standard deviation?

Name it

Making samples

  • Uniform: rng.random(n).
  • Gaussian: rng.normal(mu, sigma, n); or, from first principles, a sum of uniforms, or the Box–Muller transform.
  • Any distribution with an invertible CDF F: draw U uniform and return F−1(U). For the exponential, F−1(u)=−ln⁡(1−u)λ.
  • Discrete: rng.binomial(n, p, size) and rng.poisson(lam, size).

Checking samples

  • Compare the sample mean and standard deviation with the formulas.
  • A histogram, with counts divided by the number of samples and the bin width, estimates the density.
  • Tail fractions test the shape where approximations fail first: the twelve-uniform Gaussian has slightly too few values beyond ±2.

Exact probabilities by summation

math.comb(n, k) gives binomial coefficients exactly, so binomial probabilities can be summed directly and compared with the Poisson approximation e−λλkk! with λ=np.

Worked example

Compare the binomial and Poisson probabilities of at most 2 errors in 1000 bits at a BER of 0.003.

Show the solution
  1. Binomial. P=∑k=02(1000k)0.003k0.9971000−k=0.42285
  2. Poisson with λ=3. P=e−3(1+3+4.5)=0.42319
  3. Compare. They agree to 0.1%: with many trials and a small probability, the Poisson form is all you need.

Answer the binomial gives 0.42285 and the Poisson 0.42319, agreeing to 0.1%.

Worked example

A histogram of a million standard Gaussian samples has a bin from −0.05 to 0.05. What count should it hold?

Show the solution
  1. Density at 0. 12π=0.399
  2. Expected count. A bin of width 0.1 holds about 0.0399×106=39900 samples, slightly fewer because the density falls a little across the bin.
  3. Compare with a run. One run gives 39 534.
  4. Estimate the density. Counts divided by N× width estimate the density, 0.395 here.

Answer the bin should hold a little under 39 900 samples.

Check

What is the standard deviation of the sum of 24 independent uniform values on [0,1)?

Check

An exponential sampler with rate 2 uses y=−ln⁡(1−u)λ. What does u = 0.5 give?

Check

Make 10⁶ samples by adding 12 uniforms and subtracting 6 (rng = np.random.default_rng(1)). Set sd to their standard deviation and tail to the fraction beyond ±2.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Make 10⁵ exponential samples with rate 2 by the inverse-CDF method (np.random.default_rng(2)). Set mean and frac (the fraction below the mean, 0.5).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Set binom to the exact probability of at most 2 errors in 1000 bits at BER 0.003 (use math.comb), and poisson to the Poisson approximation.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Estimate the standard Gaussian density at 0 from 10⁶ samples (np.random.default_rng(3)), using the fraction in [−0.025,0.025] divided by the bin width. Set dens.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Draw 10⁵ sets of 9 uniform values (np.random.default_rng(4)), take the largest of each, and set mean_max and below (the fraction of maxima below 0.9).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does adding up many independent random values give a Gaussian, whatever their own distribution?

Practice

Keep practicing

4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.