An Introduction to Bayesian Mechanics
Follow one quantity, surprise, from a single outcome to the free energy bound in our PCT vs. FEP paper, then to the Markov blanket that makes the bound a physical system’s belief, the motion that carries that belief downhill, and the actions it chooses.
What “Bayesian mechanics” names
In Roachford, Mansell & Pena (2025), Bayesian mechanics is the mathematical toolkit of the Free Energy Principle, in the way that control theory is the toolkit of Perceptual Control Theory. The part of that toolkit the paper uses is variational Bayesian inference (its §2.2.1), applied to a model of bacterial chemotaxis (§2.2.2). This tutorial follows that derivation equation by equation, with the paper’s E. coli model as the running example. It then shows what makes the model’s belief a physical system’s belief (the Markov blanket), how that belief moves, and how it chooses an action, before continuing past the paper to beliefs split into several factors.
An E. coli that infers where it is heading, then approaches or tumbles
The Paper’s “Mistake”, and the Lesson
While creating this tutorial, we found that the paper's code as published resets the prior to a uniform distribution at every time step, so its action depends only on what it has inferred from the situation at that time step, rather than on previous time steps. When the E. coli's sensors are perfect, as in the paper, this makes no difference to decision making. When hi is less than one, decision making suffers, but mostly for a second reason: the code's ten gradient steps (variational inference) never settle, because a probability clipped to zero bounces back on the next step, so the belief cycles instead of reaching the posterior. An exact update that keeps the same uniform prior does almost as well as filtering (82–86% of steps inside the circle for hi from 0.6 to 0.8). The “exact filtering” panel fixes both problems at the same hi. In the simulation the readings themselves are always exact; hi sets how noisy the agent believes its sensors are.
The cycling turns out to be a matter of step size. Each of the code's steps moves the belief by 0.1 times the slope of free energy, section 07's gradient, and that step is too big: it pushes a probability below zero, the code clips it to zero, and the logarithm in the next slope, ln(0 + 10⁻¹⁶) ≈ −37, throws it back. Taking more steps of that size makes things worse: with 100 of them, the bacterium spends only 15% of its steps inside the circle at hi = 0.7. With steps of 0.01 nothing is clipped, the belief settles, and the same update keeps the bacterium inside the circle 83–87% of the time for hi from 0.6 to 0.8, level with exact filtering. Run until free energy stops changing, it still lands a little off Bayes' answer: dividing by the total after each step hands extra probability to states that are already likely, so it settles more confident than the posterior (0.96 where Bayes gives 0.70). Stepping in logits, as in section 07, removes that bias, and with the prediction in place of the uniform prior, gradient descent on free energy, run until it stops changing, reaches exactly the filtering of the right-hand panel.
Full demo: the top FEP and PCT agents from a parameter searchCode on GitHubThe paper
Is it just statistics? The equations are probability theory throughout. The word “mechanics” follows physics usage, as set out by Ramstead et al. (2023): a mechanics supplies equations of motion, and a principle explains why those equations hold. Classical mechanics follows from the principle of stationary action. Bayesian mechanics is the set of mechanical theories that follow from the free energy principle, for systems that look as if they hold probabilistic beliefs about their environment.
How is it different from statistical mechanics? Statistical mechanics assigns probabilities to a system’s states. Bayesian mechanics keeps that description and adds a second one: the system’s internal states are read as the parameters of a probability distribution, a belief, about the external states on the other side of its Markov blanket (section 08). The shared mathematics is exact rather than a loose analogy. Section 12 shows that variational free energy has the form of the Helmholtz free energy, energy minus entropy, where the energy of a configuration is its surprisal.
Sections 01–02 are a brief recap; From Information Theory to Information Geometry develops entropy, cross-entropy and KL divergence in full. Sections 03–08 and 10 follow the PCT vs FEP paper. Sections 09 and 11–14 go beyond it.
Surprise and entropy
An outcome with probability carries a surprisal of . Rare outcomes carry more information than common ones, and two independent outcomes carry the sum of their information, because the logarithm turns the product of their probabilities into a sum. Averaging surprisal over the outcomes the distribution produces gives its entropy:
Entropy is also the shortest possible average code length: an optimal code spends bits on outcome . For discrete outcomes, every integral in this tutorial becomes a sum. Base 2 counts bits; from section 03 on we use natural logarithms, which count nats (1 nat ≈ 1.44 bits). Changing the base only changes the unit.
Cross-entropy and KL divergence
Suppose outcomes come from but we encode them with a code built for . Each outcome now costs , and the average cost is the cross-entropy . Its first argument is what gets encoded; its second is the code. Subtracting the unavoidable cost leaves the excess caused by using the wrong code: the Kullback–Leibler divergence.
Turning that difference into the usual single integral is where signs are easy to lose. Each line below changes one thing:
Two properties carry the rest of the tutorial. KL divergence is never negative (Gibbs’ inequality), and it is zero only when the two distributions agree. And it is not symmetric: averages over , while averages over . Wherever the averaging distribution has probability and the other has none, the divergence is infinite.
Bayes’ theorem and the marginal likelihood
The paper writes beliefs about a hidden state , updated by an observation , with Bayes’ theorem (its Figure 8):
The product is the joint distribution , which the paper calls the agent’s generative model. The denominator needs that product for every possible hidden state:
The E. coli model. The hidden state is the bacterium’s distance from the attractant: near, mid or far. The observation is the angle between its heading and the direction of the target, binned as 0–120°, 120–240° and 240–360° (the paper’s Figure 10). The likelihood matrix A stores , and the prior D stores , uniform in Table 3. Table 7 sets A with one number, the likelihood sharpness: probability hi on the diagonal and lo = (1 − hi)/2 elsewhere. As the vector and matrix the model uses:
Multiply prior by likelihood, add the products, divide
With three states, Equation (1) is a three-term sum. The difficulty the paper describes appears as the model grows: with K hidden factors of n states each, the sum has terms, and for continuous states the integral rarely has a closed form. Variational inference is a way to approximate the posterior without ever computing .
Guessing the posterior
Bayes’ theorem can be rearranged so that the unknown marginal likelihood stands alone. The paper’s Figure 9 does this and puts a guess, , where the posterior was:
The paper calls the conjectured new prior, because after an observation the posterior becomes the prior for the next one. If the guess is the exact posterior, the right-hand side gives the same number for every hidden state, and that number is the marginal likelihood we could not compute. If the guess is wrong, the number changes from state to state.
In the E. coli model with hi = 0.7 and the 0–120° bin observed, two guesses give:
The prior is a wrong guess: its ratios disagree. The posterior is the right guess: every ratio equals .
This is the idea behind variational inference. Instead of computing the posterior directly, guess , measure how wrong the guess is, and adjust it. In the paper’s words, variational free energy minimization formalizes this procedure by allowing for adjustments based on a statistical gradient. The next section derives the measure.
Variational free energy bounds surprise
The guess is called the approximate posterior. To make it as close as possible to , minimize the divergence between them (the paper’s Equation 2):
The posterior inside the logarithm still contains . Replace it using Bayes’ rule:
Dividing by a fraction multiplies by its reciprocal, so moves to the numerator, and the logarithm of the product separates into a sum:
The second term does not depend on , so its integral against is . This gives Equation (4):
Every factor inside is available: our own , the likelihood and the prior. The remaining term is the log model evidence; written as it is the surprise of the observation, Shannon’s quantity from section 01. Rearranging gives Equation (5):
Equation (5) says the surprise equals minus the KL divergence. Rearranged, . The KL divergence is never negative. Jensen’s inequality shows why: the negative log is convex, so the average of the negative log is at least the negative log of the average:
Start from the definition, Equation (2).
Flip the fraction inside the log, which flips its sign: log(a/b) = −log(b/a). The integral is now the average, over q(s), of the negative log of p(s|o)/q(s).
Jensen’s inequality: move the negative log outside the average. Because the negative log is convex, the average of the negative log is at least the negative log of the average.
q(s) cancels.
The posterior is a probability distribution, so it integrates to 1, and log 1 = 0.
Therefore:
Subtract from both sides:
Variational free energy is an upper bound on surprise. Once has been observed, is fixed, so lowering by changing can only lower the KL term. Minimizing free energy moves toward the true posterior without computing .
Move q(s) and watch free energy sit above surprise
| state | ratio | ||
|---|---|---|---|
| near | 0.233 | 0.200 | 1.167 |
| mid | 0.050 | 0.300 | 0.167 |
| far | 0.050 | 0.500 | 0.100 |
| true marginal likelihood p(o) | 0.333 | ||
The table in the lab repeats section 04’s test for any guess: its ratios agree only when matches the posterior, which is also when the gap reaches zero.
Both, depending on the sign. Machine learning maximizes the evidence lower bound, , which lies below . The free energy literature minimizes , which lies above . It is the same quantity with the same optimum; most machine learning papers use the lower-bound wording.
Why KL argument order matters
Section 02 defined : the excess bits left over from encoding with . That interpretation is always true. The first argument is what gets encoded, and every average is taken over it. The second argument is the code that does the encoding. Likewise, Equation (2), , is the excess bits left over from encoding with .
What changes is which distribution we encode. In both procedures we adjust our distribution to match a fixed distribution , and the goal is the same: zero excess bits, which happens only when . Adjusting toward that goal is called fitting to ; when can’t equal , fitting stops at the that leaves the fewest excess bits. Maximum likelihood estimation is a procedure for finding the parameters of a distribution, our model , that best fit some data. Let’s call the distribution the data come from . Maximum likelihood looks for the model that best encodes . Variational inference instead looks for the that is best encoded by the posterior . Those are the two ways to fit to a distribution: by encoding it, or by being encoded by it.
| First argument (encoded) | Second argument (the code) | What we adjust | |
|---|---|---|---|
| Maximum likelihood | the data | the model | the code |
| Variational inference | the guess | the posterior | what is encoded |
One rule sets the order in both: the first argument must be a distribution we can average over. Averaging a quantity over a distribution , written , means weighting each value by how probable says is. There are two ways to do it: “analytically,” as we have been doing so far in this tutorial, and “empirically,” using samples from a particular distribution, as in maximum likelihood. This is illustrated in the following figure:
Weight every value by its probability and add them up. Exact, but it needs the formula for .
Used for our guess , whose formula we chose.
Draw samples from and average over them. An estimate that improves as grows.
Used for the data, samples of , whose formula we don’t know.
Maximum likelihood averages empirically, over the data. Because does not depend on , minimizing is minimizing the cross-entropy , the average negative log-likelihood. In variational inference we cannot average over the posterior directly: its formula needs , the number we set out to avoid, and sampling it takes slow methods such as Markov chain Monte Carlo. So our guess goes first, and every average is analytical: in Equations (2)–(4), every average is over , and separated as a constant because .
In short, KL divergence is not symmetric: swapping its arguments changes its value, and so changes which approximation is best. Machine learning calls the maximum likelihood order, , the forward (or inclusive) direction, and the variational order, , the reverse (or exclusive) direction.
Both orders reach zero only when , so if can match exactly, both pick the same . They disagree only when it can’t, and then each order charges for a different mistake:
Encoding with costs many excess bits wherever puts probability that lacks.
Like a grader who marks down every false claim: the safest answer says less, but only true things.
Encoding with costs many excess bits wherever has probability that lacks.
Like a grader who marks down every true thing left out: the safest answer covers everything.
Same target, different cost of mistakes. To see it, take a distribution with two modes. A mode is a peak of a distribution, a value more probable than the values around it. A Gaussian has one mode, at its mean; the distribution below is an equal mix of two Gaussians, so it has two. Now fit a single Gaussian to it from both directions. In maximum likelihood it plays , and we look for the that best encodes it. In variational inference it plays the posterior , and we look for the that it best encodes:
Fit one Gaussian to a two-mode distribution
- the distribution q is fit to: pdata in maximum likelihood, p in variational inference
- q that p best encodes, KL[q ‖ p] (variational inference)
- q that best encodes pdata, KL[pdata ‖ q] (maximum likelihood)
The variational direction prefers a that stays inside , even if that means ignoring part of it. Section 11 shows the same preference as overconfidence when is forced to factorize.
Both orders in one algorithm
Maximum likelihood is direct when the model has no hidden variables. When it has some, the two orders work together. Write the model as , with parameters and hidden states . In maximum likelihood with hidden state variables, the likelihood is hard to work with, just as is in variational inference: both are sums over the hidden states, here . With that sum inside the log, the parameters can’t be solved for directly, and with many hidden states the sum itself becomes too expensive. So expectation–maximization lowers instead, alternating two steps:
Adjust with fixed: inference about the hidden states.
, the guess first.
Adjust with fixed: maximum likelihood with the hidden states filled in by .
Raises the bound on .
EM repeats the two steps until neither changes anything. The whole procedure is maximum likelihood, with the data first: . Each first argument is one we can average over: the guess analytically, the data empirically.
Four methods, one question
Maximum likelihood and Bayes’ theorem answer different questions. Maximum likelihood gives one best value for the parameters , which every observation shares. Bayes’ theorem gives a belief distribution over the hidden states : the posterior, the belief that the model and the observation imply. Each has a direct form and an approximate form, and one question decides which you can use: can you work with the sum over the hidden states directly?
| No hidden sum, or an easy one | A hard sum over hidden states | |
|---|---|---|
| Parameters : one best value | maximum likelihood, directly | expectation–maximization: lower over and |
| Hidden states : a belief distribution | Bayes’ theorem, directly | variational inference: lower over |
So both approximate forms are paths through a free energy landscape, but expectation–maximization alternates the method it uses to move through that space. The question one might have is: why does expectation–maximization alternate?
It alternates because depends on two different unknowns, but each of these two unknown variables is easy to find when we hold the other fixed. With fixed, the best is the posterior, or the closest its family allows. With fixed, the best is maximum likelihood with the hidden states filled in by . Variational inference with a single factor has only to find, so there is nothing to alternate between. Once splits into several factors, variational inference alternates too: coordinate ascent in section 11 updates one factor while the others hold still. Expectation–maximization is the same move. Splitting into two factors, and , gives two unknowns to alternate between, just as and do in expectation–maximization. Either way, each step lowers with respect to one unknown while the other holds still.
One factor: exact Bayes, and how variational inference reaches it
The E. coli model has a single hidden factor, the distance , and may be any distribution over its three states. Nothing forces an approximation, so Bayes’ theorem gives the posterior directly, as the figure in section 03 computed it:
The second form is the paper’s matrix notation, with o a one-hot vector that selects the observed row of A and the softmax (exponentiate, then normalize). It is the same computation: the logarithm turns likelihood times prior into a sum, and the softmax exponentiates it back and divides by the total, .
After the bacterium acts, the prior for the next step is its prediction from the transition matrix for that action (Table 5 for “approach”): . With one factor, this is exact Bayesian inference, repeated at every time step.
The same answer by variational inference
Variational inference isn’t needed here, but this small model is the clearest place to learn how it works, because every step can be checked against Bayes’ theorem. Start from any guess and nudge it downhill on until stops falling. The nudges come from the chain rule of calculus, just as in training a neural network: write through unconstrained numbers , work out how changes with each one, and move each one the other way.
Free energy, Eq. (4), with the likelihood times the prior written as the joint p(o, s).
Write the guess as the softmax of unconstrained numbers v, one per state, the way a neural network writes class probabilities. Every v gives a valid distribution, so v can move freely.
How F changes with each probability: the derivative of q ln q is ln q + 1.
How each probability changes with each v: the derivative of the softmax, where δ is 1 when s = j and 0 otherwise. Raising v(j) raises q(j) and lowers every other q(s).
The chain rule: v(j) reaches F through every q(s), so add up those paths, as backpropagation does through the layers of a network.
Multiply out. The δ keeps only the s = j term; the −q(j) multiplies a q-weighted sum over every state.
The +1s cancel because q sums to 1, and the remaining sum is F itself.
Nudge every v(j) a small step η against its slope, then repeat. With small enough steps, F falls every time.
Where v comes from: writing q through logits
is a categorical distribution: one probability for each state. Gradient descent doesn’t know that probabilities must stay at or above zero and add to 1, so a step taken on the probabilities themselves can break either rule. Writing as a function of other numbers avoids that. Give each state a number , which can be any real number, and build from them in two moves: exponentiate, which makes every value positive, then divide by the total, which makes them add to 1. That is the softmax, and machine learning calls the numbers logits. is still the same categorical distribution; the logits are simply the numbers we adjust instead of the probabilities.
A coin shows how it works. With two states, heads and tails, :
| v(heads), v(tails) | q(heads) | q(tails) |
|---|---|---|
| 0, 0 | 0.50 | 0.50 |
| 2, 0 | 0.88 | 0.12 |
| −1, 0 | 0.27 | 0.73 |
| 3, 1 | 0.88 | 0.12 |
Only the differences between logits matter: adding the same number to every logit leaves unchanged, which is why the last row matches the second.
The logits are not fixed in advance. They start somewhere, here at the prior’s logits , and gradient descent moves them. For the E. coli model at hi = 0.7 with a 0–120° reading:
| v(near) | v(mid) | v(far) | q = σ(v) | |
|---|---|---|---|---|
| start: | −1.10 | −1.10 | −1.10 | 0.33, 0.33, 0.33 |
| end: , plus any constant | −1.46 | −3.00 | −3.00 | 0.70, 0.15, 0.15 |
Since , the end row is the inside of the matrix formula above, . Bayes’ theorem in matrix form is the softmax of the logits at the bottom of the hill.
How the paper did it. The paper’s §2.2.1 says that in practice, minimizing free energy means gradient-based optimization of the parameters of , and leaves the method open. Its code (src/core/Bayes.ts) steps the probabilities directly instead of logits. It uses the gradient from the third row above, , and repairs the two rules after every step: it sets negative values to zero and divides by the total. It takes ten steps of size 0.1 for each observation, starting from the current belief. The same file also has a one-step version, the softmax of , which is the matrix formula above. Both routes aim at the same posterior; logits make every step valid without the repairs.
Every slope is zero exactly when is the same for every state, which means is proportional to the joint: . There the common value is , so equals the surprise, the floor of the bound. Variational inference arrives at the answer Bayes’ theorem gave directly, which is also why the Set q = posterior button in section 05 reaches the floor. Section 09 runs these nudges as a continuous motion.
The approximation enters only when is restricted. The paper notes that numerous algorithms exist for minimizing free energy; section 11 builds one of the most widely used, for beliefs split into several factors.
The Markov blanket: whose belief, about what
So far has been a distribution we compute. Bayesian mechanics asks when it is something a physical system has. The paper’s §1.3 gives the starting point: to be picked out as a “thing” at all, a system must be sparsely coupled to what surrounds it. Its toy example is a bucket split by a divider, red liquid on one side and blue on the other; the divider is what lets an observer tell them apart as two things. The paper formalizes that separation by partitioning a system’s states into internal, sensory and active states, with everything else designated external. The partition is a Markov blanket.
Its defining property is conditional independence: external states are independent of internal states given the sensory and active states. The environment can affect the internal states only through the sensory states, and the internal states can affect the environment only through the active states. Together, sensory and active states are the blanket states. With external states , sensory states , active states and internal states :
Ramstead et al. (2023) write η for external states and s for sensory states. This tutorial keeps s for the hidden state and o for the observation, as in every section before this one.
Four kinds of state, and the blanket between them
| Kind of state | In the E. coli | In the paper's model |
|---|---|---|
| External | the attractant field, and where the cell is and which way it heads in it | the hidden state s: near, mid or far |
| Sensory | the receptors’ reading | the observation o: the angle bin |
| Active | the flagellar motors: run or tumble | the action a: approach or tumble |
| Internal | the signaling chemistry inside the cell | the parameters of the belief q(s) |
Ramstead et al. (2023) add the step that makes this mechanics Bayesian. Because internal and external states are independent given the blanket, the internal states can be read as the parameters of a belief about the external states. A function called the synchronization map takes the most likely internal state, given the blanket states, to the most likely external state. For any system that meets three conditions, the map follows automatically: the system has settled into a steady state; the map describes its long-run behavior rather than every moment; and different blanket states lead to different most likely internal states, so each internal state points back to one blanket state. These are conditions on the physical system, not on a model someone writes down. A system that meets them comes with its own generative model, its steady-state distribution. A system that doesn’t cannot be read this way.
In the E. coli model, the internal state is the belief over near, mid and far. The paper’s generative model is an abstraction its authors chose: it describes the bacterium’s situation with three distances, three angle bins and two actions, a coarse-graining of continuous position, heading and time. Deriving the model from the cell itself would mean starting from equations of motion for its chemistry and surroundings, showing that they have a Markov blanket and a steady state, and reading the generative model off that steady state.
Each step is hard. Equations for E. coli’s signaling chemistry exist (Tu, Shimizu & Berg 2008), but joined to the cell’s motion through an attractant gradient they form a nonlinear, noisy system, and the steady-state distributions of such systems rarely have a closed form (Risken 1996). Exact blankets are rare too: even in linear systems, sparse coupling in the equations of motion does not in general produce a Markov blanket in the steady state (Aguilera et al. 2022). Where the physics isn’t known, deriving the model is impossible.
Bruineberg et al. (2022) discuss the gap between blankets used as a modeling tool and blankets claimed as physical boundaries. Writing down an abstraction, as the paper does, is standard practice in applied active inference, and the blanket still says which parts of the cell such a model describes. The next two sections take the blanket’s two halves in turn: section 09 how the internal state moves, and section 10 how the active state is chosen.
Inference as motion in belief space
A mechanics needs an equation of motion. Section 07 nudged downhill on in small steps. Making the steps infinitely small turns them into a flow, an equation of motion for the belief:
Along this flow, free energy can only fall. By the chain rule in time, its rate of change is a sum of squares with a minus sign, and it stops falling only where every slope is zero, at the posterior:
Press play and watch the belief flow downhill on free energy
Two motions run here, carried by different parts of the cell. The cell moves through its surroundings: at each step it gets a reading and acts, approaching or tumbling. Its internal states move too. Read through section 08’s blanket, each configuration of them is a belief over near, mid and far, a point in belief space, so their motion is a motion in belief space, from the prior toward the posterior for the latest reading. In a cell these internal states might be chemical concentrations; Da Costa et al. (2020) read them, in a brain, as a neuron’s membrane potential () and firing rate ().
In the paper’s model, the two motions are nested. The belief moves within each step of the cell’s motion, between a reading and the action it informs, and it steers the cell’s motion through the action it selects (section 10). Small steps of variational inference, as in the demo above, trace the path of the motion in belief space; exact Bayes gives where it ends, once the motion has had time to settle.
How much motion in belief space fits into each step of the cell’s motion is a choice of the model. The paper’s code runs ten steps of 0.1 downhill on the same , written for the probabilities rather than the logits , and then stops; the note under section 00’s demo shows what steps that coarse do. The right panel of section 00 applies exact Bayes at every step, which assumes the belief settles before the next reading arrives.
What does this interpretation add? It reads inference as something a physical system does: the internal states that carry the belief move continuously in time, and this equation is their motion. It does not add a new answer, since Bayes’ rule already gives the point where the motion in belief space comes to rest. A model that updates by Bayes’ rule at every step of the cell’s motion is then a Bayesian mechanics coarse-grained in time: each of its steps stands for a motion in belief space that has already finished. A model fine-grained in time would run both motions on one clock, each step of belief motion matched by a step of the cell’s motion. Each new reading would arrive before the belief settles, so the cell would act on a belief still on its way, and its behavior would show the belief’s path, not only where it ends.
Action: approach or tumble by expected free energy
Active states are the bacterium’s only way to change what its sensory states will read. The paper’s agent has two actions: keep its heading, “approach”, or “tumble” to a random new heading. Each action has its own transition matrix , the probability of the next distance given the current one. The agent’s preferences are a vector C of log preferences over the observations (Table 2). The paper notes that preferences need not sum to 1; the softmax turns them into a preferred distribution, which puts 0.9996 on the 0–120° bin.
To choose, the agent predicts what each action would bring and scores the prediction. The paper’s step list names the score, “Produce Error (Expected Free Energy)”, before “Take Action”. For one step ahead, in the form of Parr, Pezzulo & Friston (2022):
Predict the next hidden state: carry the current belief through the action’s transition matrix.
Predict the next observation: carry that prediction through the likelihood.
Risk: the excess bits left over from encoding the predicted observations with the preferred ones.
Ambiguity: how uncertain the observation would stay even if the hidden state were known, averaged over the predicted states.
Expected free energy adds the two.
The lower an action’s expected free energy, the more probable the action.
Predict what each action brings, then score it against the preferences
| action | risk | ambiguity | G(a) | q(a) = σ(−G) |
|---|---|---|---|---|
| approach | 3.198 | 0.819 | 4.016 | 0.795 |
| tumble | 4.555 | 0.819 | 5.374 | 0.205 |
The result is run and tumble. After a 0–120° reading the belief leans toward near, “approach” predicts more of the preferred reading, and the bacterium keeps swimming. After a 120–240° or 240–360° reading, “approach” predicts more of the same unwanted readings, and tumbling, which spreads the prediction evenly, carries less risk. Acting changes the next observation, which section 07’s update turns into the next belief: the loop the blanket in section 08 drew.
One caution from Ramstead et al. (2023): expected free energy is not a bound on surprise. It scores predicted futures; it does not sit above a fixed floor the way variational free energy does in section 05.
Many factors: mean field, one factor at a time
The paper’s model has one hidden factor, the distance . Larger models have several. An E. coli model could also track whether a repellent is nearby, which way it is heading, or its distance at each of several time steps. A model of vision might track what an object is and where it is. Section 07 found the belief for one factor:
One factor, section 07: at the bottom of the hill, the log belief is the log joint, up to the constant that normalizes it.
The same answer in the paper’s matrix notation: Bayes’ theorem.
With several factors the formula is unchanged, but q is now a table with an entry for every combination of states.
With K factors of n states each, that table holds numbers; ten factors of three states already need 59,049.
The mean-field approximation assumes the factors are independent under , so never has to describe how they interact. Then is a product of one distribution per factor, and storage falls from numbers to :
The logarithm turns the product into a sum, so the entropy of the product is the sum of the factor entropies. Only the first term, the average of the negative log of the generative model, still couples the factors:
What is given up: under a product, cannot say “if is this state, must be that one.” When the true posterior factorizes, nothing is lost. When it is correlated, every product leaves a gap , and free energy stays above surprise.
What is gained is the license to reuse section 07. Hold every factor but one fixed, and , seen from that one factor, is a single-factor problem again: the same free energy, with the other factors averaged out. Each factor can then be updated by itself while the others hold still.
Coordinate ascent: one factor at a time
For one factor the minimum can even be written down in one step, without following the gradient. Collect the terms of that involve , writing for the average over all the other factors:
Define , with the constant that normalizes it. The bracket is then , so
With one factor there is nothing to average over, and the update is the first row above. With several, this is where the name comes from: each factor feels the others only through an average, , the way a particle in a physicist’s mean-field model feels its neighbors only through their average field, or a neuron in a neural mean-field model responds only to the average input from its population. Only the KL term depends on , and it is zero exactly when . Each update therefore solves its one-factor problem exactly, so never increases. Cycling through the factors until stops changing reaches a point where every factor is optimal given the others, usually a local minimum. Machine learning writes the same procedure as maximizing the ELBO, hence the name coordinate ascent variational inference (CAVI); in free energy terms it is coordinate descent. Each factor is a coordinate, and each step moves along one of them while the rest stay put.
A worked example: a repellent nearby? E. coli swims away from repellents as well as toward attractants, and its receptors combine both into one signal (Tso & Adler 1974; Sourjik & Wingreen 2012). Give the paper’s model a second hidden factor for this, : whether a repellent is nearby, absent or present, with a prior of 30% on present. Without a repellent, the reading depends on the distance through the paper’s A, as before. A nearby repellent counts against the attractant, so it pushes the reading one bin worse: from 0–120° to 120–240°, or from 120–240° to 240–360°, where the far state’s readings already sit. A middle reading, 120–240°, now has two explanations: mid distance with no repellent, or near the food with a repellent nearby. Knowing either factor would tell you the other, so the exact posterior ties them together. The boxed update gives each factor section 07’s formula, with the log likelihood averaged over the other factor’s current belief:
The distance, averaging the log likelihood over the current belief about the repellent. D₁ is the paper’s uniform prior.
The repellent, averaging over the current belief about the distance. D₂ puts 0.7 on absent and 0.3 on present.
Update one factor at a time and compare the product with the exact posterior
Two features of the result hold well beyond this example. The product cannot say “either mid with no repellent, or near with one”, so it cannot keep both explanations, and free energy stays above surprise. Instead it commits to the more likely one and becomes overconfident in it: after a middle reading at hi = 0.9, mean field puts 0.96 on no repellent, where the exact posterior puts 0.70, and leaves 0.2% on “near, with a repellent”, which the exact posterior gives 27%. This is the variational direction’s preference for staying inside the posterior, from section 06: settles inside one explanation rather than spreading over both.
Ideally, we’d like to spread over both explanations; this is what the exact posterior does. A product can’t do it without also spreading over the two combinations the reading rules out, “near, no repellent” and “mid, with a repellent”, so coordinate ascent picks one explanation instead. A richer can spread. Keeping the whole table costs six numbers here instead of five and is exact Bayes. A structured writes the dependence in directly, which with two factors is exact too (section 13). A mixture of products keeps one product per explanation. Mean field earns its savings in models with many factors, not in one this small.
The second factor changes how the bacterium swims, and so does whether can hold both explanations. Below, three agents swim in the paper’s arena, which has no repellent:
The same arena, with one hidden factor or two, and two ways to hold them
An objection: what if the repellent is really there? We tried it. In an arena with a patch where every reading is pushed one bin worse, no reading inside the patch is ever the best bin, so none of the three agents keeps to a direction for long. Each wanders until it happens to drift out: across 30 runs, some escaped early and some never did, and each agent’s share of steps inside the circle ranged from 0% in some runs to 75–90% in others, whatever its model. Believing a repellent is present can’t help when the model gives the repellent no direction and no way for actions to change it. A model built for a repellent world would need both: a reading that tells the bacterium which way the repellent lies, and transitions in which swimming away leaves it behind. The PCT vs. FEP paper’s discussion names repellents as a next step for chemotaxis models: bacteria decide what to move away from as well as what to move toward. Like the paper’s model (section 08), this one is an abstraction, chosen here to teach mean field rather than to describe chemotaxis in full.
Energy minus entropy: where “free energy” comes from
Split the logarithm in the other way. Since ,
The second line is the form most active inference texts use: free energy is low when explains the observation well (accuracy) without moving far from the prior (complexity). The first line is the one that explains the name. In statistical mechanics, a system in configuration with energy at temperature has the Boltzmann probability , and its Helmholtz free energy is . Set and define the energy of each hidden state as its surprisal under the generative model:
The posterior is a Boltzmann distribution, the marginal likelihood is its partition function, and the surprise is its Helmholtz free energy. Variational free energy evaluates the same expression, average energy minus entropy, at a trial distribution , and it reaches exactly at the Boltzmann distribution. This is the Gibbs variational principle, and it is why the name was borrowed.
Physicists do not rename entropy as energy. In this correspondence, the surprisal of one configuration plays the role of its energy, and entropy remains the average surprisal, here of . Statistical mechanics and Bayesian inference share these equations. Bayesian mechanics adds an interpretation: is a belief whose parameters are carried by the system’s internal states.
When the energy is physical
So far “energy” is a borrowed word: the surprisal of a hidden state. It is a real, physical energy only when two conditions hold. The states must be physical configurations of a system, and their probabilities must be that system’s probabilities in thermal equilibrium. Then each configuration has the Boltzmann probability and its surprisal is its physical energy, measured in units of plus the constant .
The E. coli model meets neither condition as written. Its hidden state, the distance, takes just three values, near, mid and far. Each is a category that stands for an enormous number of physical configurations of the cell and its surroundings: exact positions, headings and molecule counts. And its probabilities were written down by its authors (section 08), not measured or derived from the cell.
The first condition can be repaired, and repairing it changes the energy into a free energy. Suppose the cell were in equilibrium. A category’s probability is the sum of the probabilities of the configurations it contains, and written as a Boltzmann factor, that sum’s energy is
the average energy of those configurations minus times their entropy, which counts how many there are. Numbers count as much as energy: one configuration at energy 0 against ten at each makes the group of ten times as probable. So the energy of “near” would be the free energy of everything “near” lumps together. Lumping states into categories like this is called coarse-graining.
The second condition is harder. A living cell is not in equilibrium: it spends energy to adapt quickly and accurately (Lan et al. 2012), so its steady state is not a Boltzmann distribution. Its surprisal is still well defined, but it is no longer each configuration’s physical energy divided by : the steady state depends on the energy flowing through the cell, not only on the energies of its configurations, and the cell’s motion includes currents that circulate rather than run downhill. Even when the equations are known, such steady states rarely have a closed form (section 08).
The physics and the model come closest at the flagellar motor. It turns one way while the cell runs and the other way when it tumbles, and its switching between the two is well described as an equilibrium between two shapes whose free energies depend on how much of the signaling protein CheY is bound to it (Scharf et al. 1998). More bound CheY favors tumbling, steeply (Cluzel, Surette & Leibler 2000). So the motor’s chance of tumbling is a softmax of physical free energies, the same form as section 10’s . For the model to describe the motor, though, the shared form is not enough. We would need a mapping like section 08’s synchronization map: from the cell’s chemistry, such as its bound CheY, to the model’s belief, under which the motor’s free-energy difference equals the difference in expected free energy between tumbling and running. Finding mappings like that, from physical states to beliefs, is the hard part, and it is where most of the work in Bayesian mechanics lies.
The PCT vs. FEP paper’s discussion asks for the same work from another side. It calls for chemotaxis models that specify the cell’s receptors, its internal mechanisms and the fluid around it, with molecular noise low enough to drive goal-directed behavior reliably. And it proposes category theory to keep two readings of a generative model apart: as beliefs held within the cell, or as our beliefs about the parameters and structure of the cell’s physical control system, which a functor from FEP agents to PCT agents makes precise. The second reading doesn’t need the cell to carry a belief at all; the first needs exactly the mapping above.
Physical systems are also where mean field most often falls short. It is exact when each part interacts with very many others, so their average stands in for them, and it fails where neighbors move together: in low dimensions and near phase transitions. For a one-dimensional magnet, it predicts a transition that never happens. Modeling physical systems faithfully usually means keeping some of those correlations, which is what the next section’s methods do.
Beyond mean field
The remaining gap in the lab is the correlation a product cannot express. Three families of methods restore dependence:
- Structured approximations keep the dependencies that matter most. For hidden states over time, mirrors the paper’s transition matrix B, and for such chains the forward–backward algorithm computes the required averages exactly.
- Full-covariance Gaussians let tilt: for continuous hidden variables, an off-diagonal covariance captures the correlation a product drops, at the cost of covariance parameters instead of n variances.
- Normalizing flows pass samples from a simple distribution through a chain of invertible learned transformations. Their densities remain computable, so free energy can still be estimated and minimized.
These methods generally lose the closed-form coordinate update, and free energy is minimized instead by gradient descent on the parameters of , section 07’s method, which is the setting the paper describes at the end of its §2.2.1.
Levels of explanation: how each bounds the others
The paper’s model is one level of description among many. The same bacterium can be described by its molecules, its signaling chemistry, its motor’s two states, or the paper’s three distances. Describing it at a coarser level lumps many fine-grained states into one, so detail is thrown away on the way up. A full account of a system would say how these levels relate, and each level turns out to limit what the others can be.
Higher levels bound lower ones, along some directions. Many fine-grained models produce the same coarse behavior, so the coarse level can never pin down everything below it. It does rule out every fine-grained model that would behave differently, and more data rules out more. Machta et al. (2013) found that in models across physics and biology, coarse observations depend on only a few combinations of the microscopic parameters. Those few can be pinned down with growing confidence as data accumulate; the rest stay free however much data comes in.
This tutorial’s demos show both kinds of direction. At hi = 0.7, section 00’s exact filtering agent swims as well as the paper’s agent at hi = 1, so watching the bacterium swim says little about the likelihood sharpness its model assumes. A repellent that isn’t there halves the time spent inside the circle at hi = 0.6 (section 11), so watching it swim does reveal whether its model posits one.
Energy bounds which physical systems can carry a model. Precision costs energy. The thermodynamic uncertainty relation says a steady process can be made more precise only by producing more entropy, so a measured precision sets a minimum energy budget. For E. coli, Lan et al. (2012) showed that adapting quickly and accurately costs energy in its methylation cycle. Memory costs too: a system that keeps information about its past that doesn’t help it predict its future must dissipate energy, at least in proportion to that useless memory (Still et al. 2012). Together these turn a claim about beliefs into a claim about physics that can fail: a generative model the cell is said to carry with a given precision requires at least a certain energy budget, and a cell that spends less cannot be carrying it.
Lower levels bound higher ones. The limits run the other way too. If a system’s physics has no Markov blanket or no steady state, its internal states cannot be read as beliefs at all, whatever generative model someone writes down (section 08; Aguilera et al. 2022). Noise sets limits as well: a cell that senses a chemical by catching its molecules cannot measure the concentration more precisely than the randomness of their arrival allows (Berg & Purcell 1977), so no generative model can credit E. coli with sharper readings than that.
Putting the levels together. These limits suggest a way to ask which generative model a real system carries, instead of only positing one. List the models that reproduce the observed behavior. Discard those whose precision would cost more energy than the system spends, or that its noise forbids. Keep those that the levels below could produce when coarse-grained. What survives is bounded from above by behavior and from below by physics, and each new measurement at either level tightens the bounds.
Where to go next
The Mean Field Approximation tutorial connects this variational mean field to neural mean-field models. Scale-Free Active Inference builds on the POMDP model, and the natural-gradient section of From Information Theory to Information Geometry treats gradient-based variational inference.
Sources
- T. Roachford, W. Mansell & R. Pena (2025), “PCT vs. FEP: A Comparison Between Reorganization Theory and Bayesian Inference,” Foundations 5(4), 35. Sections 03–07 follow its §2.2.1–2.2.2, Figures 8–10, Equations (1)–(7) and Tables 3, 5 and 7; section 08 its §1.3; section 10 its Tables 2, 5 and 6.
- M. J. D. Ramstead et al. (2023), “On Bayesian Mechanics: A Physics of and by Beliefs,” Interface Focus 13, 20220029. The definition of Bayesian mechanics in section 00; the Markov blanket, internal states as beliefs and the synchronization map in section 08; expected free energy not bounding surprise in section 10.
- D. M. Blei, A. Kucukelbir & J. D. McAuliffe (2017), “Variational Inference: A Review for Statisticians,” Journal of the American Statistical Association 112, 859–877. Mean field and coordinate ascent, section 11.
- C. M. Bishop (2006), Pattern Recognition and Machine Learning, §1.6.1, §9.4 and §10.1. Maximum likelihood as KL minimization, expectation–maximization as lowering F, mean field’s overconfidence and the direction of KL, sections 06 and 11.
- T. Parr, G. Pezzulo & K. J. Friston (2022), Active Inference: The Free Energy Principle in Mind, Brain, and Behavior, MIT Press. Expected free energy as risk plus ambiguity, section 10; the energy–entropy and complexity–accuracy forms, section 12.
- L. Da Costa, T. Parr, N. Sajid, S. Veselic, V. Neacsu & K. Friston (2020), “Active inference on discrete state-spaces: A synthesis,” Journal of Mathematical Psychology 99, 102447. The neuronal reading of belief updating (membrane potential and firing rate), section 09; action selection by expected free energy, section 10.
- W.-W. Tso & J. Adler (1974), “Negative Chemotaxis in Escherichia coli,” Journal of Bacteriology 118, 560–576. E. coli swims away from repellents, section 11.
- V. Sourjik & N. S. Wingreen (2012), “Responding to chemical gradients: bacterial chemotaxis,” Current Opinion in Cell Biology 24, 262–268. One receptor array combines attractant and repellent signals into one output, section 11.
- B. B. Machta, R. Chachra, M. K. Transtrum & J. P. Sethna (2013), “Parameter Space Compression Underlies Emergent Theories and Predictive Models,” Science 342, 604–607. Coarse observations depend on a few combinations of microscopic parameters, section 14.
- A. C. Barato & U. Seifert (2015), “Thermodynamic Uncertainty Relation for Biomolecular Processes,” Physical Review Letters 114, 158101. Precision costs entropy production, section 14.
- G. Lan, P. Sartori, S. Neumann, V. Sourjik & Y. Tu (2012), “The energy–speed–accuracy trade-off in sensory adaptation,” Nature Physics 8, 422–428. E. coli’s adaptation costs energy, sections 12 and 14.
- B. E. Scharf, K. A. Fahrner, L. Turner & H. C. Berg (1998), “Control of direction of flagellar rotation in bacterial chemotaxis,” PNAS 95, 201–206. The motor switches as a two-state equilibrium set by bound CheY, section 12.
- P. Cluzel, M. Surette & S. Leibler (2000), “An Ultrasensitive Bacterial Motor Revealed by Monitoring Signaling Proteins in Single Cells,” Science 287, 1652–1655. The motor’s steep response to CheY, section 12.
- S. Still, D. A. Sivak, A. J. Bell & G. E. Crooks (2012), “Thermodynamics of Prediction,” Physical Review Letters 109, 120604. Memory that doesn’t help prediction costs energy, section 14.
- H. C. Berg & E. M. Purcell (1977), “Physics of chemoreception,” Biophysical Journal 20, 193–219. The noise floor on sensing a concentration, section 14.
- J. Tu, T. S. Shimizu & H. C. Berg (2008), “Modeling the chemotactic response of Escherichia coli to time-varying stimuli,” PNAS 105, 14855–14860. Equations for E. coli’s signaling chemistry, section 08.
- H. Risken (1996), The Fokker–Planck Equation: Methods of Solution and Applications, 2nd ed., Springer. Steady-state distributions of noisy systems and when they have a closed form, section 08.
- M. Aguilera, B. Millidge, A. Tschantz & C. L. Buckley (2022), “How particular is the physics of the free energy principle?,” Physics of Life Reviews 40, 24–50. Sparse coupling without a Markov blanket in the steady state, even in linear systems, sections 08 and 14.
- J. Bruineberg, K. Dołęga, J. Dewhurst & M. Baltieri (2022), “The Emperor’s New Markov Blankets,” Behavioral and Brain Sciences 45, e183. Blankets as a modeling tool versus blankets as physical boundaries, section 08.
