"""Wasserman (2004), Chapter 18: Undirected Graphs.

Companion code for Chapter 18 of L. Wasserman, All of Statistics: A Concise Course in Statistical
Inference (Springer, 2004), pp. 281-289, written from scratch with numpy, scipy and matplotlib. The
functions are short and meant to be read next to the lecture notes (../ch18-undirected-graphs.html):

* Section 18.1, graphs: Graph (vertices, edges, adjacency), separates (does C separate A and B? delete C
  and search the remaining graph), minimal_separators (every inclusion-minimal separating set of a
  non-adjacent pair, by search over subsets), maximal_cliques (the Bron-Kerbosch algorithm with
  pivoting), complete_subsets (every clique, maximal or not) and markov_statements (the pairwise,
  local and global statements that a graph asserts).
* Section 18.2, probability and graphs: joint probability tables of binary variables (arrays of shape
  (2,)*p), marginal, cmi (the conditional mutual information I(A; B | C), zero exactly when A and B are
  conditionally independent given C), gibbs_ising (a positive distribution that is Markov to a graph)
  and noisy_copies (copies of one fair coin, each flipped with probability eta: the counterexample of
  Filling the gap 18.4 when eta = 0).
* Section 18.3, cliques and potentials: gibbs_from_potentials (the factorisation (18.1) with seeded
  random clique potentials, as in the cliques-and-potentials demo), mobius_terms (the interaction terms
  lambda_S of log f; Hammersley-Clifford says that for a positive f they vanish outside the cliques).
* Section 18.4, fitting graphs to data: g2_test (the likelihood ratio test of conditional independence
  in a contingency table, Exercise 18.4), partial_correlations (from the precision matrix, and again
  from regression residuals), pcor_tests (the exact t test and Fisher's z test of each partial
  correlation), edge_exclusion (a graph from the tests, with Bonferroni or Benjamini-Hochberg control),
  coris_continuous (the eight continuous variables of the book's coris.dat), and gauss_cycle (the
  Gaussian four-cycle whose marginal covariance cancels: Filling the gap 18.5).
* Exercises 18.1-18.5, including the breast cancer table of Exercise 18.5 (BREAST).
* JSRandom: the seeded generator of the browser demonstrations (DK.rng in courses/demo-kit.js), so that
  the demonstrations' random potentials, subsamples and simulated data are reproduced here number for
  number.

Figures. All thirteen figures of the chapter are drawings of graphs. They are reproduced from the
graphs' edge lists, and each drawing carries what is computed from the graph: the maximal cliques
(found by maximal_cliques and shaded when they have three or more vertices) and, for Figures
18.1-18.8, the inclusion-minimal separation statements found by minimal_separators. Figures
18.10-18.13 belong to Exercise 18.3 and are drawn without annotations (the answers are in the
solutions). Nothing is skipped. Five supplementary figures illustrate the gaps filled in the notes:
  18.S1 why positivity and one-way definitions are needed (Filling the gaps 18.4 and 18.5);
  18.S2 a Gaussian graphical model for the coris data (partial correlations and edge exclusion tests);
  18.S3 the exact t test of a partial correlation against Fisher's z approximation (Theorem 18.L);
  18.S4 the interaction terms of log f vanish outside the cliques (Hammersley-Clifford, Figure 18.9);
  18.S5 the graph fitted to the breast cancer data of Exercise 18.5.
Run `python3 ch18_undirected_graphs.py --list`, `python3 ch18_undirected_graphs.py` (all eighteen
figures, under a minute on one CPU thread) or `python3 ch18_undirected_graphs.py --fig 18.9`.
Files go to ../img/ch18/; the coris variables used by the partial-correlation demo are written to
../data/ch18-coris.json by export_json() (run with the figures).
"""
import itertools
import json
import math
import os

import numpy as np
from scipy import stats

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

CH = 18
M32 = 0xFFFFFFFF
INDEP = r"\perp\!\!\!\perp"


# =========================================================================== 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 that returns r cos(t) and keeps r sin(t) 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_shuffle(rng, a):
    """ML.shuffle of courses/ml/ml-kit.js: Fisher-Yates from the end, using rng.u()."""
    a = list(a)
    for i in range(len(a) - 1, 0, -1):
        j = int(math.floor(rng.u() * (i + 1)))
        a[i], a[j] = a[j], a[i]
    return a


# =========================================================================== 18.1 graphs
class Graph:
    """An undirected graph on vertices 0..p-1 with names; edges are unordered pairs."""

    def __init__(self, names, edges):
        self.names = list(names)
        self.p = len(self.names)
        idx = {nm: i for i, nm in enumerate(self.names)}
        self.adj = [set() for _ in range(self.p)]
        for a, b in edges:
            i, j = (idx[a], idx[b]) if not isinstance(a, int) else (a, b)
            if i == j:
                raise ValueError("no loops")
            self.adj[i].add(j)
            self.adj[j].add(i)

    @property
    def edges(self):
        return sorted((i, j) for i in range(self.p) for j in self.adj[i] if i < j)

    def adjacent(self, i, j):
        return j in self.adj[i]

    def ne(self, i):
        return set(self.adj[i])

    def is_complete(self, S):
        S = list(S)
        return all(self.adjacent(a, b) for a, b in itertools.combinations(S, 2))


def reachable(g, start, blocked):
    """Vertices reachable from the set `start` by paths that avoid the set `blocked`."""
    seen = set(v for v in start if v not in blocked)
    stack = list(seen)
    while stack:
        v = stack.pop()
        for w in g.adj[v]:
            if w not in blocked and w not in seen:
                seen.add(w)
                stack.append(w)
    return seen


def separates(g, A, B, C):
    """True if every path from A to B meets C (A, B, C disjoint; Section 18.1)."""
    A, B, C = set(A), set(B), set(C)
    if (A & B) or (A & C) or (B & C):
        raise ValueError("A, B and C must be disjoint")
    return not (reachable(g, A, C) & B)


def minimal_separators(g, a, b):
    """All inclusion-minimal sets C (not containing a or b) that separate a and b. Requires a, b
    non-adjacent. Search over subsets by increasing size; fine for the small graphs of the chapter."""
    if g.adjacent(a, b):
        return []
    others = [v for v in range(g.p) if v not in (a, b)]
    found = []
    for k in range(len(others) + 1):
        for C in itertools.combinations(others, k):
            Cs = set(C)
            if any(F <= Cs for F in found):
                continue
            if separates(g, {a}, {b}, Cs):
                found.append(Cs)
    return [tuple(sorted(F)) for F in found]


