"""Wasserman (2004), Chapter 24: Simulation Methods.

Companion code for Chapter 24 of L. Wasserman, All of Statistics: A Concise Course in Statistical
Inference (Springer, 2004), pp. 403-422, written from scratch with numpy, scipy and matplotlib. The
functions are short and meant to be read next to the lecture notes (../ch24-simulation-methods.html):

* Section 24.2, basic Monte Carlo integration: mc_estimate (the estimate (24.2), its standard error
  and the Normal interval), two_binomials and delta_density (Example 24.3: the posterior of
  p2 - p1 by simulation and by numerical convolution), dose_response (Example 24.4: the
  order-restricted posterior by rejection, and the posterior of the LD50).
* Section 24.3, importance sampling: is_estimate (the estimate (24.5)), is_second_moment (the
  integral (24.6)), tail_example (Example 24.6, plain Monte Carlo against a N(4, 1) proposal),
  outlier_posterior and snis (Example 24.7: the self-normalised estimate of a posterior mean
  under a t likelihood, with a Normal and a Cauchy proposal), ess (the effective sample size).
* Section 24.4, Metropolis-Hastings: metropolis (random-walk and independence proposals,
  vectorised over nothing, written exactly as the algorithm reads), mh_kernel_check (detailed
  balance of the Metropolis-Hastings kernel on a finite state space), acf and ess_chain (the
  autocorrelation function and the effective sample size of a chain, Geyer's initial positive
  sequence).
* Section 24.5, Gibbs sampling: hier_data and gibbs_hier (Example 24.11, the hierarchical model
  for k = 20 cities), gelman_rubin (the potential scale reduction factor), mwg_logistic
  (Metropolis within Gibbs for a logistic regression, Exercise 5).
* Exercises 24.1 to 24.5: ex1, ex2, ex3, ex4, ex5.
* JSRandom, js_uniforms, js_normals, js_pnorm and the demo_* functions: the seeded generator and
  the exact algorithms of the three browser demonstrations, so that their numbers can be
  reproduced here.

Figures. Chapter 24 has four figures, all computed from simulations; all four are reproduced:
  24.1 the posterior of delta = p2 - p1 from simulation (Example 24.3);
  24.2 three random-walk Metropolis chains for the Cauchy target, b = 0.1, 1, 10 (Example 24.10);
  24.3 trace plots of p1 and mu from the Gibbs sampler of Example 24.11;
  24.4 the posterior of mu, and raw proportions against Bayes estimates (Example 24.11).
Nothing is skipped. Seven supplementary figures of our own illustrate the gaps filled in the notes:
  24.S1 Monte Carlo error against N, and the Normal approximation of the estimate (Section 24.2);
  24.S2 the dose-response example: the order-restricted posterior and the LD50 (Example 24.4);
  24.S3 importance sampling: Example 24.6, the variance against the proposal, Example 24.7;
  24.S4 tuning random-walk Metropolis: acceptance rate, autocorrelation and effective sample size;
  24.S5 convergence diagnostics: dispersed starting values and the Gelman-Rubin statistic;
  24.S6 Exercises 24.1 to 24.3;
  24.S7 Exercises 24.4 and 24.5.
Run `python3 ch24_simulation_methods.py --list`, `python3 ch24_simulation_methods.py` (all eleven
figures, about a minute on one CPU thread) or `python3 ch24_simulation_methods.py --fig 24.2`.
Figures go to ../img/ch24/. The demonstrations simulate everything in the browser and need no
data files.
"""
import math
import os

import numpy as np
from scipy import integrate, special, stats

import common
from common import BLUE, GREEN, GREY, MAGENTA, ORANGE, PURPLE, RED, savefig

CH = 24
M32 = 0xFFFFFFFF
HERE = os.path.dirname(os.path.abspath(__file__))
SQ2PI = math.sqrt(2.0 * math.pi)


# =========================================================================== the demos' generator
def _imul(a, b):
    return (a * b) & M32


class JSRandom:
    """The seeded random numbers of the browser demos (DK.rng in courses/demo-kit.js): u() is
    mulberry32 and norm() the Box-Muller transform, exactly as in JavaScript."""

    def __init__(self, seed):
        self.a = int(seed) & M32
        self.spare = None

    def u(self):
        a = (self.a + 0x6D2B79F5) & M32
        self.a = a
        t = _imul(a ^ (a >> 15), 1 | a)
        t = ((t + _imul(t ^ (t >> 7), 61 | t)) & M32) ^ t
        return ((t ^ (t >> 14)) & M32) / 4294967296.0

    def norm(self):
        if self.spare is not None:
            s, self.spare = self.spare, None
            return s
        x = 0.0
        while x == 0.0:
            x = self.u()
        r = math.sqrt(-2.0 * math.log(x))
        th = 2.0 * math.pi * self.u()
        self.spare = r * math.sin(th)
        return r * math.cos(th)


def js_uniforms(seed, n, start=0):
    """Uniforms number start+1, ..., start+n of DK.rng(seed), vectorised (mulberry32 adds a constant
    to its state at each call, so the j-th output is a hash of seed + j * 0x6D2B79F5 mod 2^32)."""
    j = np.arange(start + 1, start + n + 1, dtype=np.uint64)
    m = np.uint64(M32)
    a = (np.uint64(int(seed) & M32) + j * np.uint64(0x6D2B79F5)) & m
    t = ((a ^ (a >> np.uint64(15))) * (np.uint64(1) | a)) & m
    t = ((t + (((t ^ (t >> np.uint64(7))) * (np.uint64(61) | t)) & m)) & m) ^ t
    return ((t ^ (t >> np.uint64(14))) & m).astype(np.float64) / 4294967296.0


def js_normals(seed, n):
    """The first n values of DK.rng(seed).norm(), vectorised: Box-Muller turns uniforms 2i-1, 2i
    into normals 2i-1 (cosine) and 2i (sine). (Exact unless a uniform is exactly 0, which has
    probability 2^-32 per draw and is checked.)"""
    k = (n + 1) // 2
    u = js_uniforms(seed, 2 * k)
    if np.any(u[0::2] == 0.0):
        raise ValueError("a zero uniform: use JSRandom")
    r = np.sqrt(-2.0 * np.log(u[0::2]))
    th = 2.0 * np.pi * u[1::2]
    z = np.empty(2 * k)
    z[0::2] = r * np.cos(th)
    z[1::2] = r * np.sin(th)
    return z[:n]


def js_pnorm(z):
    """DK.pnorm of the demos: the standard Normal cdf through a rational approximation of erfc
    (relative error below 1.2e-7), copied so that the demos' numbers can be matched exactly."""
    x = abs(z) / math.sqrt(2.0)
    t = 1.0 / (1.0 + 0.5 * x)
    r = t * math.exp(-x * x - 1.26551223 + t * (1.00002368 + t * (0.37409196 + t * (0.09678418 + t * (-0.18628806 + t * (0.27886807 +
        t * (-1.13520398 + t * (1.48851587 + t * (-0.82215223 + t * 0.17087277)))))))))
    return 1.0 - 0.5 * r if z >= 0 else 0.5 * r


def js_lgamma(x):
    """DK.lgamma of the demos (Lanczos), so that the t densities agree with the page to the last digit."""
    c = [0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059,
         12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7]
    if x < 0.5:
        return math.log(math.pi / abs(math.sin(math.pi * x))) - js_lgamma(1 - x)
    x -= 1
    s, t = c[0], x + 7.5
    for i in range(1, 9):
        s += c[i] / (x + i)
    return 0.5 * math.log(2 * math.pi) + (x + 0.5) * math.log(t) - t + math.log(s)


def jround(x):
    """JavaScript's Math.round (halves go up), so that grids agree with the demos."""
    return int(math.floor(x + 0.5))


# =========================================================================== 24.2 basic Monte Carlo
def mc_estimate(y, alpha=0.05):
    """The Monte Carlo estimate (24.2) from the values Y_i = w(X_i): the mean, the standard error
    s / sqrt(N) with s^2 the sample variance (divisor N - 1), and the Normal 1 - alpha interval."""
    y = np.asarray(y, float)
    N = len(y)
    est = y.mean()
    se = y.std(ddof=1) / math.sqrt(N)
    z = stats.norm.ppf(1 - alpha / 2)
    return est, se, (est - z * se, est + z * se)


def example_24_1(N=10000, seed=241):
    """Example 24.1: I = int_0^1 x^3 dx = 1/4 with N uniforms; returns (estimate, se, exact se)."""
    x = np.random.default_rng(seed).uniform(size=N)
    est, se, _ = mc_estimate(x ** 3)
    return est, se, math.sqrt((1 / 7 - 1 / 16) / N)


def example_24_2(x=2.0, N=10000, seed=242):
    """Example 24.2: Phi(x) as the proportion of N standard Normal draws that are <= x."""
    z = np.random.default_rng(seed).standard_normal(N)
    return mc_estimate((z <= x).astype(float))[:2]


def two_binomials(X=8, n=10, Y=6, m=10, N=1000, rng=None):
    """Example 24.3: N posterior draws of delta = p2 - p1 under a flat prior,
    p1 ~ Beta(X + 1, n - X + 1) and p2 ~ Beta(Y + 1, m - Y + 1) independently."""
    rng = np.random.default_rng(243) if rng is None else rng
    p1 = rng.beta(X + 1, n - X + 1, size=N)
    p2 = rng.beta(Y + 1, m - Y + 1, size=N)
    return p2 - p1


