Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsProbability and Statistics

Common distributions

P1.D340 min●○○ BeginnerReviewed Dec 1, 2026

Why this matters for machine learning

A model must say how its output is spread over the possible answers, and the named distributions are the standard choices: nearly every loss function in machine learning is the negative log of one of them.

  • Logistic regression gives the parameter pp of a Bernoulli distribution for each example, and its loss, binary cross-entropy, is the negative log of the probability given to the true label. (P3.A7: Logistic regression, coming soon)
  • A classifier outputs the parameters of a categorical distribution through softmax, and cross-entropy is the negative log of the probability of the true class. (P3.A8: Softmax regression, coming soon)
  • Linear regression with squared error is the same as assuming Gaussian noise around the prediction. (P3.A2: Linear regression, coming soon)
  • A language model predicts a categorical distribution over its vocabulary for the next token. (P10.A1: What is a Large Language Model?, coming soon)
  • Weight initialization draws the starting weights from a Gaussian or a uniform distribution, so two training runs are two draws from the same distribution. (P4.12: Weight initialization, coming soon)
  • Random search tries hyperparameters drawn uniformly from a range. (P4.23: Hyperparameter tuning, coming soon)

Before this: Random variables

After this lesson you can

  • Say what each of the Bernoulli, binomial, categorical, uniform, Gaussian and Poisson distributions models, write its PMF or density, and state its mean and variance
  • Compute binomial and Poisson probabilities with their formulas, and show with numbers that a binomial with large nn and p=λ/np=\lambda/n is close to a Poisson with parameter λ\lambda
  • Standardize a Gaussian value with z=(x−μ)/σz=(x-\mu)/\sigma, use the 68–95–99.7 rule with its exact values, and explain why a density can be taller than 1
  • Choose a distribution for a quantity (a yes/no label, a count out of nn, a class, a count with no upper limit, a real-valued noise) and name the loss it leads to: binary cross-entropy, cross-entropy or mean squared error

A spam filter answers yes or no. A web server counts how many requests arrive each second. A scale reports a weight with a small random error. All three are random variables (P1.D2), but they behave very differently: one has two values, one counts 0,1,2,…0,1,2,\dots with no upper limit, and one takes any real value.

Luckily, a handful of recipes cover most cases. Each is a PMF or density with one or two adjustable numbers, and each fits a typical kind of quantity. Learn the recipes, and “what is the distribution of this?” often becomes a short checklist.

Recipes with knobs: distributions and parameters

A named distribution is a rule that gives a PMF (for a discrete variable) or a density (for a continuous one) with a few adjustable numbers called parameters. Think of a recipe with a few knobs. The knobs decide which member of the family you get: a fair coin and a coin that lands heads 90%90\% of the time are the same recipe with two settings of one knob.

  • Parameters describe the distribution. They are fixed numbers, such as pp, nn, μ\mu, σ\sigma and λ\lambda below.
  • Values are what the random variable can take, such as 00, 11, 22 or 3.73.7.

“XX has the Bernoulli distribution with parameter pp” is written X∼Bernoulli(p)X\sim\mathrm{Bernoulli}(p). The symbol ∼\sim means “is distributed as”. Fitting a model to data (P1.D10) often means choosing the parameters that make the data most likely.

Each distribution below comes with its mean, the average value you see over many draws, and its variance, the average squared distance of a draw from the mean. The standard deviation is the square root of the variance, and it is in the same units as the values. P1.D4 shows how to compute the mean and variance in general. Here they are stated, and the gallery below checks them.

Bernoulli: one yes/no trial

A Bernoulli trial has two outcomes. Call them 11 (“success”) and 00 (“failure”). The word “success” is only a label: a spam email, a defective part or a click can all be the “success”. The distribution has one parameter, pp, the probability of 11:

P(X=1)=p,P(X=0)=1−p.\begin{aligned} \mathbb{P}(X=1)&=p,\\ \mathbb{P}(X=0)&=1-p. \end{aligned}

One formula covers both: P(X=x)=px(1−p)1−x\mathbb{P}(X=x)=p^{x}(1-p)^{1-x} for x∈{0,1}x\in\{0,1\}. Check: x=1x=1 gives p1(1−p)0=pp^1(1-p)^0=p, and x=0x=0 gives 1−p1-p.

  • Mean pp. If p=0.8p=0.8, then 80%80\% of many draws are 1s, so their average is 0.80.8.
  • Variance p(1−p)p(1-p). For p=0.8p=0.8 it is 0.8×0.2=0.160.8\times0.2=0.16, so the standard deviation is 0.40.4. The variance is largest, 0.250.25, at p=0.5p=0.5 (most uncertain), and it is 00 at p=0p=0 or p=1p=1, where the outcome is certain.
  • CDF. A staircase (P1.D2): F(x)=0F(x)=0 for x<0x\lt0, F(x)=1−pF(x)=1-p for 0≤x<10\le x\lt1 and F(x)=1F(x)=1 for x≥1x\ge1.

Bernoulli variables are everywhere in ML: the label of a spam example, whether a neuron is kept or dropped by dropout (P4.17), and whether a coin lands heads (P1.D1). A fair coin is Bernoulli(0.50.5).

