TExcelTAD
Home › Randomized Algorithms & Probabilistic Analysis › Stationary Distributions and Mixing Time

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 π\pi (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 μ\mu and ν\nu on a finite set Ω\Omega is the largest difference they assign to any event:

∥μ−ν∥TV=max⁡A⊆Ω∣μ(A)−ν(A)∣=12∑x∈Ω∣μ(x)−ν(x)∣.\lVert \mu - \nu \rVert_{TV} = \max_{A \subseteq \Omega} \lvert \mu(A) - \nu(A) \rvert = \frac12 \sum_{x \in \Omega} \lvert \mu(x) - \nu(x) \rvert .

(The maximum is attained by A={x:μ(x)>ν(x)}A = \{x : \mu(x) > \nu(x)\}.) It lies between 0 and 1, and it is the right notion for sampling: if the output of an algorithm is within ε\varepsilon of π\pi in total variation, then every event, and every bounded statistic computed from the output, is off by at most ε\varepsilon (or ε\varepsilon times the bound).

For a chain with transition matrix PP, let Pt(x,⋅)P^t(x, \cdot) be the distribution after tt steps from state xx, and

d(t)=max⁡x∈Ω∥Pt(x,⋅)−π∥TV,tmix(ε)=min⁡{t:d(t)≤ε},tmix=tmix(1/4).d(t) = \max_{x \in \Omega} \lVert P^t(x, \cdot) - \pi \rVert_{TV}, \qquad t_{\text{mix}}(\varepsilon) = \min\{t : d(t) \le \varepsilon\}, \qquad t_{\text{mix}} = t_{\text{mix}}(1/4).

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): d(t)d(t) never increases, and

tmix(ε)≤⌈log⁡21ε⌉tmix.t_{\text{mix}}(\varepsilon) \le \left\lceil \log_2 \frac{1}{\varepsilon} \right\rceil t_{\text{mix}} .

So the constant 1/41/4 is arbitrary: once the distance is below 1/41/4, every further tmixt_{\text{mix}} steps at least halve it. The proof uses the submultiplicativity dˉ(s+t)≤dˉ(s) dˉ(t)\bar d(s + t) \le \bar d(s)\,\bar d(t) of dˉ(t)=max⁡x,y∥Pt(x,⋅)−Pt(y,⋅)∥TV\bar d(t) = \max_{x,y} \lVert P^t(x,\cdot) - P^t(y,\cdot) \rVert_{TV} and d(t)≤dˉ(t)≤2d(t)d(t) \le \bar d(t) \le 2d(t).

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 π(v)=deg⁡(v)2∣E∣\pi(v) = \frac{\deg(v)}{2\lvert E \rvert}, 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 12\frac12, otherwise move as before, Plazy=12(I+P)P_{\text{lazy}} = \frac12(I + P). 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 π\pi and the mixing time.

python · try it yourself
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 π\pi. 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.)

01234567891011after 40 steps: circle area ∝ probability (start at node 0)0¼½102040steps tdistance to π

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 CnC_n the mixing time grows like n2n^2: in tt steps a walk spreads only about t\sqrt t positions. For the lazy walk on the hypercube {0,1}d\{0, 1\}^d (flip a random coordinate with probability 12\frac12) it is about 12dln⁡d\frac12 d \ln d, although the hypercube has 2d2^d 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 0d0^d the distribution depends only on the number of ones, so a chain on {0,…,d}\{0, \ldots, d\} suffices, with π\pi binomial.

python · try it yourself
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 n2n^2 settles near a constant, and the hypercube's stays close to 12dln⁡d\frac12 d \ln d (a little above it, by a term of order dd) while the number of states explodes. The last line checks the halving fact: the time to reach smaller ε\varepsilon grows only logarithmically in 1/ε1/\varepsilon, well within the bound.

In C++

Exact mixing times of the lazy walk on the cycle in C++ for larger nn, by repeatedly applying the transition rule to a probability vector (no randomness is involved).

cpp · try it yourself
#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 μ\mu and ν\nu on a finite set?

  1. ∑x∣μ(x)−ν(x)∣\sum_x \lvert \mu(x) - \nu(x) \rvert
  2. 12∑x∣μ(x)−ν(x)∣\frac12 \sum_x \lvert \mu(x) - \nu(x) \rvert
  3. max⁡x∣μ(x)−ν(x)∣\max_x \lvert \mu(x) - \nu(x) \rvert
  4. ∑x(μ(x)−ν(x))2\sqrt{\sum_x (\mu(x) - \nu(x))^2}

Why are random walks used in algorithms usually made lazy?

  1. To make them faster
  2. To make them aperiodic (bipartite graphs would never converge) and their eigenvalues nonnegative, at the cost of at most doubling the mixing time
  3. To change the stationary distribution
  4. To make them reversible

If tmix=tmix(1/4)=100t_{\text{mix}} = t_{\text{mix}}(1/4) = 100, which bound holds for tmix(1/1024)t_{\text{mix}}(1/1024)?

  1. At most 1000
  2. At most 25 600
  3. At most 102 400
  4. No bound follows

How does the mixing time of the lazy walk on the cycle CnC_n grow?

  1. Like log⁡n\log n
  2. Like nn
  3. Like n2n^2
  4. Like 2n2^n