"""Wasserman (2004), Chapter 7: Estimating the cdf and Statistical Functionals.

Companion code for Chapter 7 of L. Wasserman, All of Statistics: A Concise Course in Statistical
Inference (Springer, 2004), pp. 97-105, written from scratch with numpy, scipy and matplotlib. The
functions are short and meant to be read next to the lecture notes (../ch07-estimating-cdf.html):

* Section 7.1, the empirical distribution function: ecdf (F_n at any points), dkw_eps and dkw_band
  (the band of (7.3)), wald_pointwise (the pointwise interval for F(x) of Theorem 7.3), sup_dist
  (the Kolmogorov distance D_n = sup_x |F_n(x) - F(x)| for a continuous or a discrete F),
  ks_exact_cdf (the exact law of D_n for continuous F), kolmogorov_cdf / kolmogorov_pdf (its limit)
  and elementary_dkw (a DKW-type bound with an explicit, elementary proof).
* The populations used by the demonstrations (POPS): Normal, Cauchy, Exponential, Uniform,
  Lognormal, Poisson(3) and the nerve data used as a finite population, each with its cdf, its
  quantile function and, for the discrete ones, its support and cumulative probabilities.
* Section 7.2, statistical functionals: plug_in (the plug-in estimates of the mean, variance,
  skewness, quantiles and correlation of Examples 7.10 to 7.14), sample_quantile (the book's
  F_n^{-1}(p) = inf{x : F_n(x) >= p}), influence (the influence functions), se_hat (the estimated
  standard error from the empirical influence function, the nonparametric delta method) and
  true_functional (T(F) and the asymptotic variance tau^2 = E L_F(X)^2 for each population).
* JSRandom, js_uniforms and js_normals: the seeded generator of the browser demonstrations
  (DK.rng in courses/demo-kit.js), so that the simulations on the pages can be repeated here
  number for number (band_coverage_sim, gc_paths, plugin_sim).
* Exercises 7.2 to 7.10 (exercise_numbers) on the book's data sets: fijiquakes.dat, faithful.dat
  and clouds.dat from the book's website.

Figures reproduced: 7.1 (the empirical cdf of the nerve data with the 95 percent DKW band), plus six
supplementary figures:
  7.S1 Theorem 7.3 and Exercises 7.4 and 7.5: the law of F_n(x), its variance, and the covariance
       of F_n at two points;
  7.S2 the Glivenko-Cantelli theorem: F_n against F as n grows, D_n against n, and sqrt(n) D_n
       against Kolmogorov's limit;
  7.S3 the DKW inequality: exact P(D_n > eps) against the bound, exact coverage of the band, and
       simultaneous against pointwise coverage (Exercise 7.3);
  7.S4 plug-in estimators and the Normal approximation (7.5) for six functionals;
  7.S5 influence functions of the mean, variance, skewness, median and 0.9 quantile;
  7.S6 Exercises 7.7, 7.8 and 7.10 on the Fiji earthquake, Old Faithful and cloud-seeding data.
Run `python3 ch07_estimating_cdf.py --list`, `python3 ch07_estimating_cdf.py` (all figures, under ten
seconds on one CPU thread) or `python3 ch07_estimating_cdf.py --fig 7.1`. Files go to
../img/ch07/; `python3 ch07_estimating_cdf.py --export` writes the demonstrations' data to ../data/.

Figures not reproduced, and why:
* Figure 7.2 (histograms of plasma cholesterol for 51 and 320 patients, Example 7.15) is computed
  from data of Scott et al. (1978) that are not distributed on the book's website, so it cannot be
  redrawn. The numbers of Example 7.15 are re-derived from the summary values the book quotes.

Settings chosen where the book gives none:
* Figure 7.1: the band is the 95 percent DKW band of Example 7.6, clipped to [0, 1].
* Figure 7.S2: 20 paths of Exponential(1) samples up to n = 10,000 (JSRandom seeds 7200 + k, as in the
  Glivenko-Cantelli demonstration), and 2000 samples for the histogram (default_rng(72)).
* Figure 7.S3(c) and Exercise 7.3: 1000 samples of size 100 with JSRandom seeds 7100 + r, the stream
  of the DKW demonstration.
* Figure 7.S4: 4000 samples of size 100 from Exponential(1) (bivariate Normal with correlation 0.5
  for the correlation), JSRandom seeds 7300 + r, the stream of the plug-in demonstration.
"""
import argparse
import json
import math
import os
import sys

import numpy as np
from scipy import special, stats

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

CH = 7
M32 = 0xFFFFFFFF
HERE = os.path.dirname(os.path.abspath(__file__))
DATA_OUT = os.path.normpath(os.path.join(HERE, "..", "data"))


# =========================================================================== the demos' generator
def _imul(a, b):
    """32-bit integer multiplication with wrap-around, as Math.imul (result as unsigned)."""
    return (a * b) & M32


class JSRandom:
    """The seeded random numbers of the browser demos (DK.rng in courses/demo-kit.js), one call at a
    time: u() is mulberry32 and norm() the Box-Muller transform that returns r cos(theta) and keeps
    r sin(theta) for the next call, 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() (Box-Muller pairs: cos first, then the cached sin)."""
    npair = (n + 1) // 2
    u = js_uniforms(seed, 2 * npair)
    u1, u2 = u[0::2], u[1::2]
    if np.any(u1 == 0.0):
        raise RuntimeError("a zero uniform: use JSRandom for this stream")
    r = np.sqrt(-2.0 * np.log(u1))
    th = 2.0 * np.pi * u2
    out = np.empty(2 * npair)
    out[0::2] = r * np.cos(th)
    out[1::2] = r * np.sin(th)
    return out[:n]


# =========================================================================== the book's data
def nerve():
    """The nerve data (nerve.dat): 799 waiting times (seconds) between successive pulses along a
    nerve fibre (Cox and Lewis, 1966). Whitespace-separated, with a ragged last row."""
    with open(common.aos_file("nerve.dat")) as f:
        return np.array(f.read().split(), float)


def fiji_magnitudes():
    """Magnitudes of the 1000 earthquakes near Fiji (fijiquakes.dat, column 'mag')."""
    d = common.aos_data("fijiquakes.dat")
    return d["mag"].to_numpy(float)


def faithful():
    """Old Faithful (faithful.dat): returns (eruptions in minutes, waiting in minutes), 272 each.
    The file starts with a text description; the table follows the header line 'eruptions waiting'."""
    with open(common.aos_file("faithful.dat")) as f:
        lines = f.read().splitlines()
    k = next(i for i, s in enumerate(lines) if s.split() == ["eruptions", "waiting"])
    rows = [s.split() for s in lines[k + 1:] if s.strip()]
    arr = np.array([[float(r[1]), float(r[2])] for r in rows])
    return arr[:, 0], arr[:, 1]


