Skip to main content

Why Every AI Model is Actually Bayesian

Why Every AI Model is Actually Bayesian

Topic Overview

Standard probability asks: “What is the chance of this event happening in the entire universe?” Conditional probability asks: “Given that I already know this to be true, what is the chance of that happening?”

Bayes’ Theorem is the engineering tool that flips this condition around. It answers the most critical question in machine learning: “Given the data I just observed, what is the probability that my hypothesis is true?”

Why does it exist? Because in the real world, we usually know \(P(\text{Data}|\text{Hypothesis})\)—how likely a certain observation is under a specific model—but what we actually want is \(P(\text{Hypothesis}|\text{Data})\)—how likely our model is to be correct given the observation. Bayes’ Theorem is the mathematical bridge between the data we generate and the truths we want to infer.

What problem does it solve? It provides a rigorous, mathematically sound mechanism for updating your beliefs as new evidence arrives. It prevents you from confusing “the probability of seeing the data if the model is true” with “the probability the model is true given the data.”

Why should engineers care? Standard deep learning models output point estimates (e.g., “This image is a cat with 99% confidence”) which are notoriously miscalibrated and overconfident. Bayesian deep learning uses Bayes’ Theorem to output distributions over weights, allowing models to express uncertainty. Without Bayes, VAEs cannot be trained, hyperparameter search is blind guessing, and classifiers cannot be calibrated.


Prerequisites

Concept Why It Is Needed Where It Appears
Basic Probability Understanding \(P(A)\), sample spaces, and the intersection of events \(P(A \cap B)\). Defining the restricted sample space in conditional probability.

Historical Motivation

In the 18th century, mathematicians were obsessed with inverse probability: If you shoot an arrow and it lands 2 feet left of the target, what is the probability that you were aiming exactly at the center?

They knew how to calculate the forward probability: If you aim at the center, what’s the chance it lands 2 feet left? (This is just a distribution, like a Gaussian). But reversing the direction—going from the effect back to the cause—was an unsolved problem.

In 1763, Thomas Bayes’ posthumous paper introduced a framework to solve this. Later, Pierre-Simon Laplace formalized it.

Engineering necessity: Every machine learning system is an inverse problem. You have the data (the effect). You want the parameters (the cause). You know how data behaves given parameters (the forward model). Bayes’ Theorem is the only mathematically consistent way to invert the forward model to infer the parameters.


Intuitive Explanation

Imagine you are a doctor. A patient walks in with a cough.

You know the forward probabilities: If someone has the flu, there is a 90% chance they have a cough. \(P(\text{Cough}|\text{Flu}) = 0.90\).

But the patient doesn’t care about that. They want to know: “Given that I have a cough, what is the probability I have the flu?” \(P(\text{Flu}|\text{Cough}) = ?\)

This depends on two things: 1. How rare is the flu? If it’s summer and the flu is basically non-existent (the Prior), then the cough is probably just allergies. 2. Are there other things that cause a cough? If a thousand other things cause a cough (allergies, cold, dry air), then any single cough is weak evidence for the flu specifically.

Bayes’ Theorem is the Detective’s Logic: 1. Start with your Prior belief (The flu is rare, only 1% of people have it). 2. Multiply by the Likelihood (If it were the flu, a cough is very likely, 90%). 3. Divide by the Evidence (Coughs are common in general, maybe 10% of everyone has a cough right now).

Bayes forces you to weigh new evidence against how unlikely the hypothesis was to begin with, and against how common the evidence itself is.


Visual Understanding

Draw this: A Venn diagram inside a rectangular box representing the entire Universe \(U\).

  • Circle \(A\): The set of all events where Hypothesis A is true.
  • Circle \(B\): The set of all events where Evidence B is observed.
  • Overlap \(A \cap B\): The events where both A is true AND B is observed.
+-----------------------------------+
|              U                     |
|                                   |
|       +-------+    +-------+      |
|       |       |    |       |      |
|       |   A   |////|   B   |      |
|       |       |////|       |      |
|       +-------+----+-------+      |
|                                   |
+-----------------------------------+

When we ask \(P(A|B)\), we are saying: “Restrict the entire universe to only the inside of Circle B.”

