Search

Search papers and lessons

Detailed Balance

Level 3 · 10 min read

Detailed balance is a pairwise test for stationarity: for every pair of states, the probability flowing one way equals the flow back, which makes a chain's stationary distribution easy to verify.

On this page

  • Colour = topic mix
  • Probability
  • Linear algebra
  • Stochastic processes
  • Discrete math
  • Sampling
  • Sign in to see your progress

TL;DR

A Markov chain has a stationary distribution π\boldsymbol{\pi} when the total probability flowing into each state equals the total flowing out. Detailed balance is a stronger, pairwise condition: for every pair of states, the flow from ii to jj equals the flow from jj to ii. Any chain that satisfies it has π\boldsymbol{\pi} as a stationary distribution, and that check is usually much easier than checking the global balance equations.

Where you'll see it: in the proof that the Metropolis algorithm samples the right distribution, and in the design of Markov chain Monte Carlo samplers in general.

Intuition

Two tanks and a pipe

Picture two tanks joined by a pipe. Water flows from one to the other, and the amount of water in each tank can stay constant over time. The water level is stable overall if inflow equals outflow for each tank. It is stable in a stronger sense if, between any two tanks, the water moving one way per second equals the water moving the other way. Then no net current flows along any pipe at all.

A Markov chain works the same way. A state is a tank, and the probability of jumping from one state to another per step is the flow along a pipe. Stationarity says the total flow in and out of each state balances. Detailed balance says the flow along each individual pipe balances, so there is no net circulation between any pair of states.

The plot below shows the function that the Metropolis algorithm uses to make this happen, which we come back to later.

00.511.522.5300.20.40.60.811.2ratio r of target probabilitiesacceptance probability
  • acceptance probability min(1, r)
The acceptance rule keeps ratios below 1 as they are and caps larger ratios at 1. The marked points are r = 0.5, 1 and 2.

The math

Definition

Let PijP_{ij} be the probability of moving from state ii to state jj in one step, and let π\boldsymbol{\pi} be a probability vector over states.

Definition: Detailed balance

A chain with transition probabilities PijP_{ij} satisfies detailed balance with respect to π\boldsymbol{\pi} if, for every pair of states ii and jj,

πi Pij=πj Pji.\pi_i\, P_{ij} = \pi_j\, P_{ji}.

A chain with this property is called reversible.

Read the equation in words. The left side is the stationary probability of being in ii, times the chance of moving from ii to jj: the flow along the pipe from ii to jj. The right side is the same flow in the other direction. The condition says the two flows match, for every pair. The pair i=ji = j is trivially true.

