"""Chapter 4 of Wasserman (2006), All of Nonparametric Statistics: Smoothing, General Concepts.

Reproduces every figure of the chapter that is computed from data or a model:

* Figure 4.1  histograms of the galaxy redshifts and the cross-validation score against the number of bins;
* Figure 4.3  kernel density estimates of the redshifts and the least-squares cross-validation score;
* Figure 4.5  the first 400 CMB (WMAP) power-spectrum points and three local linear fits;
* Figure 4.6  regressograms of the LIDAR data for four bin widths;
* Figure 4.7  the BPD data: logistic regression, local likelihood and local linear regression;
* Figure 4.8  the additive model for the rock data, fitted by backfitting;
* Figure 4.9  the bias-variance trade-off, here computed exactly for the estimator (4.17) of Example 4.16
               (the book draws a schematic; the curves below are the exact binomial bias and variance);
* Figure 4.10 the boxcar, Gaussian, Epanechnikov and tricube kernels.

Skipped figures:

* Figure 4.2 is a schematic drawing of a pencil-beam survey (nothing computed);
* Figure 4.4 is the WMAP satellite temperature map of the sky, an image produced by the WMAP team and not
  something the book computes from its data files.

The methods the chapter is about (histogram and kernel density cross-validation, regressograms, local linear
regression with its leave-one-out shortcut, local likelihood, logistic regression by Newton's method and
backfitting) are implemented here from scratch.

Usage: python ch04_smoothing_concepts.py [--fig 4.1 4.3 ...]
"""
import json
import os

import numpy as np
from scipy.stats import norm

import common
from common import BLUE, RED, GREEN, GREY, ORANGE, nps_file, savefig, run

CH = 4
HERE = os.path.dirname(os.path.abspath(__file__))
DATA_OUT = os.path.normpath(os.path.join(HERE, "..", "data"))


# =========================================================================== data
def galaxy():
    """Redshifts of the galaxies with redshift below 0.2 (n = 1266, as in Example 4.3)."""
    z = np.loadtxt(nps_file("galaxy.dat"))[:, 2]
    return np.sort(z[z < 0.2])


def cmb400():
    """Multipole and power of the first 400 WMAP power-spectrum points (Example 4.4)."""
    w = np.loadtxt(nps_file("wmap.dat"), skiprows=1)
    return w[:400, 0], w[:400, 1]


def lidar():
    d = np.loadtxt(nps_file("lidar.dat"), skiprows=1)
    return d[:, 0], d[:, 1]


def bpd():
    d = np.loadtxt(nps_file("bpd.dat"), skiprows=1)
    return d[:, 0], d[:, 1]


def rock():
    """Columns area, peri, shape, perm (48 rock samples)."""
    return np.loadtxt(nps_file("rock.dat"), skiprows=1)


# =========================================================================== kernels (Section 4.2)
def k_boxcar(x):
    return 0.5 * (np.abs(x) <= 1)


def k_gauss(x):
    return np.exp(-0.5 * np.asarray(x) ** 2) / np.sqrt(2 * np.pi)


def k_epan(x):
    x = np.asarray(x)
    return 0.75 * (1 - x ** 2) * (np.abs(x) <= 1)


def k_tricube(x):
    x = np.asarray(x)
    return 70.0 / 81.0 * (1 - np.abs(x) ** 3) ** 3 * (np.abs(x) <= 1)


KERNELS = {"boxcar": k_boxcar, "Gaussian": k_gauss, "Epanechnikov": k_epan, "tricube": k_tricube}


# =========================================================================== histograms (Example 4.3)
def hist_counts(x, m, a=0.0, b=0.2):
    h = (b - a) / m
    j = np.clip(np.floor((x - a) / h).astype(int), 0, m - 1)
    return np.bincount(j, minlength=m), h