def clouds():
    """Cloud seeding (clouds.dat): returns (unseeded, seeded) rainfall in acre-feet, 26 each."""
    with open(common.aos_file("clouds.dat")) as f:
        lines = f.read().splitlines()
    k = next(i for i, s in enumerate(lines) if s.split() == ["Unseeded_Clouds", "Seeded_Clouds"])
    rows = [s.split() for s in lines[k + 1:] if s.strip()]
    arr = np.array([[float(a), float(b)] for a, b in rows])
    return arr[:, 0], arr[:, 1]


# =========================================================================== Section 7.1
def ecdf(x, t):
    """F_n(t) = #{i : x_i <= t} / n at the points t (Definition 7.1)."""
    xs = np.sort(np.asarray(x, float))
    return np.searchsorted(xs, np.asarray(t, float), side="right") / xs.size


def dkw_eps(n, alpha):
    """Half-width of the 1 - alpha DKW band, eps_n = sqrt(log(2 / alpha) / (2 n)) (Section 7.1)."""
    return math.sqrt(math.log(2.0 / alpha) / (2.0 * n))


def dkw_band(x, t, alpha=0.05):
    """(F_n, L, U) at the points t: L = max(F_n - eps_n, 0), U = min(F_n + eps_n, 1)."""
    F = ecdf(x, t)
    e = dkw_eps(len(x), alpha)
    return F, np.maximum(F - e, 0.0), np.minimum(F + e, 1.0)


def wald_pointwise(x, t, alpha=0.05):
    """The pointwise Normal-based interval F_n(t) +/- z sqrt(F_n (1 - F_n) / n) (Theorem 7.3, (7.6))."""
    F = ecdf(x, t)
    z = stats.norm.ppf(1 - alpha / 2)
    h = z * np.sqrt(F * (1 - F) / len(x))
    return F, np.maximum(F - h, 0.0), np.minimum(F + h, 1.0)


def sup_dist_uniform(u):
    """D_n = sup_u |G_n(u) - u| for a sample u from Uniform(0, 1); the supremum is attained at a
    jump, so D_n = max_i max(i/n - u_(i), u_(i) - (i-1)/n). For X_i = F^{-1}(U_i) with F continuous
    this is exactly sup_x |F_n(x) - F(x)|."""
    us = np.sort(np.asarray(u, float), axis=-1)
    n = us.shape[-1]
    i = np.arange(1, n + 1)
    return np.maximum(np.max(i / n - us, axis=-1), np.max(us - (i - 1) / n, axis=-1))


def sup_dist(x, cdf):
    """D_n = sup_x |F_n(x) - F(x)| for a continuous cdf F (a vectorised function)."""
    return float(sup_dist_uniform(cdf(np.asarray(x, float))))


def sup_dist_discrete_u(u, cum):
    """D_n for a discrete F with cumulative probabilities cum[j] = F(s_j) at its support points,
    when X_i = Q(U_i): then F_n(s_j) = #{i : U_i <= cum[j]} / n, and both F_n and F are constant
    between support points, so the supremum is a maximum over the support points."""
    us = np.sort(np.asarray(u, float))
    Fn = np.searchsorted(us, cum, side="right") / us.size
    return float(np.max(np.abs(Fn - cum)))


def ks_exact_cdf(n, eps):
    """P(D_n <= eps) for continuous F (scipy's exact Kolmogorov-Smirnov distribution)."""
    return float(stats.kstwo.cdf(eps, n))


def kolmogorov_cdf(t, terms=50):
    """Kolmogorov's limit law of sqrt(n) D_n:
    K(t) = 1 - 2 sum_{k>=1} (-1)^(k-1) exp(-2 k^2 t^2), used for t >= 1, and the equivalent
    (Jacobi theta) form K(t) = sqrt(2 pi) / t sum_{k>=1} exp(-(2k-1)^2 pi^2 / (8 t^2)) for t < 1,
    where the alternating series converges too slowly."""
    t = np.atleast_1d(np.asarray(t, float))
    out = np.zeros_like(t)
    k = np.arange(1, terms + 1)[:, None]
    big = t >= 1
    if big.any():
        tb = t[big][None, :]
        out[big] = 1.0 - 2.0 * np.sum((-1.0) ** (k - 1) * np.exp(-2.0 * k ** 2 * tb ** 2), axis=0)
    small = (t > 0) & ~big
    if small.any():
        ts = t[small][None, :]
        a = (2 * k - 1) ** 2 * np.pi ** 2 / 8.0
        out[small] = math.sqrt(2 * np.pi) / ts[0] * np.sum(np.exp(-a / ts ** 2), axis=0)
    return np.clip(out, 0.0, 1.0)


def kolmogorov_pdf(t, terms=50):
    """Density of Kolmogorov's law, the derivative of kolmogorov_cdf (same two series)."""
    t = np.atleast_1d(np.asarray(t, float))
    out = np.zeros_like(t)
    k = np.arange(1, terms + 1)[:, None]
    big = t >= 1
    if big.any():
        tb = t[big][None, :]
        out[big] = 8.0 * tb[0] * np.sum((-1.0) ** (k - 1) * k ** 2 * np.exp(-2.0 * k ** 2 * tb ** 2), axis=0)
    small = (t > 0) & ~big
    if small.any():
        ts = t[small][None, :]
        a = (2 * k - 1) ** 2 * np.pi ** 2 / 8.0
        out[small] = math.sqrt(2 * np.pi) * np.sum(np.exp(-a / ts ** 2) * (2 * a / ts ** 4 - 1 / ts ** 2), axis=0)
    return out


def elementary_dkw(n, eps):
    """The elementary bound P(D_n > eps) <= min_k 2 (k - 1) exp(-2 n (eps - 1/k)^2) over integers k with
    1/k < eps (grid of k points plus Hoeffding's inequality and a union bound; see the notes).
    Returns (bound, best k); the bound is capped at 1."""
    best, bk = 1.0, None
    for k in range(int(math.floor(1.0 / eps)) + 1, 20000):
        b = 2.0 * (k - 1) * math.exp(-2.0 * n * (eps - 1.0 / k) ** 2)
        if b < best:
            best, bk = b, k
        if k > 5 and 1.0 / k < eps / 50:
            break
    return min(best, 1.0), bk


def elementary_eps(n, alpha):
    """Smallest half-width eps for which the elementary bound is at most alpha (bisection)."""
    lo, hi = 1e-6, 1.0
    for _ in range(80):
        mid = 0.5 * (lo + hi)
        if elementary_dkw(n, mid)[0] <= alpha:
            hi = mid
        else:
            lo = mid
    return hi


# =========================================================================== populations
def poisson_cum(lam=3.0, jmax=60):
    """Cumulative probabilities F(0), ..., F(jmax) of Poisson(lam), by the recurrence used in the
    browser (p_0 = e^{-lam}, p_j = p_{j-1} lam / j, running sum), so both give identical numbers."""
    p, c, out = math.exp(-lam), 0.0, []
    for j in range(jmax + 1):
        if j > 0:
            p = p * lam / j
        c += p
        out.append(c)
    return np.array(out)


