"""Murphy (2023), Chapter 26: Generative adversarial networks (and Supplement Chapter 26).

Companion code for Chapter 26 of K. P. Murphy, Probabilistic Machine Learning: Advanced Topics
(MIT Press, 2023; December 2025 draft), pp. 893-924. The supplement chapter S26 (pp. 207-210 of the
supplement) contains only a chapter heading and no material. Written from scratch with numpy, scipy,
matplotlib and PyTorch (CPU, one thread), to be read next to the lecture notes
(../ch26-gans.html).

Scale. Every network is a small multilayer perceptron (32 to 128 hidden units) trained on two-dimensional
data in seconds to a minute; the book's convolutional GANs on faces need GPU scale and are not reproduced.
All training is seeded and the results are cached in ./cache/ch26.

Methods
* Section 26.1: pushforward of a latent variable through a generator, the density by change of variables,
  an implicit model trained by MMD matching (fig_26_1).
* Section 26.2: divergences of one-dimensional densities on a grid (KL, JSD, Pearson chi-square, total
  variation, Wasserstein-1 from the CDFs, MMD with an RBF kernel) with their closed forms for Gaussians
  (functions kl, jsd, chi2, tv, w1, mmd2 and mmd2_gauss); optimal discriminator and the
  proper scoring rules of Table 26.1 (scoring_optimum); f-divergences, their conjugates and the optimal
  critics of Table 26.2 (F_DIV, fgan_bound); the optimal Wasserstein and MMD critics (wasserstein_critic,
  mmd_witness; Figure 26.3); non-overlapping support (Figure 26.4: a critic trained on the KL bound gives a
  usable gradient where the divergence itself does not).
* Section 26.3: saturating and non-saturating generator losses (Figure 26.5), alternating gradient descent
  (Algorithms 26.1 and 26.2, train_gan) on the mixture of 16 Gaussians (Figure 26.6), DiracGAN with
  simultaneous, alternating and continuous dynamics (Figures 26.7 and 26.8, dirac_run).
* Section 26.4: a conditional GAN whose discriminator does, or does not, see the label.
* Section 26.7.4: the score-function estimator against the reparameterisation estimator.

Skipped figures, with reasons: 26.2 (diagram of approaches), 26.9 (BiGAN diagram), 26.10 and 26.11 (DCGAN
architecture diagrams), 26.12 (attention maps of a trained SAGAN), 26.13 and 26.14 (LapGAN and ProgressiveGAN
diagrams), 26.15 (photographs of faces from large GANs), 26.16 and 26.18 (pix2pix and CycleGAN results on
photographs, trained models and data sets at GPU scale), 26.17 (CycleGAN diagram).

Own figures (not in the book), named as in the notes: x26.a to x26.f.

Data: simulation only (mixtures of Gaussians), as in the book.
"""
import os
import sys
import time

import numpy as np
from scipy import optimize, special

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import common  # noqa: E402
from common import BLUE, CYAN, GREEN, GREY, ORANGE, PURPLE, RED, MAGENTA, plt, savefig, panel_grid, panel_label  # noqa: E402

CH = 26
CACHE_DIR = os.path.join(HERE, "cache", "ch26")
DATA_OUT = os.path.normpath(os.path.join(HERE, "..", "data"))
os.makedirs(CACHE_DIR, exist_ok=True)
LOG2 = np.log(2.0)

# the one-dimensional grid shared with the browser demonstrations (trapezoid rule on N points of [-12, 12])
GN, GLO, GHI = 1201, -12.0, 12.0
XG = np.linspace(GLO, GHI, GN)
DX = XG[1] - XG[0]


# =========================================================================== torch (lazy: the grid-only
# functions below need numpy only)
def _torch():
    return common.torch_setup(0)


# =========================================================================== densities on a grid
def npdf(x, m, s):
    return np.exp(-0.5 * ((x - m) / s) ** 2) / (s * np.sqrt(2 * np.pi))


def mix_pdf(x, comps):
    """comps: list of (weight, mean, sd)."""
    return sum(w * npdf(x, m, s) for w, m, s in comps)


def trap(y, x=XG):
    return float(np.trapz(y, x))


def cdf_grid(p, x=XG):
    """Cumulative trapezoid integral of the density p on the grid."""
    return np.concatenate([[0.0], np.cumsum(0.5 * (p[1:] + p[:-1]) * np.diff(x))])


def kl(p, q, x=XG):
    """KL(p || q) = int p log(p / q); +inf if q = 0 somewhere p has mass (p below 1e-14 max is ignored)."""
    live = p > 1e-14 * p.max()
    if np.any(q[live] <= 0):
        return np.inf
    f = np.zeros_like(p)
    f[live] = p[live] * np.log(p[live] / q[live])
    return trap(f, x)


def jsd(p, q, x=XG):
    """Jensen-Shannon divergence (26.16), 0 <= JSD <= log 2."""
    m = 0.5 * (p + q)
    return 0.5 * kl(p, m, x) + 0.5 * kl(q, m, x)


def chi2(p, q, x=XG):
    """Pearson chi-square divergence int (p - q)^2 / q = D_f with f(u) = (u-1)^2."""
    live = (p > 1e-14 * p.max()) | (q > 1e-14 * q.max())
    if np.any(q[live & (p > 0)] <= 0):
        return np.inf
    f = np.zeros_like(p)
    ok = q > 0
    f[ok] = (p[ok] - q[ok]) ** 2 / q[ok]
    return trap(f, x)


def tv(p, q, x=XG):
    return 0.5 * trap(np.abs(p - q), x)


def w1(p, q, x=XG):
    """Wasserstein-1 distance in one dimension: int |F_p - F_q| dx."""
    return trap(np.abs(cdf_grid(p, x) - cdf_grid(q, x)), x)


def rbf_matrix(x, ell):
    return np.exp(-0.5 * ((x[:, None] - x[None, :]) / ell) ** 2)


def mmd2(p, q, ell, x=XG, K=None):
    """Squared MMD (26.35) with the RBF kernel exp(-(x-y)^2/(2 ell^2)), by trapezoid quadrature of the double integral."""
    if K is None:
        K = rbf_matrix(x, ell)
    w = np.full(len(x), x[1] - x[0])
    w[0] *= 0.5
    w[-1] *= 0.5
    d = (p - q) * w
    return float(d @ K @ d)


def mmd2_gauss(m1, s1, m2, s2, ell):
    """MMD^2 between N(m1, s1^2) and N(m2, s2^2) for the RBF kernel with bandwidth ell (closed form)."""
    def ek(ma, sa, mb, sb):
        v = ell ** 2 + sa ** 2 + sb ** 2
        return np.sqrt(ell ** 2 / v) * np.exp(-0.5 * (ma - mb) ** 2 / v)
    return ek(m1, s1, m1, s1) - 2 * ek(m1, s1, m2, s2) + ek(m2, s2, m2, s2)


def kl_gauss(m1, s1, m2, s2):
    """KL(N(m1, s1^2) || N(m2, s2^2))."""
    return np.log(s2 / s1) + (s1 ** 2 + (m1 - m2) ** 2) / (2 * s2 ** 2) - 0.5


