TExcelTAD
Home › Math for Machine Learning › Discrete and Continuous Distributions

Discrete and Continuous Distributions

A class label takes one of a few values; a house price can be any real number. The first is described by a probability mass function, the second by a probability density function. Both have a cumulative distribution function. Knowing which one you have prevents classic mistakes, like reading a density value of 3 as "probability 3".

Discrete: the pmf

For a discrete random variable, p(x)=P(X=x)p(x) = P(X = x) with p(x)≥0p(x) \ge 0 and ∑xp(x)=1\sum_x p(x) = 1. Workhorses in ML:

Distribution pmf Models
Bernoulli(μ\mu) μx(1−μ)1−x\mu^x(1 - \mu)^{1-x}, x∈{0,1}x \in \{0, 1\} a binary label
Categorical(π\boldsymbol{\pi}) πk\pi_k for class kk a multi-class label, a next token
Binomial(NN, μ\mu) (Nx)μx(1−μ)N−x\binom{N}{x}\mu^x(1 - \mu)^{N-x} number of successes in NN trials

Continuous: the pdf

For a continuous random variable, P(X=x)=0P(X = x) = 0 for every single value. Probabilities come from integrating a density p(x)≥0p(x) \ge 0 with ∫p(x) dx=1\int p(x)\,dx = 1:

P(a≤X≤b)=∫abp(x) dxP(a \le X \le b) = \int_a^b p(x)\,dx

A density can exceed 1: the uniform density on [0,0.25][0, 0.25] is 4 everywhere on that interval. Only areas are probabilities. Common densities: uniform, Gaussian, exponential λe−λx\lambda e^{-\lambda x} for x≥0x \ge 0, Beta (for probabilities themselves).

The cdf

Both kinds have a cumulative distribution function F(x)=P(X≤x)F(x) = P(X \le x), non-decreasing from 0 to 1. For a continuous variable F′(x)=p(x)F'(x) = p(x). The inverse cdf turns uniform random numbers into samples of any distribution (inverse transform sampling).

python · try it yourself
import numpy as np

lam = 2.0                                     # exponential distribution, rate 2
pdf = lambda x: lam * np.exp(-lam * x)
cdf = lambda x: 1 - np.exp(-lam * x)
# probability as an area: trapezoid rule vs the cdf
x = np.linspace(0.5, 1.5, 10001)
area = np.sum((pdf(x[1:]) + pdf(x[:-1])) / 2 * np.diff(x))
print(f"P(0.5 <= X <= 1.5): integral {area:.6f}, cdf {cdf(1.5) - cdf(0.5):.6f}")
print("density at 0:", pdf(0.0), "(a density, not a probability)")
# inverse transform sampling: X = -log(1 - U) / lam
rng = np.random.default_rng(0)
u = rng.uniform(size=200000)
samples = -np.log(1 - u) / lam
print("sample mean:", round(samples.mean(), 4), " theory 1/lambda =", 1 / lam)
P(0.5 <= X <= 1.5): integral 0.318092, cdf 0.318092
density at 0: 2.0 (a density, not a probability)
sample mean: 0.4984  theory 1/lambda = 0.5

Joint, marginal and conditional

With two variables X,YX, Y, the joint distribution p(x,y)p(x, y) describes them together. The marginal p(x)p(x) sums (or integrates) out yy, and the conditional p(y∣x)=p(x,y)/p(x)p(y \mid x) = p(x, y)/p(x) describes YY once XX is known. A table makes it concrete:

python · try it yourself
import numpy as np

# joint pmf of weather (rows: sun, rain) and umbrella (cols: no, yes)
P = np.array([[0.56, 0.04],
              [0.08, 0.32]])
print("total:", P.sum())
p_weather = P.sum(axis=1); p_umbrella = P.sum(axis=0)
print("marginal weather (sun, rain):", p_weather)
print("marginal umbrella (no, yes):", p_umbrella)
print("P(rain | umbrella) =", round(P[1, 1] / p_umbrella[1], 4))
total: 1.0
marginal weather (sun, rain): [0.6 0.4]
marginal umbrella (no, yes): [0.64 0.36]
P(rain | umbrella) = 0.8889

These sums and ratios are the sum and product rules.

In software

  • A classifier's softmax output is a categorical pmf; a regression model with Gaussian noise outputs a density for yy.
  • Language models sample the next token from a categorical distribution (Sampling and Temperature).
  • scipy.stats.expon(scale=1/lam).pdf, .cdf and .ppf (the inverse cdf) mirror the code above.

Exercises

Exercise 1: Valid density?

For which cc is p(x)=cxp(x) = cx on [0,2][0, 2] (and 0 elsewhere) a density?

Answer

c=12c = \frac{1}{2}.

Solution

∫02cx dx=c⋅x22∣02=2c=1\int_0^2 cx\,dx = c \cdot \frac{x^2}{2}\Big|_0^2 = 2c = 1, so c=12c = \frac{1}{2}. Note p(2)=1p(2) = 1, and with a narrower interval the density would exceed 1.

Exercise 2: Binomial probability

A classifier is right 90% of the time, independently. What is the probability that it gets exactly 9 of 10 test examples right?

Answer

About 0.387.

Solution
(109)0.99⋅0.1=10⋅0.38742⋅0.1≈0.387\binom{10}{9}0.9^9 \cdot 0.1 = 10 \cdot 0.38742 \cdot 0.1 \approx 0.387

Exercise 3: Inverse transform (code)

Sample from the density p(x)=2xp(x) = 2x on [0,1][0, 1] using F(x)=x2F(x) = x^2, and compare the sample mean with the exact mean 23\frac{2}{3}.

Answer

X=UX = \sqrt{U}; the sample mean is close to 0.66670.6667.

Solution

Solve u=F(x)=x2u = F(x) = x^2 for xx: x=ux = \sqrt{u}. The mean is ∫01x⋅2x dx=23\int_0^1 x \cdot 2x\,dx = \frac{2}{3}.

python · try it yourself
import numpy as np
rng = np.random.default_rng(1)
x = np.sqrt(rng.uniform(size=100000))
print(round(x.mean(), 3))
0.667

Test yourself

For a continuous random variable, P(X=2)P(X = 2) is…

  1. p(2)p(2)
  2. 0
  3. 1
  4. F(2)F(2)

Can a probability density take the value 5?

  1. No, densities are at most 1
  2. Yes, as long as it integrates to 1
  3. Only for discrete variables
  4. Only at x=0x = 0

The marginal p(x)p(x) is obtained from the joint p(x,y)p(x, y) by…

  1. dividing by p(y)p(y)
  2. summing or integrating over yy
  3. multiplying by p(y)p(y)
  4. taking the maximum over yy