In this new, smaller universe (Circle B), what is the probability of A? It is the area of the overlap \(A \cap B\) divided by the total area of Circle B.

\[P(A|B) = \frac{\text{Area of Overlap}}{\text{Area of Circle B}} = \frac{P(A \cap B)}{P(B)}\]


Mathematical Foundations

Conditional Probability

\[P(A|B) = \frac{P(A \cap B)}{P(B)}\]

Explain every symbol: - \(P(A|B)\): Read “Probability of A given B.” The conditional probability of event A occurring, assuming B has already occurred. - \(P(A \cap B)\): The joint probability. The probability of both A and B happening. - \(P(B)\): The marginal probability of B. The total probability that B happens, regardless of A.

Why does this equation exist? Because conditioning on \(B\) shrinks the sample space. We are no longer asking about the whole universe; we are only asking about the universe where \(B\) is true. So we divide by the size of that new universe, \(P(B)\).

What happens if \(P(B) = 0\)? The equation is undefined. You cannot condition on an impossible event.

Bayes’ Theorem

By symmetry, we can also write: \(P(B|A) = \frac{P(A \cap B)}{P(A)}\).

Since both equations equal \(P(A \cap B)\), we can set them equal to each other: \(P(A|B)P(B) = P(B|A)P(A)\)

Divide both sides by \(P(B)\):

\[P(A|B) = \frac{P(B|A)P(A)}{P(B)}\]

This is Bayes’ Theorem. Now, let’s translate this into the language of machine learning:

\[\underbrace{P(\theta|\mathcal{D})}_{\text{Posterior}} = \frac{\overbrace{P(\mathcal{D}|\theta)}^{\text{Likelihood}} \cdot \overbrace{P(\theta)}^{\text{Prior}}}{\underbrace{P(\mathcal{D})}_{\text{Evidence}}}\]

Explain every symbol: - Posterior \(P(\theta|\mathcal{D})\): What we want. The probability distribution of the model parameters \(\theta\) given the observed data \(\mathcal{D}\). - Likelihood \(P(\mathcal{D}|\theta)\): How probable the observed data is under a specific set of parameters \(\theta\). This is what standard deep learning optimizes. - Prior \(P(\theta)\): Our belief about the parameters before seeing any data. - Evidence \(P(\mathcal{D})\): The total probability of the data, across all possible parameter settings. \(P(\mathcal{D}) = \int P(\mathcal{D}|\theta)P(\theta)d\theta\).


Worked Numerical Example

Let’s compute a Spam Filter (Naive Bayes).

Given: - 1% of all emails are Spam. \(P(\text{Spam}) = 0.01\) (Prior) - 99% are Ham. \(P(\text{Ham}) = 0.99\) - The word “FREE” appears in 80% of Spam emails. \(P(\text{FREE}|\text{Spam}) = 0.80\) (Likelihood) - The word “FREE” appears in 10% of Ham emails. \(P(\text{FREE}|\text{Ham}) = 0.10\)

Problem: You receive an email containing the word “FREE”. What is the probability it is Spam? \(P(\text{Spam}|\text{FREE}) = ?\)

Step 1: Calculate the Joint Probabilities (Numerator) - Probability it is Spam AND contains FREE: \(P(\text{FREE}|\text{Spam}) \times P(\text{Spam}) = 0.80 \times 0.01 = 0.008\) - Probability it is Ham AND contains FREE: \(P(\text{FREE}|\text{Ham}) \times P(\text{Ham}) = 0.10 \times 0.99 = 0.099\)

Step 2: Calculate the Evidence (Denominator) Total probability of seeing the word “FREE” (regardless of class): \(P(\text{FREE}) = P(\text{FREE} \cap \text{Spam}) + P(\text{FREE} \cap \text{Ham}) = 0.008 + 0.099 = 0.107\)

Step 3: Apply Bayes’ Theorem \[P(\text{Spam}|\text{FREE}) = \frac{0.008}{0.107} \approx 0.0747\]

Result: Even though 80% of spam has “FREE”, an email with “FREE” is only 7.47% likely to be spam. Why? Because the prior probability of spam is so low (1%), and “FREE” is quite common in regular Ham (10%), the sheer volume of Ham emails containing “FREE” overwhelms the Spam signal.