def wasserstein_critic(p, q, x=XG):
    """A maximiser of E_p f - E_q f over 1-Lipschitz f in one dimension: f(x) = int_{-inf}^x sign(F_q - F_p) dt."""
    s = np.sign(cdf_grid(q, x) - cdf_grid(p, x))
    f = cdf_grid(s, x)
    return f


def mmd_witness(p, q, ell, x=XG, K=None):
    """The unit-norm RKHS witness f = (mu_p - mu_q) / ||mu_p - mu_q||, evaluated on the grid."""
    if K is None:
        K = rbf_matrix(x, ell)
    w = np.full(len(x), x[1] - x[0])
    w[0] *= 0.5
    w[-1] *= 0.5
    g = K @ ((p - q) * w)
    nrm = np.sqrt(max(float((p - q) * w @ g), 1e-300))
    return g / nrm, nrm


# the three data distributions offered in the one-dimensional demonstration
def p_star(kind):
    if kind == "gauss":
        return mix_pdf(XG, [(1.0, 0.0, 1.0)])
    if kind == "bimodal":
        return mix_pdf(XG, [(0.5, -2.0, 0.6), (0.5, 2.0, 0.6)])
    if kind == "narrow":
        return mix_pdf(XG, [(1.0, 0.0, 0.3)])
    raise ValueError(kind)


def all_divergences(p, q, ell=1.0, K=None):
    return dict(kl_pq=kl(p, q), kl_qp=kl(q, p), jsd=jsd(p, q), chi2=chi2(p, q), tv=tv(p, q), w1=w1(p, q),
                mmd2=mmd2(p, q, ell, K=K))


# =========================================================================== f-divergences (Table 26.2)
# f, conjugate f^dagger, optimal critic as a function of the ratio r = p/q, and the domain map v -> t(v)
F_DIV = {
    "KL": dict(f=lambda u: u * np.log(u), fc=lambda t: np.exp(t - 1), opt=lambda r: 1 + np.log(r), act=lambda v: v),
    "reverse KL": dict(f=lambda u: -np.log(u), fc=lambda t: -1 - np.log(-t), opt=lambda r: -1.0 / r, act=lambda v: -np.exp(v)),
    "JSD": dict(f=lambda u: u * np.log(u) - (u + 1) * np.log((u + 1) / 2), fc=lambda t: -np.log(2 - np.exp(t)),
                opt=lambda r: np.log(2 * r / (1 + r)), act=lambda v: LOG2 - np.logaddexp(0, -v)),
    "Pearson": dict(f=lambda u: (u - 1) ** 2, fc=lambda t: t ** 2 / 4 + t, opt=lambda r: 2 * (r - 1), act=lambda v: v),
}


def fdiv(p, q, name, x=XG):
    """D_f(p || q) = int q f(p/q) for the f of Table 26.2 (note: the JSD row equals 2 JSD of (26.16))."""
    live = q > 1e-300
    r = np.where(live, p / np.where(live, q, 1), 1.0)
    integrand = np.where(live & (p > 0), q * F_DIV[name]["f"](np.where(r > 0, r, 1.0)), 0.0)
    if name == "reverse KL":
        integrand = np.where(live & (p > 0), integrand, np.where(live, q * 1e300 * 0, 0.0))
    return trap(integrand, x)


def fgan_bound(p, q, name, basis, x=XG, w0=None):
    """max over w of E_p t - E_q f^dagger(t), t = act(sum_k w_k basis_k(x)), expectations by quadrature.

    basis is a (K, n) array of functions on the grid. Returns (bound, w, t on the grid)."""
    act, fc = F_DIV[name]["act"], F_DIV[name]["fc"]
    Bm = np.asarray(basis, float)

    def neg(w):
        t = act(w @ Bm)
        return -(trap(p * t, x) - trap(q * fc(t), x))
    w0 = np.zeros(Bm.shape[0]) if w0 is None else w0
    best = None
    for start in (w0, w0 + 0.1, w0 - 0.1):
        r = optimize.minimize(neg, start, method="BFGS", options=dict(gtol=1e-10, maxiter=2000))
        if best is None or r.fun < best.fun:
            best = r
    return -best.fun, best.x, act(best.x @ Bm)


def poly_basis(deg, x=XG):
    return np.stack([x ** k for k in range(deg + 1)])


# =========================================================================== proper scoring rules (Table 26.1)
def scoring_optimum(p, q, rule, x=XG):
    """The optimal discriminator D*(x) and the optimal value of E_p s1(D) + E_q s0(D) (to be maximised) for the
    scoring rules of Table 26.1 that have a closed form; the weights are those of (26.10): Eq. (1/2)(...)"""
    D = np.where(p + q > 0, p / np.maximum(p + q, 1e-300), 0.5)
    if rule == "bernoulli":
        val = trap(p * np.log(np.maximum(D, 1e-300)) + q * np.log(np.maximum(1 - D, 1e-300)), x)
    elif rule == "brier":
        val = trap(-p * (1 - D) ** 2 - q * D ** 2, x)
    elif rule == "exponential":
        val = trap(-p * np.sqrt((1 - D) / np.maximum(D, 1e-300)) - q * np.sqrt(D / np.maximum(1 - D, 1e-300)), x)
    elif rule == "hinge":
        t = np.where(p >= q, 1.0, -1.0)    # optimal logit
        val = trap(-p * np.maximum(0, 1 - t) - q * np.maximum(0, 1 + t), x)
        D = special.expit(t)
    else:
        raise ValueError(rule)
    return D, val


# =========================================================================== DiracGAN (Section 26.3.5)
def dirac_l(kind):
    """The function l of (26.50)-(26.51) and its derivative: 'logistic' is l(z) = -log(1+e^{-z}), 'linear' is l(z) = z."""
    if kind == "logistic":
        return (lambda z: -np.logaddexp(0, -z)), (lambda z: special.expit(-z))
    if kind == "linear":
        return (lambda z: z), (lambda z: np.ones_like(np.asarray(z, float)))
    raise ValueError(kind)


def dirac_field(phi, theta, lp, gamma=0.0):
    """The vector field (dphi, dtheta) = (-grad_phi L_D, -grad_theta L_G) of (26.56)-(26.57), with an optional
    penalty (gamma/2) phi^2 = (gamma/2)|grad_x D|^2 on the discriminator."""
    c = lp(theta * phi)
    return theta * c - gamma * phi, -phi * c


def dirac_step(scheme, phi, theta, h, lp, gamma=0.0):
    """One step of: 'simultaneous' (26.54-26.55), 'alternating' (26.52-26.53), or 'rk4' (fourth-order Runge-Kutta
    of the ODE (26.56-26.57) with step h)."""
    if scheme == "simultaneous":
        a, b = dirac_field(phi, theta, lp, gamma)
        return phi + h * a, theta + h * b
    if scheme == "alternating":
        a, _ = dirac_field(phi, theta, lp, gamma)
        phi2 = phi + h * a
        _, b = dirac_field(phi2, theta, lp, gamma)
        return phi2, theta + h * b
    if scheme == "rk4":
        def f(u):
            return np.array(dirac_field(u[0], u[1], lp, gamma))
        u = np.array([phi, theta], float)
        k1 = f(u)
        k2 = f(u + 0.5 * h * k1)
        k3 = f(u + 0.5 * h * k2)
        k4 = f(u + h * k3)
        u = u + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
        return u[0], u[1]
    raise ValueError(scheme)