def hist_cv(x, m, a=0.0, b=0.2):
    """Leave-one-out cross-validation score of a histogram with m bins on [a, b]:
    J(h) = 2/((n-1)h) - (n+1)/((n-1)h) * sum_j p_j^2 (an unbiased estimate of the risk minus int f^2)."""
    n = len(x)
    c, h = hist_counts(x, m, a, b)
    p = c / n
    return 2.0 / ((n - 1) * h) - (n + 1) / ((n - 1) * h) * np.sum(p ** 2)


def hist_cv_brute(x, m, a=0.0, b=0.2):
    """The same score from its definition int fhat^2 - (2/n) sum_i fhat_{(-i)}(X_i) (slow; for checking)."""
    n = len(x)
    c, h = hist_counts(x, m, a, b)
    int_f2 = np.sum((c / (n * h)) ** 2) * h
    j = np.clip(np.floor((x - a) / h).astype(int), 0, m - 1)
    loo = (c[j] - 1) / ((n - 1) * h)
    return int_f2 - 2.0 * loo.mean()


# =========================================================================== kernel density (Example 4.3)
def kde(x, grid, h):
    return k_gauss((grid[:, None] - x[None, :]) / h).mean(1) / h


def kde_lscv(x, h):
    """Least-squares cross-validation score for a Gaussian-kernel density estimate:
    int fhat^2 - (2/n) sum_i fhat_{(-i)}(X_i), with int fhat^2 in closed form."""
    n = len(x)
    d = x[:, None] - x[None, :]
    a = np.exp(-d ** 2 / (4 * h * h)).sum() / (n * n * 2 * h * np.sqrt(np.pi))
    k = np.exp(-d ** 2 / (2 * h * h)) / (h * np.sqrt(2 * np.pi))
    loo = (k.sum(1) - k.diagonal()) / (n - 1)
    return a - 2 * loo.mean()


def kde_hgrid():
    return np.linspace(0.0001, 0.01, 100)


# =========================================================================== linear smoothers
def local_linear_matrix(x, xe, h, kernel=k_gauss):
    """Smoothing matrix L (len(xe) x n) of the local linear estimator: rhat(xe) = L @ Y."""
    u = xe[:, None] - x[None, :]
    w = kernel(u / h)
    s1 = (w * u).sum(1, keepdims=True)
    s2 = (w * u * u).sum(1, keepdims=True)
    b = w * (s2 - u * s1)          # weights b_i(x) = K_i (S2 - (x - x_i) S1) up to the sign convention
    return b / b.sum(1, keepdims=True)


def loocv_linear(L, y):
    """Leave-one-out CV of a linear smoother by the shortcut mean(((Y - rhat)/(1 - L_ii))^2)."""
    r = L @ y
    return np.mean(((y - r) / (1 - np.diag(L))) ** 2)


def regressogram(x, y, m, a, b):
    """Bin averages on m equal bins of [a, b]; returns bin edges and the bin means (nan for empty bins)."""
    w = (b - a) / m
    edges = a + w * np.arange(m + 1)
    j = np.clip(np.floor((x - a) / w).astype(int), 0, m - 1)
    s = np.bincount(j, weights=y, minlength=m)
    c = np.bincount(j, minlength=m)
    with np.errstate(invalid="ignore", divide="ignore"):
        mean = s / c
    return edges, mean, c


def regressogram_loocv(x, y, m, a, b):
    """Leave-one-out CV for a regressogram: a point alone in its bin cannot be predicted and is skipped."""
    edges, mean, c = regressogram(x, y, m, a, b)
    j = np.clip(np.floor((x - a) / ((b - a) / m)).astype(int), 0, m - 1)
    ok = c[j] > 1
    res = (y - mean[j]) * c[j] / np.maximum(c[j] - 1, 1)   # Y_i minus the mean of the others in the bin
    return np.mean(res[ok] ** 2), int((~ok).sum())