def maximal_cliques(g):
    """Bron-Kerbosch with pivoting (Bron and Kerbosch 1973; Tomita et al. 2006). Returns the maximal
    cliques as sorted tuples, in canonical order (by size of the first vertex, then lexicographically)."""
    out = []

    def bk(R, P, X):
        if not P and not X:
            out.append(tuple(sorted(R)))
            return
        u = max(P | X, key=lambda v: len(g.adj[v] & P))
        for v in list(P - g.adj[u]):
            bk(R | {v}, P & g.adj[v], X & g.adj[v])
            P = P - {v}
            X = X | {v}

    bk(set(), set(range(g.p)), set())
    return sorted(out)


def complete_subsets(g):
    """Every non-empty complete subset (clique, maximal or not), as sorted tuples."""
    out = []
    for k in range(1, g.p + 1):
        for S in itertools.combinations(range(g.p), k):
            if g.is_complete(S):
                out.append(S)
    return out


def markov_statements(g):
    """The statements a graph asserts, as (kind, A, B, C) with A, B, C tuples of vertices:
    pairwise (a, b | rest) for each non-adjacent pair; local (v, non-neighbours | neighbours) for each
    vertex with a non-neighbour; global (a, b | C) for each non-adjacent pair and each minimal separator.
    The same list, in the same order, is built by the demos."""
    out = []
    V = set(range(g.p))
    for a, b in itertools.combinations(range(g.p), 2):
        if not g.adjacent(a, b):
            out.append(("pairwise", (a,), (b,), tuple(sorted(V - {a, b}))))
    for v in range(g.p):
        nn = V - g.adj[v] - {v}
        if nn:
            out.append(("local", (v,), tuple(sorted(nn)), tuple(sorted(g.adj[v]))))
    for a, b in itertools.combinations(range(g.p), 2):
        if not g.adjacent(a, b):
            for C in minimal_separators(g, a, b):
                out.append(("global", (a,), (b,), C))
    return out


def statement_tex(g, A, B, C):
    nm = lambda S: ",".join(g.names[i] for i in S)
    s = nm(A) + " " + INDEP + " " + (nm(B) if len(B) == 1 else "(" + nm(B) + ")")
    if C:
        s += r" \mid " + nm(C)
    return "$" + s + "$"


# =========================================================================== the book's graphs
BOOK_GRAPHS = {
    "18.1": (["X", "Y", "Z"], [("X", "Y"), ("Y", "Z")], {"X": (0, 0), "Y": (1, 0.62), "Z": (2, 0)}),
    "18.2": (["W", "X", "Y", "Z"], [("W", "X"), ("W", "Y"), ("Y", "X"), ("X", "Z")],
             {"W": (0, 1), "X": (1, 1), "Y": (0, 0), "Z": (1, 0)}),
    "18.3": (["X", "Y", "Z"], [("X", "Y"), ("Y", "Z")], {"X": (0, 0), "Y": (1, 0.62), "Z": (2, 0)}),
    "18.4": (["X", "Y", "Z"], [("X", "Y"), ("Y", "Z"), ("X", "Z")], {"X": (0, 0), "Y": (1, 0.62), "Z": (2, 0)}),
    "18.5": (["X", "Y", "Z", "W"], [("X", "W"), ("X", "Y"), ("Y", "Z"), ("W", "Z")],
             {"X": (0, 1), "W": (1, 1), "Y": (0, 0), "Z": (1, 0)}),
    "18.6": (["X", "Y", "Z", "W"], [("X", "Y"), ("Y", "Z"), ("Z", "W")],
             {"X": (0, 0), "Y": (1, 0), "Z": (2, 0), "W": (3, 0)}),
    "18.7": (["X", "Y", "Z"], [("Y", "Z")], {"X": (0, 0.25), "Y": (0.9, 0.7), "Z": (1.8, 0.25)}),
    "18.8": (["X", "Y", "Z", "W"], [("X", "Y"), ("Y", "Z"), ("Y", "W"), ("Z", "W")],
             {"X": (0, 1), "Y": (0.9, 1), "Z": (0.9, 0), "W": (1.7, 0.5)}),
    "18.9": (["X1", "X2", "X3", "X4", "X5", "X6"],
             [("X1", "X2"), ("X1", "X3"), ("X2", "X4"), ("X2", "X5"), ("X2", "X6"), ("X3", "X5"), ("X5", "X6")],
             {"X1": (0, 0.5), "X2": (1, 1), "X3": (1, 0), "X4": (2, 1), "X5": (2, 0), "X6": (3, 0.5)}),
    "18.10": (["X1", "X2", "X3", "X4"], [("X1", "X2"), ("X2", "X4"), ("X2", "X3")],
              {"X1": (0, 1), "X2": (1, 1), "X4": (2, 1), "X3": (1, 0)}),
    "18.11": (["X1", "X2", "X3", "X4"], [("X1", "X2"), ("X2", "X3"), ("X3", "X4")],
              {"X1": (0, 0), "X2": (1, 0), "X3": (2, 0), "X4": (3, 0)}),
    "18.12": (["X1", "X2", "X3", "X4"], [("X3", "X2"), ("X2", "X1"), ("X1", "X4"), ("X4", "X3")],
              {"X3": (0, 1), "X2": (1, 1), "X4": (0, 0), "X1": (1, 0)}),
    "18.13": (["X1", "X2", "X3", "X4", "X5", "X6"],
              [("X1", "X2"), ("X1", "X3"), ("X2", "X3"), ("X2", "X4"), ("X2", "X5"), ("X3", "X5"),
               ("X3", "X6"), ("X4", "X5"), ("X5", "X6")],
              {"X1": (1, 2), "X2": (0.5, 1), "X3": (1.5, 1), "X4": (0, 0), "X5": (1, 0), "X6": (2, 0)}),
}


def book_graph(key):
    names, edges, pos = BOOK_GRAPHS[key]
    return Graph(names, edges), pos


def tex_name(nm):
    return "$%s_{%s}$" % (nm[0], nm[1:]) if len(nm) > 1 else "$%s$" % nm


# =========================================================================== 18.2 binary distributions
def cell_index(x):
    """Index of the configuration x (tuple of 0/1, vertex 0 first) in a flat table of 2^p cells,
    vertex 0 being the least significant bit (the order used by the demos)."""
    return sum(int(b) << k for k, b in enumerate(x))


