1# Listening for convergence
3A minimal lab for hearing MCMC chains reach (or fail to reach) their stationary distribution, on a 2D mixture of two normals:
5 p(θ) = ½ 𝒩(θ | m₁, Σ₁) + ½ 𝒩(θ | m₂, Σ₂), m₁,₂ = (∓μ/2, 0), Σₖ = diag(σₖ) [[1, ρₖ], [ρₖ, 1]] diag(σₖ)
7```bash
8npm run dev # serves app/ at http://127.0.0.1:8003
9npm run check # node --check on every module, and runs the sampler check
10node scripts/check-samplers.mjs # acceptance, mode switches and moments on the five targets
11```
13Static `index.html`, `style.css` and ES modules, no build step; fonts bundled under `app/vendor/`.
15## What's in it
17- **Target**: the five settings of the notes (easy, separated, ridge, stiff, crossed), and sliders for μ, the four σ's, the two ρ's, and τ, the radius of the circle the chains start on. The plane shades the density on a log scale (3.5 decades), with equal scales on both axes.
18- **Sampler**: random-walk Metropolis, MALA, ULA (unadjusted Langevin), generalized HMC (one leapfrog step, partial momentum refresh γ, momentum flipped on rejection) and HMC (L leapfrog steps), with step size ε; the stability limit of leapfrog and Langevin, 2 √λmin, is shown under it. All chains run the same sampler with the same tuning, each with its own seed.
19- **Chains** (1 to 32, default 8): start at random points on the τ circle (*Restart*), or all at one point (click the plane). Each shows its last proposal (the HMC trajectory, or a line; dashed and crossed out if rejected) and a short trail. The chains start paused: *Run* sets them going and *Pause* stops them; *+1000* runs 1000 iterations of each at once, silently.
20- **Sound** (the big button in the header): every iteration, each chain plays a note, all with the same sound, each chain from its own place in the stereo field. θ₁ sets the pitch: three octaves, C3 to C6, of the chosen scale and key (chromatic by default), over θ₁ = ±(μ/2 + 3.2 max σₖ₁). θ₂ sets the timbre, over θ₂ = ±3.2 max σₖ₂: an equal-power crossfade from a soft, nearly pure tone at the bottom to a bright, buzzy, nasal one at the top (a sawtooth-like spectrum with a formant near 2 kHz), matched in loudness so only the timbre changes. A rejected step repeats the note softly (or is silent). Chains that have mixed wander over both modes and play in the same registers; a chain stuck in one mode keeps to its octave, and a stalled one repeats itself. Tempo is iterations per second (default 50, 1 to 400 on a log scale); past 800 notes per second over all chains, the chains take turns, each playing every few iterations. Sustain (default 0.6 s) and volume as in the Duffing app; at most 192 notes sound at once (the oldest fades out).
21- **Hearing mixing without knowing the target** (all in the Sound panel):
22 - *Notes from: rank among all chains' draws*: pitch and timbre from the rank of θ₁ and θ₂ among the pooled draws after warm-up (a random sample of 4000 of them, refreshed four times a second) instead of θ itself. Pooled, the notes then spread evenly over the whole range; if the chains have mixed, each one does too and they sound alike, while a chain stuck in part of the space keeps to part of the range. A chain that moves slowly plays a stepwise melody; one that mixes fast jumps around at random.
23 - *Drone* (default 50%): under the notes, per chain, a soft tone at the key's root (for θ₁) and one at its fifth (for θ₂), each detuned by 400 z² cents (up to an octave), z the chain's mean after warm-up minus the pooled mean, in pooled standard deviations. Chains that disagree beat and clash; as they agree (split R̂ → 1), the drone settles into a plain fifth. Silent while paused.
24 - *Listen back*: 3 s of the chains as they run, a 0.6 s gap, then 3 s replayed from the history, centered halfway back (once there are three times that many draws), a gap, and live again; the plane holds still during the replay, and the hint says which iterations are playing. *Shuffled* plays random draws from the whole first half of the history instead of one stretch: the earlier distribution rather than one moment of it. A difference between live and replay means the chains have not settled; no difference is weaker evidence (3 s is a few hundred draws, and two stuck stretches sound alike).
25 - None of these can reveal a mode no chain has found (*stiff*, with a gradient sampler): only wide starts, more chains and other samplers help there.
26- **Draws**: below the plane, the θ₁ marginal of the draws after warm-up (the first half of each chain) against the exact mixture marginal, and each chain's θ₁ trace over its last 1000 draws (the modes' centers dashed, the end of warm-up marked). In the panel, each chain's acceptance rate, and the mean, variances, covariance and P(θ₁ > 0) of the pooled draws after warm-up against the exact values (mean 0, Cov = ½ Σₖ (Σₖ + mₖ mₖᵀ)), and split R̂ for θ₁ and θ₂ (highlighted above 1.05). Changing the target or the sampler keeps the chains where they are and starts a fresh set of draws.
28Things to try (from the notes): RWM on *separated* with ε = 0.1, 0.75, 2 and 4; GHMC at ε = 0.2 sweeping γ from 1 down to 0.01; HMC on *ridge* at ε = 0.5 (above the 0.45 limit: frozen); ULA on *ridge* at ε = 0.75 (diverges); every gradient sampler on *stiff* (they never find the needle; RWM with ε ≈ 2 does). There, MALA at ε = 0.5 gives split R̂ ≈ 1.00 while P(θ₁ > 0) is about 0.02 against ½: all chains agree, on the wrong answer, and they all keep to the low register. Start all chains in one mode of *separated* by clicking it, and listen for the first to cross.
30## Code
32- `app/js/mixture.js`: a port of `mixture_mcmc.py`, with the same model, samplers and interface in camelCase (`new HMC(model, 0.2, { nSteps: 10, seed: 1 })`, `s.next()`, `s.theta`, `s.proposal`, `s.accepted`, `s.acceptProb`, `s.path`, `s.momentum`, `s.diverged`, setters that validate), plus a seeded generator (sfc32 with Box-Muller normals), the exact θ₁ marginal, and the ground truth. The random numbers are not NumPy's, so a chain is not the same draw for draw as the Python one; `scripts/check-samplers.mjs` reproduces the notes' tables (acceptance rates, switches, the stiff-model lesson) instead.
33- `app/js/voices.js`: the two tones (wavetables with an exponential decay, loudness matched by K-weighted RMS as in the Duffing app), the crossfade, the drone, and the scales.
34- `app/main.js`: the chains, the scheduling on the audio clock, the plots and the statistics (split R̂ as in BDA3).