# =========================================================================== logistic regression and local likelihood
def logistic_irls(X, y, w=None, iters=50):
    """Maximum likelihood (weighted, if w is given) logistic regression by Newton's method."""
    n, p = X.shape
    w = np.ones(n) if w is None else w
    beta = np.zeros(p)
    for _ in range(iters):
        eta = X @ beta
        mu = 1 / (1 + np.exp(-eta))
        g = X.T @ (w * (y - mu))
        H = (X * (w * mu * (1 - mu))[:, None]).T @ X
        step = np.linalg.solve(H, g)
        beta = beta + step
        if np.max(np.abs(step)) < 1e-12:
            break
    return beta


def local_logistic(x, y, xe, h):
    """Local linear likelihood: at each x0 maximise sum_i K((x_i - x0)/h) log-lik(a + b (x_i - x0))."""
    out = np.empty(len(xe))
    for k, x0 in enumerate(xe):
        w = k_gauss((x - x0) / h)
        X = np.column_stack([np.ones_like(x), (x - x0) / h])
        out[k] = 1 / (1 + np.exp(-logistic_irls(X, y, w)[0]))
    return out


BPD_H = 150.0     # bandwidth (grams) of the two nonparametric fits in Figure 4.7


# =========================================================================== additive model by backfitting
def backfit(X, y, h, iters=1000, tol=1e-10):
    """Backfitting for y = alpha + sum_j r_j(x_j) + eps with local linear smoothers (bandwidth h[j] for x_j).
    Each component is centred to mean zero. Returns alpha, the n x p matrix of fitted components and the
    smoothing matrices."""
    n, p = X.shape
    alpha = y.mean()
    S = [local_linear_matrix(X[:, j], X[:, j], h[j]) for j in range(p)]
    F = np.zeros((n, p))
    for _ in range(iters):
        old = F.copy()
        for j in range(p):
            partial = y - alpha - F.sum(1) + F[:, j]
            f = S[j] @ partial
            F[:, j] = f - f.mean()
        if np.max(np.abs(F - old)) < tol:
            break
    return alpha, F, S


ROCK_H = (0.5, 0.5, 0.5)   # bandwidths in standard-deviation units of each covariate


def rock_fit():
    R = rock()
    X = R[:, :3]
    y = np.log(R[:, 3])
    sd = X.std(0)
    alpha, F, S = backfit(X, y, [ROCK_H[j] * sd[j] for j in range(3)])
    return X, y, alpha, F


# =========================================================================== Example 4.16
def ex416_exact(h, n, f0=None, cdf=norm.cdf):
    """Exact bias, variance and mse of fhat(0) = (number of X_i in (-h/2, h/2)) / (n h)."""
    f0 = norm.pdf(0) if f0 is None else f0
    p = cdf(h / 2) - cdf(-h / 2)
    bias = p / h - f0
    var = p * (1 - p) / (n * h * h)
    return bias, var, bias ** 2 + var


def ex416_approx(h, n, f0=norm.pdf(0), f2=-norm.pdf(0)):
    """The approximations (4.19)-(4.21): bias f''(0)h^2/24, variance f(0)/(nh)."""
    A, B = f2 ** 2 / 576, f0
    return A * h ** 4, B / (n * h), A * h ** 4 + B / (n * h)


def ex416_hopt(n, f0=norm.pdf(0), f2=-norm.pdf(0)):
    A, B = f2 ** 2 / 576, f0
    return (B / (4 * A * n)) ** 0.2


# =========================================================================== Exercise 4.6 (d dimensions)
def ex46(d, n):
    """Cube estimator of f(0) for X ~ N(0, I_d): A = (Laplacian f(0))^2/576, B = f(0); returns h*, mse*."""
    f0 = (2 * np.pi) ** (-d / 2)
    lap = -d * f0
    A, B = lap ** 2 / 576, f0
    h = (d * B / (4 * A * n)) ** (1 / (4 + d))
    return h, A * h ** 4 + B / (n * h ** d), A, B