Binomial: count the successes in nn trials

Flip a coin with P(heads)=0.3\mathbb{P}(\text{heads})=0.3 three times, and count the heads. What is the probability of exactly 22? List the sequences with two heads: HHT, HTH and THH. If the flips do not affect each other (this is independence, P1.D5), each sequence has probability 0.3×0.3×0.7=0.0630.3\times0.3\times0.7=0.063. There are 33 such sequences, so P(X=2)=3×0.063=0.189\mathbb{P}(X=2)=3\times0.063=0.189.

The same two steps work for any nn and kk.

  1. Every particular sequence with kk successes and n−kn-k failures has probability pk(1−p)n−kp^k(1-p)^{n-k}, because the trials multiply.
  2. The number of such sequences is the number of ways to choose which kk of the nn trials are successes, (nk)\binom nk (P1.D1).

So the binomial distribution, written X∼Binomial(n,p)X\sim\mathrm{Binomial}(n,p), has

P(X=k)=(nk) pk(1−p)n−k\mathbb{P}(X=k)=\binom nk\,p^k(1-p)^{n-k}

for k=0,1,…,nk=0,1,\dots,n.

For n=3n=3 and p=0.3p=0.3 the whole PMF is 0.3430.343, 0.4410.441, 0.1890.189 and 0.0270.027 for k=0,1,2,3k=0,1,2,3, and these add to 11. The binomial is the right model only under three conditions: a fixed number nn of trials, each trial a yes/no result with the same pp, and the trials independent. Dealing five cards from a deck and counting the hearts is not binomial: each card changes what is left in the deck, so the trials are not independent.

  • Mean npnp and variance np(1−p)np(1-p). A binomial is a sum of nn independent Bernoulli variables, so these are nn times the Bernoulli mean and variance (P1.D4 covers sums of random variables, and P1.D5 defines independence).
  • n=1n=1 is exactly Bernoulli(pp).
  • Shape. For p=0.5p=0.5 the bars are symmetric. For small pp the bars pile up near 00 with a long tail to the right, and for pp near 11 the pile is near nn with a tail to the left. For larger nn the shape becomes a smooth bell (see the Go-deeper box on the Gaussian approximation), and the spread relative to the mean shrinks: for p=0.5p=0.5, the standard deviation is 31.6%31.6\% of the mean at n=10n=10, 10%10\% at n=100n=100 and 3.2%3.2\% at n=1000n=1000.

Worked example. Take n=10n=10 and p=0.3p=0.3. Then P(X=3)=(103) 0.33 0.77=120×0.027×0.0824=0.2668\mathbb{P}(X=3)=\binom{10}{3}\,0.3^3\,0.7^7=120\times0.027\times0.0824=0.2668. The mean is np=3np=3, the variance is 10×0.3×0.7=2.110\times0.3\times0.7=2.1 and the standard deviation is 1.4491.449. The probability of 33 or fewer successes is P(X≤3)=0.6496\mathbb{P}(X\le3)=0.6496. With p=0.5p=0.5 and n=10n=10, P(X=5)=252/1024=0.2461\mathbb{P}(X=5)=252/1024=0.2461, the number you met in P1.D1.

Categorical: one of KK classes

When there are more than two outcomes, say the class of a photo (cat, dog or bird), the categorical distribution gives each class k=1,…,Kk=1,\dots,K a probability pkp_k, with every pk≥0p_k\ge0 and p1+⋯+pK=1p_1+\cdots+p_K=1. So P(X=k)=pk\mathbb{P}(X=k)=p_k. The model in P1.D1 that said cat 0.700.70, dog 0.200.20, bird 0.100.10 is a categorical distribution with K=3K=3. The parameters are the probabilities themselves, and because they add to 11, only K−1K-1 of them can be chosen freely.

A draw is usually recorded as a one-hot vector: a vector of KK numbers with a 11 at the drawn class and 00 everywhere else. Class 22 out of 33 is (0,1,0)(0,1,0). This is how classification labels enter a model (P3.A8).

  • Two classes is Bernoulli: call the classes 00 and 11, and p1p_1 plays the role of pp.
  • No mean. The class names 1,2,31,2,3 are labels, not quantities. “Dog” is not the average of “cat” and “bird”, so asking for the mean of the class number has no meaning. (If you give the classes numbers on purpose, such as star ratings, you can ask for that mean, but it is a choice you made.)
  • Where the probabilities come from. A network produces raw scores called logits, and softmax turns them into probabilities (P3.A8). The scores (2,1,0)(2,1,0) become (0.665, 0.245, 0.090)\bigl(0.665,\,0.245,\,0.090\bigr), which add to 11. The gallery below does exactly this.
  • Many draws. If you draw nn times independently and count how often each class appears, the vector of counts follows the multinomial distribution. With probabilities (0.7,0.2,0.1)(0.7,0.2,0.1) and n=10n=10, the counts (7,2,1)(7,2,1) have probability 10!7! 2! 1! 0.77 0.22 0.1=0.1186\frac{10!}{7!\,2!\,1!}\,0.7^7\,0.2^2\,0.1=0.1186. The binomial is the multinomial with K=2K=2. This is only a preview.