def dirac_run(scheme, h, steps, kind="logistic", gamma=0.0, start=(1.0, 1.0)):
    """Trajectory (steps + 1, 2) of (phi, theta) from start."""
    lp = dirac_l(kind)[1]
    out = np.zeros((steps + 1, 2))
    out[0] = start
    phi, theta = start
    for t in range(steps):
        phi, theta = dirac_step(scheme, phi, theta, h, lp, gamma)
        out[t + 1] = (phi, theta)
    return out


# =========================================================================== figures: Section 26.1 and 26.2
def fig_26_5(out=None):
    """Figure 26.5: saturating log(1 - D(G(z))) against non-saturating -log D(G(z)): losses and gradients."""
    d = np.linspace(0.001, 0.999, 999)
    fig, ax = panel_grid(1, 3, size=(3.1, 2.7))
    ax[0, 0].plot(d, np.log(1 - d), color=BLUE, label=r"$\log(1-D(G(z)))$")
    ax[0, 0].plot(d, -np.log(d), color=ORANGE, label=r"$-\log D(G(z))$")
    ax[0, 0].set_xlabel("$D(G(z))$")
    ax[0, 0].set_ylabel("generator loss")
    ax[0, 0].set_ylim(-5, 5)
    ax[0, 0].legend(loc="upper center", fontsize=7.5)
    panel_label(ax[0, 0], "a", "lower left")
    ax[0, 1].plot(d, -1 / (1 - d), color=BLUE, label=r"$\log(1-D)$")
    ax[0, 1].plot(d, -1 / d, color=ORANGE, label=r"$-\log D$")
    ax[0, 1].set_ylim(-200, 5)
    ax[0, 1].set_xlabel("$D(G(z))$")
    ax[0, 1].set_ylabel(r"gradient w.r.t. $D(G(z))$")
    ax[0, 1].legend(loc="lower center", fontsize=7.5)
    panel_label(ax[0, 1], "b", "upper left")
    a = np.linspace(-8, 8, 801)                  # the discriminator logit; D = sigmoid(a)
    sg = special.expit(a)
    ax[0, 2].plot(a, -sg, color=BLUE, label=r"saturating: $-\sigma(a)$")
    ax[0, 2].plot(a, -(1 - sg), color=ORANGE, label=r"non-saturating: $-(1-\sigma(a))$")
    ax[0, 2].set_xlabel("logit $a$ of $D(G(z))$")
    ax[0, 2].set_ylabel(r"gradient w.r.t. the logit $a$")
    ax[0, 2].legend(loc="center left", fontsize=7.5)
    panel_label(ax[0, 2], "c", "upper left")
    fig.tight_layout()
    return savefig(fig, CH, "fig26-5", out)


def _crit_panel(ax, p, q, f, title, labels=True, win=(-6, 7)):
    ax.fill_between(XG, 0, p, color=BLUE, alpha=0.35, label=r"$p^*$")
    ax.fill_between(XG, 0, q, color=GREEN, alpha=0.35, label=r"$q_\theta$")
    m = (XG >= win[0]) & (XG <= win[1])
    f = f - 0.5 * (f[m].max() + f[m].min())
    s = 0.45 * max(p.max(), q.max()) / np.abs(f[m]).max()
    ax.plot(XG, f * s, color=RED, lw=1.8, label=r"critic $f^*$ (rescaled)")
    ax.axhline(0, color=GREY, lw=0.6)
    ax.set_yticks([])
    ax.set_xlabel("$x$")
    ax.set_title(title)
    ax.set_ylim(-0.6 * max(p.max(), q.max()), 1.45 * max(p.max(), q.max()))
    if labels:
        ax.legend(fontsize=7.5, loc="upper left", ncol=3, columnspacing=0.8, handlelength=1.2)


def fig_26_3(out=None):
    """Figure 26.3: optimal critics of two integral probability metrics, for a data density and a model density with disjoint bulk."""
    p = mix_pdf(XG, [(1.0, -1.5, 0.7)])
    q = mix_pdf(XG, [(1.0, 2.5, 0.7)])
    fw = wasserstein_critic(p, q)
    fm, nm = mmd_witness(p, q, 1.0)
    fig, ax = panel_grid(1, 2, size=(4.0, 2.6))
    _crit_panel(ax[0, 0], p, q, fw, "(a) optimal Wasserstein critic")
    _crit_panel(ax[0, 1], p, q, fm, "(b) optimal MMD critic", labels=False)
    for a in ax[0]:
        a.set_xlim(-6, 7)
    fig.tight_layout()
    return savefig(fig, CH, "fig26-3", out)


def _uniform(x, a, b):
    return np.where((x >= a) & (x <= b), 1.0 / (b - a), 0.0)