This is the Base Rate Fallacy, and Bayes’ Theorem is the mathematical cure for it.


Computational Interpretation

The Intractability of the Evidence Term

The denominator \(P(\mathcal{D}) = \int P(\mathcal{D}|\theta)P(\theta)d\theta\) is the bottleneck of Bayesian ML.

For a discrete hypothesis space with 10 possibilities, computing the sum is easy. For a deep neural network with 1 billion continuous parameters, this is a \(10^{9}\)-dimensional integral. It is fundamentally impossible to compute exactly.

What the computer is actually doing: Because the evidence \(P(\mathcal{D})\) is a constant with respect to \(\theta\), we often compute the unnormalized posterior: \[P(\theta|\mathcal{D}) \propto P(\mathcal{D}|\theta)P(\theta)\]

To find the single best parameter setting (Maximum A Posteriori - MAP), we don’t need the denominator at all: \[\theta_{MAP} = \arg\max_\theta [P(\mathcal{D}|\theta)P(\theta)]\]

If we drop the prior too, we get Maximum Likelihood Estimation (MLE), which is exactly what standard PyTorch CrossEntropyLoss minimizes.

When we do need the full distribution (e.g., for VAEs), we must approximate the integral. This is the entire field of Variational Inference.


Implementation Perspective

NumPy: Discrete Bayes

import numpy as np

# Prior probabilities
p_spam = 0.01
p_ham = 0.99

# Likelihoods: P(FREE | Class)
p_free_given_spam = 0.80
p_free_given_ham = 0.10

# Joint probabilities (Numerators)
p_spam_and_free = p_free_given_spam * p_spam
p_ham_and_free = p_free_given_ham * p_ham

# Evidence (Denominator)
p_free = p_spam_and_free + p_ham_and_free

# Posterior
p_spam_given_free = p_spam_and_free / p_free

print(f"P(Spam | FREE) = {p_spam_given_free:.4f}") # 0.0748

PyTorch: Continuous Distributions & Log-Space

In deep learning, multiplying probabilities causes underflow (e.g., \(0.1^{1000} \approx 0\)). We must use log probabilities.

import torch
from torch.distributions import Normal

# We want to infer a weight theta given some observed data D.
# Assume Prior: theta ~ N(0, 1)
# Assume Likelihood: D ~ N(theta, 0.5)

prior = Normal(torch.tensor(0.0), torch.tensor(1.0))
# Observed data point
D = torch.tensor(2.5) 

# We evaluate the posterior at a specific guess: theta = 2.0
theta_guess = torch.tensor(2.0)

# 1. Log Prior: log P(theta)
log_prior = prior.log_prob(theta_guess)

# 2. Log Likelihood: log P(D | theta)
likelihood = Normal(theta_guess, torch.tensor(0.5))
log_likelihood = likelihood.log_prob(D)

# 3. Unnormalized Log Posterior: log P(theta) + log P(D|theta)
# (We skip the evidence denominator because it's constant w.r.t theta)
log_unnorm_posterior = log_prior + log_likelihood

print(f"Log Prior: {log_prior.item():.4f}")
print(f"Log Likelihood: {log_likelihood.item():.4f}")
print(f"Log Unnorm Posterior: {log_unnorm_posterior.item():.4f}")

Every line explained: - Normal(0, 1): Creates a Gaussian distribution. This is our prior belief about the weight before seeing data. - log_prob(): Computes \(\log P(x)\). PyTorch handles the math of the Gaussian PDF under the hood. - log_prior + log_likelihood: In log space, multiplication becomes addition. This is the core computation in training Bayesian Neural Networks and VAEs.


Where It Appears In AI

AI Component Exact Usage of Conditional Probability / Bayes
VAE (Variational Autoencoder) The entire architecture exists to approximate \(P(z|x)\) (the posterior), which is intractable. The ELBO is derived directly from Bayes’ theorem.
Naive Bayes Classifier Computes \(P(y|x) \propto P(x|y)P(y)\) assuming features are conditionally independent given the class.
Bayesian Hyperparameter Optimization Models \(P(\text{Score}|\text{Hyperparameters})\) as a Gaussian Process and uses Bayes to find \(P(\text{Hyperparameters}|\text{Score})\).
Classifier Calibration Standard Softmax outputs are not true probabilities. Platt Scaling uses Bayes’ logic to map logits to calibrated probabilities.
Reinforcement Learning Thompson sampling balances exploration/exploitation by sampling actions from the posterior \(P(\text{Action}|\text{Reward History})\).
Diffusion Models The reverse process trains a model to approximate \(P(x_{t-1}|x_t)\), the conditional probability of a less noisy image given a noisier one.