In a continuous state space, sums become integrals and the matrix becomes a transition density K(x→x′)K(x \to x'). The condition reads

π(x) K(x→x′)=π(x′) K(x′→x)\pi(x)\, K(x \to x') = \pi(x')\, K(x' \to x)

for all xx and x′x'. Everything below carries over with that substitution.

Detailed balance implies stationarity

Suppose detailed balance holds. Look at the jj-th entry of πP\boldsymbol{\pi} P:

(πP)j=∑iπiPij.(\boldsymbol{\pi} P)_j = \sum_i \pi_i P_{ij}.

Replace each term using detailed balance, πiPij=πjPji\pi_i P_{ij} = \pi_j P_{ji}:

∑iπiPij=∑iπjPji=πj∑iPji=πj.\sum_i \pi_i P_{ij} = \sum_i \pi_j P_{ji} = \pi_j \sum_i P_{ji} = \pi_j.

The last step uses the fact that each row of PP sums to 1: the probabilities of leaving state jj add up to 1. So πP=π\boldsymbol{\pi} P = \boldsymbol{\pi}, and π\boldsymbol{\pi} is stationary.

This is the reason detailed balance is so useful. You do not need to solve the linear system π(P−I)=0\boldsymbol{\pi}(P - I) = 0. You check one equation for each pair of states, and each one involves only two states.

Detailed balance is sufficient, not necessary

The converse fails. A chain can have a stationary distribution without detailed balance, because probability can circulate around a loop. Consider three states arranged in a cycle, where the chain always moves 1→2→3→11 \to 2 \to 3 \to 1. Every state has the same flow in and out, so the uniform distribution is stationary. But the flow from 1 to 2 is positive while the flow from 2 to 1 is zero, so detailed balance fails. Such a chain is called non-reversible. Its stationary distribution is still correct, but you cannot prove that with the pairwise test.

Why it matters for sampling

Markov chain Monte Carlo builds a chain whose stationary distribution is a target π\pi that is hard to sample directly. The recipe is to design transition probabilities, check detailed balance with respect to π\pi, and conclude that π\pi is stationary. Then run the chain and average along it.

The check is far simpler than the global equations, because the target often appears only through ratios. Detailed balance can be rewritten as

PijPji=πjπi.\frac{P_{ij}}{P_{ji}} = \frac{\pi_j}{\pi_i}.

The normalising constant of π\pi cancels in the ratio. This is why MCMC works when you know π\pi only up to a constant.

The Metropolis construction

Choose a symmetric proposal, meaning Q(i→j)=Q(j→i)Q(i \to j) = Q(j \to i), which says how likely you are to suggest moving from ii to jj. Then from state ii, propose jj and accept it with probability

α(i→j)=min⁡ ⁣(1,πjπi).\alpha(i \to j) = \min\!\left(1, \frac{\pi_j}{\pi_i}\right).

Otherwise stay at ii. The transition probability for i≠ji \neq j is Pij=Q(i→j) α(i→j)P_{ij} = Q(i \to j)\,\alpha(i \to j).

Check detailed balance for i≠ji \neq j:

πi Q(i→j) min⁡ ⁣(1,πjπi)=Q(i→j) min⁡(πi,πj).\pi_i\, Q(i \to j)\, \min\!\left(1, \frac{\pi_j}{\pi_i}\right) = Q(i \to j)\,\min(\pi_i, \pi_j).

The right side is symmetric in ii and jj, because QQ is symmetric. Swapping the roles gives the same expression, so detailed balance holds. The construction works because min⁡(πi,πj)\min(\pi_i, \pi_j) is the same whichever direction you look from.

Worked examples

Example
Two weather states

Let sunny and rainy be the states. Suppose P(sunny→rainy)=0.2P(\text{sunny} \to \text{rainy}) = 0.2 and P(rainy→sunny)=0.4P(\text{rainy} \to \text{sunny}) = 0.4.

  1. Detailed balance with the unknown πs\pi_s (sunny) and πr\pi_r (rainy) says πs×0.2=πr×0.4\pi_s \times 0.2 = \pi_r \times 0.4.
  2. So πs=2πr\pi_s = 2\pi_r.
  3. With πs+πr=1\pi_s + \pi_r = 1, we get 3πr=13\pi_r = 1, so πr=1/3\pi_r = 1/3 and πs=2/3\pi_s = 2/3.
  4. Check against the two-state formula πsunny=b/(a+b)=0.4/0.6=2/3\pi_{\text{sunny}} = b/(a+b) = 0.4/0.6 = 2/3. It matches.
  5. Interpretation: in the long run the chain is sunny two days in three. The pairwise balance gave the answer in one line, with no matrix to invert.
Example
Metropolis on three states

Three states have unnormalised target weights w1=1w_1 = 1, w2=2w_2 = 2 and w3=4w_3 = 4, so π=(1,2,4)/7\pi = (1, 2, 4)/7. The proposal picks one of the other two states with probability 1/21/2 each, which is symmetric.

  1. Acceptance probabilities: from 1 to 2, min⁡(1,2/1)=1\min(1, 2/1) = 1. From 2 to 1, min⁡(1,1/2)=0.5\min(1, 1/2) = 0.5. From 2 to 3, min⁡(1,4/2)=1\min(1, 4/2) = 1. From 3 to 2, min⁡(1,2/4)=0.5\min(1, 2/4) = 0.5. From 1 to 3, min⁡(1,4/1)=1\min(1, 4/1) = 1. From 3 to 1, min⁡(1,1/4)=0.25\min(1, 1/4) = 0.25.
  2. Transition probabilities for i≠ji \neq j are Q×α=12×Q \times \alpha = \tfrac{1}{2} \times acceptance: P12=0.5P_{12} = 0.5, P13=0.5P_{13} = 0.5, P21=0.25P_{21} = 0.25, P23=0.5P_{23} = 0.5, P31=0.125P_{31} = 0.125, P32=0.25P_{32} = 0.25.
  3. Stay probabilities fill each row to 1: P22=1−0.25−0.5=0.25P_{22} = 1 - 0.25 - 0.5 = 0.25, P33=1−0.125−0.25=0.625P_{33} = 1 - 0.125 - 0.25 = 0.625, and P11=0P_{11} = 0.
  4. Check detailed balance on the pair (1, 2): π1P12=(1/7)(0.5)=0.5/7\pi_1 P_{12} = (1/7)(0.5) = 0.5/7, and π2P21=(2/7)(0.25)=0.5/7\pi_2 P_{21} = (2/7)(0.25) = 0.5/7. They match.
  5. Check the pair (2, 3): π2P23=(2/7)(0.5)=1/7\pi_2 P_{23} = (2/7)(0.5) = 1/7, and π3P32=(4/7)(0.25)=1/7\pi_3 P_{32} = (4/7)(0.25) = 1/7. They match.
  6. Interpretation: the chain visits state 3, the heaviest state, most often, in proportion to its weight. It never needed the normalising constant 7, because every acceptance step used only the ratios of weights.
Example
A cycle is stationary but not reversible

A chain on three states moves deterministically 1→2→3→11 \to 2 \to 3 \to 1, so P12=P23=P31=1P_{12} = P_{23} = P_{31} = 1.

  1. The uniform distribution π=(1/3,1/3,1/3)\pi = (1/3, 1/3, 1/3) is stationary, because each state receives exactly one unit of probability from its predecessor and sends one unit onward.
  2. Detailed balance on the pair (1, 2) asks π1P12=π2P21\pi_1 P_{12} = \pi_2 P_{21}. The left side is 1/31/3, and the right side is 00, because P21=0P_{21} = 0.
  3. So detailed balance fails, even though stationarity holds.
  4. Interpretation: this chain circulates probability around the loop. Stationarity is a global statement, and detailed balance is a stronger local one that forbids circulation.

Tradeoffs and failure modes

Detailed balance is a design tool, and it has limits.

SituationWhat goes wrongBetter choice
Chain has useful circulation (directed sweeps through the state space)Reversible chains can mix slowly when they must retrace their stepsNon-reversible samplers, which keep the stationary distribution but move with momentum
Proposal is not symmetricThe simple acceptance rule no longer satisfies detailed balanceMetropolis–Hastings, which adds a correction factor Q(j→i)/Q(i→j)Q(j \to i)/Q(i \to j) to the ratio
Acceptance is very lowAlmost every proposal is rejected, so the chain stays putSmaller steps for random walks, or gradient-based proposals
The normalising constant of the target is unknownComputing π\pi exactly would be impracticalNothing needed: the ratios in the acceptance rule cancel the constant
You need the answer by a deadlineDetailed balance guarantees the stationary distribution, not the speed of convergenceDiagnose mixing with running averages or multiple chains

A frequent error is to check detailed balance and forget that it proves only stationarity. The chain also has to be irreducible and aperiodic for the long-run average to converge to the expectation under π\pi.

History

Detailed balance came from statistical physics in the nineteenth century. Ludwig Boltzmann studied how gases approach equilibrium, and the idea that each microscopic process is balanced by its reverse grew out of that work. In the early twentieth century, physicists described it as "microscopic reversibility" or the principle of detailed balance, and it became a standard tool for showing that a system settles into the Boltzmann distribution.

The Metropolis algorithm was introduced in 1953 by Nicholas Metropolis, Arianna Rosenbluth, Marshall Rosenbluth, Augusta Teller and Edward Teller. They used it to simulate a gas of interacting molecules, which was too large to enumerate, and the acceptance rule was built so that the simulated system obeyed detailed balance with the Boltzmann distribution. W. Keith Hastings generalised it in 1970 to asymmetric proposals, which gave the Metropolis–Hastings algorithm used in statistics today. The pairwise check is the standard way to prove that these samplers are correct.

Check yourself

QuestionWrite the detailed balance condition for a chain with transition probabilities PijP_{ij} and stationary distribution π\boldsymbol{\pi}. Why does it imply that π\boldsymbol{\pi} is stationary?Show answer

Detailed balance says πiPij=πjPji\pi_i P_{ij} = \pi_j P_{ji} for all pairs i,ji, j. Summing over ii gives ∑iπiPij=πj∑iPji=πj\sum_i \pi_i P_{ij} = \pi_j \sum_i P_{ji} = \pi_j, because each row of PP sums to 1. That is exactly the stationarity equation πP=π\boldsymbol{\pi} P = \boldsymbol{\pi}.

QuestionA two-state chain has P(A→B)=0.3P(\text{A} \to \text{B}) = 0.3 and P(B→A)=0.6P(\text{B} \to \text{A}) = 0.6. Find the stationary distribution using detailed balance.Show answer

Detailed balance gives πA×0.3=πB×0.6\pi_A \times 0.3 = \pi_B \times 0.6, so πA=2πB\pi_A = 2\pi_B. With πA+πB=1\pi_A + \pi_B = 1, we get πB=1/3\pi_B = 1/3 and πA=2/3\pi_A = 2/3. This agrees with the formula b/(a+b)=0.6/0.9=2/3b/(a+b) = 0.6/0.9 = 2/3.

QuestionWhy does the Metropolis acceptance rule work even when the target distribution is known only up to a normalising constant?Show answer

The acceptance probability depends only on the ratio πj/πi\pi_j/\pi_i. The detailed balance check is πiQ(i→j)min⁡(1,πj/πi)=Q(i→j)min⁡(πi,πj)\pi_i Q(i \to j)\min(1, \pi_j/\pi_i) = Q(i \to j)\min(\pi_i, \pi_j), and the normalising constant appears in both πi\pi_i and πj\pi_j in a way that cancels in every ratio. The chain therefore needs only the unnormalised weights.

QuestionA sampler's chain balances the flow into and out of every state, but you suspect it circulates probability around loops. A colleague says the stationary distribution is still correct. Are they right, and what test would you use to check the circulation?Show answer

They are right that stationarity is a global condition, so a circulating chain can still have the correct stationary distribution. Circulation is not detected by the global check. You would test detailed balance pair by pair. If some pair has πiPij≠πjPji\pi_i P_{ij} \neq \pi_j P_{ji}, the chain is non-reversible. That is not an error, but it changes how quickly the chain explores the space, and it means the pairwise test cannot be used to prove correctness.

Sources