def fig_26_4(out=None):
    """Figure 26.4: the KL divergence gives no learning signal for disjoint supports, a learned smooth critic does."""
    torch = _torch()
    nn = torch.nn
    cache = os.path.join(CACHE_DIR, "fig26-4.npz")
    th_list = (3.0, 1.8)
    if os.path.exists(cache):
        Z = np.load(cache)
    else:
        # critic t(x) trained on the KL bound E_p t - E_q exp(t - 1) with p = U(0,1), q = U(theta, theta+1) (theta = 1.8)
        g = torch.Generator().manual_seed(0)
        net = nn.Sequential(nn.Linear(1, 32), nn.Tanh(), nn.Linear(32, 32), nn.Tanh(), nn.Linear(32, 1))
        opt = torch.optim.Adam(net.parameters(), lr=2e-3)
        th = 1.8
        for it in range(3000):
            xp = torch.rand(256, 1, generator=g)
            xq = th + torch.rand(256, 1, generator=g)
            loss = -(net(xp).mean() - torch.exp(net(xq) - 1).mean())
            opt.zero_grad()
            loss.backward()
            opt.step()
        xs = torch.linspace(-1, 4, 501).unsqueeze(1).requires_grad_(True)
        tt = net(xs)
        gr, = torch.autograd.grad(tt.sum(), xs)
        # generator loop: move theta by gradient descent on the bound with a re-trained critic (3 critic steps per generator step)
        net2 = nn.Sequential(nn.Linear(1, 32), nn.Tanh(), nn.Linear(32, 32), nn.Tanh(), nn.Linear(32, 1))
        o2 = torch.optim.Adam(net2.parameters(), lr=2e-3)
        theta = torch.tensor(1.8, requires_grad=True)
        og = torch.optim.SGD([theta], lr=0.02)
        path = []
        for it in range(1500):
            for _ in range(3):
                xp = torch.rand(256, 1, generator=g)
                xq = (theta.detach() + torch.rand(256, 1, generator=g))
                lc = -(net2(xp).mean() - torch.exp(net2(xq) - 1).mean())
                o2.zero_grad()
                lc.backward()
                o2.step()
            xq = theta + torch.rand(256, 1, generator=g)
            lg = -torch.exp(net2(xq) - 1).mean()          # d/dtheta of the bound is -E_q exp(t-1) as q moves
            og.zero_grad()
            lg.backward()
            torch.nn.utils.clip_grad_norm_([theta], 1.0)
            og.step()
            path.append(theta.item())
        Z = dict(x=xs.detach().numpy()[:, 0], t=tt.detach().numpy()[:, 0], dt=gr.numpy()[:, 0], path=np.array(path))
        np.savez(cache, **Z)
    x = np.asarray(Z["x"])
    fig = plt.figure(figsize=(10.6, 2.9))
    gs = fig.add_gridspec(1, 3, width_ratios=[1, 1.05, 1])
    ax = fig.add_subplot(gs[0, 0])
    ax.fill_between([0, 1], 0, 1, color=BLUE, alpha=0.35)
    ax.text(0.5, 1.07, r"$p^*$", color=BLUE, ha="center")
    for th, c in zip(th_list, (GREEN, RED)):
        ax.fill_between([th, th + 1], 0, 1, color=c, alpha=0.35)
        ax.text(th + 0.5, 1.07, r"$q_{\theta_%d}$" % (1 + th_list.index(th)), color=c, ha="center")
    wvals = list(th_list)   # W1(U(0,1), U(theta, theta+1)) = theta exactly
    ax.set_xlim(-0.5, 4.5)
    ax.set_ylim(0, 2.3)
    ax.set_yticks([])
    ax.set_xlabel("$x$")
    ax.text(0.97, 0.8, "KL$(p^*\\|q_{\\theta_1})$ = KL$(p^*\\|q_{\\theta_2})$ = $\\infty$\nJSD = log 2 for both\n$W_1$ = %.1f and %.1f" % (wvals[0], wvals[1]),
            transform=ax.transAxes, ha="right", va="center", fontsize=8, bbox=dict(boxstyle="round", fc="white", ec=GREY))
    panel_label(ax, "a", "upper left")
    ax2 = fig.add_subplot(gs[0, 1])
    ax2.fill_between([0, 1], 0, 0.9, color=BLUE, alpha=0.25)
    ax2.fill_between([1.8, 2.8], 0, 0.9, color=GREEN, alpha=0.25)
    tt = np.asarray(Z["t"])
    ax2.plot(x, (tt - tt.min()) / (tt.max() - tt.min()) * 0.9, color=GREY, label=r"critic $t_\phi(x)$ (rescaled)")
    dt = np.asarray(Z["dt"])
    ax2.plot(x, dt / np.abs(dt).max() * 0.45 + 0.45, color=ORANGE, label=r"$\nabla_x t_\phi$ (rescaled)")
    ax2.set_xlim(-1, 4)
    ax2.set_yticks([])
    ax2.set_xlabel("$x$")
    ax2.legend(fontsize=7.5, loc="upper right")
    panel_label(ax2, "b", "upper left")
    ax3 = fig.add_subplot(gs[0, 2])
    path = np.asarray(Z["path"])
    ax3.plot(np.arange(1, len(path) + 1), path, color=RED)
    ax3.axhline(0, color=BLUE, ls="--")
    ax3.set_xlabel("generator step")
    ax3.set_ylabel(r"model location $\theta$")
    ax3.text(0.97, 0.9, r"data at $\theta=0$", color=BLUE, ha="right", transform=ax3.transAxes, fontsize=8)
    panel_label(ax3, "c", "lower left")
    fig.tight_layout()
    return savefig(fig, CH, "fig26-4", out)


# =========================================================================== GAN training on 16 Gaussians (Section 26.3)
GRID_C = np.array([[a, b] for a in (-3.0, -1.0, 1.0, 3.0) for b in (-3.0, -1.0, 1.0, 3.0)])   # the 16 modes
SIGMA = 0.05                                                                                   # their standard deviation
GOOD_R = 4 * SIGMA                                                                              # "close to a mode"
SNAP_ITERS = (0, 500, 1000, 2000, 3000, 4000, 6000, 8000, 10000, 12000, 14000)

# the configurations compared (Figure x26.c and the browser demonstration); key -> (label, settings)
GAN_CONFIGS = {
    "ns": ("non-saturating GAN (the baseline)", dict(loss="ns", batch=64, K=1, lr=1e-3, b1=0.5)),
    "sat": ("saturating GAN", dict(loss="sat", batch=64, K=1, lr=1e-3, b1=0.5)),
    "mom": ("non-saturating, Adam momentum 0.9", dict(loss="ns", batch=64, K=1, lr=1e-3, b1=0.9)),
    "big": ("non-saturating, batch 512", dict(loss="ns", batch=512, K=1, lr=1e-3, b1=0.5)),
    "r1": ("non-saturating + gradient penalty on real data (R1, gamma = 10)", dict(loss="ns", batch=64, K=1, lr=1e-3, b1=0.5, r1=10.0)),
    "wgp": ("Wasserstein GAN with gradient penalty (5 critic steps, penalty weight 1)", dict(loss="wgan", batch=64, K=5, lr=1e-3, b1=0.5, wgp=1.0, steps=14000)),
    "mmd": ("MMD generator (no discriminator)", dict(loss="mmd", batch=256, K=0, lr=1e-3, b1=0.5)),
}


def sample_real(n, gen, torch):
    k = torch.randint(0, 16, (n,), generator=gen)
    C = torch.as_tensor(GRID_C, dtype=torch.float32)
    return C[k] + SIGMA * torch.randn(n, 2, generator=gen)


def mode_stats(X):
    """(number of modes covered, fraction of high-quality samples): a sample is high quality if it is within 4 sigma of
    some mode; a mode is covered if at least half of its fair share (1/32 of all samples) is high quality and assigned to it."""
    X = np.asarray(X, float)
    d = np.sqrt(((X[:, None, :] - GRID_C[None]) ** 2).sum(-1))
    a = d.argmin(1)
    good = d.min(1) < GOOD_R
    cnt = np.bincount(a[good], minlength=16) / len(X)
    return int((cnt >= 1.0 / 32).sum()), float(good.mean())


def make_mlp(nn, i, o, h):
    return nn.Sequential(nn.Linear(i, h), nn.ReLU(), nn.Linear(h, h), nn.ReLU(), nn.Linear(h, o))


def rbf_mix_mmd2(torch, X, Y, bws=(0.1, 0.3, 1.0, 3.0)):
    """Biased squared MMD with a sum of RBF kernels."""
    def k(a, b):
        d2 = torch.cdist(a, b) ** 2
        return sum(torch.exp(-d2 / (2 * s ** 2)) for s in bws)
    return k(X, X).mean() + k(Y, Y).mean() - 2 * k(X, Y).mean()