def delta_density(d, X=8, n=10, Y=6, m=10):
    """Exact posterior density of delta = p2 - p1 in Example 24.3, by numerical convolution:
    f(d) = int f1(p) f2(p + d) dp over the p with 0 < p, p + d < 1."""
    a = stats.beta(X + 1, n - X + 1)
    b = stats.beta(Y + 1, m - Y + 1)
    out = []
    for dd in np.atleast_1d(d):
        lo, hi = max(0.0, -dd), min(1.0, 1.0 - dd)
        out.append(integrate.quad(lambda p: a.pdf(p) * b.pdf(p + dd), lo, hi)[0] if hi > lo else 0.0)
    return np.array(out)


def delta_cdf(c, X=8, n=10, Y=6, m=10):
    """Exact posterior cdf P(p2 - p1 <= c) = int f1(p) F2(p + c) dp."""
    a = stats.beta(X + 1, n - X + 1)
    b = stats.beta(Y + 1, m - Y + 1)
    return integrate.quad(lambda p: a.pdf(p) * b.cdf(min(1.0, max(0.0, p + c))), 0, 1, limit=200)[0]


DOSE_Y = np.array([0, 0, 2, 2, 8, 10, 12, 14, 15, 14])
DOSE_N = 15


def ld50_index(P):
    """delta = x_j with j = min{i : P_i > 1/2} (doses x_i = i); 11 when no P_i exceeds 1/2."""
    P = np.atleast_2d(P)
    above = P > 0.5
    return np.where(above.any(1), above.argmax(1) + 1, 11)


def dose_response(N=1000, Y=DOSE_Y, n=DOSE_N, seed=244, batch=200000):
    """Example 24.4: N draws from the flat prior truncated to p1 <= ... <= p10, by drawing the ten
    independent Beta(Y_i + 1, n - Y_i + 1) posteriors and keeping the ordered draws.
    Returns the kept draws (N x 10), delta for each, and the acceptance rate."""
    rng = np.random.default_rng(seed)
    kept, tried = [], 0
    while sum(len(k) for k in kept) < N:
        P = rng.beta(Y + 1, n - Y + 1, size=(batch, len(Y)))
        tried += batch
        kept.append(P[np.all(np.diff(P, axis=1) >= 0, axis=1)])
    P = np.vstack(kept)
    acc = len(P) / tried
    P = P[:N]
    return P, ld50_index(P), acc


# =========================================================================== 24.3 importance sampling
def is_estimate(h, logf, logg, x):
    """Importance sampling (24.5): the mean of h(X_i) f(X_i) / g(X_i), X_i ~ g; returns the
    estimate and the terms."""
    y = h(x) * np.exp(logf(x) - logg(x))
    return y.mean(), y


def is_second_moment(h, f, g, lo, hi, points=None):
    """The integral (24.6), int h^2 f^2 / g, by adaptive quadrature over (lo, hi)."""
    return integrate.quad(lambda x: h(x) ** 2 * f(x) ** 2 / g(x) if g(x) > 0 else 0.0, lo, hi,
                          limit=400, points=points)[0]


def ess(w):
    """Effective sample size (sum w)^2 / sum w^2 of importance weights (Kong 1992)."""
    w = np.asarray(w, float)
    s2 = np.sum(w * w)
    return float(np.sum(w) ** 2 / s2) if s2 > 0 else 0.0


def tail_example(N=100, R=10000, c=3.0, mu=4.0, seed=246):
    """Example 24.6: R independent estimates of P(Z > c) from N draws, by plain Monte Carlo and by
    importance sampling from N(mu, 1). Returns the two arrays of estimates and the exact sds."""
    rng = np.random.default_rng(seed)
    p = stats.norm.sf(c)
    z = rng.standard_normal((R, N))
    plain = (z > c).mean(1)
    x = mu + rng.standard_normal((R, N))
    w = np.exp(-0.5 * x ** 2 + 0.5 * (x - mu) ** 2) * (x > c)
    imp = w.mean(1)
    # second moment of the IS term: e^{mu^2} * P(Z > c + mu), by completing the square
    m2 = math.exp(mu * mu) * stats.norm.sf(c + mu)
    return plain, imp, math.sqrt(p * (1 - p) / N), math.sqrt((m2 - p * p) / N)


def is_tail_sd(mu, s, c=3.0, N=1):
    """Exact sd of the IS estimate of P(Z > c) from N draws of g = N(mu, s^2); inf when s^2 <= 1/2.
    Completing the square, with k = 2 s^2 - 1 > 0, the second moment (24.6) is
    s^2 / sqrt(k) * exp(mu^2 / k) * P(W > c) for W ~ N(-mu / k, s^2 / k)."""
    p = stats.norm.sf(c)
    if s * s <= 0.5:
        return math.inf
    k = 2 * s * s - 1
    m2 = s * s / math.sqrt(k) * math.exp(mu * mu / k) * stats.norm.sf(c, -mu / k, s / math.sqrt(k))
    return math.sqrt(max(m2 - p * p, 0.0) / N)


# Example 24.7: the book does not print its two observations. For n = 2 the flat-prior posterior is
# symmetric about (X1 + X2) / 2, so the book's posterior mean -0.54 fixes their average; we take two
# observations 5 apart with that average.
OUTLIER_X = np.array([-3.04, 1.96])
NU = 3


def t_logpdf(x, nu=NU):
    return (special.gammaln((nu + 1) / 2) - special.gammaln(nu / 2) - 0.5 * math.log(nu * math.pi)
            - (nu + 1) / 2 * np.log1p(np.asarray(x, float) ** 2 / nu))


def outlier_loglik(theta, X=OUTLIER_X, nu=NU):
    theta = np.asarray(theta, float)
    return sum(t_logpdf(xi - theta, nu) for xi in X)


def outlier_posterior(X=OUTLIER_X, nu=NU):
    """Example 24.7 with a flat prior: the normalising constant, posterior mean and posterior sd of
    theta, by quadrature."""
    L = lambda th: math.exp(float(outlier_loglik(th, X, nu)))
    pts = sorted(X.tolist())
    c = sum(integrate.quad(L, a, b, limit=400)[0] for a, b in zip([-np.inf] + pts, pts + [np.inf]))
    m1 = sum(integrate.quad(lambda t: t * L(t), a, b, limit=400)[0] for a, b in zip([-np.inf] + pts, pts + [np.inf])) / c
    m2 = sum(integrate.quad(lambda t: t * t * L(t), a, b, limit=400)[0] for a, b in zip([-np.inf] + pts, pts + [np.inf])) / c
    return c, m1, math.sqrt(m2 - m1 * m1)


def snis(x, logtarget, logg, h=lambda t: t):
    """Self-normalised importance sampling: sum w h / sum w with w = target / g (the target need
    only be known up to a constant). Returns the estimate and the weights."""
    lw = logtarget(x) - logg(x)
    w = np.exp(lw - lw.max())
    return float(np.sum(w * h(x)) / np.sum(w)), w


def snis_asvar(g, X=OUTLIER_X):
    """Asymptotic variance N * V(estimate) of self-normalised IS of the posterior mean in Example
    24.7: int (theta - I)^2 pi(theta)^2 / g(theta) d theta with pi the normalised posterior (the delta
    method applied to the ratio of two means)."""
    c, I, _ = outlier_posterior(X)
    pts = sorted(X.tolist())
    edges = [-np.inf] + pts + [np.inf]
    fn = lambda t: (t - I) ** 2 * math.exp(2 * float(outlier_loglik(t, X))) / c ** 2 / g(t)
    return sum(integrate.quad(fn, a, b, limit=400)[0] for a, b in zip(edges[:-1], edges[1:]))


def example_24_7(N=10000, seed=247, X=OUTLIER_X):
    """Example 24.7: posterior mean of theta by self-normalised IS with a standard Normal and a
    standard Cauchy proposal. Returns a dict."""
    rng = np.random.default_rng(seed)
    c, pm, psd = outlier_posterior(X)
    m = 0.0
    lt = lambda t: outlier_loglik(t, X)
    xn = m + rng.standard_normal(N)
    en, wn = snis(xn, lt, lambda t: stats.norm.logpdf(t, m, 1))
    xc = m + rng.standard_cauchy(N)
    ec, wc = snis(xc, lt, lambda t: stats.cauchy.logpdf(t, m, 1))
    return {"post_mean": pm, "post_sd": psd, "normal": en, "cauchy": ec, "ess_normal": ess(wn), "ess_cauchy": ess(wc),
            "xn": xn, "wn": wn, "xc": xc, "wc": wc}


# =========================================================================== 24.4-24.5 MCMC
def metropolis(logf, N, b, x0=0.0, rng=None, independence=False, logg=None, draw_g=None):
    """Metropolis-Hastings as written in Section 24.4. Random walk: Y = X_i + b * N(0, 1), accept
    with probability min(1, f(Y)/f(X_i)). Independence: Y ~ g (draw_g), accept with probability
    min(1, f(Y) g(X_i) / (f(X_i) g(Y))). Returns the chain X_1, ..., X_N and the acceptance rate."""
    rng = np.random.default_rng(0) if rng is None else rng
    x, lx = float(x0), float(logf(x0))
    out = np.empty(N)
    acc = 0
    for i in range(N):
        if independence:
            y = float(draw_g(rng))
            lr = float(logf(y)) - lx + float(logg(x)) - float(logg(y))
        else:
            y = x + b * rng.standard_normal()
            lr = float(logf(y)) - lx
        if rng.uniform() < math.exp(min(0.0, lr)):
            x, lx = y, float(logf(y))
            acc += 1
        out[i] = x
    return out, acc / N