Deep Dive Into Real Models

VAEs: The Intractable Posterior

A VAE aims to generate data. We want to infer the latent vector \(z\) given data \(x\): \(P(z|x)\). By Bayes: \(P(z|x) = \frac{P(x|z)P(z)}{P(x)}\). \(P(x) = \int P(x|z)P(z)dz\) is impossible to integrate over high-dimensional \(z\). The VAE uses an “Encoder” network \(q(z|x)\) to approximate the true posterior \(P(z|x)\). The Evidence Lower Bound (ELBO) is the mathematical objective used to train the model, derived directly from forcing \(q(z|x)\) to match Bayes’ theorem.

LLMs: Calibration and Hallucination

When an LLM outputs “I am 99% sure,” it is usually miscalibrated. The Softmax output is \(P(\text{Token}|\text{Context})\), which is a likelihood-like metric, not a true posterior belief of correctness. Calibration methods (like Temperature Scaling) adjust these outputs so they align with true Bayesian posterior probabilities, allowing systems to detect when the model is guessing vs. when it truly knows.

Reinforcement Learning: Bayesian RL

In standard RL, an agent estimates the expected reward of an action. In Bayesian RL, the agent maintains a posterior distribution over the environment’s transition probabilities or reward function: \(P(\text{Environment}|\text{History})\). By sampling from this posterior (Thompson Sampling), the agent naturally explores when uncertain and exploits when confident.


Failure Modes

Failure 1: The Base Rate Fallacy (Ignoring the Prior)

Cause: Assuming \(P(A|B) \approx P(B|A)\). “If the test is 99% accurate, and I tested positive, I must be 99% sick.” Symptoms: Extreme overconfidence in rare events. In ML: Classifiers predicting rare events (fraud, disease) with high precision, but massive false-positive rates. Solution: Always incorporate the prior class distribution into the loss function (using class weights) or during inference (calibrating thresholds).

Failure 2: Numerical Underflow

Cause: Multiplying many probabilities together (e.g., in Naive Bayes over thousands of features). \(0.1 \times 0.05 \times \dots \times 0.01 \to 0.0\) (float64 underflows to 0). Symptoms: The model outputs 0 for all classes, or NaN. Solution: Operate entirely in log-space. \(\log(P(A|B)) = \log(P(B|A)) + \log(P(A)) - \log(P(B))\). Sums do not underflow.

Failure 3: Prior Misspecification

Cause: Choosing a prior \(P(\theta)\) that assigns zero probability to the true parameter. If \(P(\theta_{true}) = 0\), then no amount of data can ever update the posterior: \(\frac{0 \times \text{Likelihood}}{\text{Evidence}} = 0\). Symptoms: The model completely fails to learn, regardless of how much training data is provided. Solution: Use uninformative priors (like uniform distributions) or weakly informative priors (wide Gaussians) that have support over all plausible values.


Engineering Insights

MAP vs MLE: The Regularization Connection

Maximum A Posteriori (MAP) estimation maximizes the unnormalized posterior: \[\theta_{MAP} = \arg\max_\theta [\log P(\mathcal{D}|\theta) + \log P(\theta)]\] If the prior \(P(\theta)\) is a Gaussian with mean 0, then \(\log P(\theta) \propto -\lambda \|\theta\|^2\). Insight: L2 Regularization (Weight Decay) in standard deep learning is mathematically equivalent to assuming a Gaussian prior over the weights and performing MAP inference. Bayes’ Theorem explains why weight decay works: it encodes the prior belief that small weights are more probable.

The Cost of Full Bayes vs Point Estimates