def ex46_n_for_relmse(d, target=0.1):
    """Sample size at which the optimal mse equals target * f(0)^2 (the relative mse is 'target')."""
    f0 = (2 * np.pi) ** (-d / 2)
    _, m1, _, _ = ex46(d, 1.0)                    # mse*(n) = m1 * n^(-4/(4+d))
    return (m1 / (target * f0 ** 2)) ** ((4 + d) / 4)


def ex46_n_for_mse(d, target=0.1):
    _, m1, _, _ = ex46(d, 1.0)
    return (m1 / target) ** ((4 + d) / 4)


# =========================================================================== local averages (4.23)-(4.24), for the demos
def nw_matrix(x, xe, h, kernel=k_boxcar):
    """Weights l_i(x) of the local average (4.24): K((x_i - x)/h) / sum_j K((x_j - x)/h). Rows with no
    point inside the kernel's support are nan."""
    w = kernel((x[None, :] - xe[:, None]) / h)
    s = w.sum(1, keepdims=True)
    with np.errstate(invalid="ignore", divide="ignore"):
        return w / s


def nw_loocv(x, y, h, kernel):
    """Leave-one-out CV of the local average; points with no neighbour inside the window are skipped.
    Returns the score and the number skipped."""
    K = kernel((x[None, :] - x[:, None]) / h)
    num = K @ y - np.diag(K) * y
    den = K.sum(1) - np.diag(K)
    ok = den > 1e-300
    r = num[ok] / den[ok]
    return np.mean((y[ok] - r) ** 2), int((~ok).sum())


DEMO_GRID = {"lidar": (2.9, 200.0), "cmb": (2.1, 200.0), "bpd": (49.0, 800.0)}
DEMO_NGRID = 60


def demo_hgrid(name):
    a, b = DEMO_GRID[name]
    k = np.arange(DEMO_NGRID)
    return np.exp(np.log(a) + (np.log(b) - np.log(a)) * k / (DEMO_NGRID - 1))


def demo_data(name):
    return {"lidar": lidar, "cmb": cmb400, "bpd": bpd}[name]()


def demo_bins(x, h):
    """Number of regressogram bins used by the demo for bin width h: round(range / h), at least 1."""
    return max(1, int(round((x.max() - x.min()) / h)))


def demo_cv(name, kind, h):
    """The leave-one-out score the local-average demo shows (kind 'regressogram' or a kernel name)."""
    x, y = demo_data(name)
    if kind == "regressogram":
        return regressogram_loocv(x, y, demo_bins(x, h), x.min(), x.max())
    return nw_loocv(x, y, h, KERNELS[kind])


# =========================================================================== Example 4.16 for three densities (demo)
MIX_MU = 1.5
DENS = {
    "normal": dict(cdf=norm.cdf, f0=norm.pdf(0), f2=-norm.pdf(0)),
    "bimodal": dict(cdf=lambda t: 0.5 * (norm.cdf(t - MIX_MU) + norm.cdf(t + MIX_MU)), f0=norm.pdf(MIX_MU),
                    f2=norm.pdf(MIX_MU) * (MIX_MU ** 2 - 1)),
    "laplace": dict(cdf=lambda t: np.where(t < 0, 0.5 * np.exp(np.minimum(t, 0)), 1 - 0.5 * np.exp(-np.maximum(t, 0))),
                    f0=0.5, f2=None),
}


def ex416_curve(dens, n, h):
    D = DENS[dens]
    return ex416_exact(h, n, f0=D["f0"], cdf=D["cdf"])


def ex416_exact_opt(dens, n):
    """Bin width minimising the exact mse (bounded scalar search on log h)."""
    from scipy.optimize import minimize_scalar
    r = minimize_scalar(lambda t: ex416_curve(dens, n, np.exp(t))[2], bounds=(np.log(1e-4), np.log(5.0)),
                        method="bounded", options={"xatol": 1e-10})
    return np.exp(r.x), r.fun