def nerve_support():
    """Support points and cumulative probabilities of the nerve data used as a population F:
    s_j are the 88 distinct values and F(s_j) = (number of values <= s_j) / 799."""
    x = nerve()
    s, cnt = np.unique(x, return_counts=True)
    return s, np.cumsum(cnt) / x.size


def discrete_quantile(u, cum, support):
    """Q(u) = inf{s : F(s) >= u} for a discrete F (the inverse-cdf draw used by the demos)."""
    idx = np.searchsorted(cum, np.asarray(u, float), side="left")
    idx = np.minimum(idx, len(cum) - 1)
    return np.asarray(support)[idx]


POPS = {
    "normal": dict(label="Normal(0, 1)", kind="cont", cdf=stats.norm.cdf, ppf=stats.norm.ppf),
    "cauchy": dict(label="Cauchy", kind="cont", cdf=stats.cauchy.cdf, ppf=stats.cauchy.ppf),
    "exponential": dict(label="Exponential(1)", kind="cont", cdf=stats.expon.cdf, ppf=stats.expon.ppf),
    "uniform": dict(label="Uniform(0, 1)", kind="cont", cdf=stats.uniform.cdf, ppf=stats.uniform.ppf),
    "poisson": dict(label="Poisson(3)", kind="disc"),
    "nerve": dict(label="nerve data as F", kind="disc"),
}


def pop_support(name):
    """(support, cumulative probabilities) of a discrete population."""
    if name == "poisson":
        cum = poisson_cum(3.0, 60)
        return np.arange(cum.size, dtype=float), cum
    if name == "nerve":
        return nerve_support()
    raise ValueError(name)


# =========================================================================== demo 1: the DKW band
def band_events(u, name, alpha):
    """For one sample X_i = F^{-1}(U_i): does F lie inside (a) the DKW band everywhere and (b) the
    pointwise Wald band on [X_(1), X_(n)) (where 0 < F_n < 1)? Also returns D_n.
    Continuous F: everything is computed on the uniform scale (D_n is distribution-free).
    Discrete F: at the support points, where both F_n and F jump."""
    n = len(u)
    e = dkw_eps(n, alpha)
    z = stats.norm.ppf(1 - alpha / 2)
    us = np.sort(np.asarray(u, float))
    if POPS[name]["kind"] == "cont":
        D = float(sup_dist_uniform(us))
        i = np.arange(1, n)               # on [u_(i), u_(i+1)) F_n = i/n
        Fi = i / n
        h = z * np.sqrt(Fi * (1 - Fi) / n)
        inside = np.all(us[:-1] >= Fi - h) and np.all(us[1:] <= Fi + h)
    else:
        _, cum = pop_support(name)
        Fn = np.searchsorted(us, cum, side="right") / n
        D = float(np.max(np.abs(Fn - cum)))
        mid = (Fn > 0) & (Fn < 1)
        h = z * np.sqrt(Fn[mid] * (1 - Fn[mid]) / n)
        inside = bool(np.all(np.abs(Fn[mid] - cum[mid]) <= h))
    return dict(D=D, dkw=D <= e, wald=bool(inside))


def band_coverage_sim(name, n, alpha, R, seed=7100):
    """Simulated coverage of the DKW band and of the pointwise band, sample r using the uniforms
    1..n of DK.rng(seed + r) (the stream of the DKW demonstration)."""
    hits_d = hits_w = 0
    Ds = []
    for r in range(R):
        ev = band_events(js_uniforms(seed + r, n), name, alpha)
        hits_d += ev["dkw"]
        hits_w += ev["wald"]
        Ds.append(ev["D"])
    return dict(dkw=hits_d / R, wald=hits_w / R, D=np.array(Ds))


def pointwise_exact_at_median(n, alpha):
    """Exact probabilities that F(x0) = 1/2 lies in the DKW interval and in the Wald interval at the
    median x0 of a continuous F: n F_n(x0) ~ Binomial(n, 1/2)."""
    k = np.arange(n + 1)
    pmf = stats.binom.pmf(k, n, 0.5)
    ph = k / n
    e = dkw_eps(n, alpha)
    z = stats.norm.ppf(1 - alpha / 2)
    dk = np.abs(ph - 0.5) <= e
    wa = np.abs(ph - 0.5) <= z * np.sqrt(ph * (1 - ph) / n)
    return float(pmf[dk].sum()), float(pmf[wa].sum())


N_GRID_DKW = [5, 10, 15, 20, 25, 30, 40, 50, 60, 80, 100, 120, 150, 200, 250, 300, 400, 500, 600, 799,
              1000, 1500, 2000, 3000, 5000]
ALPHA_GRID = [round(0.01 * k, 2) for k in range(1, 51)]


def exact_coverage_table():
    """P(D_n <= eps_n(alpha)) for continuous F on N_GRID_DKW x ALPHA_GRID (exported for the demo)."""
    return [[ks_exact_cdf(n, dkw_eps(n, a)) for a in ALPHA_GRID] for n in N_GRID_DKW]


# =========================================================================== demo 3: Glivenko-Cantelli
def gc_ngrid(nmax=10000, m=61):
    """The grid of sample sizes of the Glivenko-Cantelli demo: round(10^(j log10(nmax) / (m - 1))),
    j = 0, ..., m - 1, duplicates removed."""
    out = []
    for j in range(m):
        v = int(math.floor(10 ** (j * math.log10(nmax) / (m - 1)) + 0.5))
        if not out or v > out[-1]:
            out.append(v)
    return out


def gc_paths(name, K, nmax=10000, seed=7200, grid=None):
    """D_n along K sample paths (path k uses the uniforms of DK.rng(seed + k)) at the sizes in grid."""
    grid = grid or gc_ngrid(nmax)
    cum = None if POPS[name]["kind"] == "cont" else pop_support(name)[1]
    out = np.empty((K, len(grid)))
    for k in range(K):
        u = js_uniforms(seed + k, grid[-1])
        for j, n in enumerate(grid):
            out[k, j] = sup_dist_uniform(u[:n]) if cum is None else sup_dist_discrete_u(u[:n], cum)
    return grid, out


# =========================================================================== Section 7.2: functionals
def sample_quantile(x, p):
    """The book's sample quantile F_n^{-1}(p) = inf{x : F_n(x) >= p} = X_(ceil(n p)) (Example 7.14)."""
    xs = np.sort(np.asarray(x, float))
    k = int(math.ceil(len(xs) * p - 1e-9))
    return float(xs[max(k, 1) - 1])


