"""Murphy (2022), Chapter 2: Probability: Univariate Models.

Companion code for Chapter 2 of K. P. Murphy, Probabilistic Machine Learning: An Introduction
(MIT Press, 2022; April 2025 draft), written from scratch with numpy, scipy and matplotlib, to be read next
to the lecture notes (../ch02-probability-univariate.html). scipy and scikit-learn are only used to confirm
the implementations.

Contents
* Section 2.3, Bayes' rule: posterior_binary (2.55)-(2.60), monty_hall_posterior (2.61)-(2.65) for any number of
  doors and for two different host behaviours, monty_hall_simulate.
* Section 2.4-2.5, Bernoulli, sigmoid, softmax: sigmoid, logit, softplus, softmax_T (temperature),
  logsumexp_stable (2.100), logreg_newton (binary logistic regression by Newton's method),
  softmax_regression (multiclass, L2-penalised, L-BFGS with the exact gradient).
* Section 2.6-2.7, distributions: gauss_pdf/gauss_cdf, student_pdf, cauchy_pdf, laplace_pdf, beta_pdf, gamma_pdf,
  invgamma_pdf; fit_gauss, fit_laplace, fit_student_em (EM for a Student location-scale model, fixed nu),
  hetero_regression (homoskedastic and heteroskedastic Gaussian regression, softplus scale).
* Section 2.8, transformations: affine_moments (2.162)-(2.165), conv_density (numerical convolution of densities
  on a grid), the central limit theorem with exact grid convolutions (clt_grid), Monte Carlo for y = x^2.
* Section 2.2.6: Anscombe's quartet (the 44 published pairs of Anscombe 1973) and a simulated-annealing
  construction of data sets with identical summary statistics (sa_dataset).

Figures reproduced (figure number: function): 2.1, 2.2, 2.3, 2.4, 2.5, 2.6 (stand-in, see below), 2.7 (stand-in),
2.9, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18, 2.19, 2.20, 2.21, 2.22, 2.23, 2.24; supplementary
figures 2.S1 (CLT for several base distributions) and 2.S2 (Student t converging to the Gaussian).

Skipped: Figure 2.8 (a hand-drawn illusion: planar line drawing consistent with many 3-D shapes; nothing is
computed). Table 2.4 (the "flip and drag" convolution) is reproduced as a printed array by conv_table().

Stand-ins. Figure 2.6 uses the 142-point Datasaurus data of the original paper, which are not available offline,
so the panels start from a Gaussian cloud with the published summary statistics (mean (54.26, 47.83), standard
deviations (16.77, 26.94), correlation -0.064) and are moved to other shapes by simulated annealing, as in the
paper's method; the dinosaur panel is therefore replaced by the starting cloud. Figure 2.7 builds seven
one-dimensional data sets with the same median and inter-quartile range from standard shapes.

Data. Iris comes from scikit-learn. Everything else is simulated with fixed seeds.
Usage:  python ch02_probability_univariate.py [--fig 2.5 2.11 ...] [--list]
"""
import json
import os
import sys
from math import comb, lgamma

import numpy as np
from scipy import optimize, special, stats
from scipy.signal import fftconvolve

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import common  # noqa: E402
from common import (BLUE, CYAN, GREEN, GREY, MAGENTA, ORANGE, PURPLE, RED, logsumexp, panel_grid,  # noqa: E402
                    panel_label, run, savefig, sigmoid)
import matplotlib.pyplot as plt  # noqa: E402
from matplotlib.patches import Polygon  # noqa: E402

CH = 2
DATA_JSON = os.path.normpath(os.path.join(common.HERE, "..", "data"))


# =========================================================================== Section 2.3: Bayes' rule
def posterior_binary(prior, tpr, tnr, y):
    """p(H=1 | Y=y) for a binary test, equations (2.55)-(2.60). tpr = sensitivity, tnr = specificity."""
    if y == 1:
        num = tpr * prior
        den = tpr * prior + (1 - tnr) * (1 - prior)
    else:
        num = (1 - tpr) * prior
        den = (1 - tpr) * prior + tnr * (1 - prior)
    return num / den


def monty_hall_posterior(n_doors=3, host="informed"):
    """Posterior over the location of the prize after we pick door 1 and the host opens one other door.

    host = "informed": the host knows the prize, opens n-2 doors that hide no prize (for n=3: one door), as in
    Section 2.3.2. We return the posterior for the contestant's door and for each other closed door, given that
    the opened doors show no prize (symmetric in the unopened door).
    host = "random": the host opens one of the other n-1 doors uniformly at random and it happens to show no prize.
    Returns (p_stay, p_switch_to_a_given_other_unopened_door, p_total_switch).
    """
    n = n_doors
    if host == "informed":
        # host opens n-2 of the n-1 other doors, never the prize. P(open set | prize at door 1) = 1/(n-1),
        # prize at an unopened door j != 1: opened set is forced, probability 1.
        # Evidence: the specific set opened (n-2 doors, leaving door j closed).
        lik_stay = 1.0 / (n - 1)
        lik_other = 1.0
        post_unnorm_stay = lik_stay / n
        post_unnorm_other = lik_other / n
        Z = post_unnorm_stay + post_unnorm_other      # only door j and door 1 remain possible
        return post_unnorm_stay / Z, post_unnorm_other / Z, post_unnorm_other / Z
    if host == "random":
        # the host opens one of the n-1 other doors uniformly at random and it shows no prize: the likelihood is
        # 1/(n-1) for every hypothesis except "prize behind the opened door" (0), so the n-1 doors still
        # closed (ours and n-2 others) are equally likely
        return 1.0 / (n - 1), 1.0 / (n - 1), 1.0 / (n - 1)
    raise ValueError(host)


def monty_hall_simulate(n_games, n_doors=3, host="informed", seed=0):
    """Simulate games; return (stay win rate, switch win rate, number of games kept)."""
    rng = np.random.default_rng(seed)
    stay = switch = kept = 0
    for _ in range(n_games):
        prize = rng.integers(n_doors)
        pick = 0
        others = [d for d in range(n_doors) if d != pick]
        if host == "informed":
            keep_door = prize if prize != pick else rng.choice(others)
            # the host leaves keep_door (and the contestant's door) closed, opens the rest of the others
            switch_wins = prize == keep_door
        else:
            opened = rng.choice(others)
            if opened == prize:
                continue        # the host revealed the prize: this game is not in the conditioning event
            kept_closed = [d for d in others if d != opened]
            # contestant switches to a uniformly chosen remaining closed door
            target = rng.choice(kept_closed)
            switch_wins = prize == target
        kept += 1
        stay += prize == pick
        switch += switch_wins
    return stay / kept, switch / kept, kept


# =========================================================================== Sections 2.4, 2.5
def softplus(x):
    return np.logaddexp(0.0, x)


def logit(p):
    p = np.asarray(p, dtype=float)
    return np.log(p) - np.log1p(-p)


def softmax_T(a, T=1.0):
    """softmax(a / T), computed with the log-sum-exp trick (2.100)-(2.102)."""
    z = np.asarray(a, dtype=float) / T
    return np.exp(z - logsumexp(z, axis=-1, keepdims=True))


def logsumexp_stable(a):
    """lse(a) = m + log sum exp(a - m), m = max a, equation (2.100)."""
    a = np.asarray(a, dtype=float)
    m = a.max()
    return m + np.log(np.exp(a - m).sum())


