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 independent uniform values on , scaled to unit variance: a box for , a triangle for 2, and for 3 a smooth hump made of parabolas.
- Already at it's close to the Gaussian (dashed). That's the central limit theorem at work.
- The readout is the fraction outside for the Gaussian, which the sum approaches.
Predict
Twelve independent uniform values on are added and 6 is subtracted. What are the result's mean and standard deviation?
Mean 0 and standard deviation 1. Each uniform has mean and variance ; twelve of them have mean 6 and variance 1. Subtracting 6 centers it. By the central limit theorem the result is very nearly Gaussian, which made this a cheap way to generate Gaussian noise on early computers. Its one flaw: it can never exceed ±6.
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 : draw uniform and return . For the exponential, .
- Discrete:
rng.binomial(n, p, size)andrng.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 .
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 with .
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
- Binomial.
- Poisson with .
- 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 to . What count should it hold?
Show the solution
- Density at 0.
- Expected count. A bin of width 0.1 holds about samples, slightly fewer because the density falls a little across the bin.
- Compare with a run. One run gives 39 534.
- Estimate the density. Counts divided by 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 ?
Check
An exponential sampler with rate 2 uses . What does = 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 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?
Each value contributes its own small, independent nudge to the sum, and the sum's distribution is the convolution of all their distributions. Convolving smooths: a box convolved with a box is a triangle, the triangle with another box a curve made of parabolas, and each further convolution rounds the shape more. In the frequency domain, convolution is multiplication of characteristic functions, and the product of many functions that each start at 1 with zero slope and some curvature tends to , whose inverse is a Gaussian. Only the means and variances survive, as long as no single term dominates. That's why noise made of many small independent sources, like thermal noise, is Gaussian.
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.