Stationary Distributions and Mixing Time
Many randomized algorithms run a Markov chain and use where it ends up: shuffling cards, sampling a random colouring, estimating a volume or a partition function, exploring a huge state space. The fundamental limit theorem guarantees that a finite irreducible aperiodic chain converges to its stationary distribution (Ergodic Chains and Stationary Distributions); for an algorithm the question is how fast. This lesson defines the total-variation distance and the mixing time, explains lazy chains, and measures mixing on a few graphs. The next lessons prove mixing bounds by coupling and by conductance.
Distance to stationarity
The total-variation distance between two distributions and on a finite set is the largest difference they assign to any event:
(The maximum is attained by .) It lies between 0 and 1, and it is the right notion for sampling: if the output of an algorithm is within of in total variation, then every event, and every bounded statistic computed from the output, is off by at most (or times the bound).
For a chain with transition matrix , let be the distribution after steps from state , and
The maximum over starting states makes the definition worst-case, like everything else in algorithm analysis.
Facts (Levin, Peres and Wilmer, Markov Chains and Mixing Times, chapter 4): never increases, and
So the constant is arbitrary: once the distance is below , every further steps at least halve it. The proof uses the submultiplicativity of and .
Random walks on graphs, made lazy
The simple random walk on a connected undirected graph moves to a uniformly random neighbour; its stationary distribution is , uniform for regular graphs. On a bipartite graph (an even cycle, a hypercube) the walk is periodic and never converges. The standard fix is the lazy walk: stay put with probability , otherwise move as before, . It has the same stationary distribution, is aperiodic, and has only nonnegative eigenvalues, which simplifies spectral arguments. Laziness at most doubles the mixing time.
The program computes the exact distribution of the lazy walk from node 0 on three 12-node graphs: a cycle, a barbell (two 6-cliques joined by one edge) and a 4-regular circulant (each node joined to the nodes 1 and 5 steps away), and reports the distance to and the mixing time.
def lazy_walk_distances(n, edges, steps):
adj = [[] for _ in range(n)]
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
deg = [len(x) for x in adj]
pi = [d / (2 * len(edges)) for d in deg]
dist = [1.0] + [0.0] * (n - 1)
tv = []
for t in range(steps + 1):
tv.append(sum(abs(p - q) for p, q in zip(dist, pi)) / 2)
new = [p / 2 for p in dist]
for u in range(n):
for v in adj[u]:
new[v] += dist[u] / (2 * deg[u])
dist = new
return tv
n = 12
cycle = [(i, (i + 1) % n) for i in range(n)]
barbell = [(a + s, b + s) for s in (0, 6) for a in range(6) for b in range(a + 1, 6)] + [(5, 6)]
circulant = [(i, (i + j) % n) for i in range(n) for j in (1, 5)]
print("graph d(0) d(2) d(5) d(10) d(20) d(40) t_mix (start at 0)")
for name, edges in [("cycle", cycle), ("barbell", barbell), ("circulant", circulant)]:
tv = lazy_walk_distances(n, edges, 40)
mix = next(t for t, d in enumerate(tv) if d <= 0.25)
print(f"{name:10} " + " ".join(f"{tv[t]:6.3f}" for t in (0, 2, 5, 10, 20, 40)) + f" {mix:5}")graph d(0) d(2) d(5) d(10) d(20) d(40) t_mix (start at 0)
cycle 0.917 0.625 0.474 0.320 0.156 0.039 14
barbell 0.919 0.492 0.457 0.402 0.311 0.186 29
circulant 0.917 0.396 0.158 0.038 0.002 0.000 4
The lab shows the same three walks step by step: the circle at each node has area proportional to the probability of being there, and the chart traces the distance to . Its readout gives the mixing times from node 0: 14, 29 and 4 steps. (By symmetry every start is equally bad for the cycle and the circulant, so these are their mixing times; for the barbell the walk starts at node 0, one of the clique nodes away from the bridge.)
The barbell is slow for a structural reason: almost all of the probability must cross a single bridge edge. That bottleneck is what conductance measures (Conductance and the Cheeger Bound). The circulant has no bottleneck and mixes in 4 steps, close to the diameter, the least any walk could need.
How mixing scales
For the lazy walk on the cycle the mixing time grows like : in steps a walk spreads only about positions. For the lazy walk on the hypercube (flip a random coordinate with probability ) it is about , although the hypercube has vertices: mixing time is about the structure of the chain, not the number of states. The program computes exact mixing times. For the hypercube it uses symmetry: from the distribution depends only on the number of ones, so a chain on suffices, with binomial.
from math import ceil, comb, log, log2
def cycle_tmix(n, eps=0.25):
dist = [1.0] + [0.0] * (n - 1)
t = 0
while sum(abs(p - 1 / n) for p in dist) / 2 > eps:
dist = [dist[i] / 2 + dist[i - 1] / 4 + dist[(i + 1) % n] / 4 for i in range(n)]
t += 1
return t
def hypercube_tmix(d, eps=0.25):
pi = [comb(d, w) / 2 ** d for w in range(d + 1)]
dist = [1.0] + [0.0] * d # distribution of the number of ones
t = 0
while sum(abs(p - q) for p, q in zip(dist, pi)) / 2 > eps:
new = [p / 2 for p in dist]
for w in range(d + 1):
if w > 0:
new[w - 1] += dist[w] * w / (2 * d) # flip a one to zero
if w < d:
new[w + 1] += dist[w] * (d - w) / (2 * d) # flip a zero to one
dist = new
t += 1
return t
print("cycle C_n: n t_mix t_mix / n^2")
for n in [8, 16, 32, 64]:
t = cycle_tmix(n)
print(f" {n:4} {t:5} {t / n ** 2:10.3f}")
print("hypercube: d states t_mix (d/2) ln d")
for d in [4, 16, 64, 256]:
print(f" {d:4} 2^{d:<5} {hypercube_tmix(d):5} {d / 2 * log(d):9.1f}")
t_mix = cycle_tmix(32)
for eps in (0.125, 0.0625, 0.01):
print(f"C_32: t_mix({eps}) = {cycle_tmix(32, eps)}, bound ceil(log2(1/eps)) * t_mix = {ceil(log2(1 / eps)) * t_mix}")cycle C_n: n t_mix t_mix / n^2
8 6 0.094
16 25 0.098
32 98 0.096
64 389 0.095
hypercube: d states t_mix (d/2) ln d
4 2^4 5 2.8
16 2^16 29 22.2
64 2^64 162 133.1
256 2^256 825 709.8
C_32: t_mix(0.125) = 169, bound ceil(log2(1/eps)) * t_mix = 294
C_32: t_mix(0.0625) = 241, bound ceil(log2(1/eps)) * t_mix = 392
C_32: t_mix(0.01) = 430, bound ceil(log2(1/eps)) * t_mix = 686
The cycle's mixing time divided by settles near a constant, and the hypercube's stays close to (a little above it, by a term of order ) while the number of states explodes. The last line checks the halving fact: the time to reach smaller grows only logarithmically in , well within the bound.
In C++
Exact mixing times of the lazy walk on the cycle in C++ for larger , by repeatedly applying the transition rule to a probability vector (no randomness is involved).
#include <cmath>
#include <cstdio>
#include <vector>
int cycle_tmix(int n) {
std::vector<double> p(n, 0.0), q(n);
p[0] = 1.0;
for (int t = 0;; ++t) {
double tv = 0;
for (double x : p) tv += std::fabs(x - 1.0 / n);
if (tv / 2 <= 0.25) return t;
for (int i = 0; i < n; ++i) q[i] = p[i] / 2 + p[(i + n - 1) % n] / 4 + p[(i + 1) % n] / 4;
p.swap(q);
}
}
int main() {
std::printf(" n t_mix t_mix / n^2\n");
for (int n : {16, 32, 64, 128, 256}) {
int t = cycle_tmix(n);
std::printf("%4d %6d %10.4f\n", n, t, double(t) / n / n);
}
} n t_mix t_mix / n^2
16 25 0.0977
32 98 0.0957
64 389 0.0950
128 1555 0.0949
256 6218 0.0949
Test yourself
Which formula gives the total-variation distance between and on a finite set?
Why are random walks used in algorithms usually made lazy?
- To make them faster
- To make them aperiodic (bipartite graphs would never converge) and their eigenvalues nonnegative, at the cost of at most doubling the mixing time
- To change the stationary distribution
- To make them reversible
If , which bound holds for ?
- At most 1000
- At most 25 600
- At most 102 400
- No bound follows
How does the mixing time of the lazy walk on the cycle grow?
- Like
- Like
- Like
- Like