Standard backprop finds a single point (MAP). Full Bayesian inference finds a distribution. The computational cost of finding the distribution (e.g., via MCMC or ensembles) is often 10x-100x higher. In practice, engineers use point estimates for deployment speed and use Bayesian methods (like MC Dropout) only when uncertainty quantification is strictly necessary (e.g., autonomous driving, medical diagnosis).


Interview Questions

Beginner

Q1: Explain Bayes’ Theorem in plain English. A1: Bayes’ Theorem tells you how to update your beliefs based on new evidence. It states that your posterior belief (how likely the hypothesis is given the data) is proportional to how likely the data is under the hypothesis (likelihood) multiplied by how likely the hypothesis was before you saw the data (prior).

Q2: What is the difference between Likelihood and Posterior? A2: Likelihood is \(P(\text{Data}|\text{Hypothesis})\): assuming the hypothesis is true, how probable is the observed data? Posterior is \(P(\text{Hypothesis}|\text{Data})\): given the data we just saw, how probable is the hypothesis? They are inverse conditional probabilities.

Intermediate

Q3: Why is the Evidence term in Bayes’ Theorem usually intractable for deep learning models? A3: The Evidence \(P(\mathcal{D})\) requires marginalizing over all possible parameter settings: \(\int P(\mathcal{D}|\theta)P(\theta)d\theta\). For a deep neural network with millions of parameters, this is a high-dimensional integral with no closed-form solution, and approximating it via sampling requires exponentially many samples (the curse of dimensionality).

Q4: How does L2 Regularization relate to Bayes’ Theorem? A4: L2 Regularization (weight decay) adds a penalty \(\lambda \|\theta\|^2\) to the loss function. From a Bayesian perspective, minimizing the negative log posterior \(-\log P(\theta|\mathcal{D})\) results in minimizing the negative log likelihood plus the negative log prior. If the prior \(P(\theta)\) is a zero-mean Gaussian, the negative log prior is proportional to \(\|\theta\|^2\). Thus, L2 regularization is equivalent to Maximum A Posteriori (MAP) inference with a Gaussian prior.

Advanced

Q5: How does a Variational Autoencoder (VAE) use Bayes’ Theorem, and what problem does it solve? A5: A VAE aims to compute the posterior \(P(z|x)\) (the latent vector given the data). By Bayes, \(P(z|x) \propto P(x|z)P(z)\). Because the evidence \(P(x)\) is intractable, the VAE introduces an approximate distribution \(q(z|x)\) (the encoder). The VAE is trained by maximizing the ELBO (Evidence Lower Bound), which minimizes the KL divergence between \(q(z|x)\) and the true posterior \(P(z|x)\). It solves the intractability problem by turning integration into optimization.

Q6: Explain Platt Scaling in the context of Bayes’ Theorem. A6: Neural network Softmax outputs are discriminative scores, not true posterior probabilities. Platt Scaling trains a logistic regression model \(P(y=1|x) = \frac{1}{1 + e^{(ax+b)}}\) on the validation set logits. This maps the uncalibrated scores to true Bayesian posterior probabilities by learning the correct prior and evidence scaling (parameters \(a\) and \(b\)) that were ignored or distorted by the standard cross-entropy training.


Research Perspective

Current Limitations

  1. Scalability: Exact Bayesian inference is impossible for LLMs. Even approximate methods like MC Dropout or Deep Ensembles are computationally expensive.
  2. Prior Specification: In high dimensions, even “uninformative” priors can have unintended consequences, pulling the model into bad basins of the loss landscape.

Open Problems

  1. Scalable Bayesian DL: Developing variational inference methods or MCMC samplers that can scale to 100B+ parameter LLMs without requiring 10x the compute of standard training.
  2. Calibration of Generative Models: LLMs hallucinate with high confidence. How can we structure the inference process to output well-calibrated posteriors over generated tokens?

Modern Improvements

  1. Laplace Approximation: A post-hoc method that takes a standard point-estimate neural network and approximates the posterior around the MAP estimate using the Hessian matrix, turning an MLE model into a Bayesian model without retraining.
  2. Conformal Prediction: A distribution-free approach to uncertainty that provides coverage guarantees. While not Bayesian, it solves the same practical problem (uncertainty quantification) without requiring prior specification.

