3 · Variables & random variables
The abstraction that connects probability to code — from die rolls to neural network outputs.
A random variable is a function that maps outcomes to numbers — and ALL of probability builds from this single idea.
Without this:
Without the random-variable abstraction, you can't write a likelihood, a loss, or a Bayesian update — the math of ML has no foundation.
In everyday language, "variable" means any column in a spreadsheet. In statistics and probability, variable types matter for choosing the right tool:
| Type | Sub-type | Examples | |---|---|---| | Categorical | Nominal | Country, color, blood type | | Categorical | Ordinal | Rating (1–5), education level | | Numerical | Discrete | # of clicks, die result | | Numerical | Continuous | Height, temperature, probability score |
A random variable (RV) is something more precise: a function X: Ω → ℝ that assigns a real number to every outcome in a sample space Ω. The key insight is that X is NOT a variable with random values — it is a deterministic function whose argument (the outcome) is random.
Conventionally: uppercase X denotes the random variable (the function); lowercase x denotes a specific observed value (a realization). You'll see this convention in every stats textbook and ML paper.
Python (in browser)
E[X] = 3.5 for a fair die — the 'expected value' is the probability-weighted average outcome. Note that 3.5 is never actually rolled; it's a property of the distribution, not a possible outcome.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Python (in browser)
A Bernoulli RV has the simplest possible PMF — two outcomes, one probability p. The label in binary classification IS a Bernoulli random variable.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
If X is the sum of two fair dice, what is the support of X (the set of all possible values)?
Discrete and Continuous Probabilities — the formal treatment of PMFs and PDFs that underpins everything in this track. Read it alongside this lesson for the mathematical rigour.
- Variable types: **categorical** (nominal, ordinal) and **numerical** (discrete, continuous). Choosing the right statistical tool depends on the variable type.
- A **random variable** X is a function from a sample space to ℝ. Uppercase X = the function; lowercase x = a specific realized value.
- Discrete RVs have a **PMF** (probability mass function); continuous RVs have a **PDF** (probability density function). Expectation E[X] is the probability-weighted average.
Labels are random variables — classification models predict a categorical RV, regression models predict a continuous RV. The loss function is an expected value over the random label.
If you remove it: You can't formalize what 'expected loss' means, which is the objective every supervised model optimizes.