def all_cells(p):
    return [tuple((c >> k) & 1 for k in range(p)) for c in range(2 ** p)]


def to_array(flat, p):
    """Flat table (vertex 0 least significant) -> array of shape (2,)*p indexed [x0, x1, ...]."""
    P = np.zeros((2,) * p)
    for c, x in enumerate(all_cells(p)):
        P[x] = flat[c]
    return P


def marginal(P, keep):
    keep = tuple(sorted(keep))
    drop = tuple(i for i in range(P.ndim) if i not in keep)
    return P.sum(axis=drop) if drop else P


def cmi(P, A, B, C):
    """Conditional mutual information I(X_A; X_B | X_C) in nats of a joint table P (shape (2,)*p);
    0 exactly when X_A and X_B are independent given X_C (0 log 0 = 0)."""
    A, B, C = tuple(A), tuple(B), tuple(C)
    keep = tuple(sorted(A + B + C))
    Q = marginal(P, keep)
    pos = {v: k for k, v in enumerate(keep)}
    ax = lambda S: tuple(pos[v] for v in S)

    def m(S):
        drop = tuple(k for k in range(len(keep)) if k not in ax(S))
        return Q.sum(axis=drop, keepdims=True) if drop else Q

    pabc, pac, pbc, pc = Q, m(A + C), m(B + C), m(C) if C else np.array(1.0)
    with np.errstate(divide="ignore", invalid="ignore"):
        t = pabc * (np.log(pabc) + np.log(pc) - np.log(pac) - np.log(pbc))
    return float(np.nansum(np.where(pabc > 0, t, 0.0)))


def gibbs_ising(g, J, h=0.0):
    """f(x) proportional to exp(J sum_{edges} s_i s_j + h sum_i s_i) with s = 2x - 1: a positive
    distribution that factorises over the edges, hence is Markov to g. Returns the (2,)*p array."""
    flat = []
    for x in all_cells(g.p):
        s = [2 * b - 1 for b in x]
        flat.append(J * sum(s[i] * s[j] for i, j in g.edges) + h * sum(s))
    flat = np.exp(np.array(flat) - max(flat))
    return to_array(flat / flat.sum(), g.p)


def noisy_copies(p, copies, eta):
    """U a fair coin; X_i = U xor N_i (N_i ~ Bernoulli(eta), independent) for i in `copies`; the other
    variables are independent fair coins. eta = 0 makes the copies identical (not positive)."""
    flat = np.zeros(2 ** p)
    others = [i for i in range(p) if i not in copies]
    for c, x in enumerate(all_cells(p)):
        tot = 0.0
        for u in (0, 1):
            pr = 0.5
            for i in copies:
                pr *= (1 - eta) if x[i] == u else eta
            tot += pr
        flat[c] = tot * 0.5 ** len(others)
    return to_array(flat, p)


def check_statements(g, P, tol=1e-12):
    """[(kind, A, B, C, cmi, holds)] for every statement of markov_statements(g)."""
    out = []
    for kind, A, B, C in markov_statements(g):
        v = cmi(P, A, B, C)
        out.append((kind, A, B, C, v, v < tol))
    return out


# =========================================================================== 18.3 cliques and potentials
def demo_cliques(g):
    """Maximal cliques in the order used by the cliques-and-potentials demo."""
    return maximal_cliques(g)


def random_potentials(g, strength, seed):
    """log-potentials for each maximal clique: for clique C (canonical order) and each configuration of
    x_C (first clique vertex least significant) draw u from the demos' generator and set
    log psi = strength * (2u - 1). Returns {clique: array of length 2^|C|}."""
    rng = JSRandom(seed)
    pot = {}
    for C in demo_cliques(g):
        pot[C] = np.array([strength * (2 * rng.u() - 1) for _ in range(2 ** len(C))])
    return pot


def gibbs_from_potentials(g, logpot, extra=None, delta=0.0):
    """f(x) = prod_C psi_C(x_C) / Z, as in (18.1), optionally times exp(delta * prod_{i in extra} x_i)
    (an interaction on a set `extra` that need not be a clique). Returns (P array, log Z)."""
    flat = []
    for x in all_cells(g.p):
        s = 0.0
        for C, lp in logpot.items():
            s += lp[cell_index([x[v] for v in C])]
        if extra:
            s += delta * float(all(x[v] for v in extra))
        flat.append(s)
    flat = np.array(flat)
    logZ = float(np.log(np.exp(flat - flat.max()).sum()) + flat.max())
    return to_array(np.exp(flat - logZ), g.p), logZ


def mobius_terms(P):
    """Interaction terms of log f relative to the reference configuration 0:
    lambda_S = sum_{U subset of S} (-1)^{|S|-|U|} log f(1_U, 0_rest), for every non-empty S.
    For binary variables, log f(x) = sum_S lambda_S prod_{i in S} x_i (plus log f(0)). Returns {S: value}."""
    p = P.ndim
    logf = np.log(P)
    out = {}
    for k in range(1, p + 1):
        for S in itertools.combinations(range(p), k):
            tot = 0.0
            for r in range(k + 1):
                for U in itertools.combinations(S, r):
                    x = [0] * p
                    for v in U:
                        x[v] = 1
                    tot += (-1) ** (k - r) * logf[tuple(x)]
            out[S] = tot
    return out


def binary_markov_dimension(g):
    """Free parameters of the positive binary distributions Markov to g: one per non-empty clique."""
    return len(complete_subsets(g))


# =========================================================================== 18.4 discrete: tests
def g2_test(counts, i, j, cond):
    """Likelihood ratio (G^2) and Pearson statistics for X_i independent of X_j given X_cond in a
    contingency table `counts` (array, one axis per variable). df = (r_i - 1)(r_j - 1) prod r_c
    (cells with an empty stratum are dropped from the df). Returns dict(G2, X2, df, p, pX2)."""
    cond = tuple(cond)
    keep = (i, j) + cond
    others = tuple(a for a in range(counts.ndim) if a not in keep)
    n = counts.sum(axis=others) if others else counts
    order = sorted(keep)
    n = np.moveaxis(n, [order.index(a) for a in keep], list(range(len(keep))))
    nk = n.sum(axis=(0, 1))
    nik = n.sum(axis=1)
    njk = n.sum(axis=0)
    with np.errstate(divide="ignore", invalid="ignore"):
        e = nik[:, None, ...] * njk[None, :, ...] / nk[None, None, ...]
        G = 2 * np.nansum(np.where(n > 0, n * np.log(n / e), 0.0))
        X2 = np.nansum(np.where(e > 0, (n - e) ** 2 / e, 0.0))
    ri, rj = n.shape[0], n.shape[1]
    df = (ri - 1) * (rj - 1) * int(np.sum(nk > 0))
    return dict(G2=float(G), X2=float(X2), df=df, p=float(stats.chi2.sf(G, df)), pX2=float(stats.chi2.sf(X2, df)))