def train_gan(key, steps=14000, seed=0, hidden=32, zdim=2, snap_iters=SNAP_ITERS, eval_every=250, n_eval=2000, verbose=True):
    """Algorithm 26.2 on the mixture of 16 Gaussians. Returns a dict with snapshots of samples, mode statistics
    and the two losses over training."""
    torch = _torch()
    nn, F = torch.nn, torch.nn.functional
    label, cfg = GAN_CONFIGS[key]
    steps = cfg.get("steps", steps)
    gen = torch.Generator().manual_seed(seed)
    torch.manual_seed(seed)
    G = make_mlp(nn, zdim, 2, hidden)
    D = make_mlp(nn, 2, 1, hidden)
    betas = (cfg["b1"], 0.9 if cfg["loss"] == "wgan" else 0.999)
    oG = torch.optim.Adam(G.parameters(), lr=cfg["lr"], betas=betas)
    oD = torch.optim.Adam(D.parameters(), lr=cfg["lr"], betas=betas)
    zeval = torch.randn(n_eval, zdim, generator=torch.Generator().manual_seed(999))
    B = cfg["batch"]
    snaps, curve, ld_hist, lg_hist = {}, [], [], []
    t0 = time.time()

    def record(it):
        with torch.no_grad():
            X = G(zeval).numpy()
        m, q = mode_stats(X)
        curve.append((it, m, q))
        if it in snap_iters:
            snaps[it] = X.round(3)

    record(0)
    ld = lg = float("nan")
    for it in range(1, steps + 1):
        for _ in range(cfg["K"]):
            x = sample_real(B, gen, torch)
            z = torch.randn(B, zdim, generator=gen)
            with torch.no_grad():
                f = G(z)
            if cfg["loss"] == "wgan":
                a = torch.rand(B, 1, generator=gen)
                xh = (a * x + (1 - a) * f).requires_grad_(True)
                gr, = torch.autograd.grad(D(xh).sum(), xh, create_graph=True)
                L = D(f).mean() - D(x).mean() + cfg["wgp"] * ((gr.norm(dim=1) - 1) ** 2).mean()
            else:
                xr = x.clone().requires_grad_(cfg.get("r1", 0) > 0)
                dr = D(xr)
                L = F.softplus(-dr).mean() + F.softplus(D(f)).mean()
                if cfg.get("r1", 0) > 0:
                    gr, = torch.autograd.grad(dr.sum(), xr, create_graph=True)
                    L = L + 0.5 * cfg["r1"] * (gr ** 2).sum(1).mean()
            oD.zero_grad()
            L.backward()
            oD.step()
            ld = float(L)
        z = torch.randn(B, zdim, generator=gen)
        if cfg["loss"] == "mmd":
            LG = rbf_mix_mmd2(torch, G(z), sample_real(B, gen, torch))
        elif cfg["loss"] == "wgan":
            LG = -D(G(z)).mean()
        elif cfg["loss"] == "sat":
            LG = -F.softplus(D(G(z))).mean()          # log(1 - D(G(z))) = -softplus(logit)
        else:
            LG = F.softplus(-D(G(z))).mean()          # -log D(G(z))
        oG.zero_grad()
        LG.backward()
        oG.step()
        lg = float(LG)
        if it % 50 == 0:
            ld_hist.append(ld)
            lg_hist.append(lg)
        if it % eval_every == 0 or it in snap_iters:
            record(it)
            if verbose and it % 2000 == 0:
                print("  %s it %5d  modes %2d  quality %.3f  (%.0f s)" % (key, it, curve[-1][1], curve[-1][2], time.time() - t0), flush=True)
    return dict(key=key, label=label, cfg=cfg, snaps=snaps, curve=np.array(curve), ld=np.array(ld_hist), lg=np.array(lg_hist))


def gan_results(keys=None, force=False):
    """Train (or load from the cache) every configuration of GAN_CONFIGS."""
    import pickle
    out = {}
    for k in (keys or GAN_CONFIGS):
        path = os.path.join(CACHE_DIR, "gan_%s.pkl" % k)
        if os.path.exists(path) and not force:
            out[k] = pickle.load(open(path, "rb"))
        else:
            r = train_gan(k)
            pickle.dump(r, open(path, "wb"))
            out[k] = r
    return out


def _density_panel(ax, X, lim=4.6, bins=64):
    H, xe, ye = np.histogram2d(X[:, 0], X[:, 1], bins=bins, range=[[-lim, lim], [-lim, lim]])
    ax.imshow(H.T, origin="lower", extent=[-lim, lim, -lim, lim], cmap="Blues", vmin=0, vmax=max(1, H.max() * (0.6 if bins >= 60 else 0.3)))
    ax.set_xticks([])
    ax.set_yticks([])


def fig_26_6(out=None):
    """Figure 26.6: a GAN trained on a mixture of 16 Gaussians: samples after 2000 to 14000 iterations."""
    r = gan_results(["ns"])["ns"]
    fig, ax = panel_grid(2, 3, size=(2.6, 2.6))
    torch = _torch()
    gen = torch.Generator().manual_seed(1)
    _density_panel(ax[0, 0], sample_real(4000, gen, torch).numpy())
    ax[0, 0].set_title("(a) data")
    for a, it, lab in zip(ax.flat[1:], (2000, 4000, 8000, 12000, 14000), "bcdef"):
        s = r["snaps"][it]
        _density_panel(a, s)
        c = r["curve"][r["curve"][:, 0] == it][0]
        a.set_title("(%s) %d iterations: %d mode%s" % (lab, it, c[1], "" if c[1] == 1 else "s"), fontsize=9.5)
    fig.tight_layout()
    return savefig(fig, CH, "fig26-6", out)


def fig_26_1(out=None):
    """Figure 26.1: a prescribed model (a density) against an implicit model (a simulator), fitted to the same data."""
    from sklearn.mixture import GaussianMixture
    torch = _torch()
    nn = torch.nn
    rng = np.random.default_rng(3)
    n = 600
    k = rng.random(n) < 0.4
    data = np.where(k, rng.normal(-2, 0.5, n), rng.normal(1.5, 0.7, n))
    gm = GaussianMixture(2, random_state=0).fit(data[:, None])
    x = np.linspace(-5, 5, 500)
    dens = np.exp(gm.score_samples(x[:, None]))
    cache = os.path.join(CACHE_DIR, "fig26-1.npz")
    if os.path.exists(cache):
        Z = np.load(cache)["samples"]
    else:
        # implicit model: x = G(z), z ~ N(0,1), trained by minimising the squared MMD (Section 26.2.4) on the data
        torch.manual_seed(0)
        G = make_mlp(nn, 1, 1, 32)
        opt = torch.optim.Adam(G.parameters(), lr=3e-3)
        gen = torch.Generator().manual_seed(0)
        dt = torch.tensor(data[:, None], dtype=torch.float32)
        for it in range(2500):
            idx = torch.randint(0, n, (256,), generator=gen)
            loss = rbf_mix_mmd2(torch, G(torch.randn(256, 1, generator=gen)), dt[idx], bws=(0.2, 0.5, 1.0, 2.0))
            opt.zero_grad()
            loss.backward()
            opt.step()
        with torch.no_grad():
            Z = G(torch.randn(5000, 1, generator=gen)).numpy()[:, 0]
        np.savez(cache, samples=Z)
    fig, ax = panel_grid(1, 2, size=(4.2, 2.5))
    ax[0, 0].hist(data, bins=40, range=(-5, 5), density=True, color=BLUE, alpha=0.35, label="data")
    ax[0, 0].plot(x, dens, color=RED, label="model density $q_\\theta(x)$")
    ax[0, 0].set_title("(a) prescribed (explicit) model")
    ax[0, 1].hist(data, bins=40, range=(-5, 5), density=True, color=BLUE, alpha=0.35, label="data")
    ax[0, 1].hist(Z, bins=40, range=(-5, 5), density=True, histtype="step", color=GREEN, lw=1.4, label="samples of $G_\\theta(z)$")
    ax[0, 1].set_title("(b) implicit model")
    for a in ax[0]:
        a.set_xlabel("$x$")
        a.set_yticks([])
        a.legend(fontsize=7.5, loc="upper left")
        a.set_ylim(0, 0.75)
    fig.tight_layout()
    return savefig(fig, CH, "fig26-1", out)


