This Probability concept is the current idea: keep the same invariant visible across Intuition, Math, Code, Interactive Demo.
Probability
Variational Inference
Variational inference turns posterior inference into optimization over a tractable family: maximize the ELBO, and the remaining gap is KL(q || posterior).
Concept Structure
Variational Inference
Start with the picture, metaphor, or geometric mechanism.
Make the objects explicit and connect them with notation.
Mirror the equations with runnable implementation details.
Manipulate the mechanism and watch the idea respond.
Learner Contract
What this page should let you do.
4 prerequisites listed; refresh them before leaning on the math or code.
Explain the mechanism, trace the main notation, and test one prediction in the live demo.
Read the intuition before the notation; the math should name a mechanism you already felt.
Follow this edge after making one prediction here; the next page should reuse the result, not restart the route.
Claim/source review status
Substantive review recorded
1/1 claims have bounded review metadata; still check caveats and source scope.Metadata-derived; review may be AI-assisted. Not a human certification.01
Intuition
Build the mental picture first so the rest of the page has something to attach to.
Bayesian inference gives the object we want:
the posterior distribution over hidden variables after observing data .
The trouble is that this posterior often requires a sum or integral over many hidden configurations. In a realistic latent-variable model, exact posterior inference can be the expensive part.
Variational inference changes the question. Instead of asking for the exact posterior, it asks:
Which distribution inside a tractable family is closest to the posterior?
Call that tractable distribution . It might be a fully factorized mean-field distribution, a Gaussian with diagonal covariance, or a neural network that predicts distribution parameters. The family is the bargain. It makes optimization possible, but it also decides what kinds of uncertainty can be represented.
The core learning move is to watch the gap:
The left side, , is the log evidence or marginal log likelihood. The first term, , is the evidence lower bound (ELBO). The second term is the price of approximation. If can match the true posterior, the KL gap is zero. If the family cannot express the posterior, the gap remains.
This is why variational inference is not "Bayes, but faster" in a casual sense. It is posterior inference turned into optimization, with a visible approximation contract.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
Let be observed data and let be latent. A latent-variable model specifies a joint density or mass function
where are model parameters. The exact posterior is
The evidence is the normalizer
or an integral when is continuous. This sum is often the hard object.
Choose a variational distribution from a family . The ELBO is
Equivalently,
where is entropy. A good variational distribution must put mass on high-joint-probability latent states, but it is also rewarded for keeping enough entropy when that helps the bound.
Now derive the gap. Start from the reverse KL to the exact posterior:
Use :
So
Because KL divergence is nonnegative,
This is also Jensen's inequality in disguise:
assuming only where the ratio is well-defined.
Mean-field variational inference chooses a factorized family such as
This can make coordinate updates tractable: optimize one factor while holding the others fixed. The cost is representational. If the posterior has strong dependence or multiple separated modes, a simple family may fit only one part of it.
The direction of KL matters. The usual ELBO identity minimizes , often called exclusive or reverse KL in this context. With a restricted family, it can prefer a sharp approximation around one mode rather than spreading mass through a low-probability region between modes. That is a feature of the objective, not a bug in the arithmetic.
Amortized variational inference changes how is produced. Instead of optimizing separate variational parameters for every , a network predicts them:
That is the bridge to variational autoencoders. The general idea here is still the same: optimize an ELBO, inspect the KL gap, and remember what the variational family can and cannot express.
03
Code
Keep the implementation aligned with the notation so the algorithm is legible.
import numpy as np
z = np.array([-2, -1, 0, 1, 2])
log_joint = np.array([-2.9, -0.62, -3.9, -0.78, -2.4])
candidates = {
"one_mode": np.array([0.06, 0.78, 0.11, 0.04, 0.01]),
"covers_both": np.array([0.14, 0.24, 0.24, 0.24, 0.14]),
"wrong_center": np.array([0.05, 0.18, 0.54, 0.18, 0.05]),
}
def logsumexp(v):
m = v.max()
return m + np.log(np.exp(v - m).sum())
log_px = logsumexp(log_joint)
posterior = np.exp(log_joint - log_px)
def score(q):
expected_joint = np.sum(q * log_joint)
entropy = -np.sum(q * np.log(q))
elbo = expected_joint + entropy
gap = log_px - elbo
kl = np.sum(q * (np.log(q) - np.log(posterior)))
return expected_joint, entropy, elbo, gap, kl
print("posterior:", np.round(posterior, 3).tolist())
print("log evidence:", round(log_px, 3))
for name, q in candidates.items():
ej, h, elbo, gap, kl = score(q)
print(name, "ELBO", round(elbo, 3), "gap", round(gap, 3), "KL", round(kl, 3))
The code mirrors the identity. log_px is exact because this toy latent space has only five states. Each candidate gets an expected joint score, an entropy reward, an ELBO, and a KL gap. The printed gap and KL match because
04
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
The lab below hides the posterior and the ELBO ledger until you commit.
Choose a latent posterior case, inspect the candidate distributions, and predict which candidate has the highest ELBO. After reveal, compare three things:
- the exact posterior ;
- the ELBO ledger for each ;
- the gap .
Use the two-mode case as the main warning. A broad has more entropy, but it also puts mass through a low-joint-probability bridge. Under , the one-mode approximation can win even though it misses the other explanation.
Then switch to the tight case. When the family contains a good approximation, the KL gap nearly disappears. That contrast is the mechanism.
Live Concept Demo
Explore Variational Inference
The stage is code-native and interactive. Use it to test the explanation against the mechanism.
Manipulate one control and predict the visible change.
Commit to what Variational Inference should make visible before reading the result.
After The First Pass
Turn the concept into an inspected object.
Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.
Mechanism Storyboard
See the idea move before the page explains it
Variational inference turns posterior inference into optimization over a tractable family: maximize the ELBO, and the remaining gap is KL(q || posterior).
Start with the picture, metaphor, or geometric mechanism.
Before reading further, choose the kind of change Variational Inference should make visible.
Visual Inquiry
Make the image answer a mathematical question
Variational inference turns posterior inference into optimization over a tractable family: maximize the ELBO, and the remaining gap is KL(q || posterior).
Which visible object should carry the first intuition?
Pick the cue that should make Variational Inference easier to reason about before the page gives the answer.
Source Grounding
Canonical references for the mechanism on this page.
Supports VI as inference-as-optimization, KL(q||p), the variational lower bound/ELBO, reverse-KL support behavior, mean-field families, and coordinate-ascent intuition.
Open sourceReview source for VI as approximating difficult posterior densities by optimizing within a chosen family using KL divergence, with mean-field and stochastic-optimization context.
Open sourceDeep-learning textbook anchor for approximate inference, variational methods, KL direction, and mean-field caveats.
Open sourceBroad probabilistic-ML reference for modern approximate inference and the relationship between variational and Monte Carlo inference.
Open sourceSupports the latent-variable ELBO, the KL gap to the true posterior, black-box VI, amortized VI, and the VAE bridge.
Open sourceClaim Review
Variational inference turns posterior inference into optimization over a tractable family: maximize the ELBO, and the remaining gap is KL(q || posterior).
Claims without a substantive review badge still need exact source-support review.
cs228-variational-inference-notes, blei-2017-vi-review, goodfellow-2016-deep-learning-inference, murphy-2023-probml-advanced, cs236-vae-notes
Use equations, runnable code, and demos to check whether the source support is operational.
The sources support the ELBO identity, family-restricted approximation, reverse-KL objective, mean-field coordinate optimization context, stochastic/amortized VI bridge, and the caveat that optimization/family choices control the returned approximation.
Sources: CS228 Notes: Variational Inference, Variational Inference: A Review for Statisticians, Deep Learning, Chapter 19: Approximate Inference, Probabilistic Machine Learning: Advanced Topics, Deep Generative Models Notes: Variational AutoencodersFinite discrete witness only; excludes continuous estimators, structured/collapsed/flow VI, convergence guarantees, and calibration.A bounded review summary is present; still check caveats and exact reference scope.Checked CS228 for VI as optimization over a family, KL(q||p), the variational lower bound/ELBO, reverse-KL support behavior, and mean-field coordinate ascent; checked Blei et al. for the family-plus-KL optimization framing and caveats; checked CS236-style notes for latent-variable ELBO, KL gap, black-box VI, amortized VI, and VAE bridge. GPT Pro publication critique remains pending.
Reviewer: codex-local-source-review; reviewed 2026-07-02Source support candidates
course-notes 2026CS228 Notes: Variational InferenceSupports VI as inference-as-optimization, KL(q||p), the variational lower bound/ELBO, reverse-KL support behavior, mean-field families, and coordinate-ascent intuition.
paper 2017Variational Inference: A Review for StatisticiansReview source for VI as approximating difficult posterior densities by optimizing within a chosen family using KL divergence, with mean-field and stochastic-optimization context.
book 2016Deep Learning, Chapter 19: Approximate InferenceDeep-learning textbook anchor for approximate inference, variational methods, KL direction, and mean-field caveats.
book 2023Probabilistic Machine Learning: Advanced TopicsBroad probabilistic-ML reference for modern approximate inference and the relationship between variational and Monte Carlo inference.
Practice Loop
Try the idea before it explains itself
Variational inference turns posterior inference into optimization over a tractable family: maximize the ELBO, and the remaining gap is KL(q || posterior).
Before touching the demo, predict one visible change that should happen in Variational Inference.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
A concrete answer is on the canvas.
The answer names why the claim should hold.
It touches the page context or a neighboring idea.
Research Room
Attach the question to a claim, equation, code, or demo
Pick the concept, equation, source, runnable code, claim, misconception, or demo state before asking for help. The handoff keeps that page item in context.Open the draft below to save one note and next action in this browser.
Variational Inference
What is the smallest example that makes Variational Inference click without losing the math?
Local action draftNo local draft saved yetExpand only when ready to capture one local next action
This draft stays in this browser, attached to the selected learning item.
- References to inspect: attached references on this page.
- Definition, prerequisite, and contrast concept links
- The equation or runnable code that makes the concept operational
- One demo state that shows the invariant instead of a slogan
- The learner can state the mechanism in their own words
- The learner can name the prerequisite that would repair confusion
- The learner can predict how the mechanism changes under one perturbation
I am working in Continuous Function's research reading room. Object: concept - Variational Inference Selected item key: recorded for copy. Context: Probability Page anchor: recorded for copy. Open question: What is the smallest example that makes Variational Inference click without losing the math? Evidence to inspect: - References to inspect: attached references on this page. - Definition, prerequisite, and contrast concept links - The equation or runnable code that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.
concept/concept-notebook/probability/variational-inference
concept:probability/variational-inference