# Exercise 18.5: breast cancer data (Morrison et al. 1973) as printed in the book.
# Axes: X1 diagnostic centre (0 Boston, 1 Glamorgan); X2 nuclear grade (0 malignant, 1 benign);
# X3 survival (0 died, 1 survived).
BREAST = np.array([[[35, 59], [47, 112]], [[42, 77], [26, 76]]], dtype=float)


def breast_mle():
    return BREAST / BREAST.sum()


def breast_death_glamorgan_benign():
    """P(died | Glamorgan, benign) and its delta-method standard error from the multinomial MLE."""
    d, s = BREAST[1, 1, 0], BREAST[1, 1, 1]
    p = d / (d + s)
    return p, math.sqrt(p * (1 - p) / (d + s))


def breast_tests():
    return {(1, 2): g2_test(BREAST, 0, 1, (2,)), (1, 3): g2_test(BREAST, 0, 2, (1,)),
            (2, 3): g2_test(BREAST, 1, 2, (0,))}


def g2_joint_independence(counts, a, B):
    """G^2 for X_a independent of the vector X_B (all other axes summed out), df = (r_a - 1)(prod r_B - 1)."""
    keep = (a,) + tuple(B)
    others = tuple(k for k in range(counts.ndim) if k not in keep)
    n = counts.sum(axis=others) if others else counts
    order = sorted(keep)
    n = np.moveaxis(n, [order.index(k) for k in keep], list(range(len(keep))))
    n = n.reshape(n.shape[0], -1)
    e = n.sum(1, keepdims=True) * n.sum(0, keepdims=True) / n.sum()
    G = 2 * np.sum(np.where(n > 0, n * np.log(n / e), 0.0))
    df = (n.shape[0] - 1) * (n.shape[1] - 1)
    return dict(G2=float(G), df=df, p=float(stats.chi2.sf(G, df)))


# =========================================================================== 18.4 Gaussian graphical models
def partial_correlations(S):
    """Partial correlation of each pair given all the others, -K_jk / sqrt(K_jj K_kk) with K = S^{-1}
    (Theorem 18.K); the diagonal is set to 1."""
    K = np.linalg.inv(S)
    d = np.sqrt(np.diag(K))
    R = -K / np.outer(d, d)
    np.fill_diagonal(R, 1.0)
    return R


def partial_corr_residuals(X, j, k):
    """The same partial correlation computed the long way: correlate the residuals of X_j and X_k after
    least-squares regression (with intercept) on all the other columns."""
    n, p = X.shape
    rest = [c for c in range(p) if c not in (j, k)]
    Z = np.column_stack([np.ones(n), X[:, rest]])
    rj = X[:, j] - Z @ np.linalg.lstsq(Z, X[:, j], rcond=None)[0]
    rk = X[:, k] - Z @ np.linalg.lstsq(Z, X[:, k], rcond=None)[0]
    return float(rj @ rk / math.sqrt((rj @ rj) * (rk @ rk)))


def pcor_tests(X, kind="partial"):
    """Test every pair. kind='partial': r = partial correlation given the other p-2 variables,
    t = r sqrt((n-p)/(1-r^2)) ~ t_{n-p} under H0 (exact for Gaussian data, Theorem 18.L).
    kind='marginal': ordinary correlation, df n-2. Also Fisher's z: sqrt(n-k-3) atanh(r) ~ N(0,1)
    approximately, k = number of conditioning variables. Returns a list of dicts in pair order."""
    n, p = X.shape
    S = np.cov(X.T)
    if kind == "partial":
        R, df, kc = partial_correlations(S), n - p, p - 2
    else:
        d = np.sqrt(np.diag(S))
        R, df, kc = S / np.outer(d, d), n - 2, 0
    out = []
    for j, k in itertools.combinations(range(p), 2):
        r = R[j, k]
        t = r * math.sqrt(df / (1 - r * r))
        z = math.sqrt(n - kc - 3) * math.atanh(r)
        out.append(dict(j=j, k=k, r=float(r), t=float(t), p=float(2 * stats.t.sf(abs(t), df)),
                        z=float(z), pz=float(2 * stats.norm.sf(abs(z))), df=df))
    return out


def adjust(pvals, method):
    """Rejection decisions at level alpha are made by comparing these adjusted p-values with alpha:
    'none', 'bonferroni' (m p) or 'bh' (Benjamini-Hochberg step-up adjusted p-values)."""
    p = np.asarray(pvals, float)
    m = len(p)
    if method == "none":
        return p
    if method == "bonferroni":
        return np.minimum(1.0, m * p)
    order = np.argsort(p, kind="stable")
    adj = np.empty(m)
    run = 1.0
    for rank in range(m, 0, -1):
        i = order[rank - 1]
        run = min(run, p[i] * m / rank)
        adj[i] = run
    return adj


def edge_exclusion(X, alpha=0.05, method="none", kind="partial"):
    """Keep the edge j-k when the test of 'no edge' rejects at level alpha (after correction)."""
    res = pcor_tests(X, kind)
    adj = adjust([r["p"] for r in res], method)
    for r, a in zip(res, adj):
        r["padj"] = float(a)
        r["edge"] = bool(a < alpha)
    return res


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


def coris_continuous(transform="log"):
    """The eight continuous variables of the book's coris.dat (462 men); with transform='log' the two
    skewed variables tobacco and alcohol are replaced by log(1 + x)."""
    import pandas as pd
    df = pd.read_csv(common.aos_file("coris.dat"), skiprows=[1, 2, 3], skipinitialspace=True)
    X = df[CORIS_VARS].to_numpy(dtype=float)
    if transform == "log":
        for c in ("tobacco", "alcohol"):
            k = CORIS_VARS.index(c)
            X[:, k] = np.log1p(X[:, k])
    return X


