tl;dr: why Bayes’ theorem is a neat way to work with probabilities
Probability is central to machine learning, and it comes with several interpretations and definitions. Our intuition handles simple cases well, yet it can mislead us in others. This post covers the fundamentals of probability theory, so that our intuition stays reliable when we move to complex, high-dimensional models.
From events to probability
I promise this is not ‘from x to y’ dramatization
The sample space $\Omega$ is the set of all possible results of a trial, and its elements $\omega\in\Omega$ are called outcomes. An event is a subset of the sample space, and the event space $\mathcal{F}$ is the collection of events we can assign a probability to.
Together, the sample space and the event space form the measurable space $(\Omega, \mathcal{F})$. A probability measure $P$ assigns a probability to each event in $\mathcal{F}$, which completes the probability triplet $(\Omega, \mathcal{F}, P)$. A random variable $X$ is a measurable function $X: \Omega \rightarrow A$ that maps outcomes $\omega$ to a state space $A$ (e.g., $\{0,1\}$ or $\mathbb{R}$), so that outcomes become numbers we can compute with. In other words, probabilities are assigned to events in $\mathcal{F}$, while random variables extract measurable values from individual outcomes.
We often want the probability that $X$ lands in a measurable subset $S \subseteq A$ of the state space. Since $P$ only acts on events, we must first map the target values back to a subset of $\Omega$. This is known as the pull-back $X^{-1}(S)$, which produces an event in $\mathcal{F}$ (guaranteed by the measurability of $X$) and can therefore be assigned probability
\begin{equation} P_X(S) = P(X^{-1}(S)) = P\big(\{\omega \in \Omega \mid X(\omega) \in S\}\big). \end{equation}Through the pull-back, the random variable $X$ transports the probability measure $P$ from $\mathcal{F}$ onto the state space $A$. The resulting $P_X$ is a probability measure in its own right, known as the distribution (or pushforward) of $X$, and it lets us assign probabilities directly to numerical events without referring back to the underlying outcomes.
If the range of \( X \) is countable, then we refer to it as a discrete random variable. If it is not countable, i.e., \( X \) is defined in interval ranges, then it is referred to as a continuous random variable. For example, a discrete random variable can be the number of cards in your hand, while a continuous random variable can be the time it takes to shuffle the deck. The relationship of the random variable with probability is described by a probability density function (PDF), which assigns values to a range of values. A point value of continuous \( X \) has zero probability and its value on the PDF is referred to as the likelihood. On the other hand, the terminology for likelihood and probability coincide in discrete cases, described by the probability mass function (PMF).
Probability mass function
Probability density function
Useful identities
Consider a probability space $(\Omega, \mathcal{F}, P)$ and two events $ A, B \in \mathcal{F} $. The probability of the union of these events is given by the inclusion-exclusion principle
\begin{equation} P(A \cup B) = P(A) + P(B) - P(A \cap B). \end{equation}If the events $ A $ and $ B $ are statistically independent, the probability of their intersection is
\begin{equation} P(A \cap B) = P(A) P(B). \end{equation}A special case arises when two events are mutually exclusive (or disjoint), meaning they cannot occur simultaneously. In this scenario, the intersection contains no outcomes, i.e. $P(A \cap B) = 0$. Consequently, the inclusion-exclusion principle simplifies to the direct sum of their individual probabilities:
\begin{equation} P(A \cup B) = P(A) + P(B). \end{equation}The complement $\neg A$ is the event that $A$ does not occur. Together they cover the sample space, so their probabilities sum to one. This relationship is described as
\begin{equation} P(\neg A) = 1 - P(A) \end{equation}Finally, we define conditional probability, which allows us to update our beliefs based on new information. The probability of $A$ occurring, given that event $B$ has already been observed, is defined by the identity:
\begin{equation}\label{eq:condpr} P(A \vert B) = \frac{P(A \cap B)}{P(B)}, \end{equation}provided that $ P(B) \neq 0 $. Conceptually, this operation restricts the sample space from $\Omega$ to the outcomes in $B$. We can extend this concept to the Law of Total Probability. Let us assume a countable set of disjoint events $\{B_n\}$ that forms a partition of the sample space $\Omega$, meaning that $\bigcup_n B_n = \Omega$ and $\sum_n P(B_n) = 1$. The probability of any event $A$ can be found by summing its intersections with each part of this partition. By applying the product rule, we arrive at the standard formulation:
\begin{equation}\label{eq:lotp} P(A) = \sum_n P(A, B_n) = \sum_n P(A \vert B_n) P(B_n). \end{equation}This law effectively allows us to determine the marginal probability of $A$ by considering all possible scenarios of $B$.
Joint probabilities
Consider a simple example of a discrete probability mass function $ P_X $ of a random variable $ X \in \{0, 1\} $. We define another random variable, $ Y $, and describe their joint probability distribution in the table below:
| $ P_{X,Y} $ | $ X = 1 $ | $ X = 0 $ |
|---|---|---|
| $ Y = 1 $ | 0.1 | 0.3 |
| $ Y = 0 $ | 0.05 | 0.55 |
We can calculate the marginal probability of $X$ using the Law of Total Probability. By summing over all possible states of $Y$, we obtain:
\begin{equation} \begin{aligned} P_X(X=0) &= \sum_{y \in \{0,1\}} P_{X,Y}(X=0, Y=y) \\ &= P_{X,Y}(0,0) + P_{X,Y}(0,1) \\ &= 0.55 + 0.3 = 0.85. \end{aligned} \end{equation}
In other words, the probability of $ X=0 $ regardless of the value of $ Y $ is the sum of the cases where $ Y=0 $ and $ Y=1 $. This concept extends to continuous variables. Assuming $ X, Y \in \mathbb{R} $ are continuous, the summation is replaced by an integral. The Law of Total Probability allows us to find the marginal density $ p_X(x) $ by integrating out $ Y $:
While these identities may seem abstract, they form the backbone of probabilistic modeling for two practical reasons.
- Decomposition: It is often difficult to estimate a complex probability directly from raw data. However, it is much easier to estimate conditional relationships such as $P(X \mid Y)$. The Law of Total Probability allows us to build the complex “global” probability $P_X$ by combining simpler, “local” building blocks $P_{X\mid Y}$.
- Marginalization: In real-world data, our observations $X$ are often affected by noise or environmental factors $Y$ that we cannot control. By integrating out $Y$, we can make predictions that are robust, effectively averaging out the varying noise conditions.
Bayes’ rule
A fundamental property of the joint probability is its symmetry. The probability of $X$ and $Y$ occurring together is identical regardless of the order in which we consider them, i.e., $P(X, Y) = P(Y, X)$. By expanding both sides using the conditional identity defined earlier, rearranging the terms allows us to express the posterior probability of $Y$ given $X$ in terms of the reverse conditional probability
\begin{equation} \underbrace{P(Y \mid X)}_{\text{posterior}} = \frac{\overbrace{P(X \mid Y)}^{\text{likelihood}} \, \overbrace{P(Y)}^{\text{prior}}}{\underbrace{P(X)}_{\text{evidence}}}. \end{equation}While the Law of Total Probability (discussed in the previous section) allows us to reason forward from causes to effects, Bayes’ Theorem allows us to perform inverse or reverse probability. We reason from the observed data $X$ back to uncertain causes $Y$. The denominator, or evidence, serves as a normalization constant ensuring the posterior sums to 1. While the likelihood describes how the data is generated, the prior acts as the bridge between past knowledge and current inference. The prior also penalizes explanations we consider unlikely, which is what makes Bayesian methods useful and also why they are debated.
Critics often argue that the prior introduces subjectivity, yet no model is free of assumptions. A frequentist model treats the parameters as fixed unknowns without placing a distribution over them, and its assumptions are still there. The choice of likelihood fixes what counts as noise, the parameterization fixes which values are comparable, and any regularizer pulls the estimate toward some values over others. These commitments enter therefore through the estimator, which can make them harder to inspect. Bayes’ rule requires us to write them down explicitly as a prior.
Occam’s razor
Consider competing hypotheses $H$, each of which assigns a probability to every possible dataset $D$. A simple hypothesis is strict and specific; it concentrates its probability mass over a narrow range of outcomes, resulting in high-magnitude peaks. Conversely, a complex hypothesis is flexible and capable of explaining a wide variety of outcomes. To account for this broad range of potential data, it must spread its probability mass thinly, as the likelihood function must integrate to exactly $1$ over the entire space of possible data outcomes. Consequently, for any single specific observation $D$, the likelihood $P(D|H)$ of the complex model is comparatively small. When substituted into Bayes’ theorem, a simple hypothesis yields a much larger numerator compared to a complex one, because it did not waste probability mass on outcomes that did not occur. Thus, Bayes’ rule naturally implements Occam’s Razor, where it penalizes the complex model for its vagueness.
Preservation of uncertainty
Bayes’ rule can also be read as a check on overconfidence, controlling how much our beliefs narrow in light of new data. If the observed data is noisy, sparse, or ambiguous, the resulting likelihood function will be diffuse, failing to strongly distinguish between hypotheses. Since the posterior $P(H \mid D)$ is proportional to the product of the prior and the likelihood, a weak likelihood fails to sharpen the prior, and the posterior remains broad. Optimization collapses to a single point estimate regardless of how weakly the data support it, whereas Bayes’ rule carries this uncertainty through the update. This safeguard only holds if the prior is diffuse, since a sharp prior yields a sharp posterior whether or not it is placed correctly.
Influence of the prior
To illustrate the critical role of the prior, consider a rare disease $K$ with prevalence $P(K^+)=0.01$ and a test $T$ with sensitivity $P(T^+|K^+)=0.99$ and specificity $P(T^-|K^-)=0.99$. While intuition suggests that a positive test result $ T^+ $ implies a near-certain infection, Bayes’ rule shows otherwise. By expanding the evidence term $ P(T^+) $ via marginalization (Equation \ref{eq:lotp}), we find
\begin{equation} \begin{aligned} P(K^+\vert T^+) &= \frac{P(T^+\vert K^+)P(K^+)}{P(T^+)} \\ &=\frac{P(T^+\vert K^+)P(K^+)}{\underbrace{P(T^+\vert K^+) P(K^+) + P(T^+\vert \neg K^+) P(\neg K^+)}_{\text{Law of Total Probability}}} \\ &= \frac{0.99\times 0.01}{0.99\times 0.01 + 0.01\times 0.99} = 0.5, \end{aligned} \end{equation}which is significantly lower than the test accuracy suggests. This demonstrates that without incorporating the prior (the low base rate of the disease), relying solely on the likelihood leads to a false sense of certainty. To expand on this, we can observe how Bayesian inference naturally handles accumulating evidence. Suppose we administer a second test, $ T_2 $, which is conditionally independent of the first. Because the tests are independent, the likelihood of two false positives drops drastically ($ 0.01^2 $), while the likelihood of two true positives remains high. Applying Bayes’ rule again for the joint events $ T_1^+, T_2^+ $ as
\begin{equation} P(K^+ \vert T_1^+, T_2^+) = \frac{P(T_1^+ \vert K^+) P(T_2^+ \vert K^+) P(K^+)}{P(T_1^+, T_2^+)} \approx 0.99. \end{equation}With the second confirmation, the probability jumps from 50% to 99%. Bayesian inference is iterative, as the posterior after one observation becomes the prior for the next, so the model grows more confident as data accumulates
Closing thoughts
Knowledge of probability theory helps us understand how to reason with uncertainty, and update beliefs based on new information. However, translating these concepts to contemporary machine learning models is not straightforward. Fortunately for us, this problem has been researched for decades and has resulted in many interesting probabilistic machine learning architectures.
Thank you Claude for the great example. ↩︎