def fig_26_7(out=None):
    """Figure 26.7: DiracGAN; the problem (a) and alternating gradient descent (b), which circles the equilibrium."""
    tr = dirac_run("alternating", 0.2, 400, "logistic", 0.0, (1.0, 1.0))
    fig = plt.figure(figsize=(7.4, 3.0))
    gs = fig.add_gridspec(1, 2, width_ratios=[1.05, 1])
    ax = fig.add_subplot(gs[0, 0])
    xs = np.linspace(-2, 2, 50)
    for ph, al in ((-0.6, 0.35), (0.6, 0.35), (1.2, 0.35)):
        ax.plot(xs, ph * xs, color=ORANGE, alpha=0.8, lw=1.2)
    ax.text(-1.95, 1.75, r"$D_\phi(x)=\phi x$", color=ORANGE, fontsize=9)
    ax.plot([0, 0], [0, 1.6], color=BLUE, lw=3)
    ax.text(0.05, 1.7, r"$p^*=\delta_0$", color=BLUE, fontsize=9)
    th = tr[0, 1]
    ax.plot([th, th], [0, 1.6], color=GREEN, lw=3)
    ax.text(th + 0.05, 1.7, r"$q_\theta=\delta_\theta$", color=GREEN, fontsize=9)
    ax.set_xlim(-2, 2)
    ax.set_ylim(-2.2, 2.2)
    ax.set_xlabel("$x$")
    ax.set_title("(a) the DiracGAN problem")
    ax = fig.add_subplot(gs[0, 1])
    ax.plot(tr[:, 0], tr[:, 1], ".", color=BLUE, ms=2.5)
    ax.plot(tr[0, 0], tr[0, 1], "o", color=GREEN, ms=5)
    ax.plot([0], [0], "x", color=RED, ms=8, label="equilibrium (0, 0)")
    ax.set_xlabel(r"$\phi$")
    ax.set_ylabel(r"$\theta$")
    ax.set_aspect("equal")
    ax.legend(fontsize=7.5, loc="lower left")
    ax.set_title("(b) alternating gradient descent")
    fig.tight_layout()
    return savefig(fig, CH, "fig26-7", out)


def fig_26_8(out=None):
    """Figure 26.8: continuous dynamics (conserved radius) against simultaneous gradient descent (spiralling out), logistic l."""
    h, T = 0.3, 150
    sim = dirac_run("simultaneous", h, T)
    cont = dirac_run("rk4", 0.01, int(round(h * T / 0.01)))
    fig, ax = panel_grid(1, 2, size=(3.6, 3.3))
    for a, tr, ttl, lab in ((ax[0, 0], cont, "(a) continuous dynamics", "continuous (RK4)"), (ax[0, 1], sim, "(b) simultaneous gradient descent", "simultaneous, $h=%.1f$" % h)):
        a.plot(tr[:, 0], tr[:, 1], ".", ms=2.2, color=BLUE, label=lab)
        a.plot(tr[0, 0], tr[0, 1], "o", color=GREEN, ms=5)
        a.plot([0], [0], "x", color=RED, ms=8)
        a.set_aspect("equal")
        a.set_xlabel(r"$\phi$")
        a.set_ylabel(r"$\theta$")
        a.set_title(ttl, fontsize=9.5)
        a.set_xlim(-3.2, 3.2)
        a.set_ylim(-3.2, 3.2)
    fig.tight_layout()
    return savefig(fig, CH, "fig26-8", out)


# =========================================================================== own figures
def fig_x26_a(out=None):
    """Own Figure x26.a: divergences and distances between N(0, s^2) and N(delta, s^2) as the model moves away from the data."""
    s = 0.5
    deltas = np.linspace(0, 6, 61)
    rows = []
    K = rbf_matrix(XG, 1.0)
    for d in deltas:
        p, q = npdf(XG, 0, s), npdf(XG, d, s)
        rows.append([kl(p, q), jsd(p, q), tv(p, q), w1(p, q), mmd2(p, q, 1.0, K=K)])
    R = np.array(rows)
    fig, ax = panel_grid(1, 3, size=(3.2, 2.7))
    ax[0, 0].plot(deltas, np.minimum(R[:, 0], 12), color=BLUE, label="KL$(p^*\\|q_\\theta)$")
    ax[0, 0].plot(deltas, R[:, 1], color=RED, label="JSD")
    ax[0, 0].axhline(LOG2, color=RED, ls=":", lw=1)
    ax[0, 0].text(0.2, LOG2 + 0.3, "log 2", color=RED, fontsize=8)
    ax[0, 0].set_ylim(0, 6)
    ax[0, 0].set_xlabel(r"shift $\delta$")
    ax[0, 0].set_title("(a) ratio-based", fontsize=9.5)
    ax[0, 0].legend(fontsize=7.5, loc="upper left")
    ax[0, 1].plot(deltas, R[:, 2], color=PURPLE, label="total variation")
    ax[0, 1].set_xlabel(r"shift $\delta$")
    ax[0, 1].set_title("(b) total variation", fontsize=9.5)
    ax[0, 1].axhline(1, color=PURPLE, ls=":", lw=1)
    ax[0, 2].plot(deltas, R[:, 3], color=GREEN, label="$W_1$")
    ax[0, 2].plot(deltas, R[:, 4], color=ORANGE, label="MMD$^2$ ($\\ell=1$)")
    ax[0, 2].set_xlabel(r"shift $\delta$")
    ax[0, 2].set_title("(c) difference-based", fontsize=9.5)
    ax[0, 2].legend(fontsize=7.5, loc="upper left")
    fig.tight_layout()
    return savefig(fig, CH, "figx26-a", out)


def dirac_radius_curve(scheme, gamma, h, steps=300, kind="logistic"):
    tr = dirac_run(scheme, h, steps, kind, gamma, (1.0, 1.0))
    return np.hypot(tr[:, 0], tr[:, 1])


def lin_map_radius(scheme, h, c, gamma=0.0):
    """Spectral radius of the linearised DiracGAN update map at the equilibrium (c = l'(0))."""
    J = np.array([[-gamma, c], [-c, 0.0]])
    if scheme == "simultaneous":
        M = np.eye(2) + h * J
    elif scheme == "rk4":
        hJ = h * J
        M = np.eye(2) + hJ + hJ @ hJ / 2 + hJ @ hJ @ hJ / 6 + hJ @ hJ @ hJ @ hJ / 24
    else:
        # phi' = phi + h(c theta - gamma phi);  theta' = theta - h c phi'
        A1 = np.array([[1 - h * gamma, h * c], [0.0, 1.0]])
        A2 = np.array([[1.0, 0.0], [-h * c, 1.0]])
        M = A2 @ A1
    return float(np.abs(np.linalg.eigvals(M)).max())


