A random variable is a variable whose value is the numeric outcome of a random process. It's the formal bridge between "something uncertain happens" and "here's a number I can compute with."
Discrete vs Continuous
| Discrete Random Variable | Continuous Random Variable | |
|---|---|---|
| Possible values | Countable (finite or countably infinite list) | Any value in a range (uncountably many) |
| Example | Number of defective items in a batch of 10 | Time until a server responds to a request |
| Described by | Probability Mass Function (PMF): \(P(X=x)\) | Probability Density Function (PDF): \(f(x)\) |
| ML example | A predicted class label (0 or 1) | A predicted house price |
Notation
Numerical Example
Let \(X\) = number of heads in 2 fair coin flips. Possible outcomes: HH, HT, TH, TT (each with probability 0.25).
# X can be 0, 1, or 2
# P(X=0) = P(TT) = 0.25
# P(X=1) = P(HT) + P(TH) = 0.5
# P(X=2) = P(HH) = 0.25
import numpy as np
outcomes = ["HH", "HT", "TH", "TT"]
X = [outcome.count("H") for outcome in outcomes]
print(X) # [2, 1, 1, 0]
Why This Matters for ML
- A classifier's predicted label is modeled as a random variable — that's what makes
predict_proba()meaningful in the first place - Every feature in your dataset can be thought of as a sample drawn from some underlying random variable — this framing underlies statistical assumptions many models make
Common Mistakes
- Treating a continuous variable's PDF value \(f(x)\) as a probability directly — for continuous variables, only the area under the curve over a range gives a probability; \(f(x)\) at a single point is a density, not a probability.
Interview Relevance
Q: "Is a predicted class label from a classifier discrete or continuous?" Discrete — it takes one of a finite, countable set of values (the class labels); the model's underlying probability output, however, is continuous (any value in [0,1]).
Practice Question
Is "number of customer support tickets filed in a day" a discrete or continuous random variable? What about "time between two consecutive tickets"?