The Unifying Mathematics of Neural and Statistical Inference
Mean field approximation is a powerful technique that appears in two seemingly different domains: statistical inference (factorizing posterior distributions) and neuroscience (replacing spike interactions with rate expectations).
This isn't just analogy—it's mathematical equivalence. Mean field neuroscience can be derived from maximum entropy, Gibbs measures, and statistical mechanics of spiking networks.
When Friston talks about neurons minimizing free energy, he is not using metaphor—he is exploiting the mathematical equivalence.
| Topic | Key Concept | Takeaway |
|---|---|---|
| Single Factor POMDP | Exact Bayesian inference | No structural approximation needed when state space is small |
| Algorithmic vs Structural | Two kinds of approximation | Gradient descent ≠ mean field factorization |
| Free Energy Gradient | ∂F/∂q = log q + 1 - log p | Where the +1 comes from (product rule!) |
| Multi Factor POMDP | Joint vs factorized inference | Correlations emerge when factors interact |
| Covariance Deep Dive | E[XY] - E[X]E[Y] | Why MFA forces Cov = 0 |
| MF Derivation | Expectations over other factors | Coordinate descent from free energy |
| The Click Moment | Fixed points & self-consistency | Independence given the mean |
| Neural Mean Field | Spikes → Rates | Same idea applied to neural populations |
| The Isomorphism | Math equivalence | One algorithm, two languages |
p(o) = Σₛ p(o|s)×p(s)q(s) = p(o|s) × p(s) / p(o)Instead of computing Bayes' rule directly, we can minimize free energy:
Since log p(o) is constant (it doesn't depend on q), minimizing F is equivalent to minimizing KL divergence—which means finding q that matches the true posterior!
F[q] = Σₛ q(s) × [ log q(s) - log p(o,s) ]
= Σₛ q(s) × [ log q(s) - log p(o|s) - log p(s) ]∂F/∂q(s) = log q(s) + 1 - log p(o|s) - log p(s)
↑ ↑
from q product rule on q×log(q)The +1 comes from the product rule: ∂/∂q [q×log q] = log q + 1. At equilibrium (∇F = 0), this gives us q* = p(s|o).
Now observations depend on both factors. If you're Near and facing Toward, you get a stronger signal than if you're Near but facing Away. This creates correlationsin the posterior.
| Toward | Away | |
|---|---|---|
| Near | 16.7% | 16.7% |
| Mid | 16.7% | 16.7% |
| Far | 16.7% | 16.7% |
This is a crucial distinction that often causes confusion:
| Type | What's Approximated | Can Recover Exact? |
|---|---|---|
| Algorithmic | The optimization path (gradient descent vs. closed-form) | ✅ Yes (with more iterations) |
| Structural (Mean Field) | The posterior's form: q(s₁,s₂) = q(s₁) × q(s₂) | ❌ No (correlations permanently lost) |
for i in iterations:
grad = ∂F/∂q
q = q - learningRate × gradThis is an iterative algorithm to find the same answer as closed-form Bayes. With enough iterations, it converges to: q(s) ∝ p(o|s) × p(s)
q(distance, direction) ≈ q(distance) × q(direction)This is a restriction on the form of the solution. Even with infinite iterations, you cannot recover the true correlated posterior.
Variational Inference (the framework)
│
├── Exact VI (family = all distributions)
│ └── Gradient descent (converges to exact)
│ └── Closed-form Bayes rule
│
├── Mean Field VI (family = factorized)
│ └── Coordinate descent
│
├── Gaussian VI (family = Gaussians)
│
└── ... other restricted familiesUnderstanding covariance is essential for grasping what mean field loses.
| Concept | Intuitive Name |
|---|---|
| Variance | "Average squared wiggle of X from its mean" |
| Standard Deviation | "Average wiggle of X from its mean" (standardized) |
| Covariance | "Average co-wiggle of X and Y from their means" |
| Correlation | "Standardized co-wiggle" (between -1 and +1) |
This asks: "How much does E[XY] differ from what it WOULD be if X and Y were independent?"
| When X is... | And Y is... | Product is... | Meaning |
|---|---|---|---|
| Above mean (+) | Above mean (+) | Positive | They move together! |
| Above mean (+) | Below mean (-) | Negative | They move opposite! |
| Below mean (-) | Below mean (-) | Positive | They move together! |
| Below mean (-) | Above mean (+) | Negative | They move opposite! |
Here's the key insight. Mean field says: q(d, dir) = q(d) × q(dir)
This defines the joint as a product of marginals. By construction:
E[distance × direction] = Σ_d Σ_dir d × dir × q(d, dir) = Σ_d Σ_dir d × dir × q(d) × q(dir) [mean field!] = (Σ_d d × q(d)) × (Σ_dir dir × q(dir)) = E[d] × E[dir]
Therefore:
Cov = E[d × dir] - E[d] × E[dir]
= E[d] × E[dir] - E[d] × E[dir]
= 0 ← ALWAYS zero in mean field!Here's something beautiful: |Cov(X,Y)| ≤ StdDev(X) × StdDev(Y). Always!
Dividing by this bound gives correlation: a unitless value between -1 and +1.
This is the "standardized" covariance—comparable across different variable pairs.
Let's derive the mean field update equations from the variational free energy. This shows exactly where the "expectation over other factors" comes from.
d ∈ {0, 1, 2} (Near, Mid, Far)
dir ∈ {0, 1} (Toward, Away)
Joint state space: 6 combinations (d, dir)
Observation: o (which angle bucket we see)
Generative model: p(o, d, dir) = p(o | d, dir) × p(d, dir)The assumption: q(d, dir) = q_d(d) × q_dir(dir)
Substitute and use log product rule:
F = Σ_d q_d(d) log q_d(d) [sum over dir = 1] + Σ_dir q_dir(dir) log q_dir(dir) [sum over d = 1] - Σ_d Σ_dir q_d(d) q_dir(dir) log p(o, d, dir)
∂F/∂q_d(d) = log q_d(d) + 1 - Σ_dir q_dir(dir) log p(o, d, dir)
└────────────────────────────────┘
This is an EXPECTATION!At the minimum:
The -1 + const is absorbed in normalization, so:
1. Initialize:
q_d = [1/3, 1/3, 1/3]
q_dir = [1/2, 1/2]
2. Repeat until converged:
a. Update q_d (holding q_dir fixed):
For each d:
log q_d(d) = Σ_dir q_dir(dir) × log p(o, d, dir)
Normalize: q_d = softmax(log q_d)
b. Update q_dir (holding q_d fixed):
For each dir:
log q_dir(dir) = Σ_d q_d(d) × log p(o, d, dir)
Normalize: q_dir = softmax(log q_dir)
3. Return q_d, q_dir| Single Factor | Multi-Factor Mean Field |
|---|---|
| ∂F/∂q(s) = log q(s) + 1 - log p(o|s) - log p(s) | ∂F/∂q_d(d) = log q_d(d) + 1 - E_q(dir)[log p(o,d,dir)] |
Mean field approximation replaces the joint posterior with a product of marginals. This makes inference tractable but forces correlations to zero.
Before we see "the click" where neural and statistical mean field become one, let's introduce the neural model from La Camera (2022).
Each neuron i has a binary state xᵢ ∈ { 0, 1 } (silent or spiking). Neurons are connected by synaptic weights wᵢⱼ.
| Symbol | Meaning | Domain |
|---|---|---|
| xᵢ(t) | Spike state of neuron i at time t | { 0, 1 } |
| rᵢ | Firing rate of neuron i (time-averaged) | [0, 1] |
| wᵢⱼ | Synaptic weight from neuron j to i | ℝ |
| Iᵢ | Total input current to neuron i | ℝ |
| I_ext | External input (sensory drive) | ℝ |
| σ(·) | Sigmoid transfer function | (0, 1) |
| β | Inverse temperature (sharpness) | ℝ⁺ |
| N | Number of neurons in the network | ℕ |
Input: Iᵢ(t) = Σⱼ wᵢⱼ × xⱼ(t) + I_ext Spike prob: P(xᵢ = 1) = σ(Iᵢ) = 1 / (1 + exp(-2β × Iᵢ)) Update: xᵢ(t+1) = 1 with probability P(xᵢ = 1)
Each neuron's firing depends on who actually spiked at time t. This creates complex dependencies and correlations.
Replace spikes with rates:
Iᵢ ≈ Σⱼ wᵢⱼ × rⱼ + I_ext
Self-consistency equation:
rᵢ = σ(Σⱼ wᵢⱼ × rⱼ + I_ext)
Or in vector form:
r = σ(W × r + I_ext)Instead of tracking binary spikes xⱼ(t), we use expected rates rⱼ = ⟨xⱼ⟩. The input becomes deterministic—no more fluctuations from individual spikes!
This is where mean field stops being a formal trick and starts to feel like a controlled lie. Let's understand the deep structure.
You have many interacting things. Tracking all interactions exactly is impossible.
| Domain | The "Things" | The "Interactions" | Why Intractable |
|---|---|---|---|
| Statistics | Random variables s₁, s₂, ... | Joint dependencies | Exponential state space |
| Neuroscience | Neurons 1, 2, ..., N | Synaptic connections wᵢⱼ | 2^N spike patterns |
| Physics | Particles, spins | Forces, couplings | 10²³ atoms |
Replace interactions with averages.
| Domain | Exact | Mean Field |
|---|---|---|
| Statistics | q(s₁,s₂) - full joint | q(s₁) × q(s₂) - product |
| Neuroscience | I = Σ wᵢⱼ × xⱼ(t) - spikes | I = Σ wᵢⱼ × rⱼ - rates |
| Physics | Each atom feels all others | Each feels average field |
Think of it as a feedback loop:
┌─────────────────────────────────────────┐ │ │ │ YOUR BELIEF ──────► WHAT YOU SEE │ │ ↑ │ │ │ │ │ │ │ └──────────────────────┘ │ │ (must agree!) │ │ │ └─────────────────────────────────────────┘
Firing rate r_i ──► determines input I_j = Σ w_ji × r_i
│
▼
determines firing rate r_j = σ(I_j)
│
▼
determines input I_i = Σ w_ij × r_j
│
▼
back to r_i!Self-consistent = the loop closes. What goes in equals what comes out. This is a fixed point.
Your belief q(s₁) ──► determines E[s₁]
│
▼
affects how you update q(s₂)
│
▼
which affects E[s₂]
│
▼
which affects how you update q(s₁)
│
▼
back to the start!This is perfectly stated:
| Term | Meaning | Example |
|---|---|---|
| Fixed point | A state that doesn't change | r* such that r* = σ(Wr* + I) |
| Iteration | Repeat until stable | r(t+1) = σ(Wr(t) + I) |
| Convergence | Getting closer to fixed point | r(t) → r* as t → ∞ |
| Self-consistent | Fixed point exists & meaningful | The physics/stats makes sense |
This sentence works for variational Bayes, spiking neural networks, magnetic materials, economics (mean field games), epidemiology (SIR models)—it's the same mathematical structure everywhere.
In a network of N neurons, each neuron i receives input from all others. The exact input depends on who actually spiked. Mean field replaces this with the expected input based on firing rates.
Exact simulation equations:
I_i(t) = Σ_{j≠i} w_ij × x_j(t) + I_ext
P(spike) = σ(I_i) = 1 / (1 + exp(-2β × I_i))
x_i(t+1) = 1 with probability P(spike)
Mean field self-consistency:
r = σ(W × r + I_ext)
For a HOMOGENEOUS network (all neurons identical):
r* = σ(J × r* + I_ext)
where J = N × w̄ is the TOTAL EFFECTIVE COUPLING
N = number of neurons
w̄ = average synaptic weight per connection
Each neuron's rate depends on others' rates. Iterate until consistent.Watch mean field converge to its fixed point
100 neurons, 500 time steps
Click "Run Comparison" to see exact vs mean field...
The equation r = σ(J × r + I_ext) has r on both sides. It doesn't guarantee a fixed point exists—you have to prove it.
For strong enough coupling J, the sigmoid curve can intersect the diagonal three times—givingtwo stable attractors and one unstable fixed point in between!
r
↑
1.0 │ .--------- ← σ(Jr + I)
│ .´
│ .´
│ .´ * ← Fixed point (stable)
│ /
│ /
│ / * ← Fixed point (unstable)
│ /
│/
│ * ← Fixed point (stable)
0.0 └────────────────────→ r
0 1Which attractor do you find? It depends on initial conditions! This isbistability—the system has memory. In the brain, bistability can represent working memory, decision making, or attention.
The mathematical structure of mean field is identical in both domains:
| Concept | Neuroscience | Variational Inference |
|---|---|---|
| Individual unit in the system | Neuron i | Variable sᵢ |
| Discrete state of each unit | Spike xᵢ ∈ {0,1} | State sᵢ ∈ {1,...,K} |
| Expected value / distribution | Firing rate rᵢ = ⟨xᵢ⟩ | Marginal qᵢ(sᵢ) |
| Interaction strength | Weight wᵢⱼ | Coupling log p(sᵢ|sⱼ) |
| Aggregated influence from others | Input Iᵢ = Σⱼ wᵢⱼrⱼ | log qᵢ = E_{qⱼ}[log p] |
| Nonlinear activation | Transfer φ(I) = sigmoid | Transfer = softmax |
| What mean field ignores | Noise η | Discarded correlations |
| Independence assumption | Asynchronous state | Factorized q = Πᵢ qᵢ |
| Self-consistency equation | r = φ(Wr + Iₑₓₜ) | Fixed point of coord descent |
| Step | Neural | Variational |
|---|---|---|
| Initialize Expectations | r_i ← 0.5 (random rates) | q_i ← uniform (uninformative marginals) |
| Compute Input | I_i = Σ_j w_ij × r_j + I_ext | log q_i = E_{q_{-i}}[log p(s_i, s_{-i})] |
| Apply Transfer | r_i ← sigmoid(I_i) | q_i ← softmax(log q_i) |
| Check Convergence | max|r_new - r_old| < ε | max|q_new - q_old| < ε |
| Scenario | Mean Field Needed? | Why? |
|---|---|---|
| Single factor, small state space | No | Exact inference is tractable |
| Multiple interacting factors | Often | Joint space grows exponentially |
| Neural population with correlations | Yes | Spike-by-spike tracking intractable |
| Weak correlations, care about means | Good fit | Mean field error is small |
| Strong correlations matter | Problematic | Mean field discards what you need |
| Concept | Equation |
|---|---|
| Bayes Rule | p(s|o) ∝ p(o|s) × p(s) |
| Free Energy | F[q] = Σ q(s)[log q(s) - log p(o,s)] |
| Free Energy Gradient | ∂F/∂q(s) = log q(s) + 1 - log p(o|s) - log p(s) |
| Mean Field Factorization | q(s₁,s₂) ≈ q(s₁) × q(s₂) |
| Neural Self-Consistency | r = σ(W × r + I_ext) |
| Covariance | Cov(X,Y) = E[XY] - E[X]×E[Y] |