# =========================================================================== figures
def _hist_axes(ax, x, m, title):
    c, h = hist_counts(x, m)
    ax.bar(np.arange(m) * h + h / 2, c / (len(x) * h), width=h, color="#9ecae1" if m < 50 else "black",
           edgecolor="black", lw=0.5 if m < 50 else 0)
    ax.set_xlim(0, 0.2)
    ax.set_ylim(0, 85)
    ax.set_xlabel("redshift")
    ax.set_ylabel(r"$\widehat f$")
    ax.set_title(title)


def galaxy_hist_scores(mmax=1266):
    x = galaxy()
    ms = np.arange(1, mmax + 1)
    J = np.array([hist_cv(x, m) for m in ms])
    return x, ms, J


def fig_4_1(out=None):
    """Histograms of the galaxy redshifts and the CV score against the number of bins."""
    x, ms, J = galaxy_hist_scores()
    mcv = int(ms[np.argmin(J)])
    fig, ax = common.panel_grid(2, 2, size=(3.4, 2.6))
    _hist_axes(ax[0, 0], x, 1000, "Undersmoothed (m = 1000)")
    _hist_axes(ax[0, 1], x, mcv, "Just right (m = %d, by CV)" % mcv)
    _hist_axes(ax[1, 0], x, 10, "Oversmoothed (m = 10)")
    a = ax[1, 1]
    a.plot(ms, J, color="black", lw=1)
    a.axvline(mcv, color=RED, ls="--", lw=1)
    a.set_xlabel("number of bins")
    a.set_ylabel("estimated risk (CV score)")
    a.set_ylim(J.min() - 1, J.min() + 12)
    fig.tight_layout()
    savefig(fig, CH, "fig4-1", out)


def galaxy_kde_scores():
    x = galaxy()
    hs = kde_hgrid()
    J = np.array([kde_lscv(x, h) for h in hs])
    return x, hs, J


def kde_hcv(x):
    """Minimise the LSCV score: grid search, then golden-section refinement."""
    from scipy.optimize import minimize_scalar
    hs = kde_hgrid()
    J = np.array([kde_lscv(x, h) for h in hs])
    k = int(np.argmin(J))
    r = minimize_scalar(lambda h: kde_lscv(x, h), bounds=(hs[max(k - 1, 0)], hs[min(k + 1, len(hs) - 1)]),
                        method="bounded", options={"xatol": 1e-9})
    return r.x, r.fun


def fig_4_3(out=None):
    """Kernel density estimates of the redshifts and the LSCV score."""
    x, hs, J = galaxy_kde_scores()
    hcv, _ = kde_hcv(x)
    g = np.linspace(0, 0.2, 800)
    fig, ax = common.panel_grid(2, 2, size=(3.4, 2.6))
    for a, h, t in [(ax[0, 0], 0.01, "Oversmoothed (h = 0.01)"), (ax[0, 1], hcv, "Just right (h = %.5f, by CV)" % hcv),
                    (ax[1, 0], hcv / 4, "Undersmoothed (h = %.5f)" % (hcv / 4))]:
        a.plot(g, kde(x, g, h), color="black", lw=1)
        a.set_xlim(0, 0.2)
        a.set_xlabel("redshift")
        a.set_ylabel(r"$\widehat f$")
        a.set_title(t)
    a = ax[1, 1]
    a.plot(hs, J, color="black", lw=1.2)
    a.axvline(hcv, color=RED, ls="--", lw=1)
    a.set_xlabel("h")
    a.set_ylabel("estimated risk (CV score)")
    fig.tight_layout()
    savefig(fig, CH, "fig4-3", out)


def cmb_scores():
    x, y = cmb400()
    hs = np.exp(np.linspace(np.log(5), np.log(150), 80))
    cv = np.array([loocv_linear(local_linear_matrix(x, x, h), y) for h in hs])
    return x, y, hs, cv