def binomial_pmf(s, N, theta):
    """Bin(s | N, theta) (2.69) computed with exact integer binomial coefficients."""
    s = np.asarray(s)
    return np.array([comb(N, int(k)) for k in s.ravel()]).reshape(s.shape) * theta ** s * (1 - theta) ** (N - s)


def logreg_newton(X, y, iters=50, ridge=0.0):
    """Binary logistic regression p(y=1|x) = sigmoid(w.x + b) by Newton's method (IRLS).

    X is (N, D); an intercept is added. Returns the parameter vector (b, w). ridge penalises w, not b."""
    N = len(y)
    A = np.column_stack([np.ones(N), X])
    th = np.zeros(A.shape[1])
    R = ridge * np.eye(A.shape[1])
    R[0, 0] = 0.0
    for _ in range(iters):
        p = sigmoid(A @ th)
        g = A.T @ (p - y) + R @ th
        H = A.T @ (A * (p * (1 - p))[:, None]) + R
        step = np.linalg.solve(H, g)
        th = th - step
        if np.max(np.abs(step)) < 1e-12:
            break
    return th


def softmax_nll(theta, X, y, C, lam):
    """Penalised multiclass negative log-likelihood and exact gradient; theta = (W (C x D), b (C)) flattened."""
    N, D = X.shape
    W = theta[:C * D].reshape(C, D)
    b = theta[C * D:]
    A = X @ W.T + b
    lse = logsumexp(A, axis=1)
    nll = -(A[np.arange(N), y] - lse).sum() + 0.5 * lam * (W ** 2).sum()
    P = np.exp(A - lse[:, None])
    P[np.arange(N), y] -= 1.0
    gW = P.T @ X + lam * W
    gb = P.sum(0)
    return nll, np.concatenate([gW.ravel(), gb])


def softmax_regression(X, y, C, lam=0.1):
    """Multinomial logistic regression (2.96), L2 penalty (lam/2)||W||^2 on the weights (not on b)."""
    th0 = np.zeros(C * X.shape[1] + C)
    r = optimize.minimize(softmax_nll, th0, args=(X, y, C, lam), jac=True, method="L-BFGS-B",
                          options={"maxiter": 5000, "ftol": 1e-15, "gtol": 1e-10})
    D = X.shape[1]
    return r.x[:C * D].reshape(C, D), r.x[C * D:]


# =========================================================================== Sections 2.6-2.7: densities
def gauss_pdf(x, mu=0.0, sigma=1.0):
    return np.exp(-0.5 * ((x - mu) / sigma) ** 2) / (np.sqrt(2 * np.pi) * sigma)


def gauss_cdf(x, mu=0.0, sigma=1.0):
    """Phi((x - mu) / sigma) = (1 + erf(z / sqrt 2)) / 2, equation below (2.109)."""
    return 0.5 * (1.0 + special.erf((np.asarray(x) - mu) / (sigma * np.sqrt(2.0))))


def student_pdf(x, mu=0.0, sigma=1.0, nu=1.0):
    """Student t density with the normalising constant of (2.130)."""
    z = (np.asarray(x) - mu) / sigma
    logc = special.gammaln((nu + 1) / 2) - special.gammaln(nu / 2) - 0.5 * np.log(nu * np.pi) - np.log(sigma)
    return np.exp(logc - 0.5 * (nu + 1) * np.log1p(z * z / nu))


def cauchy_pdf(x, mu=0.0, gamma=1.0):
    return 1.0 / (np.pi * gamma * (1 + ((np.asarray(x) - mu) / gamma) ** 2))


def laplace_pdf(x, mu=0.0, b=1.0):
    return np.exp(-np.abs(np.asarray(x) - mu) / b) / (2 * b)


def beta_pdf(x, a, b):
    x = np.asarray(x, dtype=float)
    with np.errstate(divide="ignore", invalid="ignore"):
        return np.exp((a - 1) * np.log(x) + (b - 1) * np.log1p(-x) - (special.gammaln(a) + special.gammaln(b) - special.gammaln(a + b)))


def gamma_pdf(x, a, b):
    """Gamma density with shape a and RATE b, (2.140)."""
    x = np.asarray(x, dtype=float)
    with np.errstate(divide="ignore", invalid="ignore"):
        return np.exp(a * np.log(b) - special.gammaln(a) + (a - 1) * np.log(x) - b * x)


def invgamma_pdf(x, a, b):
    """Inverse gamma density (2.145), shape a, scale b."""
    x = np.asarray(x, dtype=float)
    return np.exp(a * np.log(b) - special.gammaln(a) - (a + 1) * np.log(x) - b / x)


def fit_gauss(x):
    return float(np.mean(x)), float(np.std(x))          # maximum likelihood (divisor N)


def fit_laplace(x):
    mu = float(np.median(x))
    return mu, float(np.mean(np.abs(x - mu)))           # ML: median and mean absolute deviation about it


def fit_student_em(x, nu, iters=500, tol=1e-12):
    """ML for T(mu, sigma^2, nu) with nu fixed, by EM on the scale-mixture representation.

    E step: w_n = (nu + 1) / (nu + z_n^2), z_n = (x_n - mu) / sigma; M step: weighted mean and variance."""
    mu, s2 = float(np.median(x)), float(np.var(x))
    for _ in range(iters):
        w = (nu + 1.0) / (nu + (x - mu) ** 2 / s2)
        mu_new = float((w * x).sum() / w.sum())
        s2_new = float((w * (x - mu_new) ** 2).mean())
        done = abs(mu_new - mu) + abs(s2_new - s2) < tol
        mu, s2 = mu_new, s2_new
        if done:
            break
    return mu, float(np.sqrt(s2))


def student_loglik(x, mu, sigma, nu):
    return float(np.log(student_pdf(x, mu, sigma, nu)).sum())


def hetero_regression(x, y, hetero=True):
    """Gaussian regression y ~ N(w0 + w1 x, s(x)^2). hetero=False: constant s (closed form).
    hetero=True: s(x) = softplus(v0 + v1 x) as in (2.122)-(2.123), fitted by L-BFGS. Returns (w, v, nll)."""
    A = np.column_stack([np.ones_like(x), x])
    w0, *_ = np.linalg.lstsq(A, y, rcond=None)
    s0 = float(np.std(y - A @ w0))
    if not hetero:
        nll = 0.5 * len(y) * (np.log(2 * np.pi * s0 ** 2) + 1)
        return w0, np.array([np.log(np.expm1(s0)), 0.0]), nll

    def f(th):
        w, v = th[:2], th[2:]
        s = softplus(A @ v)
        r = y - A @ w
        return float(np.sum(np.log(s) + 0.5 * np.log(2 * np.pi) + 0.5 * (r / s) ** 2))

    th0 = np.concatenate([w0, [np.log(np.expm1(s0)), 0.0]])
    r = optimize.minimize(f, th0, method="L-BFGS-B", options={"maxiter": 5000, "ftol": 1e-15, "gtol": 1e-9})
    r = optimize.minimize(f, r.x, method="Nelder-Mead", options={"xatol": 1e-9, "fatol": 1e-12, "maxiter": 20000})
    return r.x[:2], r.x[2:], r.fun