Uniform: every value is equally likely

The discrete uniform distribution on the whole numbers a,a+1,…,ba,a+1,\dots,b gives each of the m=b−a+1m=b-a+1 values the probability 1/m1/m. A fair die is the case a=1a=1, b=6b=6: each face has probability 16\frac16, the mean is a+b2=3.5\frac{a+b}2=3.5, and the variance is m2−112=3512=2.917\frac{m^2-1}{12}=\frac{35}{12}=2.917.

The continuous uniform distribution on [a,b][a,b], written X∼Uniform(a,b)X\sim\mathrm{Uniform}(a,b), has a flat density (P1.D2):

p(x)={1b−aa≤x≤b,0otherwise.p(x)=\begin{cases}\dfrac1{b-a} & a\le x\le b,\\[4pt] 0 & \text{otherwise.}\end{cases}

The height 1b−a\frac1{b-a} makes the area of the rectangle, height times width, equal to 11. The CDF is the ramp F(x)=x−ab−aF(x)=\frac{x-a}{b-a} on [a,b][a,b], so P(c≤X≤d)=d−cb−a\mathbb{P}(c\le X\le d)=\frac{d-c}{b-a} for a≤c≤d≤ba\le c\le d\le b: the probability is the length of the part you ask about, divided by the total length. The mean is a+b2\frac{a+b}2 and the variance is (b−a)212\frac{(b-a)^2}{12}.

For Uniform(0,2)\mathrm{Uniform}(0,2) the density is 0.50.5, the mean is 11, the variance is 412=0.3333\frac4{12}=0.3333, and P(0.5≤X≤1.5)=12\mathbb{P}(0.5\le X\le1.5)=\frac{1}{2}. For Uniform(0,0.5)\mathrm{Uniform}(0,0.5) the density is 22, taller than 11: a density is a probability per unit length, and the interval is short. Whether the end points are included does not matter, because a single point has probability 00.

In ML, the random number generator of a computer produces numbers that behave like draws from Uniform(0,1)\mathrm{Uniform}(0,1), and other random draws are built from them (P1.D2, P1.D9). Random search for hyperparameters draws each setting uniformly from a range (P4.23).

Gaussian: the bell curve

The Gaussian (or normal) distribution is a continuous distribution with two parameters, the mean μ\mu (the centre) and the standard deviation σ\sigma (the width, with σ>0\sigma\gt0). Its variance is σ2\sigma^2, and we write X∼N(μ,σ2)X\sim\mathcal{N}(\mu,\sigma^2). The density is

p(x)=1σ2πexp⁡ ⁣(−(x−μ)22σ2).p(x)=\frac{1}{\sigma\sqrt{2\pi}}\exp\!\left(-\frac{(x-\mu)^2}{2\sigma^2}\right).
  • Shape. The curve is symmetric about μ\mu, which is also its highest point. Changing μ\mu slides the curve left or right. Changing σ\sigma stretches it: a larger σ\sigma gives a wider, flatter bell.
  • The constant. The factor 1σ2π\frac1{\sigma\sqrt{2\pi}} makes the total area 11. For σ=1\sigma=1 the area under e−x2/2\mathrm{e}^{-x^2/2} is 2π=2.5066\sqrt{2\pi}=2.5066 (P1.C9), and dividing by it gives the peak height 12π=0.3989\frac1{\sqrt{2\pi}}=0.3989 for the standard Gaussian N(0,1)\mathcal{N}(0,1).
  • The height can exceed 1. The peak height is 1σ2π\frac1{\sigma\sqrt{2\pi}}, so for σ=0.25\sigma=0.25 it is 1.59581.5958. This is fine, for the same reason as for the uniform density: the area is still 11, because the curve is very narrow. A density is not a probability.

Standardizing. If X∼N(μ,σ2)X\sim\mathcal{N}(\mu,\sigma^2), the number z=x−μσz=\frac{x-\mu}{\sigma} counts how many standard deviations xx is above the mean, and Z=X−μσZ=\frac{X-\mu}\sigma has the standard Gaussian distribution N(0,1)\mathcal{N}(0,1). So one table or one function, for the standard Gaussian, answers every question about every Gaussian. If exam scores are N(70,102)\mathcal{N}(70,10^2), a score of 8585 has z=1.5z=1.5.

The CDF. The CDF of the standard Gaussian is written Φ(z)\Phi(z). As P1.C9 and P1.D2 noted, the area under the bell curve has no formula made from the usual elementary functions, so Φ\Phi is computed numerically through the error function: Φ(z)=12(1+erf⁡(z/2))\Phi(z)=\frac12\bigl(1+\operatorname{erf}(z/\sqrt2)\bigr). In Python, math.erf does it. The values to remember are Φ(0)=0.5\Phi(0)=0.5 and Φ(1)=0.8413\Phi(1)=0.8413. For any Gaussian, P(X≤x)=Φ(x−μσ)\mathbb{P}(X\le x)=\Phi\bigl(\frac{x-\mu}{\sigma}\bigr). For the exam scores, P(X≤85)=Φ(1.5)=0.9332\mathbb{P}(X\le85)=\Phi(1.5)=0.9332.

The 68–95–99.7 rule. The probability of landing within 11, 22 or 33 standard deviations of the mean is the same for every Gaussian:

  • within μ±σ\mu\pm\sigma: 0.68270.6827, so the rest, 0.31730.3173, is split evenly between the two tails
  • within μ±2σ\mu\pm2\sigma: 0.95450.9545
  • within μ±3σ\mu\pm3\sigma: 0.99730.9973, so the chance of being farther than 3σ3\sigma away is 0.00270.0027, about 11 in 370370

The rounded numbers 6868, 9595 and 99.799.7 percent are easy to remember, but the exact ones matter when it counts: 95%95\% is reached at 1.961.96 standard deviations, not 22 (which gives 0.95450.9545).

Tails. The curve falls very fast: the chance of landing more than 4σ4\sigma above the mean is 0.00003170.0000317 and more than 5σ5\sigma above is about 0.000000290.00000029. Yet it never reaches 00, so any real number has a positive density, and a Gaussian model for a quantity that cannot be negative (a price, a height) puts a little probability on impossible values. Real data also often show more extreme values than a Gaussian predicts, so a Gaussian is an assumption to check, not a fact.

Why the Gaussian is everywhere. The sum or average of many small independent effects tends to look Gaussian whatever the shape of the individual effects, as long as their variance is finite and no single effect dominates (the central limit theorem, P1.D8). Extremely heavy-tailed effects, such as those of the Cauchy distribution, do not follow this rule. Measurement error is a typical case. The multivariate version of the Gaussian, with an ellipse for its shape, is the subject of P1.D7.

Poisson: counting events at a steady rate

Suppose a server receives on average λ=3\lambda=3 requests per second. In one second it may see 00, 11, 22 or more, and there is no upper limit. The Poisson distribution counts events in a fixed interval of time or space. It has one parameter, λ>0\lambda\gt0, the average number of events per interval:

P(X=k)=e−λλkk!\mathbb{P}(X=k)=\frac{\mathrm{e}^{-\lambda}\lambda^k}{k!}

for k=0,1,2,…k=0,1,2,\dots

For λ=3\lambda=3 the probabilities of k=0,1,2,3,4k=0,1,2,3,4 are 0.04980.0498, 0.14940.1494, 0.22400.2240, 0.22400.2240 and 0.16800.1680. The probabilities add to 11 because ∑kλk/k!=eλ\sum_k\lambda^k/k!=\mathrm{e}^{\lambda} (the series for eλ\mathrm{e}^\lambda), which cancels the factor e−λ\mathrm{e}^{-\lambda}.

  • Mean and variance are both λ\lambda. This is a signature of the Poisson. If counts have a variance far larger than their mean, the Poisson is a poor model for them.
  • When to use it. The events must be independent of each other, arrive at a steady average rate, and not come in clumps. Requests from many unrelated users and typos in a long text are typical.
  • A limit of the binomial. Cut the interval into nn tiny slots. If each slot has a chance p=λ/np=\lambda/n of containing one event, the number of events is Binomial(n,λ/nn,\lambda/n), with mean np=λnp=\lambda. As nn grows the slots shrink, and the binomial approaches the Poisson. With λ=3\lambda=3 and k=2k=2, the binomial gives 0.23350.2335 for n=10n=10, 0.22520.2252 for n=100n=100, 0.22420.2242 for n=1000n=1000, and the Poisson gives 0.22400.2240. The variance n p(1−p)=λ(1−λ/n)n\,p(1-p)=\lambda(1-\lambda/n) approaches λ\lambda too.
  • Waiting times. If events arrive like this, the time to wait for the next one has the exponential distribution with density λe−λx\lambda\mathrm{e}^{-\lambda x} for x≥0x\ge0 and mean 1λ\frac1\lambda. For λ=1\lambda=1 it is the e−x\mathrm{e}^{-x} of P1.C9 and P1.D2. With λ=3\lambda=3 per hour the mean wait is 2020 minutes, and the probability of no event in half an hour is e−1.5=0.2231\mathrm{e}^{-1.5}=0.2231, which is also P(X=0)\mathbb{P}(X=0) for a Poisson with λ=1.5\lambda=1.5.
Go deeper Why the binomial turns into the Poisson

Write the binomial probability with p=λ/np=\lambda/n and rearrange:

(nk)(λn)k(1−λn)n−k=λkk!⋅n(n−1)⋯(n−k+1)nk⋅(1−λn)n⋅(1−λn)−k.\begin{aligned} &\binom nk\Bigl(\frac\lambda n\Bigr)^k\Bigl(1-\frac\lambda n\Bigr)^{n-k}\\ &=\frac{\lambda^k}{k!}\cdot\frac{n(n-1)\cdots(n-k+1)}{n^k}\\ &\quad\cdot\Bigl(1-\frac\lambda n\Bigr)^{n}\cdot\Bigl(1-\frac\lambda n\Bigr)^{-k}. \end{aligned}

Hold kk and λ\lambda fixed and let n→∞n\to\infty. The fraction n(n−1)⋯(n−k+1)nk\frac{n(n-1)\cdots(n-k+1)}{n^k} is a product of kk factors, each close to 11, so it goes to 11. The last factor goes to 11 too. The factor (1−λn)n\bigl(1-\frac\lambda n\bigr)^n goes to e−λ\mathrm{e}^{-\lambda}, the same limit as in compound interest (P1.A2). What is left is λkk!e−λ\frac{\lambda^k}{k!}\mathrm{e}^{-\lambda}, the Poisson PMF.

Go deeper Approximating a binomial by a Gaussian

For large nn with pp not too close to 00 or 11, the binomial bars follow a Gaussian curve with the same mean npnp and variance np(1−p)np(1-p). For n=100n=100 and p=0.5p=0.5 the Gaussian has μ=50\mu=50 and σ=5\sigma=5. At k=50k=50 the exact binomial value is 0.07960.0796, and the Gaussian density gives 0.07980.0798. The largest gap between the bars and the curve over all kk is 0.00020.0002.

A binomial value is a whole number but the Gaussian is continuous, so for a sum of bars it helps to treat each bar kk as the interval from k−12k-\frac12 to k+12k+\frac12. This is called the continuity correction. To find P(X≤55)\mathbb{P}(X\le55), the exact value is 0.86440.8644. The Gaussian with the correction gives Φ(55.5−505)=Φ(1.1)=0.8643\Phi\bigl(\frac{55.5-50}5\bigr)=\Phi(1.1)=0.8643, and without it Φ(1)=0.8413\Phi(1)=0.8413. Rules of thumb for when the approximation is good differ between books. A common one asks that npnp and n(1−p)n(1-p) are both at least about 1010. This is the first sign of the central limit theorem (P1.D8).

Pick a distribution and move its sliders. The chart shows the probability of every value (or the density), the dashed line marks the mean, and the dark bracket marks the mean plus and minus one standard deviation. The readouts below the chart give the mean, the variance and a probability you choose.

A gallery of seven distributions: Bernoulli, binomial, categorical, uniform on whole numbers, uniform on real numbers, Gaussian and Poisson. Choose one, set its parameters with the sliders, and read the mean, variance and standard deviation. For the discrete ones you pick a value k to read P(X = k) and P(X ≤ k). For the Gaussian and the continuous uniform you shade an interval, and the area is its probability. For the Gaussian the interval ends are given in standard deviations z from the mean. The categorical chart takes logits and turns them into probabilities with softmax. The binomial can be compared with a Poisson or a Gaussian approximation, and the Poisson with a binomial. The three buttons at the bottom set up examples.

Things to try with the gallery:

  1. Choose Binomial with n=10n=10, p=0.3p=0.3 and k=3k=3. The readouts give P(X=3)=0.2668\mathbb{P}(X=3)=0.2668, mean 33, variance 2.12.1 and P(X≤3)=0.6496\mathbb{P}(X\le3)=0.6496. Slide pp to 0.50.5 and the bars become symmetric around 55. Slide pp to 00 and then to 11: all the probability sits on one bar and the variance is 00.
  2. Press Rare events (Binomial(100,0.03100,0.03) with the Poisson overlay). The dots sit almost on top of the bars, and the largest gap is 0.00340.0034. Then set n=10n=10 and p=0.5p=0.5 with the Poisson overlay: the gap grows to 0.07060.0706, because pp is not small.
  3. Press Large n (Binomial(100,0.5100,0.5) with the Gaussian overlay). The curve follows the tops of the bars, with a largest gap of 0.00020.0002.
  4. Choose Gaussian and press ±1σ\pm1\sigma, ±2σ\pm2\sigma, ±3σ\pm3\sigma. The shaded probability is 0.68270.6827, 0.95450.9545, 0.99730.9973. Change μ\mu and σ\sigma and watch that these three numbers do not change.
  5. Press Narrow Gaussian (σ=0.25\sigma=0.25). The curve is taller than 11: the highest density is 1.59581.5958, and the area is still 11.
  6. Choose Poisson and set λ=3\lambda=3. The bars at k=2k=2 and k=3k=3 are both 0.22400.2240, and the mean and variance are both 33. Turn on the Binomial overlay, which starts at n=20n=20, and raise nn to 200200 to watch the gap shrink.
  7. Choose Categorical with the logits (2,1,0)(2,1,0). The probabilities are 0.66520.6652, 0.24470.2447, 0.09000.0900. Raise one logit and the other two probabilities fall, because the total stays 11.
  8. Choose Uniform (any real number) with a=0a=0 and b=2b=2, and shade [0.5,1.5][0.5,1.5]. The probability is 0.50.5.

Intuition

Think of each distribution as a shape template that you adjust with its knobs. Bernoulli is two bars. Binomial is a hill of bars centred on npnp. Categorical is KK bars that must share one unit of probability. Uniform is a flat shelf. Gaussian is the smooth bell. Poisson is a hill that starts at 00 with a tail to the right.

The analogies break where the knobs interact. In the binomial, pp moves both the centre and the skew. In the Gaussian, μ\mu and σ\sigma move the centre and the width separately, which is one reason it is so convenient.

Math

Mean and variance of each distribution (P1.D4 shows how to get them):

  • Bernoulli(pp): mean pp, variance p(1−p)p(1-p).
  • Binomial(n,pn,p): mean npnp, variance np(1−p)np(1-p).
  • Discrete uniform on a,…,ba,\dots,b: mean a+b2\frac{a+b}2, variance (b−a+1)2−112\frac{(b-a+1)^2-1}{12}.
  • Uniform(a,ba,b): mean a+b2\frac{a+b}2, variance (b−a)212\frac{(b-a)^2}{12}.
  • Gaussian N(μ,σ2)\mathcal{N}(\mu,\sigma^2): mean μ\mu, variance σ2\sigma^2.
  • Poisson(λ\lambda): mean λ\lambda, variance λ\lambda.
  • Exponential(λ\lambda): mean 1λ\frac1\lambda.
  • Categorical: no mean for class names.

CDFs: Bernoulli is a staircase, the uniform is a ramp x−ab−a\frac{x-a}{b-a}, the Gaussian is Φ(x−μσ)=12(1+erf⁡(x−μσ2))\Phi\bigl(\frac{x-\mu}\sigma\bigr)=\frac12\Bigl(1+\operatorname{erf}\bigl(\frac{x-\mu}{\sigma\sqrt2}\bigr)\Bigr), and the binomial and Poisson CDFs are sums of their PMFs, P(X≤k)=∑j≤kP(X=j)\mathbb{P}(X\le k)=\sum_{j\le k}\mathbb{P}(X=j).

Code

Plain Python is enough for all the PMFs, because math.comb and math.erf exist.

from math import comb, erf, exp, factorial, sqrt

binom = lambda n, p, k: comb(n, k) * p**k * (1 - p)**(n - k)
poisson = lambda lam, k: exp(-lam) * lam**k / factorial(k)
phi = lambda z: 0.5 * (1 + erf(z / sqrt(2)))

print(binom(10, 0.3, 3), poisson(3, 2), phi(1))   # 0.2668..., 0.2240..., 0.8413...

Choosing a distribution

To choose, look at the quantity, not at a table of distributions.

  • Two outcomes (yes/no, 0/1): Bernoulli.
  • Number of successes among nn trials, with the same pp each time and independent trials: binomial.
  • One of KK classes: categorical. A next token out of a vocabulary is a categorical draw with a very large KK.
  • A count with no fixed upper limit, from independent events arriving at a steady average rate: Poisson.
  • A real number with noise around a centre: Gaussian.
  • A real number in a known range with no preference: uniform.

Every one of these is an assumption about the world, and the checks are as important as the choice. A binomial needs independent trials with the same pp. A Poisson needs a variance close to its mean. A Gaussian needs a quantity that is not strongly skewed or full of extreme values. When the check fails, other distributions exist, and the same ideas (a PMF or density, a few parameters) carry over.

Where these appear in a loss function

Each choice of distribution leads to a loss function, which is the negative log of the probability the model gave to what really happened. This section only previews it. P1.D10 develops the idea and P3.A2, P3.A7 and P3.A8 use it.

  • Bernoulli and binary cross-entropy. A model gives p^=0.8\hat p=0.8 for an email being spam, a Bernoulli parameter. If the email is spam (y=1y=1), the model gave the truth probability 0.80.8, and the loss is −ln⁡0.8=0.2231-\ln0.8=0.2231. If it is not spam (y=0y=0), the truth got 1−0.8=0.21-0.8=0.2, and the loss is −ln⁡0.2=1.6094-\ln0.2=1.6094. In one formula, L=−[yln⁡p^+(1−y)ln⁡(1−p^)]L=-\bigl[y\ln\hat p+(1-y)\ln(1-\hat p)\bigr]. For three examples with labels (1,0,1)(1,0,1) and predictions (0.8,0.3,0.6)(0.8,0.3,0.6) the average loss is 0.36350.3635 (P3.A7).
  • Categorical and cross-entropy. A classifier gives (0.7,0.2,0.1)(0.7,0.2,0.1) and the true class is the first. The loss is −ln⁡0.7=0.3567-\ln0.7=0.3567. Only the probability of the true class matters. Next-token prediction in a language model is the same, with the vocabulary as the classes (P3.A8, P10.A1).
  • Gaussian and mean squared error. Suppose the model says the target is y∼N(y^,σ2)y\sim\mathcal{N}(\hat y,\sigma^2). The negative log of the density is (y−y^)22σ2+ln⁡(σ2π)\frac{(y-\hat y)^2}{2\sigma^2}+\ln\bigl(\sigma\sqrt{2\pi}\bigr). With σ=1\sigma=1, y=3y=3 and y^=2.5\hat y=2.5 it is 0.125+0.9189=1.04390.125+0.9189=1.0439: half the squared error plus a constant that does not depend on the prediction. So choosing the predictions that minimize the total of these losses is the same as minimizing the total squared error (P3.A2).
  • Gaussian and uniform initial weights. Training starts from random weights, drawn from a Gaussian or a uniform distribution whose width depends on the layer size (P4.12).
  • Poisson for counts. Counts such as clicks per day can be modelled with a Poisson whose λ\lambda is predicted by the model. Because λ\lambda must be positive, the model output is usually passed through an exponential.

Try it: PMFs, simulations and the Poisson limit

The first cell computes everything exactly with math.comb and math.erf, so every run prints the same numbers. The second cell simulates with a fixed seed and compares the results with the theory. Predict the Poisson numbers before you run it.

Try it: exact probabilities

Runs real Python in your browser. First run downloads about 6 MB (once; your browser keeps it).

Challenge: add the Poisson check for another rate, λ=0.5\lambda=0.5, and find the smallest nn for which the largest gap to the binomial falls below 0.0010.001.

Try it: simulate the distributions

Runs real Python in your browser. First run downloads about 6 MB (once; your browser keeps it).

Challenge: change the seed and rerun. Which numbers move, and by how much? How many draws would you need before the variance matches the theory to two decimals?

With seed 11 the second cell printed a mean of 3.03.0 and a variance of 2.122.12 for the binomial, and the Gaussian shares inside 11, 22 and 33 standard deviations were 0.68200.6820, 0.95450.9545 and 0.99750.9975. Other seeds give slightly different numbers.

Common mistakes

Check your understanding

  1. You flip a fair coin 88 times and count the heads. Which distribution fits, and with which parameters?

  2. For X∼Binomial(10,0.3)X\sim\mathrm{Binomial}(10,0.3), what is the variance?

  3. Exam scores are N(100,152)\mathcal{N}(100,15^2). What is the zz-score of a score of 130130?

  4. A Gaussian with σ=0.25\sigma=0.25 has its peak density at 1.5961.596. What does that tell you?

  5. Calls arrive at an average rate of 22 per minute, as a Poisson process. What is the probability of no calls in a given minute?

Exercises

  1. A Bernoulli variable has p=0.2p=0.2. Find its mean, variance and standard deviation.
    Show answerThe mean is 0.20.2, the variance is 0.2×0.8=0.160.2\times0.8=0.16, and the standard deviation is 0.16=0.4\sqrt{0.16}=0.4.
  2. A multiple-choice quiz has 55 questions and a student guesses each with success probability 0.40.4, independently. Find the probability of exactly 22 right answers and of at least one right answer.
    Show answerP(X=2)=(52) 0.42 0.63=10×0.16×0.216=0.3456\mathbb{P}(X=2)=\binom52\,0.4^2\,0.6^3=10\times0.16\times0.216=0.3456. At least one: 1−0.65=1−0.07776=0.92221-0.6^5=1-0.07776=0.9222.
  3. What is the probability of exactly 55 heads in 1010 fair flips, and of exactly 33 sixes in 1010 rolls of a fair die?
    Show answer(105) 0.510=252/1024=0.2461\binom{10}5\,0.5^{10}=252/1024=0.2461. For the die, (103) (1/6)3 (5/6)7=0.1550\binom{10}3\,(1/6)^3\,(5/6)^7=0.1550.
  4. A categorical distribution has probabilities (0.5,0.3,0.2)(0.5,0.3,0.2). If three independent draws are made, what is the probability of the sequence class 1, class 1, class 3? What is the one-hot vector of class 3?
    Show answerThe draws are independent, so the probability is 0.5×0.5×0.2=0.050.5\times0.5\times0.2=0.05. The one-hot vector of class 3 is (0,0,1)(0,0,1).
  5. For X∼Uniform(2,6)X\sim\mathrm{Uniform}(2,6), find the density, the mean, the variance and P(3≤X≤5)\mathbb{P}(3\le X\le5).
    Show answerThe density is 14=0.25\frac14=0.25. The mean is 44, and the variance is 4212=1.3333\frac{4^2}{12}=1.3333. P(3≤X≤5)=24=0.5\mathbb{P}(3\le X\le5)=\frac{2}{4}=0.5.
  6. Gaussian weights of apples are N(500,502)\mathcal{N}(500,50^2) grams. Find the probability that a weight is between 450450 and 550550 grams, between 400400 and 600600 grams, and above 600600 grams.
    Show answerThe interval 450450 to 550550 is μ±σ\mu\pm\sigma, so 0.68270.6827. The interval 400400 to 600600 is μ±2σ\mu\pm2\sigma, so 0.95450.9545. Above 600600 is z=2z=2, and half of the remaining 1−0.95451-0.9545 lies in the upper tail: 0.02280.0228.
  7. A Gaussian has μ=70\mu=70 and σ=8\sigma=8. Find the zz-score of 8282, and the value with z=−2z=-2.
    Show answerz=(82−70)/8=1.5z=(82-70)/8=1.5. For z=−2z=-2, x=μ+zσ=70−16=54x=\mu+z\sigma=70-16=54.
  8. Let X∼Poisson(2)X\sim\mathrm{Poisson}(2). Find P(X=0)\mathbb{P}(X=0), P(X≥1)\mathbb{P}(X\ge1) and P(X≤2)\mathbb{P}(X\le2).
    Show answerP(X=0)=e−2=0.1353\mathbb{P}(X=0)=\mathrm{e}^{-2}=0.1353. P(X≥1)=1−0.1353=0.8647\mathbb{P}(X\ge1)=1-0.1353=0.8647. P(X≤2)=e−2(1+2+2)=5e−2=0.6767\mathbb{P}(X\le2)=\mathrm{e}^{-2}(1+2+2)=5\mathrm{e}^{-2}=0.6767.
  9. Each of 10001000 items is faulty with probability 0.0020.002, independently. Estimate the probability of exactly 33 faults with a Poisson, and compare with the exact binomial value.
    Show answerTake λ=np=2\lambda=np=2. The Poisson gives e−223/3!=0.1804\mathrm{e}^{-2}2^3/3!=0.1804. The exact binomial value is (10003) 0.0023 0.998997=0.1806\binom{1000}{3}\,0.002^3\,0.998^{997}=0.1806. They differ by only about 0.00020.0002 because nn is large and pp small.
  10. A server logs on average 1010 requests per minute, but the variance of the counts is 4040. Is a Poisson model good?
    Show answerNo. A Poisson has variance equal to its mean, so it would give variance 1010. A variance four times the mean means the counts are much more spread out, for example because the requests arrive in bursts.
  11. A model gives p^=0.9\hat p=0.9 of being spam. Find the binary cross-entropy loss when the email is spam and when it is not.
    Show answerSpam (y=1y=1): −ln⁡0.9=0.1054-\ln0.9=0.1054. Not spam (y=0y=0): −ln⁡0.1=2.3026-\ln0.1=2.3026. Being confidently wrong is penalized much more heavily.
  12. A Gaussian has σ=0.1\sigma=0.1. What is its peak density, and why is it not a problem that it exceeds 11?
    Show answer10.12π=3.989\frac1{0.1\sqrt{2\pi}}=3.989. A density is probability per unit of xx, and the curve is only a few tenths wide, so its area is still 11.

Next: P1.D4 shows how the mean and variance stated here are computed, and why the mean of a sum is the sum of the means. P1.D5 defines independence and studies joint and conditional distributions. P1.D7 extends the Gaussian to several variables, P1.D8 explains why Gaussians are everywhere, and P1.D10 turns “which parameters fit my data?” into maximum likelihood.

Key takeaways

  • A named distribution is a ready-made recipe for a PMF or a density with a few parameters. The parameters are fixed numbers that describe the distribution (pp, nn, μ\mu, σ\sigma, λ\lambda). The values are what the random variable takes. X∼Bernoulli(p)X\sim\mathrm{Bernoulli}(p) reads “XX has the Bernoulli distribution with parameter pp”.
  • Bernoulli (parameter pp) is one yes/no trial: P(X=1)=p\mathbb{P}(X=1)=p, mean pp, variance p(1−p)p(1-p). Binomial (parameters n,pn,p) counts the successes in nn independent trials with the same pp; it has mean npnp, variance np(1−p)np(1-p) and the probabilities P(X=k)=(nk)pk(1−p)n−k\mathbb{P}(X=k)=\binom nk p^k(1-p)^{n-k} for k=0,…,nk=0,\dots,n. Categorical is one draw from KK classes with probabilities p1,…,pKp_1,\dots,p_K that add to 1 (a one-hot vector records the draw); its class names are not numbers, so it has no mean.
  • Uniform gives every value the same chance: 1/m1/m for mm whole numbers, and the constant density 1/(b−a)1/(b-a) on [a,b][a,b] (mean (a+b)/2(a+b)/2, variance (b−a)2/12(b-a)^2/12). A density is not a probability, so it can be above 1: the uniform density on [0,0.5][0,0.5] has height 2.
  • Gaussian N(μ,σ2)\mathcal{N}(\mu,\sigma^2) has density 1σ2πe−(x−μ)2/(2σ2)\frac{1}{\sigma\sqrt{2\pi}}\mathrm{e}^{-(x-\mu)^2/(2\sigma^2)}. The shape is set by z=(x−μ)/σz=(x-\mu)/\sigma: the probabilities of lying within 11, 22, 33 standard deviations of μ\mu are 0.68270.6827, 0.95450.9545, 0.99730.9973. Its CDF has no elementary formula, so it is computed through the error function: Φ(z)=12(1+erf⁡(z/2))\Phi(z)=\frac12\bigl(1+\operatorname{erf}(z/\sqrt2)\bigr).
  • Poisson (parameter λ\lambda) counts independent events that arrive at a steady average rate, in a fixed interval: P(X=k)=e−λλk/k!\mathbb{P}(X=k)=\mathrm{e}^{-\lambda}\lambda^k/k!, with mean and variance both λ\lambda and no upper limit. It is the limit of Binomial(n,λ/nn,\lambda/n) as nn grows. For λ=3\lambda=3, P(X=2)=0.2240\mathbb{P}(X=2)=0.2240, and the binomial values are 0.23350.2335, 0.22520.2252, 0.22420.2242 for n=10,100,1000n=10,100,1000.
  • To pick a distribution, ask what the quantity is: two outcomes (Bernoulli), successes out of nn (binomial), one of KK classes (categorical), a count with no upper limit (Poisson), a real number with noise around a centre (Gaussian). The negative log of the model’s probability for the observed value gives the standard losses: binary cross-entropy, cross-entropy and, for a Gaussian with fixed σ\sigma, squared error (previewed here; P1.D10 develops it).
Found a mistake? Tell us