def fig_x26_b(out=None):
    """Own Figure x26.b: a gradient penalty on the discriminator makes DiracGAN converge; spectral radius of the update maps."""
    c = 0.5
    hs = np.linspace(0.02, 5.0, 250)
    fig, ax = panel_grid(1, 3, size=(3.3, 2.8))
    for sch, gam, col, ls in (("simultaneous", 0.0, RED, "-"), ("simultaneous", 0.5, RED, "--"), ("alternating", 0.0, BLUE, "-"), ("alternating", 0.5, BLUE, "--")):
        ax[0, 0].plot(hs, [lin_map_radius(sch, h, c, gam) for h in hs], color=col, ls=ls, label="%s, $\\gamma=%.1f$" % (sch, gam))
    ax[0, 0].axhline(1, color=GREY, lw=0.8)
    ax[0, 0].set_ylim(0.4, 2.0)
    ax[0, 0].set_xlabel("step size $h$")
    ax[0, 0].set_ylabel("spectral radius of the map")
    ax[0, 0].legend(fontsize=6.8, loc="upper left")
    ax[0, 0].set_title("(a) linearised update", fontsize=9.5)
    t = np.arange(301)
    for sch, gam, col, ls in (("simultaneous", 0.0, RED, "-"), ("simultaneous", 0.5, RED, "--"), ("alternating", 0.0, BLUE, "-"), ("alternating", 0.5, BLUE, "--")):
        ax[0, 1].semilogy(t, dirac_radius_curve(sch, gam, 0.2), color=col, ls=ls)
    ax[0, 1].set_xlabel("iteration")
    ax[0, 1].set_ylabel(r"$\sqrt{\theta^2+\phi^2}$")
    ax[0, 1].set_title("(b) logistic $l$, $h=0.2$", fontsize=9.5)
    # (c) trajectory with penalty (continuous time, RK4) and exact radial decay rate
    tr = dirac_run("rk4", 0.02, 1500, "logistic", 0.5, (1.0, 1.0))
    ax[0, 2].plot(tr[:, 0], tr[:, 1], color=GREEN)
    ax[0, 2].plot(tr[0, 0], tr[0, 1], "o", color=GREEN, ms=4)
    ax[0, 2].plot([0], [0], "x", color=RED, ms=8)
    ax[0, 2].set_aspect("equal")
    ax[0, 2].set_xlabel(r"$\phi$")
    ax[0, 2].set_ylabel(r"$\theta$")
    ax[0, 2].set_title(r"(c) continuous time, $\gamma=0.5$", fontsize=9.5)
    fig.tight_layout()
    return savefig(fig, CH, "figx26-b", out)


def fig_x26_c(out=None):
    """Own Figure x26.c: mode coverage and sample quality over training for seven GAN variants on the 16-Gaussian mixture."""
    R = gan_results()
    keys = list(GAN_CONFIGS)
    cols = [BLUE, ORANGE, GREEN, RED, PURPLE, CYAN, MAGENTA]
    fig = plt.figure(figsize=(10.5, 6.6))
    gs = fig.add_gridspec(2, 7, height_ratios=[1.0, 0.95], hspace=0.35, wspace=0.6)
    a1 = fig.add_subplot(gs[0, 0:3])
    a2 = fig.add_subplot(gs[0, 3:6])
    for k, c in zip(keys, cols):
        cv = R[k]["curve"]
        a1.plot(cv[:, 0], cv[:, 1], color=c, lw=1.2, label=k)
        a2.plot(cv[:, 0], cv[:, 2], color=c, lw=1.2)
    a1.set_xlabel("generator iteration")
    a1.set_ylabel("modes covered (of 16)")
    a1.set_yticks([0, 4, 8, 12, 16])
    a1.set_title("(a) modes covered", fontsize=9.5)
    a2.set_xlabel("generator iteration")
    a2.set_ylabel("fraction within $4\\sigma$ of a mode")
    a2.set_title("(b) sample quality", fontsize=9.5)
    la = fig.add_subplot(gs[0, 6])
    la.axis("off")
    h, l = a1.get_legend_handles_labels()
    la.legend(h, [GAN_CONFIGS[k][0].split(" (")[0].replace("non-saturating", "NS") for k in keys], fontsize=7, loc="center left", frameon=False)
    for j, k in enumerate(keys):
        a = fig.add_subplot(gs[1, j])
        _density_panel(a, R[k]["snaps"][14000], bins=40)
        c_ = R[k]["curve"][R[k]["curve"][:, 0] == 14000][0]
        a.set_title("%s\n%d modes" % (k, c_[1]), fontsize=8.5, color=cols[j])
    return savefig(fig, CH, "figx26-c", out)


# ---- conditional GAN (Section 26.4)
CG_CENTERS = np.array([[-2.0, -2.0], [2.0, -2.0], [-2.0, 2.0], [2.0, 2.0]])
CG_SD = 0.2


def train_cgan(cond_D, steps=5000, seed=0, hidden=64):
    """Conditional GAN on four Gaussian classes; the generator always receives the label y, the discriminator receives it
    (cond_D=True, the objective (26.61)) or not (cond_D=False, the unconditional game (26.20))."""
    torch = _torch()
    nn, F = torch.nn, torch.nn.functional
    torch.manual_seed(seed)
    gen = torch.Generator().manual_seed(seed)
    zd, B = 2, 128
    G = make_mlp(nn, zd + 4, 2, hidden)
    D = make_mlp(nn, 2 + (4 if cond_D else 0), 1, hidden)
    oG = torch.optim.Adam(G.parameters(), lr=1e-3, betas=(0.5, 0.999))
    oD = torch.optim.Adam(D.parameters(), lr=1e-3, betas=(0.5, 0.999))
    C = torch.as_tensor(CG_CENTERS, dtype=torch.float32)
    eye = torch.eye(4)
    for it in range(steps):
        y = torch.randint(0, 4, (B,), generator=gen)
        x = C[y] + CG_SD * torch.randn(B, 2, generator=gen)
        z = torch.randn(B, zd, generator=gen)
        yf = torch.randint(0, 4, (B,), generator=gen)
        with torch.no_grad():
            f = G(torch.cat([z, eye[yf]], 1))
        din = lambda a, lab: torch.cat([a, eye[lab]], 1) if cond_D else a    # noqa: E731
        L = F.softplus(-D(din(x, y))).mean() + F.softplus(D(din(f, yf))).mean()
        oD.zero_grad()
        L.backward()
        oD.step()
        z = torch.randn(B, zd, generator=gen)
        yf = torch.randint(0, 4, (B,), generator=gen)
        LG = F.softplus(-D(din(G(torch.cat([z, eye[yf]], 1)), yf))).mean()
        oG.zero_grad()
        LG.backward()
        oG.step()
    with torch.no_grad():
        ge = torch.Generator().manual_seed(77)
        y = torch.arange(4).repeat_interleave(500)
        X = G(torch.cat([torch.randn(2000, zd, generator=ge), eye[y]], 1)).numpy()
    return X, y.numpy()