def plug_in(x, fun, y=None):
    """The plug-in estimate T(F_n) for fun in mean, var, sd, skew, median, q90, corr."""
    x = np.asarray(x, float)
    if fun == "mean":
        return float(x.mean())
    if fun == "var":
        return float(np.mean((x - x.mean()) ** 2))
    if fun == "sd":
        return float(np.sqrt(np.mean((x - x.mean()) ** 2)))
    if fun == "skew":
        d = x - x.mean()
        return float(np.mean(d ** 3) / np.mean(d ** 2) ** 1.5)
    if fun == "median":
        return sample_quantile(x, 0.5)
    if fun == "q90":
        return sample_quantile(x, 0.9)
    if fun == "corr":
        y = np.asarray(y, float)
        dx, dy = x - x.mean(), y - y.mean()
        return float(np.sum(dx * dy) / math.sqrt(np.sum(dx ** 2) * np.sum(dy ** 2)))
    raise ValueError(fun)


QPROB = {"median": 0.5, "q90": 0.9}


def kde_at(x, t):
    """Gaussian kernel density estimate at t with the normal-reference bandwidth
    h = 0.9 min(sigma_hat, IQR / 1.34) n^(-1/5) (sigma_hat with divisor n, IQR from the book's sample
    quantiles); used only for the standard error of a sample quantile."""
    x = np.asarray(x, float)
    n = x.size
    s = math.sqrt(np.mean((x - x.mean()) ** 2))
    iqr = sample_quantile(x, 0.75) - sample_quantile(x, 0.25)
    a = min(s, iqr / 1.34) if iqr > 0 else s
    h = 0.9 * a * n ** (-0.2)
    return float(np.sum(np.exp(-0.5 * ((t - x) / h) ** 2)) / (n * h * math.sqrt(2 * math.pi)))


def influence(fun, F, xs, ys=None):
    """Influence function L_F evaluated at the points xs (and ys for the correlation).
    F is a dict of the functional's ingredients: mu, sigma, kappa (skewness), xi and f (quantile and
    density there), and for the correlation mux, muy, sx, sy, rho."""
    xs = np.asarray(xs, float)
    if fun == "mean":
        return xs - F["mu"]
    if fun == "var":
        return (xs - F["mu"]) ** 2 - F["sigma"] ** 2
    if fun == "skew":
        z = (xs - F["mu"]) / F["sigma"]
        k = F["kappa"]
        return z ** 3 - 1.5 * k * z ** 2 - 3 * z + 0.5 * k
    if fun in QPROB:
        p = QPROB[fun]
        return (p - (xs <= F["xi"]).astype(float)) / F["f"]
    if fun == "corr":
        a = (xs - F["mux"]) / F["sx"]
        b = (np.asarray(ys, float) - F["muy"]) / F["sy"]
        return a * b - 0.5 * F["rho"] * (a ** 2 + b ** 2)
    raise ValueError(fun)


def empirical_ingredients(fun, x, y=None):
    """The ingredients of influence() evaluated at F_n (plug-in values)."""
    x = np.asarray(x, float)
    if fun == "corr":
        y = np.asarray(y, float)
        return dict(mux=x.mean(), muy=y.mean(), sx=x.std(), sy=y.std(), rho=plug_in(x, "corr", y))
    mu, s = x.mean(), x.std()
    out = dict(mu=mu, sigma=s)
    if fun == "skew":
        out["kappa"] = plug_in(x, "skew")
    if fun in QPROB:
        xi = sample_quantile(x, QPROB[fun])
        out.update(xi=xi, f=kde_at(x, xi))
    return out


def se_hat(fun, x, y=None):
    """Estimated standard error of T(F_n): sqrt(n^-1 sum_i L_hat(X_i)^2 / n), with L_hat the influence
    function at F_n (the nonparametric delta method). For the mean this is sigma_hat / sqrt(n)
    (Example 7.10); for a quantile it is sqrt(p (1 - p)) / (f_hat(xi_hat) sqrt(n))."""
    x = np.asarray(x, float)
    n = x.size
    if fun in QPROB:
        p = QPROB[fun]
        F = empirical_ingredients(fun, x)
        return math.sqrt(p * (1 - p) / n) / F["f"]
    F = empirical_ingredients(fun, x, y)
    L = influence(fun, F, x, y)
    return math.sqrt(np.mean(L ** 2) / n)


def _central_from_raw(raw):
    """Central moments m_2..m_6 from raw moments E X^k, k = 1..6 (raw[0] = 1)."""
    mu = raw[1]
    m = {}
    for k in range(2, 7):
        m[k] = sum(math.comb(k, j) * raw[j] * (-mu) ** (k - j) for j in range(k + 1))
    return mu, m


def _moment_pop(name):
    """(mean, central moments m_2..m_6) of the univariate demo populations."""
    if name == "normal":
        return 0.0, {2: 1.0, 3: 0.0, 4: 3.0, 5: 0.0, 6: 15.0}
    if name == "exponential":
        return _central_from_raw([math.factorial(k) for k in range(7)])
    if name == "uniform":
        return _central_from_raw([1.0 / (k + 1) for k in range(7)])
    if name == "lognormal":
        return _central_from_raw([math.exp(k * k / 2.0) for k in range(7)])
    if name == "nerve":
        x = nerve()
        mu = x.mean()
        return mu, {k: float(np.mean((x - mu) ** k)) for k in range(2, 7)}
    raise ValueError(name)


def _tau2_skew(m):
    """E L^2 for the skewness, L = Z^3 - 1.5 k Z^2 - 3 Z + k / 2 with Z = (X - mu) / sigma."""
    s = math.sqrt(m[2])
    z = {k: m[k] / s ** k for k in range(2, 7)}
    z[1] = 0.0
    k = z[3]
    # expand (Z^3 + a Z^2 + b Z + c)^2 with a = -1.5 k, b = -3, c = k / 2
    a, b, c = -1.5 * k, -3.0, 0.5 * k
    EZ = lambda j: 1.0 if j == 0 else z[j]
    terms = {6: 1.0, 5: 2 * a, 4: a * a + 2 * b, 3: 2 * c + 2 * a * b, 2: b * b + 2 * a * c, 1: 2 * b * c, 0: c * c}
    return sum(v * EZ(j) for j, v in terms.items())


UNI_POPS = ("normal", "exponential", "uniform", "lognormal", "nerve")
BI_POPS = ("binormal5", "binormal9", "faithful")


def true_functional(fun, pop):
    """(T(F), tau^2) for a demo population; tau^2 = E L_F(X)^2 is the asymptotic variance of
    sqrt(n) (T(F_n) - T(F)). tau2 is None where the Normal approximation has no basis (a quantile of
    the discrete nerve population)."""
    if fun == "corr":
        if pop.startswith("binormal"):
            rho = {"binormal5": 0.5, "binormal9": 0.9}[pop]
            return rho, (1 - rho ** 2) ** 2
        e, w = faithful()
        rho = plug_in(e, "corr", w)
        F = dict(mux=e.mean(), muy=w.mean(), sx=e.std(), sy=w.std(), rho=rho)
        return rho, float(np.mean(influence("corr", F, e, w) ** 2))
    if fun in QPROB:
        p = QPROB[fun]
        if pop == "normal":
            xi = stats.norm.ppf(p); f = stats.norm.pdf(xi)
        elif pop == "exponential":
            xi = -math.log(1 - p); f = 1 - p
        elif pop == "uniform":
            xi = p; f = 1.0
        elif pop == "lognormal":
            xi = math.exp(stats.norm.ppf(p)); f = stats.lognorm.pdf(xi, 1.0)
        else:
            return sample_quantile(nerve(), p), None
        return float(xi), p * (1 - p) / f ** 2
    mu, m = _moment_pop(pop)
    if fun == "mean":
        return mu, m[2]
    if fun == "var":
        return m[2], m[4] - m[2] ** 2
    if fun == "skew":
        return m[3] / m[2] ** 1.5, _tau2_skew(m)
    raise ValueError(fun)