def coris_subsample(X, n, seed):
    """The demo's subsample: shuffle the row indices with the seeded generator and keep the first n."""
    if n >= X.shape[0]:
        return X
    idx = js_shuffle(JSRandom(seed), range(X.shape[0]))
    return X[np.array(idx[:n])]


# simulated Gaussian graphical models used by the partial-correlation demo
def sim_precision(name, rho):
    """Unit-diagonal precision K = I - rho A for the demo's simulated graphs: 'chain' (6 vertices in a
    path) or 'fig189' (the graph of Figure 18.9). Every edge has partial correlation rho."""
    if name == "chain":
        g = Graph(["X1", "X2", "X3", "X4", "X5", "X6"], [(i, i + 1) for i in range(5)])
    else:
        g = book_graph("18.9")[0]
    A = np.zeros((g.p, g.p))
    for i, j in g.edges:
        A[i, j] = A[j, i] = 1
    return np.eye(g.p) - rho * A, g


def sim_gaussian(K, n, seed):
    """n draws from N(0, K^{-1}) with the demos' generator: x = L z, L the lower Cholesky factor,
    z filled row by row from rng.norm()."""
    S = np.linalg.inv(K)
    L = np.linalg.cholesky(S)
    rng = JSRandom(seed)
    p = K.shape[0]
    Z = np.array([[rng.norm() for _ in range(p)] for _ in range(n)])
    return Z @ L.T


def gauss_cycle(r, s):
    """The four-cycle X - Y - Z - W - X of Figure 18.5 (order X, Y, Z, W) with partial correlation r on
    the edges XY, YZ, XW and s on ZW. Returns (K, Sigma). Sigma_XZ = (r^2 + r s) / det K, which is 0
    at s = -r although X and Z are not separated by the empty set (Filling the gap 18.5)."""
    K = np.array([[1, -r, 0, -r], [-r, 1, -r, 0], [0, -r, 1, -s], [-r, 0, -s, 1]], dtype=float)
    return K, np.linalg.inv(K)


# =========================================================================== drawing
def draw_graph(ax, g, pos, cliques=None, node_col="k", r=0.075, fs=12, shade=True, edge_kw=None,
               label_off=None):
    """Draw g with positions pos (name -> (x, y)); shade maximal cliques of size >= 3."""
    P = np.array([pos[nm] for nm in g.names], dtype=float)
    cols = [BLUE, GREEN, ORANGE, PURPLE, MAGENTA, RED]
    if shade and cliques:
        k = 0
        for C in cliques:
            if len(C) >= 3:
                pts = P[list(C)]
                c0 = pts.mean(0)
                ang = np.arctan2(pts[:, 1] - c0[1], pts[:, 0] - c0[0])
                pts = pts[np.argsort(ang)]
                ax.fill(pts[:, 0], pts[:, 1], color="#7f8fb0", alpha=0.25, lw=0)
                k += 1
    for i, j in g.edges:
        kw = dict(color="k", lw=2.0)
        if edge_kw:
            kw.update(edge_kw.get((i, j), {}))
        ax.plot([P[i, 0], P[j, 0]], [P[i, 1], P[j, 1]], solid_capstyle="round", zorder=1, **kw)
    ax.scatter(P[:, 0], P[:, 1], s=40, color=node_col, zorder=3)
    span = max(np.ptp(P[:, 0]), np.ptp(P[:, 1]), 1.0)
    y0, y1, x0, x1 = P[:, 1].min(), P[:, 1].max(), P[:, 0].min(), P[:, 0].max()
    for i, nm in enumerate(g.names):
        x, y = P[i]
        if y1 - y0 < 1e-9 or y >= y1 - 0.25 * (y1 - y0):
            off = (0, 0.11 * span)          # top row (or a single row): label above
        elif y <= y0 + 0.25 * (y1 - y0):
            off = (0, -0.12 * span)         # bottom row: label below
        else:
            off = (-0.1 * span, 0) if x <= x0 + 1e-9 else ((0.1 * span, 0) if x >= x1 - 1e-9 else (0, 0.11 * span))
        dx, dy = (label_off or {}).get(nm, off)
        ax.text(P[i, 0] + dx, P[i, 1] + dy, tex_name(nm), ha="center", va="center", fontsize=fs)
    ax.set_aspect("equal")
    ax.axis("off")
    pad = 0.25 * span
    ax.set_xlim(P[:, 0].min() - pad, P[:, 0].max() + pad)
    ax.set_ylim(P[:, 1].min() - pad, P[:, 1].max() + pad)


def _statement_lines(g):
    """Minimal separation statements of g (one per non-adjacent pair and minimal separator)."""
    lines = []
    for a, b in itertools.combinations(range(g.p), 2):
        if not g.adjacent(a, b):
            for C in minimal_separators(g, a, b):
                lines.append(statement_tex(g, (a,), (b,), C))
    return lines


def _clique_line(g, C):
    return r"$\{" + ",".join(g.names[i] if len(g.names[i]) == 1 else "%s_{%s}" % (g.names[i][0], g.names[i][1:])
                             for i in C) + r"\}$"


def _annotated_figure(key, out_dir, extra_lines=()):
    g, pos = book_graph(key)
    cl = maximal_cliques(g)
    fig = common.plt.figure(figsize=(6.2, 2.3))
    ax = fig.add_axes([0.0, 0.0, 0.45, 1.0])
    draw_graph(ax, g, pos, cl)
    tx = fig.add_axes([0.48, 0.0, 0.52, 1.0])
    tx.axis("off")
    lines = ["Maximal cliques: " + ", ".join(_clique_line(g, C) for C in cl)]
    st = _statement_lines(g)
    if st:
        lines.append("Global Markov (minimal separators):")
        lines += ["   " + s for s in st]
    else:
        lines.append("Complete graph: no independence asserted")
    lines += list(extra_lines)
    y = 0.9
    for ln in lines:
        tx.text(0.0, y, ln, fontsize=10, va="top", transform=tx.transAxes)
        y -= 0.13
    return savefig(fig, CH, "fig" + key.replace(".", "-"), out_dir)


def _plain_figure(key, out_dir, size=(3.4, 2.4), label_off=None):
    g, pos = book_graph(key)
    fig, ax = common.plt.subplots(figsize=size)
    draw_graph(ax, g, pos, maximal_cliques(g), shade=False, label_off=label_off)
    return savefig(fig, CH, "fig" + key.replace(".", "-"), out_dir)