def cmb_hcv():
    from scipy.optimize import minimize_scalar
    x, y, hs, cv = cmb_scores()
    k = int(np.argmin(cv))
    r = minimize_scalar(lambda h: loocv_linear(local_linear_matrix(x, x, h), y),
                        bounds=(hs[k - 1], hs[k + 1]), method="bounded", options={"xatol": 1e-6})
    return r.x


CMB_UNDER, CMB_OVER = 3.0, 150.0


def fig_4_5(out=None):
    """First 400 CMB points and local linear fits with three bandwidths."""
    x, y = cmb400()
    hcv = cmb_hcv()
    fig, ax = common.panel_grid(2, 2, size=(3.4, 2.6))
    ax[0, 0].plot(x, y, "o", ms=2.5, mfc="none", mec="black", mew=0.6)
    ax[0, 0].set_title("Data (first 400 points)")
    for a, h, t in [(ax[0, 1], CMB_UNDER, "Undersmoothed (h = %g)" % CMB_UNDER),
                    (ax[1, 0], hcv, "Just right (h = %.1f, by CV)" % hcv),
                    (ax[1, 1], CMB_OVER, "Oversmoothed (h = %g)" % CMB_OVER)]:
        a.plot(x, y, ".", ms=1.5, color=GREY)
        a.plot(x, local_linear_matrix(x, x, h) @ y, color="black", lw=1.2)
        a.set_title(t)
    for a in ax.ravel():
        a.set_xlabel("frequency (multipole)")
        a.set_ylabel("power")
        a.set_xlim(0, 400)
    fig.tight_layout()
    savefig(fig, CH, "fig4-5", out)


LIDAR_BINS = (4, 8, 12, 24)


def fig_4_6(out=None):
    """Regressograms of the LIDAR data."""
    x, y = lidar()
    a0, b0 = x.min(), x.max()
    fig, ax = common.panel_grid(2, 2, size=(3.4, 2.6))
    for a, m in zip(ax.ravel(), LIDAR_BINS):
        edges, mean, _ = regressogram(x, y, m, a0, b0)
        a.plot(x, y, "o", ms=2.5, mfc="none", mec="black", mew=0.5)
        a.step(edges, np.r_[mean, mean[-1]], where="post", color=BLUE, lw=1.8)
        a.set_title("m = %d bins, h = %.1f" % (m, (b0 - a0) / m))
        a.set_xlabel("range")
        a.set_ylabel("log ratio")
    fig.tight_layout()
    savefig(fig, CH, "fig4-6", out)


def bpd_fits():
    x, y = bpd()
    g = np.linspace(400, 1700, 131)
    s = x.std()
    beta = logistic_irls(np.column_stack([np.ones_like(x), (x - x.mean()) / s]), y)
    logit = 1 / (1 + np.exp(-(beta[0] + beta[1] * (g - x.mean()) / s)))
    loclik = local_logistic(x, y, g, BPD_H)
    loclin = local_linear_matrix(x, g, BPD_H) @ y
    b0 = beta[0] - beta[1] * x.mean() / s
    b1 = beta[1] / s
    return x, y, g, logit, loclik, loclin, (b0, b1)


def fig_4_7(out=None):
    """BPD against birth weight: three fits."""
    x, y, g, logit, loclik, loclin, _ = bpd_fits()
    fig, a = common.plt.subplots(figsize=(6.0, 3.4))
    rng = np.random.default_rng(1)
    a.plot(x, y + rng.uniform(-0.02, 0.02, len(y)), "|", color=GREY, ms=8)
    a.plot(g, logit, color="black", lw=1.6, label="logistic regression")
    a.plot(g, loclik, color=BLUE, ls="--", lw=1.6, label="local likelihood (h = %g g)" % BPD_H)
    a.plot(g, loclin, color=RED, ls=":", lw=2, label="local linear (h = %g g)" % BPD_H)
    a.set_xlabel("birth weight (grams)")
    a.set_ylabel("bronchopulmonary dysplasia")
    a.set_yticks([0, 1])
    a.legend(loc="upper right", frameon=False)
    fig.tight_layout()
    savefig(fig, CH, "fig4-7", out)