def draw_pop(pop, seed, n):
    """One sample of size n from a demo population with DK.rng(seed), as the plug-in demo draws it.
    Returns x (and y for the bivariate populations)."""
    if pop == "normal":
        return js_normals(seed, n), None
    if pop == "exponential":
        return -np.log(js_uniforms(seed, n)), None
    if pop == "uniform":
        return js_uniforms(seed, n), None
    if pop == "lognormal":
        return np.exp(js_normals(seed, n)), None
    if pop == "nerve":
        x = nerve()
        return x[np.floor(js_uniforms(seed, n) * x.size).astype(int)], None
    if pop.startswith("binormal"):
        rho = {"binormal5": 0.5, "binormal9": 0.9}[pop]
        z = js_normals(seed, 2 * n)
        a = z[0::2]
        return a, rho * a + math.sqrt(1 - rho * rho) * z[1::2]
    if pop == "faithful":
        e, w = faithful()
        idx = np.floor(js_uniforms(seed, n) * e.size).astype(int)
        return e[idx], w[idx]
    raise ValueError(pop)


def plugin_sim(fun, pop, n, R, seed=7300, alpha=0.05):
    """R samples (sample r uses DK.rng(seed + r)): the plug-in estimates, their estimated standard
    errors and the coverage of the Normal-based interval T(F_n) +/- z se_hat (7.6)."""
    T, tau2 = true_functional(fun, pop)
    z = stats.norm.ppf(1 - alpha / 2)
    est, ses = np.empty(R), np.empty(R)
    for r in range(R):
        x, y = draw_pop(pop, seed + r, n)
        est[r] = plug_in(x, fun, y)
        ses[r] = se_hat(fun, x, y)
    cover = np.mean(np.abs(est - T) <= z * ses)
    return dict(T=T, tau2=tau2, est=est, se=ses, mean=float(est.mean()), sd=float(est.std(ddof=1)),
                mean_se=float(ses.mean()), cover=float(cover))


def truth_table():
    """{functional: {population: [T(F), tau^2 or None]}} for the plug-in demonstration."""
    out = {}
    for fun in ("mean", "var", "skew", "median", "q90"):
        out[fun] = {pop: list(true_functional(fun, pop)) for pop in UNI_POPS}
    out["corr"] = {pop: list(true_functional("corr", pop)) for pop in BI_POPS}
    return out


def ingredient_table():
    """The ingredients of the influence functions at each demo population (for the curves L_F(x))."""
    out = {}
    for pop in UNI_POPS:
        mu, m = _moment_pop(pop)
        d = dict(mu=mu, sigma=math.sqrt(m[2]), kappa=m[3] / m[2] ** 1.5)
        for fun in QPROB:
            xi, tau2 = true_functional(fun, pop)
            d[fun] = [xi, None if tau2 is None else math.sqrt(QPROB[fun] * (1 - QPROB[fun]) / tau2)]
        out[pop] = d
    for pop in ("binormal5", "binormal9"):
        out[pop] = dict(mux=0.0, muy=0.0, sx=1.0, sy=1.0, rho=true_functional("corr", pop)[0])
    e, w = faithful()
    out["faithful"] = dict(mux=e.mean(), muy=w.mean(), sx=e.std(), sy=w.std(), rho=plug_in(e, "corr", w))
    return out


def nerve_table():
    """Plug-in estimates and estimated standard errors on the nerve data (and the Old Faithful
    correlation), the table of the plug-in demonstration."""
    x = nerve()
    rows = {}
    for fun in ("mean", "var", "skew", "median", "q90"):
        rows[fun] = (plug_in(x, fun), se_hat(fun, x))
    th = float(np.mean((x > 0.4) & (x <= 0.6)))
    rows["F(.6)-F(.4)"] = (th, math.sqrt(th * (1 - th) / x.size))
    e, w = faithful()
    rows["corr (Old Faithful)"] = (plug_in(e, "corr", w), se_hat("corr", e, w))
    return rows


# =========================================================================== exercises
def exercise_numbers():
    """The numerical answers of Exercises 7.3 and 7.6 to 7.10 and of Examples 7.2, 7.6 and 7.15."""
    out = {}
    x = nerve()
    n = x.size
    out["ex7_2_example"] = dict(F6=float(ecdf(x, 0.6)), F4=float(ecdf(x, 0.4)), diff=float(ecdf(x, 0.6) - ecdf(x, 0.4)))
    out["ex7_6_eps"] = dkw_eps(n, 0.05)
    th = out["ex7_2_example"]["diff"]
    se = math.sqrt(th * (1 - th) / n)
    out["ex7_6_nerve"] = dict(theta=th, se=se, lo=th - 1.959963984540054 * se, hi=th + 1.959963984540054 * se)
    # Example 7.15 from the book's summary numbers
    se1, se2 = 5.0, 2.4
    d = 216.19 - 195.27
    s = math.hypot(se1, se2)
    out["ex7_15"] = dict(theta=d, se=s, lo=d - 2 * s, hi=d + 2 * s, ci1=(195.27 - 10, 195.27 + 10), ci2=(216.19 - 4.8, 216.19 + 4.8))
    # Exercise 7.3: coverage of the 95% band, n = 100, 1000 repetitions, Normal and Cauchy
    cov = band_coverage_sim("normal", 100, 0.05, 1000, seed=7100)
    out["ex7_3"] = dict(dkw=cov["dkw"], wald=cov["wald"], exact=ks_exact_cdf(100, dkw_eps(100, 0.05)))
    rng = np.random.default_rng(73)
    hits = {}
    for nm, gen, cdf in (("normal", rng.standard_normal, stats.norm.cdf), ("cauchy", rng.standard_cauchy, stats.cauchy.cdf)):
        h = 0
        for _ in range(1000):
            h += sup_dist(gen(100), cdf) <= dkw_eps(100, 0.05)
        hits[nm] = h / 1000
    out["ex7_3_indep"] = hits
    # Exercise 7.7: Fiji magnitudes
    m = fiji_magnitudes()
    th = float(ecdf(m, 4.9) - ecdf(m, 4.3))
    se = math.sqrt(th * (1 - th) / m.size)
    out["ex7_7"] = dict(n=m.size, eps=dkw_eps(m.size, 0.05), F49=float(ecdf(m, 4.9)), F43=float(ecdf(m, 4.3)),
                        theta=th, se=se, lo=th - 1.959963984540054 * se, hi=th + 1.959963984540054 * se)
    # Exercise 7.8: Old Faithful waiting times
    e, w = faithful()
    mu, sh = w.mean(), w.std()
    se = sh / math.sqrt(w.size)
    z90 = stats.norm.ppf(0.95)
    out["ex7_8"] = dict(n=w.size, mean=float(mu), sigma=float(sh), se=se, lo=mu - z90 * se, hi=mu + z90 * se,
                        median=sample_quantile(w, 0.5), median_np=float(np.median(w)), se_median=se_hat("median", w))
    # Exercise 7.9: antibiotics
    p1, p2 = 0.90, 0.85
    se = math.sqrt(p1 * (1 - p1) / 100 + p2 * (1 - p2) / 100)
    th = p1 - p2
    out["ex7_9"] = dict(theta=th, se=se, ci80=(th - stats.norm.ppf(0.9) * se, th + stats.norm.ppf(0.9) * se),
                        ci95=(th - stats.norm.ppf(0.975) * se, th + stats.norm.ppf(0.975) * se))
    # Exercise 7.10: cloud seeding
    un, se_ = clouds()
    th = se_.mean() - un.mean()
    s = math.sqrt(un.var() / un.size + se_.var() / se_.size)
    out["ex7_10"] = dict(mean_seeded=float(se_.mean()), mean_unseeded=float(un.mean()), theta=float(th), se=s,
                         lo=th - 1.959963984540054 * s, hi=th + 1.959963984540054 * s,
                         se_n1=math.sqrt(un.var(ddof=1) / 26 + se_.var(ddof=1) / 26),
                         log_theta=float(np.log(se_).mean() - np.log(un).mean()),
                         log_se=math.sqrt(np.log(un).var() / 26 + np.log(se_).var() / 26))
    return out