# =========================================================================== Section 2.8
def affine_moments(mu, Sigma, A, b):
    """Mean and covariance of y = A x + b: A mu + b and A Sigma A^T, equations (2.162) and (2.164)."""
    return A @ mu + b, A @ Sigma @ A.T


def conv_density(p1, p2, dx):
    """Density of x1 + x2 on a common grid of spacing dx: the discrete convolution times dx (2.170).
    Both densities are sampled at x0 + k dx, k = 0..M-1; the result has 2M-1 points starting at the sum of the
    two left ends."""
    return np.convolve(p1, p2) * dx


def conv_table(x=(1, 2, 3, 4), y=(5, 6, 7)):
    """Discrete convolution z_n = sum_k x_k y_{n-k} of Table 2.4 (flip and drag)."""
    x, y = list(x), list(y)
    z = [0] * (len(x) + len(y) - 1)
    for i, a in enumerate(x):
        for j, c in enumerate(y):
            z[i + j] += a * c
    return z


def clt_grid(pdf, lo, hi, N, M=401):
    """Density of the sum of N iid variables with density pdf on [lo, hi], by N-1 numerical convolutions on a grid
    of M points (trapezoid-corrected). Returns (s, density of the sum, standardised z, density of z)."""
    x = np.linspace(lo, hi, M)
    dx = x[1] - x[0]
    p = pdf(x)
    p = p / (p.sum() * dx)
    mu = (x * p).sum() * dx
    var = ((x - mu) ** 2 * p).sum() * dx
    q = p.copy()
    for _ in range(N - 1):
        q = fftconvolve(q, p) * dx
    s = N * lo + np.arange(len(q)) * dx
    q = q / (q.sum() * dx)
    z = (s - N * mu) / np.sqrt(N * var)
    return s, q, z, q * np.sqrt(N * var)


def sup_gap_to_normal(z, fz, ngrid=2001):
    """sup |F_N(z) - Phi(z)| for the distribution with standardised density fz on the grid z (cdf by cumulative
    trapezoid)."""
    dz = np.gradient(z)
    cdf = np.cumsum(fz * dz) - 0.5 * fz * dz
    return float(np.max(np.abs(cdf - special.ndtr(z))))


# =========================================================================== summary statistics / SA
ANSCOMBE_X123 = [10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5]
ANSCOMBE = {
    "I": (ANSCOMBE_X123, [8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68]),
    "II": (ANSCOMBE_X123, [9.14, 8.14, 8.74, 8.77, 9.26, 8.10, 6.13, 3.10, 9.13, 7.26, 4.74]),
    "III": (ANSCOMBE_X123, [7.46, 6.77, 12.74, 7.11, 7.81, 8.84, 6.08, 5.39, 8.15, 6.42, 5.73]),
    "IV": ([8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8], [6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.50, 5.56, 7.91, 6.89]),
}


def seg_dist(P, segs):
    """Distance from each point of P (n, 2) to the nearest of the segments segs (k, 2, 2)."""
    P = np.atleast_2d(P)
    a, b = segs[:, 0, :], segs[:, 1, :]
    ab = b - a
    t = np.clip(((P[:, None, :] - a) * ab).sum(2) / (ab ** 2).sum(1), 0, 1)
    proj = a + t[..., None] * ab
    return np.sqrt(((P[:, None, :] - proj) ** 2).sum(2)).min(1)


SHAPES = {
    "circle": None,
    "x shape": np.array([[[20, 0], [100, 100]], [[20, 100], [100, 0]]], float),
    "horizontal lines": np.array([[[10, y], [100, y]] for y in (10, 30, 50, 70, 90)], float),
    "vertical lines": np.array([[[x, 5], [x, 95]] for x in (10, 30, 50, 70, 90)], float),
    "star": np.array([[[50, 100], [25, 5]], [[25, 5], [100, 65]], [[100, 65], [0, 65]], [[0, 65], [75, 5]], [[75, 5], [50, 100]]], float),
    "slant up": np.array([[[10, 5], [100, 95]]], float),
    "slant down": np.array([[[10, 95], [100, 5]]], float),
    "wide lines": np.array([[[20, 10], [20, 90]], [[80, 10], [80, 90]]], float),
}


def _circle_segs(c=(50, 50), r=40, k=60):
    t = np.linspace(0, 2 * np.pi, k + 1)
    pts = np.column_stack([c[0] + r * np.cos(t), c[1] + r * np.sin(t)])
    return np.stack([pts[:-1], pts[1:]], axis=1)


def start_cloud(n=142, seed=0):
    """A Gaussian cloud with exactly the published Datasaurus summary statistics."""
    rng = np.random.default_rng(seed)
    mu = np.array([54.26, 47.83])
    sd = np.array([16.77, 26.94])
    r = -0.0645
    Z = rng.normal(size=(n, 2))
    Z = (Z - Z.mean(0)) / Z.std(0, ddof=1)
    Z[:, 1] -= (Z[:, 0] @ Z[:, 1]) / (Z[:, 0] @ Z[:, 0]) * Z[:, 0]      # make the sample exactly uncorrelated
    Z = (Z - Z.mean(0)) / Z.std(0, ddof=1)
    Z[:, 1] = r * Z[:, 0] + np.sqrt(1 - r * r) * Z[:, 1]
    return mu + Z * sd


def summary2(P):
    return np.array([P[:, 0].mean(), P[:, 1].mean(), P[:, 0].std(ddof=1), P[:, 1].std(ddof=1), np.corrcoef(P.T)[0, 1]])


def _stats_from_sums(n, sx, sy, sxx, syy, sxy):
    mx, my = sx / n, sy / n
    vx = (sxx - n * mx * mx) / (n - 1)
    vy = (syy - n * my * my) / (n - 1)
    cxy = (sxy - n * mx * my) / (n - 1)
    return np.array([mx, my, np.sqrt(vx), np.sqrt(vy), cxy / np.sqrt(vx * vy)])


def sa_dataset(P0, segs, iters=300000, seed=0, tol=0.05, tol_r=0.002, T0=2.0):
    """Move the points of P0 towards the shape given by segments segs by simulated annealing while the means and
    standard deviations (to tol) and the correlation (to tol_r) stay equal to the starting values. One point is
    perturbed at a time; a move that keeps the statistics is accepted if it brings the point closer to the shape,
    or otherwise with probability exp(-increase / T) with T cooling quadratically. The sums needed for the
    statistics are updated in O(1) per move."""
    rng = np.random.default_rng(seed)
    P = P0.copy()
    n = len(P)
    target = summary2(P0)
    sx, sy = P[:, 0].sum(), P[:, 1].sum()
    sxx, syy, sxy = (P[:, 0] ** 2).sum(), (P[:, 1] ** 2).sum(), (P[:, 0] * P[:, 1]).sum()
    d = seg_dist(P, segs)
    for it in range(iters):
        T = T0 * (1 - it / iters) ** 2 + 1e-9
        i = rng.integers(n)
        old = P[i]
        step = 0.3 + 6 * (1 - it / iters) ** 2
        new = old + rng.normal(size=2) * step
        s = _stats_from_sums(n, sx - old[0] + new[0], sy - old[1] + new[1], sxx - old[0] ** 2 + new[0] ** 2,
                             syy - old[1] ** 2 + new[1] ** 2, sxy - old[0] * old[1] + new[0] * new[1])
        if np.all(np.abs(s[:4] - target[:4]) < tol) and abs(s[4] - target[4]) < tol_r:
            dn = seg_dist(new, segs)[0]
            if dn < d[i] or rng.random() < np.exp(-(dn - d[i]) / T):
                d[i] = dn
                sx, sy = sx - old[0] + new[0], sy - old[1] + new[1]
                sxx, syy = sxx - old[0] ** 2 + new[0] ** 2, syy - old[1] ** 2 + new[1] ** 2
                sxy = sxy - old[0] * old[1] + new[0] * new[1]
                P[i] = new
    return P


