AI / ML Theory
Chapter 0 · 18 min

0.5 — Joint, marginal, conditional, and Bayes' rule

Goal: State Bayes' rule, derive it from the joint, and read off the implicit generative story behind the binary classifier p(y=1|x) = σ(wᵀx).

1. Joints, marginals, and conditionals

Given a joint distribution p(x,y)p(x, y) on a pair (X,Y)(X, Y):

  • The marginal of XX is obtained by summing/integrating YY out:

    p(x)=∑yp(x,y)or∫p(x,y) dy.p(x) = \sum_y p(x, y) \quad\text{or}\quad \int p(x, y)\, dy.

    Marginalisation is the universal operation for getting rid of a variable you don't care about right now.

  • The conditional of YY given X=xX = x is the joint normalised by the marginal:

    p(y∣x)=p(x,y)p(x),defined when p(x)>0.p(y \mid x) = \frac{p(x, y)}{p(x)}, \qquad \text{defined when } p(x) > 0.

    Conditioning is the universal operation for asking "what do I know about YY once I've observed XX?".

These two are dual: marginalising forgets, conditioning updates.

2. The product rule (chain rule of probability)

Rearranging the definition of conditional probability gives the product rule:

  p(x,y)=p(x) p(y∣x)=p(y) p(x∣y).  \boxed{\;p(x, y) = p(x)\,p(y \mid x) = p(y)\,p(x \mid y).\;}

The two factorisations exist because we can condition in either direction. The identity itself is trivial — but equating the two right-hand sides is exactly Bayes' rule.

3. Bayes' rule

Equate the two factorisations of the joint:

p(x) p(y∣x)=p(y) p(x∣y),p(x)\,p(y \mid x) = p(y)\,p(x \mid y),

and solve for the conditional we typically want in inference:

  p(y∣x)=p(x∣y) p(y)p(x).  \boxed{\;p(y \mid x) = \frac{p(x \mid y)\, p(y)}{p(x)}.\;}

The denominator p(x)p(x) is just normalisation; expanded via marginalisation,

p(x)=∑y′p(x∣y′) p(y′),p(x) = \sum_{y'} p(x \mid y')\, p(y'),

it is the prior-weighted average likelihood of observing xx across all hypotheses. The four named pieces:

  • p(y)p(y) — prior, what you believed about YY before seeing data.
  • p(x∣y)p(x \mid y) — likelihood, the model's probability of generating xx given hypothesis yy.
  • p(y∣x)p(y \mid x) — posterior, updated belief about YY after observing xx.
  • p(x)p(x) — evidence / marginal likelihood, a normaliser.

In ML the model is usually parametrised by θ\theta and we write p(x∣θ)p(x \mid \theta) for the likelihood; θ\theta replaces yy in the formula and prior/posterior become priors/posteriors over parameters.

4. The binary classifier p(y=1∣x)=σ(w⊤x)p(y = 1 \mid x) = \sigma(w^\top x)

This is the model behind logistic regression and behind the output layer of every binary deep classifier. The self-test in the roadmap asks: what is the implicit generative story?

The answer is not "we drew xx from some Gaussian and applied a deterministic rule". The model is more honest than that: it directly specifies the posterior p(y∣x)p(y \mid x) and says nothing at all about p(x)p(x). This is the discriminative stance: model p(y∣x)p(y \mid x) directly; don't bother modelling how xx came to be.

Compare with the generative stance, which would model p(x∣y)p(x \mid y) and p(y)p(y), then derive the posterior by Bayes' rule:

p(y∣x)=p(x∣y) p(y)p(x).p(y \mid x) = \frac{p(x \mid y)\, p(y)}{p(x)}.

For example, if we assumed X∣Y=y∼N(μy,Σ)X \mid Y = y \sim \mathcal{N}(\mu_y, \Sigma) (Gaussian class-conditionals with shared covariance) and a Bernoulli prior over YY, then a few lines of algebra (which we don't need today) give

p(y=1∣x)=σ(w⊤x+b)p(y = 1 \mid x) = \sigma(w^\top x + b)

for specific ww and bb derived from μ0,μ1,Σ\mu_0, \mu_1, \Sigma, and the prior. The logistic-regression functional form is consistent with a Gaussian generative story — but logistic regression itself doesn't commit to that story.

The point: p(y∣x)=σ(w⊤x)p(y \mid x) = \sigma(w^\top x) is the same conditional as the one Bayes' rule would produce from a class-conditional Gaussian generative model, but logistic regression learns ww directly from the data without ever estimating the Gaussian parameters. Discriminative training is often more sample-efficient when all you need is the decision rule.

5. Independence and conditional independence

XX and YY are independent (X⊥YX \perp Y) iff

p(x,y)=p(x) p(y)for all x,y,p(x, y) = p(x)\, p(y) \quad \text{for all } x, y,

equivalently p(y∣x)=p(y)p(y \mid x) = p(y) for all xx with p(x)>0p(x) > 0. Observing XX tells you nothing new about YY.

XX and YY are conditionally independent given ZZ (X⊥Y∣ZX \perp Y \mid Z) iff

p(x,y∣z)=p(x∣z) p(y∣z)for all z.p(x, y \mid z) = p(x \mid z)\, p(y \mid z) \quad \text{for all } z.

Conditional independence is the structural primitive behind graphical models, the naive-Bayes classifier (assumes features are independent given the label), and the layered structure of probabilistic deep models later on. Worth keeping in mind even when you don't draw a graph.

6. Where this is going

Bayes' rule is the lens through which we will read MLE, MAP, variational inference, and the discriminative-versus-generative distinction that runs all the way through the course. Cross-entropy as a loss (§0.7) is MLE on a discriminative model. The score function in policy gradient (Ch. 9) is the gradient of a log-likelihood. The "likelihood" of an LLM output token is the conditional next-token probability. Same machinery, used many times.

Self-test

A test for a rare disease has 99% sensitivity (p(positive∣disease)=0.99p(\text{positive} \mid \text{disease}) = 0.99) and 99% specificity (p(negative∣healthy)=0.99p(\text{negative} \mid \text{healthy}) = 0.99). The disease has prevalence p(disease)=0.001p(\text{disease}) = 0.001 in the screened population. A patient tests positive.

  1. Identify the prior, likelihood, and what you want (the posterior).
  2. Compute p(disease∣positive)p(\text{disease} \mid \text{positive}) using Bayes' rule.
  3. The answer is surprisingly low. Why? Where does the false-positive rate enter?