# =========================================================================== figures
def _rug(ax, x, y0=0.0, h=0.04, color="k", lw=0.5):
    ax.vlines(np.unique(x), y0, y0 + h, color=color, lw=lw)


def _step(ax, x, lo, hi, **kw):
    """Draw the ECDF of x as a right-continuous step function on [lo, hi]."""
    xs = np.sort(np.asarray(x, float))
    t = np.concatenate([[lo], xs, [hi]])
    F = np.concatenate([[0.0], np.arange(1, xs.size + 1) / xs.size, [1.0]])
    ax.step(t, F, where="post", **kw)


def fig_7_1(out_dir=None):
    """Figure 7.1: the empirical cdf of the nerve data with a 95 percent DKW confidence band."""
    x = nerve()
    t = np.linspace(0, 1.4, 2801)
    F, L, U = dkw_band(x, t, 0.05)
    fig, ax = common.plt.subplots(figsize=(5.4, 3.6))
    ax.plot(t, F, color="k", lw=1.6, drawstyle="steps-post", label=r"$\widehat F_n$")
    ax.plot(t, L, color=RED, lw=1.2, ls="--", drawstyle="steps-post", label="95% DKW band")
    ax.plot(t, U, color=RED, lw=1.2, ls="--", drawstyle="steps-post")
    _rug(ax, x, 0.0, 0.05)
    ax.set_xlim(-0.02, 1.5)
    ax.set_ylim(-0.02, 1.05)
    ax.set_xlabel("waiting time between pulses (seconds)")
    ax.set_ylabel("cumulative probability")
    ax.legend(loc="lower right", frameon=False)
    ax.text(0.75, 0.42, r"$n=799,\ \epsilon_n=%.3f$" % dkw_eps(x.size, 0.05), fontsize=10)
    return savefig(fig, CH, "fig7-1", out_dir)


def fig_7_s1(out_dir=None):
    """Figure 7.S1: the law of F_n(x) (Theorem 7.3, Exercises 7.4 and 7.5)."""
    rng = np.random.default_rng(71)
    n, R = 50, 20000
    X = rng.exponential(1.0, size=(R, n))
    x0 = math.log(2.0)                      # F(x0) = 1/2
    fig, axes = common.panel_grid(1, 3, size=(3.3, 2.7))
    ax = axes[0, 0]
    k = np.sum(X <= x0, axis=1)
    ks = np.arange(n + 1)
    cnt = np.bincount(k, minlength=n + 1) / R
    ax.bar(ks / n, cnt, width=0.8 / n, color=BLUE, alpha=0.6, label="simulation")
    ax.plot(ks / n, stats.binom.pmf(ks, n, 0.5), "o", ms=2.5, color=RED, label="Binomial(50, 1/2)/50")
    ax.set_xlim(0.2, 0.8)
    ax.set_xlabel(r"$\widehat F_n(x_0)$, $F(x_0)=1/2$")
    ax.legend(frameon=False, fontsize=7.5, loc="upper left")
    common.panel_label(ax, "a", "upper right")
    ax = axes[0, 1]
    t = np.linspace(0, 4, 81)
    Fn = np.stack([np.mean(X <= tt, axis=1) for tt in t], axis=1)
    F = 1 - np.exp(-t)
    ax.plot(t, Fn.std(axis=0) ** 2 * n, "o", ms=2.5, color=BLUE, label=r"$n\,\mathbb{V}\widehat F_n(x)$, simulated")
    ax.plot(t, F * (1 - F), color=RED, label=r"$F(x)(1-F(x))$")
    ax.plot(t, n * np.abs(Fn.mean(axis=0) - F), color=GREEN, lw=1, label=r"$n\,|\mathbb{E}\widehat F_n-F|$, simulated")
    ax.set_xlabel("x (Exponential(1) data)")
    ax.legend(frameon=False, fontsize=7.5, loc="upper right")
    ax.set_ylim(-0.01, 0.35)
    common.panel_label(ax, "b", "upper left")
    ax = axes[0, 2]
    C = np.array([np.cov(Fn[:, 17], Fn[:, j])[0, 1] for j in range(t.size)]) * n
    Fa = 1 - math.exp(-t[17])
    ax.plot(t, C, "o", ms=2.5, color=BLUE, label=r"$n\,\mathrm{Cov}$, simulated")
    ax.plot(t, np.minimum(F, Fa) - F * Fa, color=RED, label=r"$F(x\wedge y)-F(x)F(y)$")
    ax.axvline(t[17], color=GREY, lw=0.8, ls=":")
    ax.set_xlabel(r"y, with x = %.1f fixed" % t[17])
    ax.legend(frameon=False, fontsize=7.5, loc="upper right")
    ax.set_ylim(-0.01, 0.3)
    common.panel_label(ax, "c", "upper left")
    fig.tight_layout()
    return savefig(fig, CH, "fig7-S1", out_dir)