# =========================================================================== book figures
def fig_18_1(out_dir=None):
    """Figure 18.1: a graph with vertices X, Y, Z and edges XY, YZ."""
    return _annotated_figure("18.1", out_dir)


def fig_18_2(out_dir=None):
    """Figure 18.2: {Y, W} and {Z} are separated by {X}."""
    g, pos = book_graph("18.2")
    ok1 = separates(g, {2, 0}, {3}, {1})
    ok2 = separates(g, {0}, {3}, {1, 2})
    return _annotated_figure("18.2", out_dir, extra_lines=[
        r"Checked: $\{X\}$ separates $\{Y,W\}$, $\{Z\}$: %s" % ("yes" if ok1 else "no"),
        r"Checked: $\{X,Y\}$ separates $W$, $Z$: %s" % ("yes" if ok2 else "no")])


def fig_18_3(out_dir=None):
    """Figure 18.3: X independent of Z given Y."""
    return _annotated_figure("18.3", out_dir)


def fig_18_4(out_dir=None):
    """Figure 18.4: the complete graph on three vertices (no independence)."""
    return _annotated_figure("18.4", out_dir)


def fig_18_5(out_dir=None):
    """Figure 18.5: the four-cycle."""
    return _annotated_figure("18.5", out_dir)


def fig_18_6(out_dir=None):
    """Figure 18.6: the chain X - Y - Z - W."""
    return _annotated_figure("18.6", out_dir)


def fig_18_7(out_dir=None):
    """Figure 18.7: X isolated, Y - Z."""
    g, pos = book_graph("18.7")
    ok = separates(g, {0}, {1, 2}, set())
    return _annotated_figure("18.7", out_dir, extra_lines=[
        r"Checked: $\emptyset$ separates $X$ and $\{Y,Z\}$: %s" % ("yes" if ok else "no")])


def fig_18_8(out_dir=None):
    """Figure 18.8: X - Y and the triangle Y, Z, W."""
    return _annotated_figure("18.8", out_dir)


def fig_18_9(out_dir=None):
    """Figure 18.9: six vertices, maximal cliques found by Bron-Kerbosch."""
    g, pos = book_graph("18.9")
    cl = maximal_cliques(g)
    fig = common.plt.figure(figsize=(6.6, 2.6))
    ax = fig.add_axes([0.0, 0.02, 0.56, 0.96])
    cols = [BLUE, GREEN, ORANGE, PURPLE, MAGENTA]
    ekw = {}
    for k, C in enumerate(cl):
        if len(C) == 2:
            ekw[tuple(sorted(C))] = dict(color=cols[k % len(cols)], lw=3.2)
    draw_graph(ax, g, pos, cl, edge_kw=ekw)
    tx = fig.add_axes([0.58, 0.0, 0.42, 1.0])
    tx.axis("off")
    tx.text(0, 0.92, "Maximal cliques (Bron-Kerbosch):", fontsize=10, va="top")
    y = 0.78
    for k, C in enumerate(cl):
        tx.text(0.04, y, _clique_line(g, C), fontsize=10, va="top", color=cols[k % len(cols)] if len(C) == 2 else "k")
        y -= 0.12
    tx.text(0, y - 0.02, "All cliques: %d; maximal: %d" % (len(complete_subsets(g)), len(cl)), fontsize=9.5, va="top")
    return savefig(fig, CH, "fig18-9", out_dir)


def fig_18_10(out_dir=None):
    """Figure 18.10 (Exercise 18.3a)."""
    return _plain_figure("18.10", out_dir)


def fig_18_11(out_dir=None):
    """Figure 18.11 (Exercise 18.3b)."""
    return _plain_figure("18.11", out_dir, size=(4.2, 1.3))


def fig_18_12(out_dir=None):
    """Figure 18.12 (Exercise 18.3c)."""
    return _plain_figure("18.12", out_dir, size=(2.6, 2.6))


def fig_18_13(out_dir=None):
    """Figure 18.13 (Exercise 18.3d)."""
    return _plain_figure("18.13", out_dir, size=(3.2, 3.0), label_off={"X2": (-0.22, 0.05), "X3": (0.22, 0.05)})


# =========================================================================== supplementary figures
def fig_18_s1(out_dir=None):
    """Figure 18.S1: positivity and one-way definitions (Filling the gaps 18.4, 18.5)."""
    fig, axes = common.panel_grid(1, 2, size=(3.6, 2.8))
    ax = axes[0, 0]
    etas = np.logspace(-5, np.log10(0.3), 120)
    pw, gl = [], []
    for e in etas:
        P = noisy_copies(3, (0, 1, 2), e)
        pw.append(cmi(P, (0,), (1,), (2,)))
        gl.append(cmi(P, (0,), (1,), ()))
    ax.loglog(etas, gl, color=RED, label=r"global: $I(X;Y)$")
    ax.loglog(etas, pw, color=BLUE, label=r"pairwise: $I(X;Y\mid Z)$")
    ax.axhline(math.log(2), color=GREY, lw=0.8, ls=":")
    ax.text(2e-3, math.log(2) * 1.35, r"$\log 2$", fontsize=8, color=GREY)
    ax.set_xlabel(r"noise $\eta$ (the copies differ with probability $\eta$)")
    ax.set_ylabel("conditional mutual inf. (nats)")
    ax.set_ylim(1e-5, 3)
    ax.legend(fontsize=7.5, frameon=False, loc="lower right")
    common.panel_label(ax, "a", loc="upper left")
    ax = axes[0, 1]
    r = 0.3
    ss = np.linspace(-0.5, 0.5, 201)
    sxz, pxz = [], []
    for s in ss:
        K, S = gauss_cycle(r, s)
        sxz.append(S[0, 2] / math.sqrt(S[0, 0] * S[2, 2]))
        pxz.append(partial_correlations(S)[0, 2])
    ax.plot(ss, sxz, color=RED, label=r"corr$(X,Z)$")
    ax.plot(ss, pxz, color=BLUE, label=r"partial corr$(X,Z\mid Y,W)$")
    ax.axhline(0, color="k", lw=0.6)
    ax.axvline(-r, color=GREY, ls=":", lw=1)
    ax.plot([-r], [0], "o", color=RED, ms=5)
    ax.set_xlabel(r"partial correlation $s$ on the edge $ZW$")
    ax.set_ylabel("correlation")
    ax.legend(fontsize=7, frameon=False, loc="upper left")
    common.panel_label(ax, "b", loc="lower right")
    fig.tight_layout()
    return savefig(fig, CH, "fig18-S1", out_dir)