def mh_kernel_check(f, Q):
    """Metropolis-Hastings on the finite state space {0, ..., K-1} with target f and proposal matrix
    Q (rows sum to 1): returns the transition matrix P, including the rejection mass on the diagonal."""
    K = len(f)
    P = np.zeros((K, K))
    for x in range(K):
        for y in range(K):
            if y != x and Q[x, y] > 0:
                P[x, y] = Q[x, y] * min(1.0, f[y] * Q[y, x] / (f[x] * Q[x, y]))
        P[x, x] = 1.0 - P[x].sum()
    return P


def acf(u, maxlag):
    """Sample autocorrelations rho_0..rho_maxlag with the biased (divisor N) autocovariance."""
    u = np.asarray(u, float)
    N = len(u)
    d = u - u.mean()
    c0 = np.dot(d, d) / N
    if c0 <= 0:
        return np.r_[1.0, np.ones(maxlag)]
    return np.array([1.0] + [np.dot(d[:N - k], d[k:]) / N / c0 for k in range(1, maxlag + 1)])


def iat(u, maxlag=None):
    """Integrated autocorrelation time tau = 1 + 2 sum rho_k by Geyer's initial positive sequence:
    add the pairs Gamma_m = rho_{2m} + rho_{2m+1} while they are positive; tau = -1 + 2 sum Gamma_m.
    A constant chain gets tau = N. Same rule as the Metropolis demonstration."""
    u = np.asarray(u, float)
    N = len(u)
    maxlag = N // 2 if maxlag is None else maxlag
    d = u - u.mean()
    c0 = np.dot(d, d) / N
    if not c0 > 0:
        return float(N)
    tau, m = -1.0, 0
    while 2 * m + 1 <= maxlag:
        r0 = 1.0 if m == 0 else np.dot(d[:N - 2 * m], d[2 * m:]) / N / c0
        r1 = np.dot(d[:N - 2 * m - 1], d[2 * m + 1:]) / N / c0
        G = r0 + r1
        if G <= 0:
            break
        tau += 2 * G
        m += 1
    return tau if tau > 0 else 1.0


def ess_chain(u, maxlag=None):
    return len(u) / iat(u, maxlag)


def ar1_iat(rho):
    """IAT of a stationary AR(1) chain with lag-one correlation rho: (1 + rho) / (1 - rho)."""
    return (1 + rho) / (1 - rho)


# ----------------------------------------------------------------- Example 24.11, hierarchical model
def hier_data(k=20, n=20, mu=0.0, tau=1.0, seed=2411):
    """Simulated data for Example 24.11: psi_i ~ N(mu, tau^2), Y_i ~ Binomial(n, expit(psi_i)), with
    the first seed (from `seed` on) that gives no Y_i equal to 0 or n (the empirical logit must be
    finite). Returns Y, the raw proportions, Z_i = logit(p_hat_i) and sigma_i^2 = 1/(n p_hat (1 - p_hat))."""
    s = seed
    while True:
        rng = np.random.default_rng(s)
        psi = rng.normal(mu, tau, size=k)
        Y = rng.binomial(n, special.expit(psi))
        if np.all((Y > 0) & (Y < n)):
            break
        s += 1
    ph = Y / n
    Z = np.log(ph / (1 - ph))
    sig2 = 1.0 / (n * ph * (1 - ph))
    return {"Y": Y, "n": n, "phat": ph, "Z": Z, "sig2": sig2, "psi": psi, "seed": s}


def gibbs_hier(Z, sig2, N=1000, tau=1.0, mu0=0.0, psi0=None, rng=None):
    """The Gibbs sampler of Example 24.11 with tau known and a flat prior on mu:
    mu | rest ~ N(mean(psi), tau^2/k); psi_i | rest ~ N(e_i, d_i^2) with
    e_i = (Z_i/sigma_i^2 + mu/tau^2) / (1/sigma_i^2 + 1/tau^2), d_i^2 = 1/(1/sigma_i^2 + 1/tau^2).
    Returns the draws of mu (N) and psi (N x k)."""
    rng = np.random.default_rng(0) if rng is None else rng
    Z, sig2 = np.asarray(Z, float), np.asarray(sig2, float)
    k = len(Z)
    psi = Z.copy() if psi0 is None else np.array(psi0, float)
    mus, psis = np.empty(N), np.empty((N, k))
    d2 = 1.0 / (1.0 / sig2 + 1.0 / tau ** 2)
    for it in range(N):
        mu = rng.normal(psi.mean(), tau / math.sqrt(k))
        e = d2 * (Z / sig2 + mu / tau ** 2)
        psi = rng.normal(e, np.sqrt(d2))
        mus[it], psis[it] = mu, psi
    return mus, psis


def hier_exact(Z, sig2, tau=1.0):
    """The exact posterior of Example 24.11 (flat prior on mu, tau known), for checking the sampler:
    marginally Z_i ~ N(mu, sigma_i^2 + tau^2), so mu | Z ~ N(m, v) with precision-weighted m;
    and E(psi_i | Z) = E(e_i | Z) is linear in mu."""
    Z, sig2 = np.asarray(Z, float), np.asarray(sig2, float)
    wts = 1.0 / (sig2 + tau ** 2)
    v = 1.0 / wts.sum()
    m = v * np.sum(wts * Z)
    d2 = 1.0 / (1.0 / sig2 + 1.0 / tau ** 2)
    epsi = d2 * (Z / sig2 + m / tau ** 2)
    return m, v, epsi


def gelman_rubin(chains):
    """Potential scale reduction factor R-hat (Gelman and Rubin 1992) of an (m chains x n) array."""
    chains = np.asarray(chains, float)
    m, n = chains.shape
    means = chains.mean(1)
    B = n * means.var(ddof=1)
    W = chains.var(1, ddof=1).mean()
    Vhat = (n - 1) / n * W + B / n
    return math.sqrt(Vhat / W)


# =========================================================================== exercises
def ex1(N=100000, seed=2401):
    """Exercise 24.1: I = int_1^2 phi(x) dx. (a) basic Monte Carlo with X ~ Unif(1, 2); (b) its exact
    standard error; (c) importance sampling from N(1.5, v^2), v = 0.1, 1, 10 (estimate, exact se,
    and the largest term); (d) the optimal g* (zero variance)."""
    rng = np.random.default_rng(seed)
    I = stats.norm.cdf(2) - stats.norm.cdf(1)
    x = rng.uniform(1, 2, size=N)
    est, se, _ = mc_estimate(stats.norm.pdf(x))
    # int_1^2 phi^2 = (1 / (2 sqrt(pi))) (Phi(2 sqrt 2) - Phi(sqrt 2))
    m2 = (stats.norm.cdf(2 * math.sqrt(2)) - stats.norm.cdf(math.sqrt(2))) / (2 * math.sqrt(math.pi))
    se_exact = math.sqrt((m2 - I * I) / N)
    out = {"I": I, "basic": est, "se_hat": se, "se_exact": se_exact, "is": {}}
    h = lambda t: ((t > 1) & (t < 2)).astype(float)
    for v in (0.1, 1.0, 10.0):
        xv = rng.normal(1.5, v, size=N)
        y = h(xv) * stats.norm.pdf(xv) / stats.norm.pdf(xv, 1.5, v)
        mom2 = integrate.quad(lambda t: stats.norm.pdf(t) ** 2 / stats.norm.pdf(t, 1.5, v), 1, 2, limit=200)[0]
        out["is"][v] = {"est": y.mean(), "se_exact": math.sqrt((mom2 - I * I) / N), "se_hat": y.std(ddof=1) / math.sqrt(N),
                        "max": y.max(), "terms": y, "m2": mom2}
    # g*: phi truncated to (1, 2), drawn by inversion; every term equals I
    u = rng.uniform(size=N)
    xs = stats.norm.ppf(stats.norm.cdf(1) + u * I)
    ys = stats.norm.pdf(xs) / (stats.norm.pdf(xs) / I)
    out["opt"] = {"est": ys.mean(), "sd": ys.std()}
    return out


def ex2_true(x):
    """Exercise 24.2(b): f_X(x) = int phi(y) N(x; y, 1 + y^2) dy."""
    return np.array([integrate.quad(lambda y: stats.norm.pdf(y) * stats.norm.pdf(xx, y, math.sqrt(1 + y * y)), -np.inf, np.inf)[0]
                     for xx in np.atleast_1d(x)])


def ex2(xs, N=1000, seed=2402, wsd=None):
    """Exercise 24.2: the estimate f_hat(x) = mean of f(x, Y_i) w(X_i) / f(X_i, Y_i) over a joint
    sample, with w the N(0, wsd^2) density (wsd = None uses w = f_{X|Y}(. | Y_i)-free choice N(0, 2))."""
    rng = np.random.default_rng(seed)
    Y = rng.standard_normal(N)
    X = rng.normal(Y, np.sqrt(1 + Y ** 2))
    wsd = math.sqrt(2.0) if wsd is None else wsd
    fxy = lambda x, y: stats.norm.pdf(y) * stats.norm.pdf(x, y, np.sqrt(1 + y * y))
    base = stats.norm.pdf(X, 0, wsd) / fxy(X, Y)
    est = np.array([np.mean(fxy(x, Y) * base) for x in np.atleast_1d(xs)])
    sd = np.array([np.std(fxy(x, Y) * base, ddof=1) / math.sqrt(N) for x in np.atleast_1d(xs)])
    return est, sd