def fig_7_s2(out_dir=None):
    """Figure 7.S2: the Glivenko-Cantelli theorem for Exponential(1) samples."""
    fig, axes = common.panel_grid(1, 3, size=(3.3, 2.7))
    ax = axes[0, 0]
    u = js_uniforms(7200, 1000)
    x = -np.log1p(-u)
    t = np.linspace(0, 5, 600)
    ax.plot(t, 1 - np.exp(-t), color="k", lw=2.2, label="F")
    for n, c in ((10, ORANGE), (100, GREEN), (1000, BLUE)):
        _step(ax, x[:n], 0, 5, color=c, lw=1.1, label="n = %d" % n)
    ax.set_xlim(0, 5)
    ax.set_xlabel("x")
    ax.legend(frameon=False, fontsize=8, loc="lower right")
    common.panel_label(ax, "a", "upper left")
    ax = axes[0, 1]
    grid, D = gc_paths("exponential", 20, 10000, seed=7200)
    g = np.array(grid)
    for k in range(D.shape[0]):
        ax.plot(g, D[k], color=GREY, lw=0.6, alpha=0.7)
    ax.plot(g, D[0], color=BLUE, lw=1.2, label="one path")
    ax.plot(g, [dkw_eps(n, 0.05) for n in g], color=RED, lw=1.5, ls="--", label=r"$\epsilon_n$ (95% DKW)")
    ax.plot(g, 0.8276 / np.sqrt(g), color=GREEN, lw=1.2, label=r"median $0.83/\sqrt{n}$")
    ax.set_xscale("log")
    ax.set_yscale("log")
    ax.set_xlabel("n")
    ax.set_ylabel(r"$D_n=\sup_x|\widehat F_n(x)-F(x)|$")
    ax.legend(frameon=False, fontsize=7.5, loc="lower left")
    common.panel_label(ax, "b", "upper right")
    ax = axes[0, 2]
    rng = np.random.default_rng(72)
    for n, c in ((10, ORANGE), (1000, BLUE)):
        Dn = sup_dist_uniform(rng.random((2000, n))) * math.sqrt(n)
        ax.hist(Dn, bins=40, range=(0, 2.4), density=True, histtype="step", color=c, lw=1.3, label=r"$\sqrt{n}D_n$, n = %d" % n)
    s = np.linspace(0.01, 2.4, 300)
    ax.plot(s, kolmogorov_pdf(s), color="k", lw=1.6, label="Kolmogorov limit")
    ax.axvline(math.sqrt(math.log(40) / 2), color=RED, ls="--", lw=1.0)
    ax.set_xlabel(r"$\sqrt{n}\,D_n$")
    ax.legend(frameon=False, fontsize=7.5, loc="upper right")
    common.panel_label(ax, "c", "upper left")
    fig.tight_layout()
    return savefig(fig, CH, "fig7-S2", out_dir)


def fig_7_s3(out_dir=None):
    """Figure 7.S3: the DKW inequality, exact tail and coverage, and pointwise against simultaneous."""
    fig, axes = common.panel_grid(1, 3, size=(3.3, 2.7))
    ax = axes[0, 0]
    for n, c in ((10, ORANGE), (100, BLUE)):
        e = np.linspace(0.005, 0.6 if n == 10 else 0.2, 200)
        ax.plot(e, [stats.kstwo.sf(v, n) for v in e], color=c, lw=1.6, label="exact, n = %d" % n)
        ax.plot(e, np.minimum(2 * np.exp(-2 * n * e ** 2), 1), color=c, lw=1.1, ls="--", label="DKW bound")
        ax.plot(e, [elementary_dkw(n, v)[0] for v in e], color=c, lw=0.9, ls=":", label="elementary bound")
    ax.set_yscale("log")
    ax.set_ylim(1e-4, 1.5)
    ax.set_xlabel(r"$\epsilon$")
    ax.set_ylabel(r"$\mathbb{P}(D_n>\epsilon)$")
    ax.legend(frameon=False, fontsize=6.8, loc="lower left")
    common.panel_label(ax, "a", "upper right")
    ax = axes[0, 1]
    ns = np.unique(np.round(np.logspace(np.log10(2), np.log10(5000), 80)).astype(int))
    for a, c in ((0.05, BLUE), (0.2, GREEN)):
        ax.plot(ns, [ks_exact_cdf(n, dkw_eps(n, a)) for n in ns], color=c, lw=1.6, label=r"$1-\alpha=%.2f$" % (1 - a))
        ax.axhline(1 - a, color=c, ls="--", lw=0.9)
        t = math.sqrt(math.log(2 / a) / 2)
        ax.axhline(float(kolmogorov_cdf(t)[0]), color=c, ls=":", lw=0.9)
    ax.set_xscale("log")
    ax.set_xlabel("n")
    ax.set_ylabel("exact coverage of the band")
    ax.legend(frameon=False, fontsize=8, loc="center right")
    common.panel_label(ax, "b", "upper left")
    ax = axes[0, 2]
    nsim = [10, 30, 100, 300, 1000]
    dk, wa = [], []
    for n in nsim:
        c = band_coverage_sim("normal", n, 0.05, 1000, seed=7100)
        dk.append(c["dkw"]); wa.append(c["wald"])
    ax.plot(nsim, dk, "o-", color=BLUE, label="DKW band, simultaneous")
    ax.plot(nsim, wa, "s-", color=RED, label="pointwise intervals, simultaneous")
    pm = [pointwise_exact_at_median(n, 0.05) for n in nsim]
    ax.plot(nsim, [p[1] for p in pm], "^--", color=ORANGE, ms=4, label="pointwise interval at the median")
    ax.plot(nsim, [p[0] for p in pm], "v--", color=GREEN, ms=4, label="DKW band at the median")
    ax.axhline(0.95, color="k", lw=0.8, ls=":")
    ax.set_xscale("log")
    ax.set_ylim(0, 1.04)
    ax.set_xlabel("n")
    ax.set_ylabel("coverage (1000 samples)")
    ax.legend(frameon=False, fontsize=6.8, loc="center right", bbox_to_anchor=(1.0, 0.64))
    common.panel_label(ax, "c", "lower left")
    fig.tight_layout()
    return savefig(fig, CH, "fig7-S3", out_dir)


FUN_LABEL = {"mean": "mean", "var": "variance", "skew": "skewness", "median": "median", "q90": "0.9 quantile", "corr": "correlation"}