# =========================================================================== figures
def _out(a):
    return a


def _xlim_pad(ax, x, p=0.05):
    lo, hi = np.min(x), np.max(x)
    ax.set_xlim(lo - p * (hi - lo), hi + p * (hi - lo))


def fig_2_1(out=None):
    """Two pmfs on {1,2,3,4}: uniform and degenerate."""
    fig, axes = panel_grid(1, 2, size=(2.9, 2.2))
    k = np.arange(1, 5)
    axes[0, 0].bar(k, np.full(4, 0.25), color=BLUE, width=0.7)
    axes[0, 1].bar(k, (k == 1).astype(float), color=BLUE, width=0.7)
    for ax, lab in zip(axes[0], "ab"):
        ax.set_xticks(k)
        ax.set_ylim(0, 1.05)
        ax.set_xlabel("$x$")
        panel_label(ax, lab, "upper right")
    axes[0, 0].set_ylabel("$p(x)$")
    axes[0, 0].set_title("uniform")
    axes[0, 1].set_title("degenerate, $p(x)=\\mathbb{I}(x=1)$")
    fig.tight_layout()
    savefig(fig, CH, "fig2-1", out)


def fig_2_2(out=None, alpha=0.05):
    """Standard normal cdf and pdf with the central 1-alpha interval."""
    fig, axes = panel_grid(1, 2, size=(3.3, 2.5))
    x = np.linspace(-3.5, 3.5, 400)
    axes[0, 0].plot(x, gauss_cdf(x), color=BLUE)
    axes[0, 0].set_xlabel("$x$")
    axes[0, 0].set_ylabel("$\\Phi(x)$")
    axes[0, 0].set_title("Gaussian cdf")
    ax = axes[0, 1]
    ax.plot(x, gauss_pdf(x), color=BLUE)
    q = stats.norm.ppf(1 - alpha / 2)
    for lo, hi in ((-3.5, -q), (q, 3.5)):
        xx = np.linspace(lo, hi, 100)
        ax.fill_between(xx, gauss_pdf(xx), color=RED, alpha=0.4)
    ax.fill_between(np.linspace(-q, q, 100), gauss_pdf(np.linspace(-q, q, 100)), color=GREY, alpha=0.2)
    ax.text(-q - 0.1, 0.12, "$\\alpha/2$", ha="right", color=RED)
    ax.text(q + 0.1, 0.12, "$\\alpha/2$", ha="left", color=RED)
    ax.text(0, 0.18, "$1-\\alpha$", ha="center")
    ax.set_xticks([-q, 0, q])
    ax.set_xticklabels(["$\\Phi^{-1}(\\alpha/2)$", "0", "$\\Phi^{-1}(1-\\alpha/2)$"], fontsize=7.5)
    ax.set_title("pdf, $\\alpha=0.05$: cutoffs $\\pm%.2f$" % q)
    for a, lab in zip(axes[0], "ab"):
        panel_label(a, lab, "upper left")
    fig.tight_layout()
    savefig(fig, CH, "fig2-2", out)


def fig_2_3(out=None):
    """A product joint p(x,y)=p(x)p(y) as an outer product, with 6 x 5 states."""
    rng = np.random.default_rng(3)
    px = rng.dirichlet(np.ones(6) * 3)
    py = rng.dirichlet(np.ones(5) * 3)
    J = np.outer(px, py)
    fig, axes = panel_grid(1, 3, size=(2.2, 2.6), gridspec_kw={"width_ratios": [5, 1, 1]})
    axes[0, 0].imshow(J, cmap="Blues", aspect="auto")
    axes[0, 0].set_title("$p(x,y)=p(x)p(y)$")
    axes[0, 1].imshow(px[:, None], cmap="Blues", aspect="auto")
    axes[0, 1].set_title("$p(x)$")
    axes[0, 2].imshow(py[None, :].T, cmap="Blues", aspect="auto")
    axes[0, 2].set_title("$p(y)$")
    for ax in axes[0]:
        ax.set_xticks([])
        ax.set_yticks([])
    axes[0, 0].set_ylabel("$X$ (6 states)")
    axes[0, 0].set_xlabel("$Y$ (5 states)")
    fig.suptitle("free parameters: joint 29, product 5 + 4 = 9", fontsize=9, y=0.02)
    savefig(fig, CH, "fig2-3", out)


def fig_2_4(out=None):
    """Mixture of two 1d Gaussians (standard deviations 0.5)."""
    x = np.linspace(-2, 4, 500)
    p = 0.5 * gauss_pdf(x, 0, 0.5) + 0.5 * gauss_pdf(x, 2, 0.5)
    fig, ax = plt.subplots(figsize=(4.2, 2.6))
    ax.plot(x, p, color=BLUE)
    ax.set_xlabel("$x$")
    ax.set_ylabel("$p(x)$")
    ax.set_title("$0.5\\,\\mathcal{N}(x|0,0.5^2)+0.5\\,\\mathcal{N}(x|2,0.5^2)$", fontsize=9)
    fig.tight_layout()
    savefig(fig, CH, "fig2-4", out)


def fig_2_5(out=None):
    """Anscombe's quartet with the common regression line."""
    fig, axes = panel_grid(2, 2, size=(2.6, 2.1), sharex=True, sharey=True)
    for ax, (name, (x, y)), lab in zip(axes.ravel(), ANSCOMBE.items(), "abcd"):
        x, y = np.array(x, float), np.array(y)
        ax.scatter(x, y, color=BLUE, s=16)
        b = np.polyfit(x, y, 1)
        xx = np.array([3, 20])
        ax.plot(xx, np.polyval(b, xx), color=RED, lw=1.2)
        ax.set_title("Dataset " + name, fontsize=9)
        ax.set_xlim(3, 20)
        ax.set_ylim(2, 14)
        panel_label(ax, lab, "upper left")
    for ax in axes[1]:
        ax.set_xlabel("$x$")
    for ax in axes[:, 0]:
        ax.set_ylabel("$y$")
    fig.tight_layout()
    savefig(fig, CH, "fig2-5", out)


def make_datasaurus(seed=0, iters=300000):
    P0 = start_cloud(seed=seed)
    cache = os.path.join(common.HERE, "cache")
    os.makedirs(cache, exist_ok=True)
    path = os.path.join(cache, "ch02_sa_%d_%d.npz" % (seed, iters))
    if os.path.exists(path):
        z = np.load(path)
        return P0, {k: z[k] for k in z.files}
    res = {}
    for i, (name, segs) in enumerate(SHAPES.items()):
        if segs is None:
            segs = _circle_segs()
        res[name] = sa_dataset(P0, segs, iters=iters, seed=seed + 1 + i)
    np.savez(path, **res)
    return P0, res