def ex3(N=1000, seed=2403):
    """Exercise 24.3(b): accept-reject for N(0, 1) from the Cauchy, with M = sup phi / g = sqrt(2 pi / e).
    Returns the sample, the number of proposals, and M."""
    rng = np.random.default_rng(seed)
    M = math.sqrt(2 * math.pi / math.e)
    out, tried = [], 0
    while len(out) < N:
        x = rng.standard_cauchy()
        u = rng.uniform()
        tried += 1
        if u <= stats.norm.pdf(x) / (M * stats.cauchy.pdf(x)):
            out.append(x)
    return np.array(out), tried, M


IG_TH1, IG_TH2 = 1.5, 2.0


def ig_logpdf(z, th1=IG_TH1, th2=IG_TH2):
    """Exercise 24.4: log density of the inverse Gaussian, normalised:
    f(z) = sqrt(th2/pi) z^{-3/2} exp(-th1 z - th2/z + 2 sqrt(th1 th2)), z > 0."""
    z = np.asarray(z, float)
    with np.errstate(divide="ignore", invalid="ignore"):
        v = 0.5 * math.log(th2 / math.pi) - 1.5 * np.log(z) - th1 * z - th2 / z + 2 * math.sqrt(th1 * th2)
    return np.where(z > 0, v, -np.inf)


def ig_logw(w, th1=IG_TH1, th2=IG_TH2):
    """Log density of W = log Z (Exercise 24.4(b)): f_W(w) = f_Z(e^w) e^w."""
    w = np.asarray(w, float)
    return ig_logpdf(np.exp(w), th1, th2) + w


def ig_cdf(z, th1=IG_TH1, th2=IG_TH2):
    """Inverse Gaussian cdf with mean mu = sqrt(th2/th1) and shape lam = 2 th2."""
    mu, lam = math.sqrt(th2 / th1), 2 * th2
    z = np.asarray(z, float)
    a = np.sqrt(lam / z)
    return stats.norm.cdf(a * (z / mu - 1)) + np.exp(2 * lam / mu) * stats.norm.cdf(-a * (z / mu + 1))


def ex4(N=1000, seed=2404, shapes=((3.46, 3.0), (1.0, 1.0), (20.0, 20.0)), b=1.0):
    """Exercise 24.4: (a) independence Metropolis-Hastings with Gamma(shape, rate) proposals;
    (b) random-walk Metropolis on W = log Z. Returns sample means of Z and 1/Z and the acceptance rates."""
    rng = np.random.default_rng(seed)
    true = (math.sqrt(IG_TH2 / IG_TH1), math.sqrt(IG_TH1 / IG_TH2) + 1 / (2 * IG_TH2))
    out = {"true": true, "ind": {}}
    for a, r in shapes:
        lg = lambda z, a=a, r=r: stats.gamma.logpdf(z, a, scale=1 / r)
        ch, acc = metropolis(ig_logpdf, N, None, x0=true[0], rng=rng, independence=True, logg=lg,
                             draw_g=lambda g, a=a, r=r: g.gamma(a, 1 / r))
        out["ind"][(a, r)] = {"chain": ch, "acc": acc, "mZ": ch.mean(), "mInv": np.mean(1 / ch)}
    chw, acc = metropolis(ig_logw, N, b, x0=0.0, rng=rng)
    z = np.exp(chw)
    out["rw"] = {"chain": z, "acc": acc, "mZ": z.mean(), "mInv": np.mean(1 / z)}
    return out


CORIS_NAMES = ["sbp", "tobacco", "ldl", "adiposity", "famhist", "typea", "obesity", "alcohol", "age"]


def coris():
    """The South African heart disease (CORIS) data of the book's website: X (462 x 9), y = chd."""
    import pandas as pd
    d = pd.read_csv(common.aos_file("coris.dat"), skiprows=4, header=None)
    d = d.iloc[:, 1:].to_numpy(float)
    return d[:, :9], d[:, 9], list(CORIS_NAMES)


def logistic_mle(X, y, iters=50):
    """Maximum likelihood for logistic regression by Newton-Raphson; X includes the intercept column.
    Returns beta and the standard errors from the inverse Fisher information."""
    beta = np.zeros(X.shape[1])
    for _ in range(iters):
        p = special.expit(X @ beta)
        H = X.T @ (X * (p * (1 - p))[:, None])
        step = np.linalg.solve(H, X.T @ (y - p))
        beta += step
        if np.max(np.abs(step)) < 1e-12:
            break
    p = special.expit(X @ beta)
    H = X.T @ (X * (p * (1 - p))[:, None])
    return beta, np.sqrt(np.diag(np.linalg.inv(H)))


def mwg_logistic(X, y, N=10000, burn=1000, steps=None, rng=None, beta0=None):
    """Metropolis within Gibbs for logistic regression with a flat prior: each coefficient in turn
    gets a random-walk Normal proposal with its own step size, all others held fixed (Section 24.5).
    The linear predictor is updated in place, so a sweep costs O(n k). Returns the draws (N x k)
    after `burn` sweeps and the acceptance rate of each coordinate."""
    rng = np.random.default_rng(0) if rng is None else rng
    n, k = X.shape
    beta = np.zeros(k) if beta0 is None else np.array(beta0, float)
    eta = X @ beta
    ll = lambda e: float(np.sum(y * e - np.logaddexp(0.0, e)))
    cur = ll(eta)
    steps = np.full(k, 0.1) if steps is None else np.asarray(steps, float)
    draws = np.empty((N, k))
    acc = np.zeros(k)
    for it in range(N + burn):
        for j in range(k):
            prop = beta[j] + steps[j] * rng.standard_normal()
            eta_new = eta + X[:, j] * (prop - beta[j])
            new = ll(eta_new)
            if math.log(rng.uniform()) < new - cur:
                beta[j], eta, cur = prop, eta_new, new
                if it >= burn:
                    acc[j] += 1
        if it >= burn:
            draws[it - burn] = beta
    return draws, acc / N


def ex5(N=10000, burn=1000, seed=2405):
    """Exercise 24.5: Bayesian logistic regression of chd on the nine CORIS covariates with a flat
    prior. Sampling is done for standardised covariates (a linear reparametrisation, so the flat
    prior is still flat) with step sizes 2.4 x the MLE standard errors in that scale (Gelman,
    Roberts and Gilks 1996), and the draws are mapped back to the original units."""
    X, y, names = coris()
    mx, sx = X.mean(0), X.std(0)
    Xs = np.column_stack([np.ones(len(y)), (X - mx) / sx])
    bs, ses = logistic_mle(Xs, y)
    draws_s, acc = mwg_logistic(Xs, y, N=N, burn=burn, steps=2.4 * ses, rng=np.random.default_rng(seed), beta0=bs)
    # back to original units: beta_j = gamma_j / s_j, beta_0 = gamma_0 - sum gamma_j m_j / s_j
    D = np.empty_like(draws_s)
    D[:, 1:] = draws_s[:, 1:] / sx
    D[:, 0] = draws_s[:, 0] - (draws_s[:, 1:] * (mx / sx)).sum(1)
    Xo = np.column_stack([np.ones(len(y)), X])
    b_mle, se_mle = logistic_mle(Xo, y)
    return {"names": ["intercept"] + names, "draws": D, "acc": acc, "mle": b_mle, "se": se_mle,
            "post_mean": D.mean(0), "post_sd": D.std(0, ddof=1), "lo": np.quantile(D, 0.025, axis=0),
            "hi": np.quantile(D, 0.975, axis=0), "ess": np.array([ess_chain(D[:, j], 2000) for j in range(D.shape[1])])}


# =========================================================================== the demos' algorithms
# ---- Monte Carlo error demo (monte-carlo-error-demo.html)
MC_X, MC_N1, MC_Y, MC_N2 = 8, 10, 6, 10


def kth_smallest(u, k):
    return sorted(u)[k - 1]


def mc_problem(prob, x0=2.0):
    """(true I, sigma^2 = Var of one term) for the demo's four problems."""
    if prob == "x3":
        return 0.25, 1 / 7 - 1 / 16
    if prob == "cdf":
        p = js_pnorm(x0)
        return p, p * (1 - p)
    if prob == "ex1":
        I = js_pnorm(2) - js_pnorm(1)
        m2 = (js_pnorm(2 * math.sqrt(2)) - js_pnorm(math.sqrt(2))) / (2 * math.sqrt(math.pi))
        return I, m2 - I * I
    if prob == "delta":
        a1, b1, a2, b2 = MC_X + 1, MC_N1 - MC_X + 1, MC_Y + 1, MC_N2 - MC_Y + 1
        v = a1 * b1 / ((a1 + b1) ** 2 * (a1 + b1 + 1)) + a2 * b2 / ((a2 + b2) ** 2 * (a2 + b2 + 1))
        return a2 / (a2 + b2) - a1 / (a1 + b1), v
    raise ValueError(prob)


def mc_draw(prob, g, x0=2.0):
    if prob == "x3":
        u = g.u()
        return u * u * u
    if prob == "cdf":
        return 1.0 if g.norm() <= x0 else 0.0
    if prob == "ex1":
        u = 1 + g.u()
        return math.exp(-u * u / 2) / math.sqrt(2 * math.pi)
    if prob == "delta":
        u1 = [g.u() for _ in range(MC_N1 + 1)]
        u2 = [g.u() for _ in range(MC_N2 + 1)]
        return kth_smallest(u2, MC_Y + 1) - kth_smallest(u1, MC_X + 1)
    raise ValueError(prob)