def fig_7_s4(out_dir=None):
    """Figure 7.S4: sampling distributions of six plug-in estimators, n = 100, with the Normal
    approximation N(T(F), tau^2 / n) of (7.5)."""
    fig, axes = common.panel_grid(2, 3, size=(3.2, 2.5))
    n, R = 100, 4000
    for ax, fun in zip(axes.ravel(), ("mean", "var", "skew", "median", "q90", "corr")):
        pop = "binormal5" if fun == "corr" else "exponential"
        r = plugin_sim(fun, pop, n, R, seed=7300)
        se = math.sqrt(r["tau2"] / n)
        lo, hi = np.percentile(r["est"], [0.2, 99.8])
        lo, hi = min(lo, r["T"] - 3.5 * se), max(hi, r["T"] + 3.5 * se)
        ax.hist(r["est"], bins=50, range=(lo, hi), density=True, color=BLUE, alpha=0.45)
        s = np.linspace(lo, hi, 300)
        ax.plot(s, stats.norm.pdf(s, r["T"], se), color=RED, lw=1.4)
        ax.axvline(r["T"], color="k", lw=0.8, ls=":")
        ax.set_title("%s: coverage %.3f" % (FUN_LABEL[fun], r["cover"]), fontsize=9.5)
        ax.set_yticks([])
    fig.tight_layout()
    return savefig(fig, CH, "fig7-S4", out_dir)


def fig_7_s5(out_dir=None):
    """Figure 7.S5: influence functions at Normal(0, 1) and Exponential(1)."""
    fig, axes = common.panel_grid(1, 2, size=(3.6, 2.8))
    for ax, pop, rng_ in ((axes[0, 0], "normal", (-3.5, 3.5)), (axes[0, 1], "exponential", (0, 5))):
        t = np.linspace(rng_[0], rng_[1], 801)
        mu, m = _moment_pop(pop)
        base = dict(mu=mu, sigma=math.sqrt(m[2]), kappa=m[3] / m[2] ** 1.5)
        for fun, c in (("mean", "k"), ("var", BLUE), ("skew", MAGENTA), ("median", GREEN), ("q90", ORANGE)):
            F = dict(base)
            if fun in QPROB:
                xi, tau2 = true_functional(fun, pop)
                F.update(xi=xi, f=math.sqrt(QPROB[fun] * (1 - QPROB[fun]) / tau2))
            L = influence(fun, F, t)
            ax.plot(t, L, color=c, lw=1.5, label=FUN_LABEL[fun] + r", $\tau^2$=%.2f" % true_functional(fun, pop)[1])
        ax.axhline(0, color=GREY, lw=0.6)
        ax.set_ylim(-6, 8)
        ax.set_xlabel("x")
        ax.set_title(POPS[pop]["label"], fontsize=10)
        ax.legend(frameon=False, fontsize=6.8, loc="upper left" if pop == "exponential" else "upper center")
    axes[0, 0].set_ylabel(r"$L_F(x)$")
    fig.tight_layout()
    return savefig(fig, CH, "fig7-S5", out_dir)


def fig_7_s6(out_dir=None):
    """Figure 7.S6: Exercises 7.7, 7.8 and 7.10."""
    fig, axes = common.panel_grid(1, 3, size=(3.3, 2.7))
    ax = axes[0, 0]
    m = fiji_magnitudes()
    t = np.linspace(3.9, 6.5, 1301)
    F, L, U = dkw_band(m, t, 0.05)
    ax.plot(t, F, color="k", lw=1.4, drawstyle="steps-post")
    ax.plot(t, L, color=RED, lw=1, ls="--", drawstyle="steps-post")
    ax.plot(t, U, color=RED, lw=1, ls="--", drawstyle="steps-post")
    for v in (4.3, 4.9):
        ax.axvline(v, color=GREY, lw=0.8, ls=":")
    ax.set_xlabel("magnitude (Fiji earthquakes)")
    common.panel_label(ax, "a", "upper left")
    ax = axes[0, 1]
    e, w = faithful()
    t = np.linspace(40, 100, 1201)
    F, L, U = dkw_band(w, t, 0.05)
    ax.plot(t, F, color="k", lw=1.4, drawstyle="steps-post")
    ax.plot(t, L, color=RED, lw=1, ls="--", drawstyle="steps-post")
    ax.plot(t, U, color=RED, lw=1, ls="--", drawstyle="steps-post")
    ax.axvline(w.mean(), color=BLUE, lw=1.2, label="mean %.1f" % w.mean())
    ax.axvline(sample_quantile(w, 0.5), color=GREEN, lw=1.2, ls="--", label="median %.0f" % sample_quantile(w, 0.5))
    ax.set_xlabel("waiting time (Old Faithful, minutes)")
    ax.legend(frameon=False, fontsize=7.5, loc="upper left", bbox_to_anchor=(0.0, 0.92))
    common.panel_label(ax, "b", "upper left")
    ax = axes[0, 2]
    un, se_ = clouds()
    _step(ax, un, 0.5, 3000, color=BLUE, lw=1.3, label="unseeded")
    _step(ax, se_, 0.5, 3000, color=ORANGE, lw=1.3, label="seeded")
    ax.set_xscale("log")
    ax.set_xlabel("rainfall (acre-feet, log scale)")
    ax.legend(frameon=False, fontsize=8, loc="lower right")
    common.panel_label(ax, "c", "upper left")
    fig.tight_layout()
    return savefig(fig, CH, "fig7-S6", out_dir)


FIGURES = {"7.1": fig_7_1, "7.S1": fig_7_s1, "7.S2": fig_7_s2, "7.S3": fig_7_s3, "7.S4": fig_7_s4,
           "7.S5": fig_7_s5, "7.S6": fig_7_s6}


# =========================================================================== export for the demos
def export(out_dir=DATA_OUT):
    """Write the data the three demonstrations load: the nerve data and the Old Faithful pairs
    (ch07-data.json) and the exact coverage of the DKW band for continuous F (ch07-dkw-exact.json)."""
    os.makedirs(out_dir, exist_ok=True)
    e, w = faithful()
    d = dict(note="Nerve data (nerve.dat, 799 waiting times in seconds between nerve pulses; Cox and Lewis 1966) and "
                  "Old Faithful (faithful.dat, 272 eruption durations and waiting times in minutes), from the website of "
                  "Wasserman (2004), All of Statistics.",
             nerve=[float(v) for v in nerve()], eruptions=[float(v) for v in e], waiting=[float(v) for v in w],
             truth=truth_table(), ingr=ingredient_table())
    with open(os.path.join(out_dir, "ch07-data.json"), "w") as f:
        json.dump(d, f, separators=(",", ":"))
    tab = exact_coverage_table()
    d = dict(note="Exact probability that the DKW band F_n +/- sqrt(log(2/alpha)/(2n)) contains a continuous F, "
                  "P(D_n <= eps_n), from the exact distribution of the Kolmogorov-Smirnov statistic. Rows: n; columns: alpha.",
             n=N_GRID_DKW, alpha=ALPHA_GRID, cov=[[round(v, 6) for v in row] for row in tab],
             z=[float(stats.norm.ppf(1 - a / 2)) for a in ALPHA_GRID])
    with open(os.path.join(out_dir, "ch07-dkw-exact.json"), "w") as f:
        json.dump(d, f, separators=(",", ":"))
    print("wrote", os.path.join(out_dir, "ch07-data.json"), "and ch07-dkw-exact.json")


if __name__ == "__main__":
    if "--export" in sys.argv:
        export()
    else:
        common.run(FIGURES, CH)