Recent Research Directions

  1. Bayesian LoRA: Applying Bayesian inference only to the low-rank adaptation matrices during fine-tuning, allowing for uncertainty quantification in fine-tuned LLMs at a fraction of the computational cost.
  2. Flow Matching for Posteriors: Using normalizing flows to approximate complex, multi-modal posteriors \(P(\theta|\mathcal{D})\) that standard variational inference (which assumes unimodal Gaussians) fails to capture.

Connections

                 PREREQUISITES
                      |
               Basic Probability
                      |
                      v
               ╔══════════════════╗
               ║  CONDITIONAL PROB║
               ║  & BAYES THEOREM ║
               ╚══════════════════╝
              /      |       \
             /       |        \
            v        v         v
        ENABLES   ENABLES   ENABLES
           |         |         |
        Bayesian DL  VAEs   Naive Bayes
        (Uncertainty) (Gen AI)  (Baselines)
           |         |
           v         v
     Classifier Cal.  Variational Inference
     Platt Scaling    ELBO
           
  USED BY:
  - Hyperparameter Optimization (Bayesian Opt)
  - RL (Thompson Sampling)
  - Loss Function Design (Cross-Entropy is Likelihood)
  - Regularization (L2 is Gaussian Prior)

  EXTENDED BY:
  - Markov Chain Monte Carlo (MCMC)
  - Variational Inference
  - Information Theory (KL Divergence)

  RELATED CONCEPTS:
  - Base Rate Fallacy
  - Maximum Likelihood vs Maximum A Posteriori
  - Marginal Probability

Final Mental Model

Think of Bayes’ Theorem as the Belief Updater.

You walk into a room with a Prior belief (e.g., “Almost no one has the rare disease”). You observe Evidence (e.g., “The test is positive”). You apply the Likelihood (e.g., “If you had the disease, a positive test is very likely; but if you don’t, false positives still happen sometimes”).

Bayes’ Theorem multiplies your Prior by the Likelihood, and normalizes it by how common the Evidence is generally. The result is your Posterior—your new, updated belief about the world.

In Deep Learning, standard models only care about the Likelihood (making the data look probable). Bayesian models care about the Posterior (making the parameters probable given the data). The difference is the difference between a model that says “I saw this, so I guess that” and a model that says “Given what I already knew, and what I just saw, here is my calibrated confidence.”

The permanent memory aid:

Posterior \(\propto\) Likelihood \(\times\) Prior. Bayes flips the conditional. It forces the model to weigh new evidence against base rates (the prior) and the commonness of the evidence. Without it, models are overconfident and blind to uncertainty.

Comments

Popular posts from this blog

Why Everything in AI is a Vector ?

Why Everything in AI is a Vector ? Topic Overview In the previous lesson, you learned about the scalar: a single number representing magnitude. But reality is rarely one-dimensional. A single pixel has three color channels (red, green, blue). A word’s meaning has dozens of grammatical and semantic features. A model’s parameters number in the millions. To represent entities that possess multiple attributes simultaneously, we need a mathematical object that holds multiple scalars in a specific order. That object is a vector. A vector \(\mathbf{v} \in \mathbb{R}^n\) is an ordered list of \(n\) scalars. It represents a point, or an arrow from the origin, in \(n\) -dimensional space. Why does it exist? Because we need a mathematical language for things that have more than one degree of freedom. A scalar tells you “how hot.” A vector tells you “where you are” and “which way you’re going.” What problem does it solve? It provides the fundamental d...

Why Your PyTorch Code Keeps Breaking (It’s the Scalars)

Why Your PyTorch Code Keeps Breaking (It’s the Scalars) Topic Overview You are about to learn about the most fundamental object in all of mathematics and machine learning: the scalar. A scalar is a single number. That is the entire definition. But do not confuse simplicity with insignificance. Every neural network, no matter how many billions of parameters it contains, is ultimately optimized by reading and writing scalars. The loss value is a scalar. The learning rate is a scalar. The temperature parameter in softmax is a scalar. The scale factor in attention is a scalar. Every gradient clipping decision is based on a scalar. The final prediction of a classification model is a scalar probability. Why does the scalar exist as a named concept? Because not all numbers are created equal. Some numbers exist as components of larger structures—a coordinate in a vector, an entry in a matrix, a pixel in an image. A scalar is a number that stands alone...