MC_COST = {"x3": 1, "cdf": 1, "ex1": 1, "delta": 12}


def mc_N(k):
    """The demo's sample-size slider: N = round(10^(k/4))."""
    return jround(10 ** (k / 4))


def mc_grid(N):
    out, k = [], 0
    while True:
        v = jround(10 ** (1 + k / 10))
        if v >= N:
            break
        if not out or v > out[-1]:
            out.append(v)
        k += 1
    out.append(N)
    return out


def demo_mc(prob="x3", N=10000, R=200, seed=1, x0=2.0):
    """The Monte Carlo error demo: R replications r = 0..R-1, each with DK.rng(seed * 7919 + r);
    replication 0 is drawn in detail. At each N of the grid: replication 0's estimate and standard
    error; over replications the sd of the estimates and the coverage of est +- 1.96 se."""
    I, s2 = mc_problem(prob, x0)
    R = max(20, min(R, (4 * 10 ** 6) // (N * MC_COST[prob])))   # the page's budget rule
    grid = mc_grid(N)
    G = len(grid)
    est = np.zeros((R, G))
    se = np.zeros((R, G))
    for r in range(R):
        g = JSRandom(seed * 7919 + r)
        S = Q = 0.0
        j = 0
        for i in range(1, N + 1):
            y = mc_draw(prob, g, x0)
            S += y
            Q += y * y
            if i == grid[j]:
                m = S / i
                v = (Q - i * m * m) / (i - 1) if i > 1 else 0.0
                est[r, j] = m
                se[r, j] = math.sqrt(max(v, 0.0) / i)
                j += 1
    cover = np.mean(np.abs(est - I) <= 1.96 * se, axis=0)
    sdr = est.std(axis=0, ddof=1) if R > 1 else np.zeros(G)
    return {"I": I, "sigma": math.sqrt(s2), "grid": grid, "R": R, "est0": est[0], "se0": se[0], "sd": sdr,
            "cover": cover, "final": est[:, -1], "finalse": se[:, -1]}


# ---- importance sampling demo (importance-sampling-demo.html)
def _t3_pdf(x):
    return math.exp(js_lgamma(2.0) - js_lgamma(1.5) - 0.5 * math.log(3 * math.pi) - 2.0 * math.log(1 + x * x / 3))


def _phi(x):
    return math.exp(-0.5 * x * x) / math.sqrt(2 * math.pi)


def is_prop_logpdf(kind, x, mu, s):
    z = (x - mu) / s
    if kind == "normal":
        return -0.5 * z * z - math.log(s * math.sqrt(2 * math.pi))
    if kind == "cauchy":
        return -math.log(math.pi * s * (1 + z * z))
    if kind == "t3":
        return math.log(_t3_pdf(z)) - math.log(s)
    raise ValueError(kind)


def is_prop_draw(kind, g, mu, s):
    if kind == "normal":
        return mu + s * g.norm()
    if kind == "cauchy":
        return mu + s * math.tan(math.pi * (g.u() - 0.5))
    if kind == "t3":
        z, a, b, d = g.norm(), g.norm(), g.norm(), g.norm()
        c = (a * a + b * b + d * d) / 3
        return mu + s * z / math.sqrt(c)
    raise ValueError(kind)


def is_target(prob, c=3.0, X=(-3.04, 1.96)):
    """(log of the integrand weight, h, normalised?) for the demo's problems. For 'tail' and 'ex1' the
    target f is N(0, 1) and h an indicator; for 'outlier' the unnormalised posterior L(theta)."""
    if prob == "tail":
        return (lambda x: -0.5 * x * x - math.log(math.sqrt(2 * math.pi))), (lambda x: 1.0 if x > c else 0.0)
    if prob == "ex1":
        return (lambda x: -0.5 * x * x - math.log(math.sqrt(2 * math.pi))), (lambda x: 1.0 if 1 < x < 2 else 0.0)
    if prob == "outlier":
        return (lambda t: sum(math.log(_t3_pdf(xi - t)) for xi in X)), (lambda t: t)
    raise ValueError(prob)


def simpson(fn, a, b, M):
    h = (b - a) / M
    s = fn(a) + fn(b)
    for i in range(1, M):
        s += (4 if i % 2 else 2) * fn(a + i * h)
    return s * h / 3


def is_truth(prob, kind, mu, s, c=3.0, X=(-3.04, 1.96)):
    """The demo's exact quantities: I and the variance of one term (ordinary IS) or the asymptotic
    variance N V (self-normalised), by Simpson's rule on the demo's grid; inf where the integral
    diverges (decided analytically)."""
    lf, h = is_target(prob, c, X)
    if prob == "tail":
        I = 1 - js_pnorm(c)
        if kind == "normal" and s * s <= 0.5:
            return I, math.inf
        m2 = simpson(lambda x: math.exp(2 * lf(x) - is_prop_logpdf(kind, x, mu, s)), c, c + 12, 20000)
        return I, m2 - I * I
    if prob == "ex1":
        I = js_pnorm(2) - js_pnorm(1)
        m2 = simpson(lambda x: math.exp(2 * lf(x) - is_prop_logpdf(kind, x, mu, s)), 1, 2, 4000)
        return I, m2 - I * I
    # outlier: posterior moments by Simpson on [mid - 200, mid + 200]
    mid = 0.5 * (X[0] + X[1])
    a, b = mid - 200, mid + 200
    M = 40000
    Z = simpson(lambda t: math.exp(lf(t)), a, b, M)
    I = simpson(lambda t: t * math.exp(lf(t)), a, b, M) / Z
    if kind == "normal":
        return I, math.inf
    V = simpson(lambda t: (t - I) ** 2 * math.exp(2 * lf(t) - is_prop_logpdf(kind, t, mu, s)), a, b, M) / (Z * Z)
    return I, V


def demo_is(prob="tail", kind="normal", mu=4.0, s=1.0, N=100, R=200, seed=1, c=3.0, X=(-3.04, 1.96)):
    """The importance sampling demo: R replications with DK.rng(seed * 7919 + r); kind 'opt' is the
    optimal proposal g* (only for 'tail' and 'ex1'). Returns replication 0's estimate, ESS and largest
    weight share, the estimates of all replications, and their sd."""
    lf, h = is_target(prob, c, X)
    cost = (4 if kind == "t3" else 1) * (2 if prob == "outlier" else 1)
    R = max(20, min(R, int(2e6 // (N * cost))))   # the page's budget rule
    ests, ess0, wmax0 = [], None, None
    for r in range(R):
        g = JSRandom(seed * 7919 + r)
        ys, ws = [], []
        for i in range(N):
            if kind == "opt":
                # g* = f h / I, drawn by inversion; every term f h / g* equals I on the support of h
                if prob == "tail":
                    I = 1 - js_pnorm(c)
                    x = -js_qnorm(I * g.u())
                else:
                    I = js_pnorm(2) - js_pnorm(1)
                    x = js_qnorm(js_pnorm(1) + g.u() * I)
                w = I if h(x) > 0 else 0.0
                ys.append(w)
                ws.append(w)
                continue
            x = is_prop_draw(kind, g, mu, s)
            lw = lf(x) - is_prop_logpdf(kind, x, mu, s)
            w = math.exp(lw)
            if prob == "outlier":
                ws.append(w)
                ys.append(w * x)
            else:
                ys.append(h(x) * w)
                ws.append(h(x) * w)
        if prob == "outlier":
            sw = sum(ws)
            est = sum(ys) / sw if sw > 0 else float("nan")
        else:
            est = sum(ys) / N
        ests.append(est)
        if r == 0:
            sw, sw2 = sum(ws), sum(w * w for w in ws)
            ess0 = sw * sw / sw2 if sw2 > 0 else 0.0
            wmax0 = max(ws) / sw if sw > 0 else 0.0
    ests = np.array(ests)
    return {"est0": ests[0], "ess0": ess0, "wmax0": wmax0, "ests": ests, "R": R, "sd": float(ests.std(ddof=1)),
            "mean": float(ests.mean())}


def js_qnorm(p):
    """DK.qnorm of the demos (Acklam's algorithm)."""
    if p <= 0:
        return -math.inf
    if p >= 1:
        return math.inf
    a = [-3.969683028665376e+01, 2.209460984245205e+02, -2.759285104469687e+02, 1.383577518672690e+02, -3.066479806614716e+01, 2.506628277459239e+00]
    b = [-5.447609879822406e+01, 1.615858368580409e+02, -1.556989798598866e+02, 6.680131188771972e+01, -1.328068155288572e+01]
    c = [-7.784894002430293e-03, -3.223964580411365e-01, -2.400758277161838e+00, -2.549732539343734e+00, 4.374664141464968e+00, 2.938163982698783e+00]
    d = [7.784695709041462e-03, 3.224671290700398e-01, 2.445134137142996e+00, 3.754408661907416e+00]
    if p < 0.02425:
        q = math.sqrt(-2 * math.log(p))
        return (((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q + c[4]) * q + c[5]) / ((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1)
    if p > 1 - 0.02425:
        q = math.sqrt(-2 * math.log(1 - p))
        return -(((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q + c[4]) * q + c[5]) / ((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1)
    q = p - 0.5
    r = q * q
    return (((((a[0] * r + a[1]) * r + a[2]) * r + a[3]) * r + a[4]) * r + a[5]) * q / (((((b[0] * r + b[1]) * r + b[2]) * r + b[3]) * r + b[4]) * r + 1)


# ---- Metropolis tuning demo (metropolis-tuning-demo.html)
def mh_target(name):
    """(log density up to a constant, cdf F) of the demo's targets. 'ig' is W = log Z for the inverse
    Gaussian of Exercise 24.4 (theta1 = 1.5, theta2 = 2)."""
    if name == "cauchy":
        return (lambda x: -math.log(1 + x * x)), (lambda x: 0.5 + math.atan(x) / math.pi)
    if name == "normal":
        return (lambda x: -0.5 * x * x), js_pnorm
    if name == "mix":
        def lf(x):
            a, b = -0.5 * (x + 3) * (x + 3), -0.5 * (x - 3) * (x - 3)
            m = max(a, b)
            return m + math.log(math.exp(a - m) + math.exp(b - m))
        return lf, (lambda x: 0.5 * js_pnorm(x + 3) + 0.5 * js_pnorm(x - 3))
    if name == "ig":
        mu, lam = math.sqrt(IG_TH2 / IG_TH1), 2 * IG_TH2

        def F(w):
            z = math.exp(w)
            a = math.sqrt(lam / z)
            return js_pnorm(a * (z / mu - 1)) + math.exp(2 * lam / mu) * js_pnorm(-a * (z / mu + 1))
        return (lambda w: -0.5 * w - IG_TH1 * math.exp(w) - IG_TH2 * math.exp(-w)), F
    raise ValueError(name)


def demo_mh_chain(target="cauchy", sampler="rw", b=1.0, N=1000, seed=1, x0=0.0):
    """One chain of the Metropolis demo with DK.rng(seed): for each step a Normal draw (the proposal)
    then a uniform (the acceptance test U < r). sampler 'rw': Y = X + b Z; 'ind': Y = b Z, g = N(0, b^2)."""
    lf, F = mh_target(target)
    g = JSRandom(seed)
    x, lx, acc = x0, lf(x0), 0
    out = []
    for i in range(N):
        z = g.norm()
        if sampler == "rw":
            y = x + b * z
            ly = lf(y)
            lr = ly - lx
        else:
            y = b * z
            ly = lf(y)
            lr = ly - lx + (-0.5 * (x / b) * (x / b)) - (-0.5 * z * z)
        u = g.u()
        if u < math.exp(min(0.0, lr)):
            x, lx = y, ly
            acc += 1
        out.append(x)
    return out, acc / N


def js_iat(u):
    """iat() written loop by loop as in the demo (maximum lag N/2)."""
    N = len(u)
    m = sum(u) / N
    d = [v - m for v in u]
    c0 = sum(v * v for v in d) / N
    if not c0 > 0:
        return float(N)

    def rho(k):
        s = 0.0
        for t in range(N - k):
            s += d[t] * d[t + k]
        return s / N / c0
    tau, k = -1.0, 0
    while 2 * k + 1 <= N // 2:
        G = (1.0 if k == 0 else rho(2 * k)) + rho(2 * k + 1)
        if G <= 0:
            break
        tau += 2 * G
        k += 1
    return tau if tau > 0 else 1.0


def ks_uniform(u):
    """sup_x |F_N(x) - F(x)| from the values U_t = F(X_t)."""
    s = sorted(u)
    N = len(s)
    return max(max((i + 1) / N - v, v - i / N) for i, v in enumerate(s))


def mh_b(k):
    """The demo's step-size slider: b = 10^(k/20)."""
    return 10 ** (k / 20)


def mh_bgrid():
    return [10 ** (-1.5 + 0.125 * k) for k in range(29)]


def demo_mh(target="cauchy", sampler="rw", b=1.0, N=1000, seed=1, x0=0.0, sweep=True):
    """The Metropolis demo's numbers: acceptance rate, IAT and ESS of U = F(X), the KS distance, and
    (sweep) the acceptance rate and ESS/N over the grid of step sizes, each chain of length min(N, 5000)."""
    lf, F = mh_target(target)
    ch, acc = demo_mh_chain(target, sampler, b, N, seed, x0)
    u = [F(x) for x in ch]
    tau = iat(np.array(u))
    out = {"chain": ch, "acc": acc, "iat": tau, "ess": N / tau, "ks": ks_uniform(u)}
    if sweep:
        Ns = min(N, 5000)
        rows = []
        for bb in mh_bgrid():
            c2, a2 = demo_mh_chain(target, sampler, bb, Ns, seed, x0)
            t2 = iat(np.array([F(x) for x in c2]))
            rows.append((bb, a2, 1.0 / t2))
        out["sweep"] = rows
    return out


# =========================================================================== figures
def fig_24_1(out_dir=None):
    """Figure 24.1: posterior of delta = p2 - p1 from 1000 simulated draws (Example 24.3)."""
    d = two_binomials(N=1000, rng=np.random.default_rng(243))
    lo, hi = np.quantile(d, [0.025, 0.975])
    fig, ax = common.plt.subplots(figsize=(5.2, 3.3))
    ax.hist(d, bins=np.arange(-0.9, 0.61, 0.05), density=True, color="#cfd8e8", edgecolor=BLUE, lw=0.8,
            label="1000 posterior draws")
    t = np.linspace(-0.9, 0.6, 301)
    ax.plot(t, delta_density(t), color=RED, lw=1.6, label="exact density (convolution)")
    for v in (lo, hi):
        ax.axvline(v, color=GREY, ls="--", lw=1)
    ax.axvline(d.mean(), color=GREEN, lw=1.2, label="posterior mean (simulated)")
    ax.set_xlabel(r"$\delta=p_2-p_1$")
    ax.set_ylabel("density")
    ax.set_title(r"Example 24.3: $X=8$ of $n=10$, $Y=6$ of $m=10$, flat prior")
    ax.text(0.02, 0.95, "95%% interval (%.2f, %.2f)\nmean %.3f" % (lo, hi, d.mean()), transform=ax.transAxes,
            va="top", fontsize=8.5)
    ax.legend(loc="upper right", fontsize=7.5, frameon=False)
    ax.set_xlim(-0.9, 0.6)
    fig.tight_layout()
    return savefig(fig, CH, "fig24-1", out_dir)


def fig_24_2(out_dir=None):
    """Figure 24.2: three random-walk Metropolis chains for the Cauchy target, b = 0.1, 1, 10."""
    fig, axes = common.plt.subplots(3, 2, figsize=(8.2, 6.4), gridspec_kw={"width_ratios": [2.3, 1]})
    t = np.linspace(-8, 8, 400)
    for row, b in enumerate((0.1, 1.0, 10.0)):
        ch, acc = demo_mh_chain("cauchy", "rw", b, 1000, seed=1)
        ch = np.array(ch)
        ax = axes[row, 0]
        ax.plot(np.arange(1, 1001), ch, color=BLUE, lw=0.7)
        ax.set_xlim(0, 1000)
        ax.set_ylabel(r"$X_i$")
        ax.set_title(r"$b=%g$: acceptance rate %.2f" % (b, acc), fontsize=9.5)
        if row == 2:
            ax.set_xlabel("iteration $i$")
        ax = axes[row, 1]
        ax.hist(np.clip(ch, -8, 8), bins=np.linspace(-8, 8, 33), density=True, color="#cfd8e8", edgecolor=BLUE, lw=0.6)
        ax.plot(t, 1 / (np.pi * (1 + t * t)), color=RED, lw=1.4)
        ax.set_xlim(-8, 8)
        ax.set_ylim(0, 0.75)
        if row == 2:
            ax.set_xlabel("$x$")
    fig.tight_layout()
    return savefig(fig, CH, "fig24-2", out_dir)


def _hier_run(seed=24110):
    D = hier_data()
    mus, psis = gibbs_hier(D["Z"], D["sig2"], N=1000, rng=np.random.default_rng(seed))
    return D, mus, psis


def fig_24_3(out_dir=None):
    """Figure 24.3: trace plots of p1 and mu from the Gibbs sampler of Example 24.11."""
    D, mus, psis = _hier_run()
    fig, axes = common.plt.subplots(2, 1, figsize=(6.6, 4.6))
    it = np.arange(1, 1001)
    axes[0].plot(it, special.expit(psis[:, 0]), color=BLUE, lw=0.6)
    axes[0].axhline(D["phat"][0], color=RED, ls="--", lw=1, label=r"raw proportion $\hat p_1=%g$" % D["phat"][0])
    axes[0].set_ylim(0, 1)
    axes[0].set_ylabel(r"$p_1$")
    axes[0].legend(loc="lower right", fontsize=8, frameon=False)
    axes[1].plot(it, mus, color=BLUE, lw=0.6)
    axes[1].set_ylabel(r"$\mu$")
    axes[1].set_xlabel("iteration")
    for ax in axes:
        ax.set_xlim(0, 1000)
    common.panel_label(axes[0], "a")
    common.panel_label(axes[1], "b")
    fig.tight_layout()
    return savefig(fig, CH, "fig24-3", out_dir)


def fig_24_4(out_dir=None):
    """Figure 24.4: posterior histogram of mu; raw proportions against the Bayes estimates."""
    D, mus, psis = _hier_run()
    m, v, epsi = hier_exact(D["Z"], D["sig2"])
    fig, axes = common.plt.subplots(2, 1, figsize=(6.6, 5.0))
    ax = axes[0]
    ax.hist(mus, bins=np.arange(-1.0, 1.01, 0.1), density=True, color="#cfd8e8", edgecolor=BLUE, lw=0.8)
    t = np.linspace(-1, 1, 300)
    ax.plot(t, stats.norm.pdf(t, m, math.sqrt(v)), color=RED, lw=1.4, label="exact posterior of $\\mu$")
    ax.set_xlabel(r"$\mu$")
    ax.set_ylabel("density")
    ax.legend(loc="upper right", fontsize=8, frameon=False)
    ax = axes[1]
    pb = special.expit(psis).mean(0)
    for a, bb in zip(D["phat"], pb):
        ax.plot([a, bb], [0, 1], color=BLUE, lw=0.9)
    ax.plot(D["phat"], np.zeros_like(pb), "o", color=RED, ms=4, label="raw proportions $\\hat p_i$")
    ax.plot(pb, np.ones_like(pb), "s", color=GREEN, ms=4, label="Bayes estimates $E(p_i\\mid$data$)$")
    ax.set_yticks([0, 1])
    ax.set_yticklabels(["raw", "Bayes"])
    ax.set_xlim(0, 1)
    ax.set_ylim(-0.25, 1.45)
    ax.set_xlabel("proportion")
    ax.legend(loc="upper left", fontsize=8, frameon=False, ncol=2)
    common.panel_label(axes[0], "a")
    common.panel_label(axes[1], "b", loc="upper right")
    fig.tight_layout()
    return savefig(fig, CH, "fig24-4", out_dir)


def fig_24_s1(out_dir=None):
    """Figure 24.S1: Monte Carlo error against N (Examples 24.1 and 24.2) and the Normal approximation."""
    fig, axes = common.plt.subplots(1, 3, figsize=(11.4, 3.5))
    A = demo_mc("x3", N=10000, R=200, seed=1)
    B = demo_mc("cdf", N=10000, R=200, seed=1, x0=2.0)
    ax = axes[0]
    g = np.array(A["grid"])
    ax.fill_between(g, A["est0"] - 1.96 * A["se0"], A["est0"] + 1.96 * A["se0"], color=BLUE, alpha=0.2, lw=0)
    ax.plot(g, A["est0"], color=BLUE, lw=1.3, label=r"$\hat I_N\pm1.96\,\widehat{\rm se}$")
    ax.axhline(0.25, color=RED, ls="--", lw=1, label="$I=1/4$")
    ax.set_xscale("log")
    ax.set_xlabel("$N$")
    ax.set_title(r"Example 24.1: $\int_0^1x^3\,dx$, one run")
    ax.legend(loc="upper right", fontsize=8, frameon=False)
    ax = axes[1]
    for D, col, lab in ((A, BLUE, r"$x^3$ (Ex. 24.1)"), (B, GREEN, r"$\Phi(2)$ (Ex. 24.2)")):
        gg = np.array(D["grid"])
        ax.loglog(gg, D["sd"], "o", color=col, ms=3, label=lab + ": sd over 200 runs")
        ax.loglog(gg, D["sigma"] / np.sqrt(gg), color=col, lw=1)
    ax.set_xlabel("$N$")
    ax.set_ylabel(r"sd of $\hat I_N$")
    ax.set_title(r"the error falls like $\sigma/\sqrt{N}$ (lines)")
    ax.legend(loc="lower left", fontsize=7.5, frameon=False)
    ax = axes[2]
    N = 100
    D = demo_mc("x3", N=N, R=2000, seed=3)
    ax.hist(D["final"], bins=30, density=True, color="#cfd8e8", edgecolor=BLUE, lw=0.6)
    t = np.linspace(0.05, 0.45, 300)
    ax.plot(t, stats.norm.pdf(t, 0.25, D["sigma"] / math.sqrt(N)), color=RED, lw=1.4)
    ax.set_xlabel(r"$\hat I_{100}$")
    ax.set_title("2000 runs of $N=100$; coverage of the\n95%% interval %.3f" % D["cover"][-1], fontsize=9.5)
    for k, ax in enumerate(axes):
        common.panel_label(ax, "abc"[k], loc="upper left" if k != 0 else "lower left")
    fig.tight_layout()
    return savefig(fig, CH, "fig24-S1", out_dir)


def fig_24_s2(out_dir=None):
    """Figure 24.S2: Example 24.4, the order-restricted posterior and the LD50."""
    P, dl, acc = dose_response(N=5000)
    fig, axes = common.plt.subplots(1, 2, figsize=(9.6, 3.6))
    ax = axes[0]
    doses = np.arange(1, 11)
    for i in range(60):
        ax.plot(doses, P[i], color=BLUE, alpha=0.18, lw=0.8)
    ax.plot(doses, P.mean(0), color=BLUE, lw=2, label="posterior mean of $p_i$")
    ax.plot(doses, DOSE_Y / DOSE_N, "o", color=RED, ms=5, label="observed proportion $Y_i/15$")
    ax.axhline(0.5, color=GREY, ls=":", lw=1)
    ax.set_xticks(doses)
    ax.set_xlabel("dose $x_i$")
    ax.set_ylabel("probability of death")
    ax.set_title("60 ordered posterior draws (acceptance rate %.4f)" % acc, fontsize=9.5)
    ax.legend(loc="upper left", fontsize=8, frameon=False)
    ax = axes[1]
    vals, cnt = np.unique(dl, return_counts=True)
    ax.bar(vals, cnt / len(dl), color=BLUE, width=0.6)
    ax.set_xticks(doses)
    ax.set_xlim(0.5, 10.5)
    ax.set_xlabel(r"LD50 $\delta=x_j$")
    ax.set_ylabel("posterior probability")
    ax.set_title(r"posterior of $\delta$: mean %.2f" % dl.mean(), fontsize=9.5)
    common.panel_label(axes[0], "a", loc="lower right")
    common.panel_label(axes[1], "b", loc="upper right")
    fig.tight_layout()
    return savefig(fig, CH, "fig24-S2", out_dir)


def fig_24_s3(out_dir=None):
    """Figure 24.S3: importance sampling, Example 24.6 and Example 24.7."""
    fig, axes = common.plt.subplots(1, 3, figsize=(11.6, 3.6))
    plain, imp, sdp, sdi = tail_example()
    ax = axes[0]
    for v, col, lab in ((plain, RED, "plain Monte Carlo, sd %.5f" % plain.std()), (imp, BLUE, "IS from N(4, 1), sd %.5f" % imp.std())):
        xs = np.sort(v)
        ax.step(np.r_[-0.001, xs], np.r_[0, np.arange(1, len(xs) + 1) / len(xs)], where="post", color=col, lw=1.4, label=lab)
    ax.axvline(stats.norm.sf(3), color="k", ls="--", lw=1)
    ax.set_xlim(-0.0005, 0.0125)
    ax.set_ylim(0, 1.02)
    ax.set_xlabel(r"estimate of $P(Z>3)$, $N=100$")
    ax.set_ylabel("proportion of repetitions $\\leq$ value")
    ax.set_title("Example 24.6: 10000 repetitions", fontsize=9.5)
    ax.legend(loc="lower right", fontsize=7.5, frameon=False)
    ax = axes[1]
    mus = np.linspace(0, 7, 141)
    for s, col in ((0.75, GREEN), (1.0, BLUE), (2.0, PURPLE)):
        ax.semilogy(mus, [is_tail_sd(m_, s, N=100) for m_ in mus], color=col, lw=1.4, label="$g=N(\\mu,%g^2)$" % s)
    ax.axhline(sdp, color=RED, ls="--", lw=1, label="plain Monte Carlo")
    ax.set_xlabel(r"proposal mean $\mu$")
    ax.set_ylabel("exact sd of the estimate, $N=100$")
    ax.set_title("the variance depends on where $g$ puts its mass", fontsize=9.5)
    ax.legend(loc="upper right", fontsize=7.5, frameon=False)
    ax = axes[2]
    E = example_24_7(N=20000)
    for x, w, col, lab in ((E["xn"], E["wn"], RED, "Normal proposal"), (E["xc"], E["wc"], BLUE, "Cauchy proposal")):
        n = np.arange(1, len(x) + 1)
        run = np.cumsum(w * x) / np.cumsum(w)
        ax.plot(n, run, color=col, lw=1.1, label=lab + " (ESS %.0f)" % ess(w))
    ax.axhline(E["post_mean"], color="k", ls="--", lw=1, label="posterior mean %.3f" % E["post_mean"])
    ax.set_xscale("log")
    ax.set_xlabel("$N$")
    ax.set_ylabel(r"self-normalised estimate of $E(\theta\mid X)$")
    ax.set_title("Example 24.7: $t_3$ errors, $X=(%g, %g)$" % tuple(OUTLIER_X), fontsize=9.5)
    ax.legend(loc="upper right", fontsize=7.5, frameon=False)
    for k, ax in enumerate(axes):
        common.panel_label(ax, "abc"[k], loc="upper left" if k == 0 else "lower left" if k == 2 else "upper right")
    fig.tight_layout()
    return savefig(fig, CH, "fig24-S3", out_dir)


def fig_24_s4(out_dir=None):
    """Figure 24.S4: tuning random-walk Metropolis: acceptance rate, ESS/N and autocorrelation."""
    fig, axes = common.plt.subplots(1, 3, figsize=(11.6, 3.5))
    sw = {t: demo_mh(t, "rw", 1.0, N=5000, seed=1)["sweep"] for t in ("cauchy", "normal")}
    ax = axes[0]
    for t, col in (("cauchy", BLUE), ("normal", GREEN)):
        bb = [r[0] for r in sw[t]]
        ax.semilogx(bb, [r[1] for r in sw[t]], color=col, lw=1.5, label=t.capitalize() + " target")
    ax.axhline(0.5, color=GREY, ls=":", lw=1)
    ax.axhline(0.44, color=GREY, ls="--", lw=1)
    ax.set_xlabel("step size $b$")
    ax.set_ylabel("acceptance rate")
    ax.legend(loc="lower left", fontsize=8, frameon=False)
    ax = axes[1]
    for t, col in (("cauchy", BLUE), ("normal", GREEN)):
        bb = [r[0] for r in sw[t]]
        ax.semilogx(bb, [r[2] for r in sw[t]], color=col, lw=1.5, label=t.capitalize())
    ax.set_xlabel("step size $b$")
    ax.set_ylabel(r"ESS$/N$ of $F(X_i)$")
    ax.legend(loc="upper left", fontsize=8, frameon=False)
    ax = axes[2]
    F = mh_target("cauchy")[1]
    for b, col in ((0.1, RED), (1.0, BLUE), (10.0, PURPLE), (3.0, GREEN)):
        ch, _ = demo_mh_chain("cauchy", "rw", b, 5000, seed=1)
        r = acf([F(x) for x in ch], 60)
        ax.plot(np.arange(61), r, color=col, lw=1.3, label="$b=%g$" % b)
    ax.axhline(0, color=GREY, lw=0.8)
    ax.set_xlabel("lag $k$")
    ax.set_ylabel(r"autocorrelation of $F(X_i)$")
    ax.set_title("Cauchy target, $N=5000$", fontsize=9.5)
    ax.legend(loc="upper right", fontsize=8, frameon=False)
    for k, ax in enumerate(axes):
        common.panel_label(ax, "abc"[k], loc="upper right" if k == 0 else "lower right" if k == 1 else "lower left")
    fig.tight_layout()
    return savefig(fig, CH, "fig24-S4", out_dir)


def rhat_curve(seed=2412, m=4, N=1000, starts=(-6.0, -2.0, 2.0, 6.0)):
    """Four Gibbs chains of Example 24.11 started from mu = psi_i = start; R-hat of mu over the
    second halves of the first n iterations."""
    D = hier_data()
    chains = []
    for j, s0 in enumerate(starts[:m]):
        mus, _ = gibbs_hier(D["Z"], D["sig2"], N=N, rng=np.random.default_rng(seed + j), psi0=np.full(20, s0))
        chains.append(np.r_[s0, mus])
    chains = np.array(chains)
    ns = np.arange(10, N + 1, 2)
    rh = [gelman_rubin(chains[:, n // 2:n]) for n in ns]
    return chains, ns, np.array(rh)


def fig_24_s5(out_dir=None):
    """Figure 24.S5: dispersed starting values and the Gelman-Rubin statistic for Example 24.11."""
    chains, ns, rh = rhat_curve()
    fig, axes = common.plt.subplots(1, 2, figsize=(9.6, 3.4))
    ax = axes[0]
    for c, col in zip(chains, (RED, BLUE, GREEN, PURPLE)):
        ax.plot(np.arange(len(c)), c, color=col, lw=0.8)
    ax.set_xlim(0, 60)
    ax.set_xlabel("iteration")
    ax.set_ylabel(r"$\mu$")
    ax.set_title("four chains from dispersed starts (first 60 iterations)", fontsize=9.5)
    ax = axes[1]
    ax.semilogx(ns, rh, color=BLUE, lw=1.4)
    ax.axhline(1.01, color=GREY, ls="--", lw=1)
    ax.axhline(1.1, color=GREY, ls=":", lw=1)
    ax.set_ylim(0.98, 1.6)
    ax.set_xlabel("iterations $n$ (R-hat from the second half)")
    ax.set_ylabel(r"$\hat R$ for $\mu$")
    common.panel_label(axes[0], "a", loc="upper right")
    common.panel_label(axes[1], "b", loc="upper right")
    fig.tight_layout()
    return savefig(fig, CH, "fig24-S5", out_dir)


def fig_24_s6(out_dir=None):
    """Figure 24.S6: Exercises 24.1 (importance sampling terms), 24.2 (a marginal density) and 24.3
    (accept-reject)."""
    fig, axes = common.plt.subplots(1, 3, figsize=(11.6, 3.5))
    E = ex1()
    ax = axes[0]
    for v, col in ((0.1, RED), (1.0, BLUE), (10.0, GREEN)):
        y = E["is"][v]["terms"]
        y = y[y > 0]
        ax.hist(np.log10(y), bins=60, histtype="step", color=col, lw=1.3, density=True,
                label="$v=%g$: se %.1e" % (v, E["is"][v]["se_exact"]))
    ax.axvline(math.log10(E["I"]), color="k", ls="--", lw=1)
    ax.set_ylim(0, 6)
    ax.set_xlabel(r"$\log_{10}$ of the nonzero terms $h f/g$")
    ax.set_title(r"Ex. 24.1(c): $g=N(1.5,v^2)$, $N=10^5$", fontsize=9.5)
    ax.legend(loc="upper right", fontsize=7.5, frameon=False)
    ax = axes[1]
    xs = np.linspace(-6, 6, 121)
    est, sd = ex2(xs, N=1000)
    ax.fill_between(xs, est - 2 * sd, est + 2 * sd, color=BLUE, alpha=0.2, lw=0)
    ax.plot(xs, est, color=BLUE, lw=1.3, label="estimate, $N=1000$")
    ax.plot(xs, ex2_true(xs), color=RED, ls="--", lw=1.3, label="exact $f_X$")
    ax.set_xlabel("$x$")
    ax.set_title(r"Ex. 24.2(b): $Y\sim N(0,1)$, $X\mid Y\sim N(Y,1+Y^2)$", fontsize=9.5)
    ax.legend(loc="upper right", fontsize=7.5, frameon=False)
    ax = axes[2]
    s, tried, M = ex3()
    ax.hist(s, bins=30, density=True, color="#cfd8e8", edgecolor=BLUE, lw=0.6)
    t = np.linspace(-4, 4, 300)
    ax.plot(t, stats.norm.pdf(t), color=RED, lw=1.4)
    ax.set_title("Ex. 24.3(b): 1000 accepted of %d proposals\n(theory: acceptance $1/M=%.3f$)" % (tried, 1 / M), fontsize=9.5)
    ax.set_xlabel("$x$")
    for k, ax in enumerate(axes):
        common.panel_label(ax, "abc"[k], loc="lower right" if k == 0 else "upper left")
    fig.tight_layout()
    return savefig(fig, CH, "fig24-S6", out_dir)


def fig_24_s7(out_dir=None):
    """Figure 24.S7: Exercises 24.4 (inverse Gaussian) and 24.5 (Bayesian logistic regression)."""
    fig = common.plt.figure(figsize=(11.6, 6.4))
    gs = fig.add_gridspec(2, 5)
    E = ex4()
    ax = fig.add_subplot(gs[0, :2])
    z = np.linspace(0.01, 4, 400)
    ax.plot(z, np.exp(ig_logpdf(z)), color="k", lw=1.4, label="inverse Gaussian")
    key = (3.46, 3.0)
    ax.hist(E["ind"][key]["chain"], bins=40, density=True, histtype="step", color=BLUE, lw=1.2,
            label="independence MH, Gamma(3.46, 3)")
    ax.hist(E["rw"]["chain"], bins=40, density=True, histtype="step", color=GREEN, lw=1.2, label="random walk on $\\log Z$")
    ax.set_xlim(0, 4)
    ax.set_xlabel("$z$")
    ax.set_title(r"Ex. 24.4: $\theta_1=1.5$, $\theta_2=2$, $N=1000$", fontsize=9.5)
    ax.legend(loc="upper right", fontsize=7.5, frameon=False)
    R = ex5()
    pos = [(0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4)]
    show = [1, 2, 3, 5, 6, 7, 8, 9]
    for (r, c), j in zip(pos, show):
        ax = fig.add_subplot(gs[r, c])
        ax.hist(R["draws"][:, j], bins=40, density=True, color="#cfd8e8", edgecolor=BLUE, lw=0.4)
        t = np.linspace(*np.quantile(R["draws"][:, j], [0.0005, 0.9995]), 200)
        ax.plot(t, stats.norm.pdf(t, R["mle"][j], R["se"][j]), color=RED, lw=1.1)
        ax.set_title(R["names"][j], fontsize=9)
        ax.tick_params(labelsize=7)
        ax.set_yticks([])
    fig.text(0.62, 0.005, "Ex. 24.5: posterior histograms (10000 draws, flat prior) and the Normal approximation "
             "N(MLE, se$^2$) in red", ha="center", fontsize=8.5)
    fig.tight_layout(rect=(0, 0.03, 1, 1))
    return savefig(fig, CH, "fig24-S7", out_dir)


FIGURES = {"24.1": fig_24_1, "24.2": fig_24_2, "24.3": fig_24_3, "24.4": fig_24_4, "24.S1": fig_24_s1,
           "24.S2": fig_24_s2, "24.S3": fig_24_s3, "24.S4": fig_24_s4, "24.S5": fig_24_s5, "24.S6": fig_24_s6,
           "24.S7": fig_24_s7}

if __name__ == "__main__":
    common.run(FIGURES, CH)
