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, with and . Workhorses in ML:
| Distribution | pmf | Models |
|---|---|---|
| Bernoulli() | , | a binary label |
| Categorical() | for class | a multi-class label, a next token |
| Binomial(, ) | number of successes in trials |
Continuous: the pdf
For a continuous random variable, for every single value. Probabilities come from integrating a density with :
A density can exceed 1: the uniform density on is 4 everywhere on that interval. Only areas are probabilities. Common densities: uniform, Gaussian, exponential for , Beta (for probabilities themselves).
The cdf
Both kinds have a cumulative distribution function , non-decreasing from 0 to 1. For a continuous variable . The inverse cdf turns uniform random numbers into samples of any distribution (inverse transform sampling).
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 , the joint distribution describes them together. The marginal sums (or integrates) out , and the conditional describes once is known. A table makes it concrete:
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 .
- Language models sample the next token from a categorical distribution (Sampling and Temperature).
scipy.stats.expon(scale=1/lam).pdf,.cdfand.ppf(the inverse cdf) mirror the code above.
Exercises
Exercise 1: Valid density?
For which is on (and 0 elsewhere) a density?
Answer
.
Solution
, so . Note , 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
Exercise 3: Inverse transform (code)
Sample from the density on using , and compare the sample mean with the exact mean .
Answer
; the sample mean is close to .
Solution
Solve for : . The mean is .
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, is…
- 0
- 1
Can a probability density take the value 5?
- No, densities are at most 1
- Yes, as long as it integrates to 1
- Only for discrete variables
- Only at
The marginal is obtained from the joint by…
- dividing by
- summing or integrating over
- multiplying by
- taking the maximum over