def fig_2_6(out=None):
    """Data sets with identical summary statistics (a stand-in for the Datasaurus Dozen): simulated annealing."""
    P0, res = make_datasaurus()
    names = ["start"] + list(res)
    sets = [P0] + [res[k] for k in res]
    fig, axes = panel_grid(3, 3, size=(2.0, 2.0), sharex=True, sharey=True)
    for ax, nm, P in zip(axes.ravel(), names, sets):
        ax.scatter(P[:, 0], P[:, 1], s=5, color=BLUE)
        ax.set_title(nm, fontsize=8.5)
        ax.set_xlim(0, 105)
        ax.set_ylim(-5, 105)
        ax.set_xticks([0, 50, 100])
        ax.set_yticks([0, 50, 100])
    fig.tight_layout()
    savefig(fig, CH, "fig2-6", out)
    return sets


def one_d_datasets(n=300, seed=0):
    """Seven 1-d samples with the same median (0) and the same interquartile range (1) (a stand-in for Figure 2.7)."""
    rng = np.random.default_rng(seed)
    raw = {
        "Gaussian": rng.normal(size=n),
        "uniform": rng.uniform(-1, 1, n),
        "bimodal": np.where(rng.random(n) < 0.5, rng.normal(-1.5, 0.35, n), rng.normal(1.5, 0.35, n)),
        "skewed": rng.exponential(size=n),
        "heavy tails": rng.standard_t(2, n),
        "spike": np.concatenate([rng.normal(0, 0.15, n // 2), rng.uniform(-4, 4, n - n // 2)]),
        "two clumps": np.where(rng.random(n) < 0.5, rng.normal(-0.5, 0.1, n), rng.normal(0.5, 0.1, n)),
    }
    out = {}
    for k, v in raw.items():
        q1, med, q3 = np.percentile(v, [25, 50, 75])
        out[k] = (v - med) / (q3 - q1)
    return out


def fig_2_7(out=None):
    """Seven samples with equal median and IQR: box plots hide the differences, violin plots show them."""
    D = one_d_datasets()
    names = list(D)
    data = [D[k] for k in names]
    fig, axes = panel_grid(1, 3, size=(2.7, 2.6), gridspec_kw={"width_ratios": [1.3, 1, 1.3]})
    for i, v in enumerate(data):
        axes[0, 0].scatter(np.random.default_rng(i).uniform(-.3, .3, len(v)) + i, v, s=3, color=BLUE, alpha=.5)
    axes[0, 1].boxplot(data, vert=True, widths=0.6)
    parts = axes[0, 2].violinplot(data, showmedians=True, showextrema=False)
    for b in parts["bodies"]:
        b.set_facecolor(BLUE)
        b.set_alpha(0.5)
    parts["cmedians"].set_color(RED)
    for ax in axes[0]:
        ax.set_xticks(range(1, 8) if ax is not axes[0, 0] else range(7))
        ax.set_xticklabels(names, rotation=70, fontsize=7)
        ax.set_ylim(-4, 5)
    axes[0, 0].set_title("data", fontsize=9)
    axes[0, 1].set_title("box plots", fontsize=9)
    axes[0, 2].set_title("violin plots", fontsize=9)
    fig.tight_layout()
    savefig(fig, CH, "fig2-7", out)


def fig_2_9(out=None):
    """Binomial pmfs, N = 10, theta = 0.25 and 0.9."""
    fig, axes = panel_grid(1, 2, size=(3.0, 2.3))
    k = np.arange(11)
    for ax, th, lab in zip(axes[0], (0.25, 0.9), "ab"):
        ax.bar(k, binomial_pmf(k, 10, th), color=BLUE, width=0.7)
        ax.set_xticks(k)
        ax.set_xlabel("$s$")
        ax.set_title("$\\theta=%.2f$" % th)
        panel_label(ax, lab, "upper left")
    axes[0, 0].set_ylabel("$\\mathrm{Bin}(s|10,\\theta)$")
    fig.tight_layout()
    savefig(fig, CH, "fig2-9", out)


def fig_2_10(out=None):
    """Sigmoid and Heaviside functions."""
    a = np.linspace(-4, 4, 801)
    fig, axes = panel_grid(1, 2, size=(3.0, 2.3))
    axes[0, 0].plot(a, sigmoid(a), color=BLUE)
    axes[0, 0].set_title("sigmoid $\\sigma(a)$")
    axes[0, 1].plot(a, (a > 0).astype(float), color=BLUE)
    axes[0, 1].set_title("Heaviside $\\mathbb{I}(a>0)$")
    for ax, lab in zip(axes[0], "ab"):
        ax.set_xlabel("$a$")
        ax.set_ylim(-0.05, 1.05)
        panel_label(ax, lab, "lower right")
    fig.tight_layout()
    savefig(fig, CH, "fig2-10", out)


def iris_data():
    from sklearn.datasets import load_iris
    d = load_iris()
    return d.data, d.target


def fig_2_11(out=None):
    """Logistic regression for Iris-Virginica against petal width."""
    X, y = iris_data()
    w = X[:, 3:4]
    yb = (y == 2).astype(float)
    th = logreg_newton(w, yb)
    xs = np.linspace(0, 3, 300)
    fig, ax = plt.subplots(figsize=(4.4, 2.8))
    ax.scatter(w[yb == 1, 0], np.ones((yb == 1).sum()), marker="s", s=14, color=GREEN, alpha=0.6, label="Iris-Virginica")
    ax.scatter(w[yb == 0, 0], np.zeros((yb == 0).sum()), marker="o", s=14, color=BLUE, alpha=0.6, label="Not Iris-Virginica")
    ax.plot(xs, sigmoid(th[0] + th[1] * xs), color=GREEN, label="$p(y=1|x)$")
    ax.plot(xs, 1 - sigmoid(th[0] + th[1] * xs), color=BLUE, ls="--", label="$p(y=0|x)$")
    xb = -th[0] / th[1]
    ax.axvline(xb, color=GREY, ls=":")
    ax.text(xb + 0.04, 0.5, "boundary %.2f" % xb, fontsize=8, color=GREY)
    ax.set_xlabel("Petal width (cm)")
    ax.set_ylabel("Probability")
    ax.legend(loc="center left", fontsize=7.5)
    fig.tight_layout()
    savefig(fig, CH, "fig2-11", out)
    return th, xb


def fig_2_12(out=None):
    """softmax(a/T), a = (3, 0, 1), at T = 100, 2, 1."""
    a = np.array([3.0, 0.0, 1.0])
    fig, axes = panel_grid(1, 3, size=(2.1, 2.2), sharey=True)
    for ax, T in zip(axes[0], (100, 2, 1)):
        ax.bar([1, 2, 3], softmax_T(a, T), color=BLUE, width=0.7)
        ax.set_title("$T=%d$" % T)
        ax.set_xticks([1, 2, 3])
        ax.set_ylim(0, 1)
    axes[0, 0].set_ylabel("$\\mathrm{softmax}(a/T)_c$")
    fig.tight_layout()
    savefig(fig, CH, "fig2-12", out)


def iris_softmax(lam=0.1):
    X, y = iris_data()
    F = X[:, 2:4]
    W, b = softmax_regression(F, y, 3, lam)
    return F, y, W, b


def fig_2_13(out=None):
    """Softmax regression on iris with petal length and width."""
    F, y, W, b = iris_softmax()
    gx, gy = np.meshgrid(np.linspace(0.5, 7.5, 300), np.linspace(-1, 3.5, 300))
    G = np.column_stack([gx.ravel(), gy.ravel()])
    P = softmax_T(G @ W.T + b)
    cls = P.argmax(1).reshape(gx.shape)
    fig, ax = plt.subplots(figsize=(4.8, 3.2))
    ax.contourf(gx, gy, cls, levels=[-0.5, 0.5, 1.5, 2.5], colors=["#cfe0f7", "#fbe2c8", "#d8eed2"], alpha=0.8)
    for c, (col, mk, nm) in enumerate(zip((BLUE, ORANGE, GREEN), ("o", "s", "^"), ("Iris-Setosa", "Iris-Versicolor", "Iris-Virginica"))):
        ax.scatter(F[y == c, 0], F[y == c, 1], color=col, marker=mk, s=14, label=nm)
    pv = P[:, 2].reshape(gx.shape)
    cs = ax.contour(gx, gy, pv, levels=[0.15, 0.3, 0.45, 0.6, 0.75, 0.9], colors="k", linewidths=0.5)
    ax.clabel(cs, fontsize=6)
    ax.set_xlabel("Petal length")
    ax.set_ylabel("Petal width")
    ax.legend(fontsize=7.5, loc="upper left")
    fig.tight_layout()
    savefig(fig, CH, "fig2-13", out)


def hetero_data(n=120, seed=1):
    rng = np.random.default_rng(seed)
    x = rng.uniform(-20, 60, n)
    mu = 4 + 0.25 * x
    sd = softplus(-1.0 + 0.045 * (x + 20)) * 1.0
    y = mu + sd * rng.normal(size=n)
    return x, y


def fig_2_14(out=None):
    """Homoskedastic and heteroskedastic linear regression with 95% predictive bands."""
    x, y = hetero_data()
    xs = np.linspace(-20, 60, 200)
    fig, axes = panel_grid(1, 2, size=(3.2, 2.5), sharey=True)
    w0, v0, n0 = hetero_regression(x, y, False)
    w1, v1, n1 = hetero_regression(x, y, True)
    for ax, (w, v, hs), lab in zip(axes[0], ((w0, v0, False), (w1, v1, True)), "ab"):
        m = w[0] + w[1] * xs
        s = softplus(v[0] + v[1] * xs) if hs else np.full_like(xs, softplus(v[0]))
        ax.scatter(x, y, s=7, color=BLUE, alpha=0.7)
        ax.plot(xs, m, color=RED)
        ax.fill_between(xs, m - 2 * s, m + 2 * s, color=GREY, alpha=0.25)
        ax.set_xlabel("$x$")
        panel_label(ax, lab, "upper left")
    axes[0, 0].set_ylabel("$y$")
    axes[0, 0].set_title("constant $\\sigma$ (NLL %.1f)" % n0, fontsize=9)
    axes[0, 1].set_title("$\\sigma(x)$ (NLL %.1f)" % n1, fontsize=9)
    fig.tight_layout()
    savefig(fig, CH, "fig2-14", out)


def fig_2_15(out=None):
    """Gaussian, Student (nu=1, 2) and Laplace pdfs and log pdfs, matched to unit scale as in the book."""
    x = np.linspace(-5, 5, 801)
    curves = [("Gaussian", gauss_pdf(x), BLUE), ("Student ($\\nu=1$)", student_pdf(x, 0, 1, 1), RED),
              ("Student ($\\nu=2$)", student_pdf(x, 0, 1, 2), GREEN), ("Laplace", laplace_pdf(x, 0, 1 / np.sqrt(2)), ORANGE)]
    fig, axes = panel_grid(1, 2, size=(3.3, 2.6))
    for lab, p, c in curves:
        axes[0, 0].plot(x, p, color=c, label=lab)
        axes[0, 1].plot(x, np.log(p), color=c, label=lab)
    axes[0, 0].set_ylabel("pdf")
    axes[0, 1].set_ylabel("log pdf")
    axes[0, 1].set_ylim(-8, 0)
    axes[0, 0].legend(fontsize=7)
    for ax, lab in zip(axes[0], "ab"):
        ax.set_xlabel("$x$")
        panel_label(ax, lab, "lower right" if lab == "a" else "upper right")
    fig.tight_layout()
    savefig(fig, CH, "fig2-15", out)


def robust_data(seed=0, n=30, n_out=3, out_loc=8.0):
    rng = np.random.default_rng(seed)
    x = rng.normal(0.0, 1.0, n)
    return x, np.concatenate([x, out_loc + 0.3 * rng.normal(size=n_out)])


def fig_2_16(out=None):
    """Gaussian, Student and Laplace fits without and with outliers."""
    clean, dirty = robust_data()
    xs = np.linspace(-5, 12, 600)
    fig, axes = panel_grid(1, 2, size=(3.3, 2.5), sharey=True)
    for ax, d, lab in zip(axes[0], (clean, dirty), "ab"):
        ax.hist(d, bins=np.arange(-4, 11, 0.5), density=True, color="#cfd8e6", edgecolor="white")
        m, s = fit_gauss(d)
        ax.plot(xs, gauss_pdf(xs, m, s), color=BLUE, label="Gaussian")
        mt, st = fit_student_em(d, 4.0)
        ax.plot(xs, student_pdf(xs, mt, st, 4.0), color=RED, label="Student ($\\nu=4$)")
        ml, bl = fit_laplace(d)
        ax.plot(xs, laplace_pdf(xs, ml, bl), color=GREEN, ls="--", label="Laplace")
        ax.set_xlabel("$x$")
        panel_label(ax, lab, "upper right")
    axes[0, 0].legend(fontsize=7, loc="upper left")
    fig.tight_layout()
    savefig(fig, CH, "fig2-16", out)


def fig_2_17(out=None):
    """Beta and gamma densities."""
    fig, axes = panel_grid(1, 2, size=(3.4, 2.6))
    x = np.linspace(0.001, 0.999, 500)
    for (a, b), c in zip([(0.1, 0.1), (0.1, 1.0), (1.0, 1.0), (2.0, 2.0), (2.0, 8.0)], (RED, ORANGE, GREEN, BLUE, MAGENTA)):
        axes[0, 0].plot(x, beta_pdf(x, a, b), color=c, label="a=%.1f,b=%.1f" % (a, b))
    axes[0, 0].set_ylim(0, 6)
    axes[0, 0].set_title("Beta distributions", fontsize=9)
    axes[0, 0].legend(fontsize=6.5)
    x = np.linspace(0.001, 7, 500)
    for (a, b), c in zip([(1.0, 1.0), (1.5, 1.0), (2.0, 1.0), (1.0, 2.0), (1.5, 2.0), (2.0, 2.0)], (RED, ORANGE, GREEN, BLUE, MAGENTA, PURPLE)):
        axes[0, 1].plot(x, gamma_pdf(x, a, b), color=c, label="a=%.1f,b=%.1f" % (a, b))
    axes[0, 1].set_ylim(0, 2)
    axes[0, 1].set_title("Gamma distributions (rate $b$)", fontsize=9)
    axes[0, 1].legend(fontsize=6.5)
    for ax, lab in zip(axes[0], "ab"):
        panel_label(ax, lab, "upper right")
    fig.tight_layout()
    savefig(fig, CH, "fig2-17", out)


def fig_2_18(out=None):
    """Empirical pdf (spikes) and cdf (staircase) of N = 5 samples."""
    rng = np.random.default_rng(4)
    s = np.sort(rng.normal(size=5))
    fig, axes = panel_grid(1, 2, size=(3.2, 2.3))
    axes[0, 0].vlines(s, 0, 1 / 5, color=BLUE, lw=2)
    axes[0, 0].plot(s, np.full(5, 0.2), "o", color=BLUE)
    axes[0, 0].set_ylim(0, 0.3)
    axes[0, 0].set_title("empirical pdf: mass $1/N$ at each sample", fontsize=8)
    xs = np.linspace(-3, 3, 800)
    F = (s[None, :] <= xs[:, None]).mean(1)
    axes[0, 1].step(xs, F, where="post", color=BLUE)
    axes[0, 1].plot(xs, gauss_cdf(xs), color=GREY, lw=0.8, ls="--")
    axes[0, 1].set_title("empirical cdf (dashed: true cdf)", fontsize=8)
    for ax, lab in zip(axes[0], "ab"):
        ax.set_xlabel("$x$")
        panel_label(ax, lab, "upper left")
    fig.tight_layout()
    savefig(fig, CH, "fig2-18", out)


def fig_2_19(out=None):
    """Uniform(0,1) pushed through y = 2x + 1: density halves, support doubles, mass preserved."""
    rng = np.random.default_rng(0)
    x = rng.uniform(size=200000)
    y = 2 * x + 1
    fig, axes = panel_grid(1, 2, size=(3.2, 2.4))
    ax = axes[0, 0]
    ax.hist(x, bins=40, density=True, color=BLUE, alpha=0.6, label="$p(x)$")
    ax.hist(y, bins=80, density=True, color=RED, alpha=0.6, label="$p(y)$, $y=2x+1$")
    ax.plot([0, 1], [1, 1], color=BLUE)
    ax.plot([1, 3], [0.5, 0.5], color=RED)
    ax.set_ylim(0, 1.3)
    ax.legend(fontsize=7)
    ax.set_xlabel("value")
    xx = np.linspace(0, 1, 50)
    axes[0, 1].plot(xx, 2 * xx + 1, color=BLUE)
    for x0 in (0.3, 0.7):
        axes[0, 1].plot([x0, x0, 0], [0, 2 * x0 + 1, 2 * x0 + 1], color=GREY, ls=":")
        axes[0, 1].plot([x0 + 0.05, x0 + 0.05, 0], [0, 2 * x0 + 1.1, 2 * x0 + 1.1], color=GREY, ls=":")
    axes[0, 1].set_xlabel("$x$")
    axes[0, 1].set_ylabel("$y=f(x)$")
    axes[0, 1].set_title("$dy=2\\,dx$: $p(y)=p(x)/2$", fontsize=9)
    for a, lab in zip(axes[0], "ab"):
        panel_label(a, lab, "upper left")
    fig.tight_layout()
    savefig(fig, CH, "fig2-19", out)


def fig_2_20(out=None):
    """An affine map of the unit square: translation (A = I) and linear part (b = 0); area scales by det A."""
    sq = np.array([[0, 0], [1, 0], [1, 1], [0, 1]], float)
    A = np.array([[1.4, 0.6], [0.3, 0.9]])
    fig, axes = panel_grid(1, 2, size=(2.9, 2.6))
    for ax in axes[0]:
        ax.add_patch(Polygon(sq, closed=True, fc="#cfd8e6", ec=BLUE))
        ax.set_aspect("equal")
        ax.set_xlim(-0.3, 2.6)
        ax.set_ylim(-0.3, 2.0)
        ax.axhline(0, color=GREY, lw=0.5)
        ax.axvline(0, color=GREY, lw=0.5)
    b = np.array([0.8, 0.6])
    axes[0, 0].add_patch(Polygon(sq + b, closed=True, fc="#f4c9c9", ec=RED))
    axes[0, 0].set_title("$A=I$, $b=(0.8,0.6)$: area unchanged", fontsize=8)
    axes[0, 1].add_patch(Polygon(sq @ A.T, closed=True, fc="#f4c9c9", ec=RED))
    axes[0, 1].set_title("$b=0$: area $\\times\\,\\det A=%.2f$" % np.linalg.det(A), fontsize=8)
    fig.tight_layout()
    savefig(fig, CH, "fig2-20", out)


def fig_2_21(out=None):
    """The polar patch of area r dr dtheta."""
    r, dr, th, dth = 1.0, 0.35, 0.5, 0.45
    t = np.linspace(th, th + dth, 30)
    inner = np.column_stack([r * np.cos(t), r * np.sin(t)])
    outer = np.column_stack([(r + dr) * np.cos(t[::-1]), (r + dr) * np.sin(t[::-1])])
    patch = np.vstack([inner, outer])
    fig, ax = plt.subplots(figsize=(3.4, 3.0))
    ax.add_patch(Polygon(patch, closed=True, fc="#cfd8e6", ec=BLUE))
    ax.plot([0, (r + dr) * np.cos(th + dth) * 1.15], [0, (r + dr) * np.sin(th + dth) * 1.15], color=GREY, lw=0.7)
    ax.plot([0, (r + dr) * np.cos(th) * 1.15], [0, (r + dr) * np.sin(th) * 1.15], color=GREY, lw=0.7)
    area = 0.5 * ((r + dr) ** 2 - r ** 2) * dth
    ax.text(0.95, 0.8, "area $\\approx r\\,dr\\,d\\theta$\n(exact %.3f, approx %.3f)" % (area, (r + dr / 2) * dr * dth), fontsize=7.5)
    ax.set_aspect("equal")
    ax.set_xlim(0, 1.7)
    ax.set_ylim(0, 1.3)
    ax.set_xlabel("$x_1$")
    ax.set_ylabel("$x_2$")
    fig.tight_layout()
    savefig(fig, CH, "fig2-21", out)


def fig_2_22(out=None):
    """Distribution of the sum of two dice by discrete convolution."""
    d = np.full(6, 1 / 6)
    z = np.convolve(d, d)
    fig, ax = plt.subplots(figsize=(4.2, 2.5))
    ax.bar(np.arange(2, 13), z, color=BLUE, width=0.7)
    for k, v in zip(range(2, 13), z):
        ax.text(k, v + 0.003, "%d/36" % round(v * 36), ha="center", fontsize=6.5)
    ax.set_xticks(range(2, 13))
    ax.set_xlabel("$y=x_1+x_2$")
    ax.set_ylabel("$p(y)$")
    fig.tight_layout()
    savefig(fig, CH, "fig2-22", out)


def fig_2_23(out=None):
    """CLT: histogram of the mean of N Beta(1,5) draws, N = 1 and 5 (and the Gaussian limit)."""
    rng = np.random.default_rng(0)
    fig, axes = panel_grid(1, 2, size=(3.2, 2.4), sharey=True)
    mu, var = 1 / 6, 5 / (36 * 7)
    for ax, N, lab in zip(axes[0], (1, 5), "ab"):
        m = rng.beta(1, 5, size=(10000, N)).mean(1)
        ax.hist(m, bins=np.linspace(0, 1, 41), density=True, color=BLUE, alpha=0.7)
        xs = np.linspace(0.001, 1, 400)
        ax.plot(xs, gauss_pdf(xs, mu, np.sqrt(var / N)), color=RED, lw=1.2)
        ax.set_title("$N=%d$" % N)
        ax.set_xlim(0, 1)
        ax.set_xlabel("sample mean")
        panel_label(ax, lab, "upper right")
    axes[0, 0].set_ylabel("density")
    fig.tight_layout()
    savefig(fig, CH, "fig2-23", out)


def fig_2_24(out=None):
    """y = x^2 for x ~ Unif(-1,1): exact density (2.155 summed over both branches) and a Monte Carlo estimate."""
    rng = np.random.default_rng(0)
    x = rng.uniform(-1, 1, 5000)
    y = x ** 2
    ys = np.linspace(0.005, 1, 300)
    exact = 1.0 / (2 * np.sqrt(ys))           # two branches x = +-sqrt(y), each contributes (1/2)/(2 sqrt y)
    fig, axes = panel_grid(1, 3, size=(2.3, 2.3))
    xs = np.linspace(-1, 1, 100)
    axes[0, 0].plot([-1, 1], [0.5, 0.5], color=BLUE)
    axes[0, 0].set_ylim(0, 1)
    axes[0, 0].set_title("$p(x)$")
    axes[0, 1].plot(ys, exact, color=RED)
    axes[0, 1].set_ylim(0, 6)
    axes[0, 1].set_title("exact $p(y)$")
    axes[0, 2].hist(y, bins=np.linspace(0, 1, 41), density=True, color=BLUE, alpha=0.7)
    axes[0, 2].plot(ys, exact, color=RED, lw=0.8)
    axes[0, 2].set_ylim(0, 6)
    axes[0, 2].set_title("Monte Carlo, $N_s=5000$")
    fig.tight_layout()
    savefig(fig, CH, "fig2-24", out)


def fig_2_S1(out=None):
    """Supplement: the CLT for four base distributions: standardised density of the sum of N variables against the
    Gaussian, N = 1, 2, 5, 30, from exact numerical convolutions."""
    bases = [("Beta(1,5)", lambda x: beta_pdf(np.clip(x, 1e-9, 1 - 1e-9), 1, 5), 0, 1),
             ("uniform(0,1)", lambda x: np.ones_like(x), 0, 1),
             ("exponential(1)", lambda x: np.exp(-x), 0, 25),
             ("bimodal mixture", lambda x: 0.5 * gauss_pdf(x, 0.25, 0.08) + 0.5 * gauss_pdf(x, 0.75, 0.08), -0.2, 1.2)]
    fig, axes = panel_grid(1, 4, size=(2.5, 2.3), sharey=True)
    zs = np.linspace(-4, 4, 400)
    for ax, (nm, pdf, lo, hi) in zip(axes[0], bases):
        for N, c in zip((1, 2, 5, 30), (RED, ORANGE, GREEN, BLUE)):
            s, q, z, fz = clt_grid(pdf, lo, hi, N, M=801)
            ax.plot(z, fz, color=c, lw=1.0, label="N=%d" % N)
        ax.plot(zs, gauss_pdf(zs), "k--", lw=0.8)
        ax.set_xlim(-4, 4)
        ax.set_ylim(0, 1.0)
        ax.set_title(nm, fontsize=8.5)
        ax.set_xlabel("standardised sum")
    axes[0, 0].legend(fontsize=6.5)
    fig.tight_layout()
    savefig(fig, CH, "fig2-S1", out)


def fig_2_S2(out=None):
    """Supplement: the Student density approaches the Gaussian as nu grows."""
    x = np.linspace(-5, 5, 600)
    fig, axes = panel_grid(1, 2, size=(3.3, 2.5))
    for nu, c in zip((1, 2, 5, 30), (RED, ORANGE, GREEN, BLUE)):
        axes[0, 0].plot(x, student_pdf(x, 0, 1, nu), color=c, label="$\\nu=%d$" % nu)
        axes[0, 1].plot(x, np.log(student_pdf(x, 0, 1, nu)), color=c)
    axes[0, 0].plot(x, gauss_pdf(x), "k--", lw=0.8, label="Gaussian")
    axes[0, 1].plot(x, np.log(gauss_pdf(x)), "k--", lw=0.8)
    axes[0, 0].legend(fontsize=7)
    axes[0, 1].set_ylim(-9, 0)
    fig.tight_layout()
    savefig(fig, CH, "fig2-S2", out)


# =========================================================================== exports for the demos
def export_json(out=None):
    """Small data the browser demonstrations use."""
    os.makedirs(DATA_JSON, exist_ok=True)
    clean, dirty = robust_data()
    ref = {}
    for k in (0, 3):
        _, d = robust_data(n_out=k)
        m, s = fit_gauss(d)
        mt, st = fit_student_em(d, 4.0)
        ml, bl = fit_laplace(d)
        ref[str(k)] = {"gauss": [m, s], "student4": [mt, st], "laplace": [ml, bl]}
    d = {"base": [float(v) for v in clean], "outlier_seed": 0,
         "outliers_unit_noise": [round(float(v), 6) for v in np.random.default_rng(0).normal(size=(30 + 6))[30:36]],
         "reference_fits": ref}
    # the three extra points used as outliers, generated like robust_data: 8 + 0.3 z
    rng = np.random.default_rng(0)
    rng.normal(0.0, 1.0, 30)
    d["outliers_unit_noise"] = [float(v) for v in rng.normal(size=6)]
    with open(os.path.join(DATA_JSON, "ch02-robust.json"), "w") as f:
        json.dump(d, f)
    # CLT reference numbers: sup gap to the Gaussian for Beta(1,5) and the others
    clt = {}
    for nm, pdf, lo, hi in (("beta15", lambda x: beta_pdf(np.clip(x, 1e-9, 1 - 1e-9), 1, 5), 0, 1),
                            ("uniform", lambda x: np.ones_like(x), 0, 1),
                            ("exponential", lambda x: np.exp(-x), 0, 30)):
        clt[nm] = {}
        for N in (1, 2, 5, 10, 30):
            s, q, z, fz = clt_grid(pdf, lo, hi, N, M=401)
            clt[nm][str(N)] = {"sup_gap": sup_gap_to_normal(z, fz), "max_density": float(fz.max())}
    with open(os.path.join(DATA_JSON, "ch02-clt-reference.json"), "w") as f:
        json.dump(clt, f)
    print("wrote ch02-robust.json and ch02-clt-reference.json")


FIGURES = {"2.1": fig_2_1, "2.2": fig_2_2, "2.3": fig_2_3, "2.4": fig_2_4, "2.5": fig_2_5, "2.6": fig_2_6, "2.7": fig_2_7,
           "2.9": fig_2_9, "2.10": fig_2_10, "2.11": fig_2_11, "2.12": fig_2_12, "2.13": fig_2_13, "2.14": fig_2_14,
           "2.15": fig_2_15, "2.16": fig_2_16, "2.17": fig_2_17, "2.18": fig_2_18, "2.19": fig_2_19, "2.20": fig_2_20,
           "2.21": fig_2_21, "2.22": fig_2_22, "2.23": fig_2_23, "2.24": fig_2_24, "2.S1": fig_2_S1, "2.S2": fig_2_S2,
           "json": export_json}

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