def cgan_stats(X, y):
    d = np.sqrt(((X[:, None, :] - CG_CENTERS[None]) ** 2).sum(-1))
    acc = float((d.argmin(1) == y).mean())
    qual = float((d.min(1) < 4 * CG_SD).mean())
    return acc, qual


def fig_x26_d(out=None):
    """Own Figure x26.d: a conditional GAN needs a discriminator that sees the label."""
    import pickle
    path = os.path.join(CACHE_DIR, "cgan.pkl")
    if os.path.exists(path):
        R = pickle.load(open(path, "rb"))
    else:
        R = {c: train_cgan(c) for c in (False, True)}
        pickle.dump(R, open(path, "wb"))
    fig, ax = panel_grid(1, 2, size=(3.4, 3.2))
    cols = [BLUE, ORANGE, GREEN, RED]
    for a, c, ttl in ((ax[0, 0], False, "(a) $D_\\phi(x)$ ignores the label"), (ax[0, 1], True, "(b) $D_\\phi(x, y)$ sees the label")):
        X, y = R[c]
        acc, qual = cgan_stats(X, y)
        for k in range(4):
            a.plot(X[y == k, 0], X[y == k, 1], ".", ms=2.2, color=cols[k], alpha=0.6)
        for k, ctr in enumerate(CG_CENTERS):
            a.plot(*ctr, "k+", ms=9)
            a.text(ctr[0] + 0.25, ctr[1] + 0.25, "y=%d" % k, color=cols[k], fontsize=8, weight="bold")
        a.set_title(ttl, fontsize=9.5)
        a.text(0.5, -0.17, "label accuracy %.2f, quality %.2f" % (acc, qual), transform=a.transAxes, ha="center", fontsize=8.5)
        a.set_xlim(-3.6, 3.6)
        a.set_ylim(-3.6, 3.6)
        a.set_aspect("equal")
    fig.tight_layout()
    return savefig(fig, CH, "figx26-d", out)


# ---- score-function versus reparameterisation gradients (Sections 26.3.2, 26.7.4)
def grad_estimators(f, fprime, theta, n=400000, seed=0):
    """Draws of the two unbiased estimators of d/dtheta E_{x ~ N(theta,1)} f(x), single sample each."""
    rng = np.random.default_rng(seed)
    e = rng.standard_normal(n)
    x = theta + e
    rep = fprime(x)                       # reparameterisation: f'(theta + e)
    score = f(x) * e                      # score function: f(x) * d/dtheta log N(x | theta, 1) = f(x) (x - theta)
    return rep, score


def fig_x26_e(out=None):
    """Own Figure x26.e: standard deviation of the two gradient estimators of d/dtheta E[f(x)], x ~ N(theta, 1)."""
    ths = np.linspace(0, 4, 41)
    fns = {"$f(x)=x^2$": (lambda x: x ** 2, lambda x: 2 * x), "$f(x)=\\tanh(2x)$": (lambda x: np.tanh(2 * x), lambda x: 2 / np.cosh(2 * x) ** 2)}
    fig, ax = panel_grid(1, 2, size=(3.8, 2.8))
    for a, (nm, (f, fp)) in zip(ax[0], fns.items()):
        sd_rep, sd_sc = [], []
        for th in ths:
            r, s = grad_estimators(f, fp, th, n=100000)
            sd_rep.append(r.std())
            sd_sc.append(s.std())
        a.semilogy(ths, sd_sc, color=RED, label="score function")
        a.semilogy(ths, np.maximum(sd_rep, 1e-3), color=BLUE, label="reparameterisation")
        a.set_xlabel(r"$\theta$")
        a.set_ylabel("standard deviation of one draw")
        a.set_title(nm, fontsize=9.5)
        a.legend(fontsize=7.5, loc="best")
    fig.tight_layout()
    return savefig(fig, CH, "figx26-e", out)


def fig_x26_f(out=None):
    """Own Figure x26.f: the f-divergence lower bound (26.25) with critics of increasing richness (polynomials in x), for p* = N(0,1), q = N(1.5, 1.5^2)."""
    p, q = npdf(XG, 0, 1.0), npdf(XG, 1.5, 1.5)
    names = ["KL", "reverse KL", "JSD", "Pearson"]
    true = {"KL": kl(p, q), "reverse KL": kl(q, p), "JSD": fdiv(p, q, "JSD"), "Pearson": chi2(p, q)}
    degs = (0, 1, 2, 3, 4)
    fig, ax = panel_grid(1, 4, size=(2.6, 2.5))
    for a, nm in zip(ax[0], names):
        vals = []
        for d in degs:
            B_ = poly_basis(d) / np.maximum(1, np.abs(poly_basis(d)).max(1, keepdims=True)) * 1.0
            b, w, t = fgan_bound(p, q, nm, B_)
            vals.append(b)
        a.plot(degs, vals, "o-", color=BLUE)
        a.axhline(true[nm], color=RED, ls="--", label="true value")
        a.set_title(nm, fontsize=9.5)
        a.set_xlabel("degree of the polynomial critic")
        a.set_xticks(degs)
    ax[0, 0].legend(fontsize=7.5, loc="lower right")
    ax[0, 0].set_ylabel("lower bound")
    fig.tight_layout()
    return savefig(fig, CH, "figx26-f", out)


# =========================================================================== export for the browser demonstration
def export_json():
    """data/ch26-modes.json: snapshots (400 points) and training curves for the seven GAN configurations."""
    import json
    R = gan_results()
    out = {"centers": GRID_C.tolist(), "sd": SIGMA, "iters": list(SNAP_ITERS), "configs": {}}
    for k, r in R.items():
        out["configs"][k] = dict(
            label=r["label"],
            snaps=[np.round(np.asarray(r["snaps"][it][:400], dtype=float), 2).tolist() for it in SNAP_ITERS],
            curve_it=r["curve"][:, 0].astype(int).tolist(),
            modes=r["curve"][:, 1].astype(int).tolist(),
            quality=[round(v, 4) for v in r["curve"][:, 2]],
            ld=[] if r["cfg"]["K"] == 0 else [round(float(v), 3) for v in r["ld"][::5]],
            lg=[round(float(v), 3) for v in r["lg"][::5]],
        )
    path = os.path.join(DATA_OUT, "ch26-modes.json")
    os.makedirs(DATA_OUT, exist_ok=True)
    with open(path, "w") as f:
        json.dump(out, f, separators=(",", ":"))
    print("wrote", os.path.relpath(path, os.getcwd()), "%.0f kB" % (os.path.getsize(path) / 1024))


FIGURES = {
    "26.1": fig_26_1, "26.3": fig_26_3, "26.4": fig_26_4, "26.5": fig_26_5, "26.6": fig_26_6, "26.7": fig_26_7, "26.8": fig_26_8,
    "x26.a": fig_x26_a, "x26.b": fig_x26_b, "x26.c": fig_x26_c, "x26.d": fig_x26_d, "x26.e": fig_x26_e, "x26.f": fig_x26_f,
}

if __name__ == "__main__":
    common.run(FIGURES, CH)
    if "--fig" not in sys.argv:
        export_json()