def fig_18_s2(out_dir=None):
    """Figure 18.S2: a Gaussian graphical model for the coris data."""
    X = coris_continuous("log")
    n, p = X.shape
    S = np.cov(X.T)
    d = np.sqrt(np.diag(S))
    Rm = S / np.outer(d, d)
    Rp = partial_correlations(S)
    M = np.tril(Rm, -1) + np.triu(Rp, 1)
    fig = common.plt.figure(figsize=(8.4, 3.5))
    ax = fig.add_axes([0.05, 0.12, 0.42, 0.8])
    im = ax.imshow(M, cmap="RdBu_r", vmin=-0.8, vmax=0.8)
    for i in range(p):
        for j in range(p):
            if i != j:
                ax.text(j, i, "%.2f" % M[i, j], ha="center", va="center", fontsize=6.3,
                        color="white" if abs(M[i, j]) > 0.5 else "k")
    ax.set_xticks(range(p))
    ax.set_yticks(range(p))
    ax.set_xticklabels(CORIS_VARS, rotation=60, fontsize=7.5)
    ax.set_yticklabels(CORIS_VARS, fontsize=7.5)
    ax.set_title("below: correlation; above: partial correlation", fontsize=8.5)
    fig.colorbar(im, ax=ax, fraction=0.04, pad=0.02)
    common.panel_label(ax, "a", loc="lower left")
    ax = fig.add_axes([0.53, 0.02, 0.45, 0.95])
    names = CORIS_VARS
    ang = np.pi / 2 - 2 * np.pi * np.arange(p) / p
    P = np.column_stack([np.cos(ang), np.sin(ang)])
    res05 = edge_exclusion(X, 0.05, "none")
    resB = edge_exclusion(X, 0.05, "bonferroni")
    for r0, rb in zip(res05, resB):
        if r0["edge"]:
            j, k = r0["j"], r0["k"]
            col = RED if r0["r"] > 0 else BLUE
            ax.plot(P[[j, k], 0], P[[j, k], 1], color=col, lw=0.8 + 6 * abs(r0["r"]),
                    ls="-" if rb["edge"] else (0, (3, 2)), zorder=1, solid_capstyle="round")
    ax.scatter(P[:, 0], P[:, 1], s=55, color="k", zorder=3)
    for i in range(p):
        c, sn = P[i]
        ha = "center" if abs(c) < 0.2 else ("left" if c > 0 else "right")
        ax.text(P[i, 0] + 0.1 * np.sign(c) * (abs(c) >= 0.2), P[i, 1] + 0.14 * sn, names[i], ha=ha, va="center", fontsize=8.5)
    ax.set_xlim(-1.75, 1.75)
    ax.set_ylim(-1.75, 1.3)
    ax.set_aspect("equal")
    ax.axis("off")
    ax.text(0, -1.72, "solid: kept at 5% with Bonferroni; dashed: at 5% only\nred positive, blue negative; width grows with |partial corr|",
            fontsize=7, va="bottom", ha="center")
    common.panel_label(ax, "b", loc="upper left")
    return savefig(fig, CH, "fig18-S2", out_dir)


def null_t_sim(n, p, R, seed=18):
    """Simulated null distribution of the partial-correlation statistics for the pair (0, 1) when the
    p variables are independent Normals (R data sets of size n, vectorised): returns arrays (t, z)."""
    rng = np.random.default_rng(seed)
    X = rng.standard_normal((R, n, p))
    X = X - X.mean(axis=1, keepdims=True)
    K = np.linalg.inv(np.einsum("rni,rnj->rij", X, X))
    r = -K[:, 0, 1] / np.sqrt(K[:, 0, 0] * K[:, 1, 1])
    t = r * np.sqrt((n - p) / (1 - r * r))
    z = np.sqrt(n - (p - 2) - 3) * np.arctanh(r)
    return t, z


def fisher_z_size(n, p, alpha=0.05):
    """Exact type I error of Fisher's z test of a zero partial correlation given p - 2 variables:
    |z| > c iff |r| > tanh(c / sqrt(n - p - 1)), and t = r sqrt((n-p)/(1-r^2)) is exactly t_{n-p}."""
    c = stats.norm.ppf(1 - alpha / 2)
    rc = math.tanh(c / math.sqrt(n - p - 1))
    return float(2 * stats.t.sf(rc * math.sqrt((n - p) / (1 - rc * rc)), n - p))


def size_curve(ns, p, R, alpha=0.05, seed=181):
    out = []
    for i, n in enumerate(ns):
        ts, zs = null_t_sim(n, p, R, seed + i)
        ct = stats.t.ppf(1 - alpha / 2, n - p)
        out.append((n, float(np.mean(np.abs(ts) > ct)), float(np.mean(np.abs(zs) > stats.norm.ppf(1 - alpha / 2)))))
    return out


def fig_18_s3(out_dir=None):
    """Figure 18.S3: the exact t test of a partial correlation, and Fisher's z."""
    p, n = 6, 12
    ts, zs = null_t_sim(n, p, 20000)
    fig, axes = common.panel_grid(1, 2, size=(3.6, 2.7))
    ax = axes[0, 0]
    ax.hist(ts, bins=np.linspace(-6, 6, 61), density=True, color="#cfd8e8", edgecolor="white")
    xx = np.linspace(-6, 6, 400)
    ax.plot(xx, stats.t.pdf(xx, n - p), color=RED, label=r"$t_{n-p}=t_{6}$")
    ax.plot(xx, stats.norm.pdf(xx), color=BLUE, ls="--", label=r"$N(0,1)$")
    ax.set_xlabel(r"$t$ for a zero partial correlation ($n=12$, $p=6$)")
    ax.set_ylabel("density")
    ax.legend(fontsize=7.5, frameon=False)
    common.panel_label(ax, "a", loc="upper left")
    ax = axes[0, 1]
    nn = np.arange(8, 201)
    ax.plot(nn, [fisher_z_size(n, p) for n in nn], color=BLUE, label="Fisher's $z$ (exact size)")
    ax.plot(nn, [float(2 * stats.t.sf(stats.norm.ppf(0.975), n - p)) for n in nn], color=GREY, ls="--",
            label="$t$ with a Normal cut-off")
    ax.axhline(0.05, color=RED, lw=1.4, label="exact $t$ test (size 0.05)")
    sc = size_curve([8, 10, 15, 30, 100], p, 40000)
    ax.plot([s_[0] for s_ in sc], [s_[2] for s_ in sc], "s", color=BLUE, ms=4, mfc="white", label="simulated, $z$")
    ax.plot([s_[0] for s_ in sc], [s_[1] for s_ in sc], "o", color=RED, ms=4, mfc="white", label="simulated, $t$")
    ax.set_xscale("log")
    ax.set_xlabel("sample size $n$ ($p=6$)")
    ax.set_ylabel("type I error at level 0.05")
    ax.set_ylim(0.03, 0.13)
    ax.legend(fontsize=7, frameon=False, loc="upper right")
    common.panel_label(ax, "b", loc="lower left")
    fig.tight_layout()
    return savefig(fig, CH, "fig18-S3", out_dir)


