Random variables and distributions
Quick answer
A random variable maps outcomes of an experiment to numeric values. Its probability distribution assigns probability to sets of values. Discrete variables have countable possible values, while common continuous models describe intervals using a density. ML uses distributions to model labels, counts, measurements and uncertainty.
From two coin flips to a distribution
For independent fair flips, HH, HT, TH and TT each have probability 1/4. Define X as the number of heads. A probability mass function (PMF) gives P(X=x): values 0,1,2 have probabilities 1/4,1/2,1/4. All masses are nonnegative and sum to one. A cumulative distribution function (CDF) gives P(X≤x); it is nondecreasing and approaches zero and one at the extremes.
outcomes = ["HH", "HT", "TH", "TT"]for x in range(3): print(x, sum(s.count("H") == x for s in outcomes)/4)print("CDF at 1:", sum(s.count("H") <= 1 for s in outcomes)/4)Expected output (Python 3.13.5):
0 0.251 0.52 0.25CDF at 1: 0.75Two outcomes map to one head; the CDF at one includes zero and one head.
Select a distribution by its assumptions
Bernoulli(p) models a single 0/1 trial; categorical models one of several labels (encode labels as numbers only if useful). Binomial(n,p) models the success count in n independent Bernoulli trials with the same p: P(X=k)=C(n,k)p^k(1−p)^(n−k), where C counts choices of k positions. A continuous uniform model on [a,b], a<b, has constant density 1/(b−a) within the interval. A normal model has symmetric bell-shaped density with mean μ and standard deviation σ>0; it is not appropriate for every measurement. A probability density function (PDF) integrates to one; probability is area under it over an interval, not its height at a point.
from math import combn, p = 3, 0.25print([round(comb(n,k)*p**k*(1-p)**(n-k), 6) for k in range(n+1)])a,b = 0,4print("P(1 <= U <= 2):", (2-1)/(b-a))Expected output (Python 3.13.5):
[0.421875, 0.421875, 0.140625, 0.015625]P(1 <= U <= 2): 0.25The four binomial masses sum to one. Uniform interval probability is interval length divided by total length. A continuous uniform variable has zero probability at any particular point.
Common mistakes
A PDF can exceed one (for example uniform on [0,0.5] has height 2), but integrated probability cannot. A CDF is not a density. Do not use a binomial model if probabilities change across trials or trials are dependent without checking whether an alternative is needed. Normal models allow negative values and may poorly represent bounded counts.
Practice and next steps
List the PMF of three independent fair coin flips and find P(X≤1). Name a suitable distribution for one binary label and for one of four unordered classes. Next, summarize a distribution numerically.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Random variables and distributions — knowledge check
Apply probability concepts to original finite and simulated examples.
What does a random variable do?
For two fair independent flips, what is P(exactly one head)?
What does a CDF at x represent?
A continuous uniform density on [0,0.5] has height 2. Is it valid?
Which assumptions support a binomial count?