def fig_4_8(out=None):
    """Additive model for log permeability of the rock data."""
    X, y, alpha, F = rock_fit()
    names = ["area (pixels)", "perimeter (pixels)", "shape"]
    fig, ax = common.panel_grid(1, 3, size=(2.7, 2.6))
    for j in range(3):
        a = ax[0, j]
        o = np.argsort(X[:, j])
        partial = y - alpha - F.sum(1) + F[:, j]
        a.plot(X[:, j], partial + alpha, "o", ms=3, mfc="none", mec=GREY, mew=0.6)
        a.plot(X[o, j], alpha + F[o, j], color="black", lw=1.6)
        a.set_xlabel(names[j])
        a.set_ylabel("log permeability")
    fig.tight_layout()
    savefig(fig, CH, "fig4-8", out)


EX416_N = 10000


def fig_4_9(out=None):
    """Bias-variance trade-off for the estimator (4.17), X ~ N(0,1), n = 10000: exact curves."""
    n = EX416_N
    h = np.linspace(0.02, 1.4, 400)
    b, v, m = ex416_exact(h, n)
    _, _, ma = ex416_approx(h, n)
    k = np.argmin(m)
    fig, a = common.plt.subplots(figsize=(5.6, 3.6))
    a.plot(h, m, color="black", lw=2, label="risk = bias$^2$ + variance")
    a.plot(h, b ** 2, color=BLUE, lw=1.6, ls="--", label="bias$^2$")
    a.plot(h, v, color=RED, lw=1.6, ls="-.", label="variance")
    a.plot(h, ma, color=GREY, lw=1, ls=":", label=r"approximation $Ah^4+B/(nh)$")
    a.axvline(h[k], color=GREEN, lw=1)
    top = 5 * m[k]
    a.text(h[k] + 0.02, 0.06 * top, "optimal\nsmoothing", color=GREEN, fontsize=9)
    a.set_ylim(0, top)
    a.set_xlabel("bin width h  (less smoothing  ←  →  more smoothing)")
    a.set_ylabel("mse of $\\widehat f_n(0)$")
    a.legend(loc="upper center", framealpha=0.95, edgecolor="none")
    fig.tight_layout()
    savefig(fig, CH, "fig4-9", out)


def fig_4_10(out=None):
    """The four kernels of Section 4.2."""
    t = np.linspace(-3, 3, 1201)
    fig, ax = common.panel_grid(2, 2, size=(2.8, 2.1))
    for a, (name, K) in zip(ax.ravel(), KERNELS.items()):
        a.plot(t, K(t), color="black", lw=1.5)
        a.set_title(name)
        a.set_xlim(-3, 3)
        a.set_ylim(0, 0.9)
        a.set_xticks([-3, 0, 3])
    fig.tight_layout()
    savefig(fig, CH, "fig4-10", out)


# =========================================================================== data for the demos
def export_json():
    os.makedirs(DATA_OUT, exist_ok=True)
    lx, ly = lidar()
    cx, cy = cmb400()
    bx, by = bpd()
    d = {"lidar": {"x": lx.tolist(), "y": ly.tolist()},
         "cmb": {"x": cx.tolist(), "y": [round(v, 4) for v in cy.tolist()]},
         "bpd": {"x": bx.tolist(), "y": by.tolist()}}
    p = os.path.join(DATA_OUT, "ch04-smoothing.json")
    with open(p, "w") as f:
        json.dump(d, f, separators=(",", ":"))
    print("wrote", p)


FIGURES = {"4.1": fig_4_1, "4.3": fig_4_3, "4.5": fig_4_5, "4.6": fig_4_6, "4.7": fig_4_7, "4.8": fig_4_8,
           "4.9": fig_4_9, "4.10": fig_4_10}

if __name__ == "__main__":
    import sys
    if "--json" in sys.argv:
        export_json()
    else:
        run(FIGURES, CH)
        export_json()