def fig_18_s4(out_dir=None):
    """Figure 18.S4: interaction terms of log f vanish outside the cliques (Figure 18.9 graph)."""
    g, pos = book_graph("18.9")
    pot = random_potentials(g, 1.0, 7)
    fig, axes = common.panel_grid(1, 2, size=(4.2, 2.8))
    for a, (extra, delta) in enumerate((((None), 0.0), ((0, 4), 1.0))):
        P, _ = gibbs_from_potentials(g, pot, extra, delta)
        lam = mobius_terms(P)
        Ss = list(lam)
        vals = np.array([max(abs(lam[S]), 1e-17) for S in Ss])
        comp = np.array([g.is_complete(S) for S in Ss])
        ax = axes[0, a]
        x = np.arange(len(Ss))
        ax.bar(x[comp], vals[comp], color=BLUE, width=0.8, label="clique $S$")
        ax.bar(x[~comp], vals[~comp], color=RED, width=0.8, label="not a clique")
        ax.set_yscale("log")
        ax.set_ylim(1e-17, 10)
        sizes = [len(S) for S in Ss]
        for k in range(2, 7):
            ax.axvline(sizes.index(k) - 0.5, color=GREY, lw=0.5)
        ax.set_xticks([np.mean([i for i, s in enumerate(sizes) if s == k]) for k in range(1, 7)])
        ax.set_xticklabels([str(k) for k in range(1, 7)], fontsize=8)
        ax.set_xlabel("subsets $S$, grouped by size $|S|$")
        ax.set_ylabel(r"$|\lambda_S|$")
        ax.set_title("(18.1) only" if a == 0 else r"(18.1) $\times\ e^{x_1x_5}$ ($X_1,X_5$ not adjacent)", fontsize=8.5)
        if a == 1:
            ax.legend(fontsize=7, frameon=False, loc="center")
        common.panel_label(ax, "ab"[a], loc="upper right")
    fig.tight_layout()
    return savefig(fig, CH, "fig18-S4", out_dir)


def fig_18_s5(out_dir=None):
    """Figure 18.S5: Exercise 18.5, the graph fitted to the breast cancer data."""
    tests = breast_tests()
    g = Graph(["X1", "X2", "X3"], [])
    pos = {"X1": (0, 0), "X2": (2, 0), "X3": (1, 1.3)}
    labels = {(1, 2): "centre-grade", (1, 3): "centre-survival", (2, 3): "grade-survival"}
    fig, ax = common.plt.subplots(figsize=(5.0, 3.0))
    P = {1: np.array(pos["X1"]), 2: np.array(pos["X2"]), 3: np.array(pos["X3"])}
    for (i, j), t in tests.items():
        keep = t["p"] < 0.05
        ax.plot(*zip(P[i], P[j]), color="k" if keep else GREY, lw=2.4 if keep else 1.0,
                ls="-" if keep else (0, (3, 3)), zorder=1)
        mid = (P[i] + P[j]) / 2
        off = np.array([0, -0.22]) if (i, j) == (1, 2) else (np.array([-0.55, 0.1]) if (i, j) == (1, 3) else np.array([0.55, 0.1]))
        ax.text(*(mid + off), "%s\n$G^2=%.2f$, $p=%.3g$" % (labels[(i, j)], t["G2"], t["p"]), ha="center",
                va="center", fontsize=7.5)
    for k, nm in ((1, "X1 centre"), (2, "X2 grade"), (3, "X3 survival")):
        ax.scatter(*P[k], s=60, color="k", zorder=3)
        ax.text(P[k][0], P[k][1] + (0.18 if k == 3 else -0.5), nm, ha="center", fontsize=9)
    ax.set_xlim(-0.9, 2.9)
    ax.set_ylim(-0.75, 1.6)
    ax.set_aspect("equal")
    ax.axis("off")
    ax.set_title("solid: edge kept (test of 'no edge' rejects at 5%); dashed: removed", fontsize=8)
    return savefig(fig, CH, "fig18-S5", out_dir)


# =========================================================================== data for the demos
def export_json(path=None):
    """Write the coris variables used by the partial-correlation demo (raw values, 462 x 8)."""
    import pandas as pd
    df = pd.read_csv(common.aos_file("coris.dat"), skiprows=[1, 2, 3], skipinitialspace=True)
    X = df[CORIS_VARS].to_numpy(dtype=float)
    out = {"source": "coris.dat (South African heart disease data, 462 men), Wasserman (2004) book website",
           "vars": CORIS_VARS, "X": [[float(v) for v in row] for row in X]}
    path = path or os.path.join(common.HERE, "..", "data", "ch18-coris.json")
    with open(path, "w") as f:
        json.dump(out, f, separators=(",", ":"))
    print("wrote", os.path.relpath(path, os.getcwd()))
    return path


FIGURES = {"18.1": fig_18_1, "18.2": fig_18_2, "18.3": fig_18_3, "18.4": fig_18_4, "18.5": fig_18_5,
           "18.6": fig_18_6, "18.7": fig_18_7, "18.8": fig_18_8, "18.9": fig_18_9, "18.10": fig_18_10,
           "18.11": fig_18_11, "18.12": fig_18_12, "18.13": fig_18_13, "18.S1": fig_18_s1, "18.S2": fig_18_s2,
           "18.S3": fig_18_s3, "18.S4": fig_18_s4, "18.S5": fig_18_s5}

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