"""Wasserman (2004), Chapter 12: Statistical Decision Theory.

Companion code for Chapter 12 of L. Wasserman, All of Statistics: A Concise Course in Statistical
Inference (Springer, 2004), pp. 193-205, written from scratch with numpy, scipy and matplotlib. The
functions are short and meant to be read next to the lecture notes (../ch12-decision-theory.html):

* Sections 12.1-12.2, risk functions: bern_risk (exact risk of the Beta-prior posterior mean
  (Y + a)/(n + a + b) under squared or weighted squared loss, the MLE being a = b = 0), bern_max_risk
  and bern_bayes_risk (the two one-number summaries (12.1) and (12.2), the latter under any Beta
  "judge's" prior), and the crossing sample size of Example 12.5.
* Section 12.3, Bayes estimators: Beta posterior summaries (beta_summary: mean, median, mode and the
  window-loss action that maximises the posterior probability of an interval of half-width eps),
  posterior expected losses (post_loss) and the exact Bayes risks of the three Bayes rules under the
  three losses for a Beta-Binomial model (bayes_risk_table).
* Section 12.4, minimax rules: the risk of m tanh(m x) for a Normal mean restricted to [-m, m]
  (tanh_risk, Example 12.15) and the largest m for which the two-point prior is least favourable
  (two_point_threshold).
* Section 12.5: the risk of Hodges' superefficient estimator (hodges_risk), the van Trees lower bound
  (van_trees_bound) and the many-Normal-means problem of Example 12.16 (pinsker_risk, js_risk_scaled).
* Section 12.7, Stein's paradox: the exact risk of the James-Stein estimator and of its positive part
  (js_risk, js_plus_risk: Poisson mixtures of chi-square probabilities), a Monte Carlo version
  (js_mc) and the Dirichlet energies behind Blyth's method in dimensions 1, 2 and 3 (blyth_energy).
* Exercises 12.1-12.6.
* JSRandom and js_normals: the seeded generator of the browser demonstrations (DK.rng in
  courses/demo-kit.js), so that the Monte Carlo numbers on the pages can be reproduced here number
  for number.

The chapter has three figures, all computed from closed-form risk functions, and all three are
reproduced: 12.1 (two risk functions that cross), 12.2 (the risk of the MLE and of the minimax
estimator of a Bernoulli probability; the book does not say which n it draws, so two values are
shown) and 12.3 (the risk of the minimax estimator of a restricted Normal mean). Nothing is skipped.
Six supplementary figures illustrate the gaps filled in the notes:
  12.S1 maximum risk against Bayes risk as n grows (Example 12.5);
  12.S2 Bayes estimators under squared, absolute and zero-one (window) loss (Theorem 12.8);
  12.S3 when the two-point prior stops being least favourable (Example 12.15);
  12.S4 superefficiency, the local minimax bound and the many-means problem (Section 12.5);
  12.S5 Stein's paradox and why dimension 3 is special (Section 12.7);
  12.S6 the computer experiment of Exercise 12.6.
Run `python3 ch12_decision_theory.py --list`, `python3 ch12_decision_theory.py` (all nine figures,
about ten seconds on one CPU thread) or `python3 ch12_decision_theory.py --fig 12.3`.
Files go to ../img/ch12/. The demonstrations compute everything in the browser and need no data files.
"""
import math

import numpy as np
from scipy import integrate, optimize, special, stats

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

CH = 12
M32 = 0xFFFFFFFF


# =========================================================================== 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_uniforms(seed, n, start=0):
    """Uniforms number start+1, ..., start+n of DK.rng(seed), vectorised (mulberry32 adds a constant
    to its state at each call, so the j-th output is a hash of seed + j * 0x6D2B79F5 mod 2^32)."""
    j = np.arange(start + 1, start + n + 1, dtype=np.uint64)
    m = np.uint64(M32)
    a = (np.uint64(int(seed) & M32) + j * np.uint64(0x6D2B79F5)) & m
    t = ((a ^ (a >> np.uint64(15))) * (np.uint64(1) | a)) & m
    t = ((t + (((t ^ (t >> np.uint64(7))) * (np.uint64(61) | t)) & m)) & m) ^ t
    return ((t ^ (t >> np.uint64(14))) & m).astype(np.float64) / 4294967296.0


def js_normals(seed, n):
    """The first n values of DK.rng(seed).norm() (Box-Muller pairs: cos first, then the cached sin)."""
    npair = (n + 1) // 2
    u = js_uniforms(seed, 2 * npair)
    u1, u2 = u[0::2], u[1::2]
    if np.any(u1 == 0.0):
        raise RuntimeError("a zero uniform: use JSRandom for this stream")
    r = np.sqrt(-2.0 * np.log(u1))
    th = 2.0 * np.pi * u2
    out = np.empty(2 * npair)
    out[0::2] = r * np.cos(th)
    out[1::2] = r * np.sin(th)
    return out[:n]


# =========================================================================== 12.2: risk functions for a Bernoulli p
def bern_risk(p, n, a=0.0, b=0.0, loss="sq"):
    """Exact risk of (Y + a)/(n + a + b), Y ~ Binomial(n, p), at p (array). a = b = 0 is the MLE,
    a = b = sqrt(n)/2 the minimax estimator of Example 12.12. loss 'sq' is (p_hat - p)^2 and 'wsq'
    is (p_hat - p)^2 / (p(1 - p)) (Example 12.13)."""
    p = np.asarray(p, dtype=float)
    s = a + b
    r = (n * p * (1 - p) + (a - s * p) ** 2) / (n + s) ** 2
    if loss == "wsq":
        with np.errstate(divide="ignore", invalid="ignore"):
            r = r / (p * (1 - p))
    return r


def bern_max_risk(n, a=0.0, b=0.0, loss="sq"):
    """sup_p R(p, (Y + a)/(n + a + b)), equation (12.1)."""
    s = a + b
    if loss == "wsq":
        return 1.0 / n if (a == 0 and b == 0) else math.inf
    # numerator (s^2 - n) p^2 + (n - 2 a s) p + a^2: a quadratic, so its maximum is at an end or at the vertex
    cand = [0.0, 1.0]
    A, B = s * s - n, n - 2 * a * s
    if A < 0:
        cand.append(min(1.0, max(0.0, -B / (2 * A))))
    return float(max(bern_risk(np.array(cand), n, a, b)))


def bern_bayes_risk(n, a=0.0, b=0.0, a0=1.0, b0=1.0, loss="sq"):
    """Bayes risk (12.2) of (Y + a)/(n + a + b) under the 'judge's' prior Beta(a0, b0) for p.

    Squared loss: (n E p(1-p) + E (a - s p)^2) / (n + s)^2 with Beta moments.
    Weighted loss: by partial fractions (a - s p)^2 / (p(1-p)) = -s^2 + a^2/p + b^2/(1-p), so the risk is
    (n - s^2 + a^2/p + b^2/(1-p)) / (n + s)^2 and its prior mean uses E[1/p] = (a0 + b0 - 1)/(a0 - 1)
    and E[1/(1-p)] = (a0 + b0 - 1)/(b0 - 1) (infinite when a0 <= 1, resp. b0 <= 1)."""
    s = a + b
    if loss == "sq":
        m1 = a0 / (a0 + b0)
        m2 = a0 * (a0 + 1) / ((a0 + b0) * (a0 + b0 + 1))
        epq = m1 - m2
        return (n * epq + a * a - 2 * a * s * m1 + s * s * m2) / (n + s) ** 2
    tot = n - s * s
    if a != 0:
        tot += a * a * ((a0 + b0 - 1) / (a0 - 1) if a0 > 1 else math.inf)
    if b != 0:
        tot += b * b * ((a0 + b0 - 1) / (b0 - 1) if b0 > 1 else math.inf)
    return tot / (n + s) ** 2


def bayes_mle_crossing():
    """Example 12.5: with the uniform prior, r(f, p_mle) = 1/(6n) and r(f, p_minimax) = 1/(4(sqrt n + 1)^2);
    the second is larger exactly when n > (2 + sqrt 6)^2 = 19.80."""
    return (2 + math.sqrt(6)) ** 2


def minimax_better_halfwidth(n):
    """Half-width of the set of p where the minimax estimator has smaller risk than the MLE:
    |p - 1/2| < sqrt(2 sqrt n + 1) / (2 (sqrt n + 1)), of order n^(-1/4)."""
    r = math.sqrt(n)
    return math.sqrt(2 * r + 1) / (2 * (r + 1))


# =========================================================================== 12.3: Bayes estimators for a Beta posterior
def beta_window_action(A, B, eps):
    """The Bayes action under the window loss 1{|theta - a| > eps} for a Beta(A, B) posterior: the centre
    a of the interval [a - eps, a + eps] of largest posterior probability. The objective
    G(a) = I(a + eps) - I(a - eps) (ends clipped to [0, 1]) is unimodal for a unimodal posterior; a
    ternary search on [0, 1], as in the browser demo, finds its maximiser (a point of the maximising
    set when G is flat, which happens only for windows wider than the posterior's support)."""
    G = lambda t: special.betainc(A, B, min(1.0, t + eps)) - special.betainc(A, B, max(0.0, t - eps))
    lo, hi = 0.0, 1.0
    for _ in range(200):
        m1 = lo + (hi - lo) / 3
        m2 = hi - (hi - lo) / 3
        if G(m1) < G(m2):
            lo = m1
        else:
            hi = m2
    return 0.5 * (lo + hi)


def beta_summary(A, B, eps=0.05):
    """Mean, median, mode and window-loss action of a Beta(A, B) posterior (Theorem 12.8)."""
    mean = A / (A + B)
    median = float(special.betaincinv(A, B, 0.5))
    if A > 1 and B > 1:
        mode = (A - 1) / (A + B - 2)
    elif A <= 1 and B > 1:
        mode = 0.0
    elif A > 1 and B <= 1:
        mode = 1.0
    else:
        mode = float("nan")     # U-shaped or flat: no unique mode
    return {"mean": mean, "median": median, "mode": mode, "window": beta_window_action(A, B, eps)}


def post_loss(A, B, a, loss, eps=0.05):
    """Posterior expected loss of action a for a Beta(A, B) posterior: 'sq', 'abs' or 'win'."""
    mu = A / (A + B)
    if loss == "sq":
        v = A * B / ((A + B) ** 2 * (A + B + 1))
        return v + (mu - a) ** 2
    if loss == "abs":
        # E|theta - a| = a (2 I_a(A, B) - 1) + mu (1 - 2 I_a(A + 1, B))
        return a * (2 * special.betainc(A, B, a) - 1) + mu * (1 - 2 * special.betainc(A + 1, B, a))
    return 1.0 - (special.betainc(A, B, min(1.0, a + eps)) - special.betainc(A, B, max(0.0, a - eps)))


def betabinom_pmf(x, n, al, be):
    return np.exp(special.gammaln(n + 1) - special.gammaln(x + 1) - special.gammaln(n - x + 1)
                  + special.betaln(al + x, be + n - x) - special.betaln(al, be))


def bayes_risk_table(n, al, be, eps=0.05):
    """Exact Bayes risks r(prior, rule) for X ~ Binomial(n, p), p ~ Beta(al, be): rows are the three Bayes
    rules (posterior mean, median, window action), columns the three losses (sq, abs, win). By
    Theorem 12.7 each is sum_x m(x) * posterior expected loss of the rule's action at x."""
    rules = ("mean", "median", "window")
    losses = ("sq", "abs", "win")
    T = np.zeros((3, 3))
    for x in range(n + 1):
        A, B = al + x, be + n - x
        mx = betabinom_pmf(x, n, al, be)
        s = beta_summary(A, B, eps)
        for i, r in enumerate(rules):
            for j, L in enumerate(losses):
                T[i, j] += mx * post_loss(A, B, s[r], L, eps)
    return T


# =========================================================================== 12.4: restricted Normal mean
_GH_X, _GH_W = np.polynomial.hermite_e.hermegauss(120)
_GH_W = _GH_W / _GH_W.sum()


def tanh_risk(theta, m):
    """Risk of m tanh(m X) for X ~ N(theta, 1) under squared error (Example 12.15), by 120-point
    Gauss-Hermite quadrature (the integrand is smooth and bounded, so this is accurate to ~1e-14)."""
    theta = np.atleast_1d(np.asarray(theta, dtype=float))
    x = theta[:, None] + _GH_X[None, :]
    return ((m * np.tanh(m * x) - theta[:, None]) ** 2) @ _GH_W


def two_point_threshold():
    """The largest m for which R(0) <= R(m) for m tanh(m X), i.e. the root of R(0) = R(m): beyond it the
    two-point prior on {-m, m} is no longer least favourable (Casella and Strawderman, 1981)."""
    g = lambda m: tanh_risk(0.0, m)[0] - tanh_risk(m, m)[0]
    return optimize.brentq(g, 0.9, 1.3, xtol=1e-13)


def tanh_convexity_bound(m):
    """Lower bound 2[(1 - m^2)^2 - 2m * (4/(3 sqrt 3)) m^3] on R''(theta) used in the notes: positive for
    m below about 0.668, which proves that R(theta) <= R(m) on [-m, m] there."""
    return 2 * ((1 - m * m) ** 2 - 2 * m * (4 / (3 * math.sqrt(3))) * m ** 3)


# =========================================================================== 12.5: superefficiency and many means
def hodges_risk(theta, n):
    """Exact risk of Hodges' estimator: X_bar if |X_bar| > n^(-1/4), else 0, for N(theta, 1) data."""
    theta = np.asarray(theta, dtype=float)
    s = 1 / math.sqrt(n)
    t = n ** -0.25
    a = (-t - theta) / s
    b = (t - theta) / s
    P = stats.norm.cdf(b) - stats.norm.cdf(a)
    inner = P - (b * stats.norm.pdf(b) - a * stats.norm.pdf(a))     # E[Z^2 1{a <= Z <= b}]
    return s * s * (1 - inner) + theta ** 2 * P


def van_trees_bound(n, eps, info=1.0):
    """Lower bound on sup_{|theta - theta0| < eps} n R(theta, any estimator): with the prior
    (1/eps) cos^2(pi (theta - theta0) / (2 eps)), whose Fisher information is pi^2 / eps^2, van Trees'
    inequality gives n / (n I + pi^2 / eps^2)."""
    return n / (n * info + (math.pi / eps) ** 2)


def pinsker_risk(sig2, c2):
    """Asymptotic minimax risk sigma^2 c^2 / (sigma^2 + c^2) for many Normal means in a ball (Example 12.16)."""
    return sig2 * c2 / (sig2 + c2)


def js_risk(k, lam):
    """Exact risk of the James-Stein estimator (1 - (k-2)/||X||^2) X for X ~ N_k(theta, I), lam = ||theta||^2:
    k - (k-2)^2 E[1/(k - 2 + 2K)] with K ~ Poisson(lam/2)."""
    if k <= 2:
        return float(k)
    c = k - 2
    J = np.arange(0, int(lam / 2 + 40 * math.sqrt(lam / 2 + 1) + 60))
    w = stats.poisson.pmf(J, lam / 2)
    return float(k - c * c * np.sum(w / (k - 2 + 2 * J)))


def js_plus_risk(k, lam):
    """Exact risk of the positive-part James-Stein estimator (12.12). With W = ||X||^2 (noncentral chi-square)
    and c = k - 2, Stein's identity gives R = lam + E[(2k - W - c^2/W) 1{W > c}]; writing W as a Poisson(lam/2)
    mixture of chi-square(k + 2j) variables, each term is a chi-square tail probability."""
    if k <= 2:
        return float(k)
    c = k - 2
    J = np.arange(0, int(lam / 2 + 40 * math.sqrt(lam / 2 + 1) + 60))
    w = stats.poisson.pmf(J, lam / 2)
    nu = k + 2 * J
    Q = lambda df: stats.chi2.sf(c, df)
    term = 2 * k * Q(nu) - nu * Q(nu + 2) - c * c * Q(nu - 2) / (nu - 2)
    return float(lam + np.sum(w * term))


def js_plus_risk_quad(k, lam):
    """The same risk by direct numerical integration against the noncentral chi-square density (an independent route)."""
    c = k - 2
    f = lambda w: (2 * k - w - c * c / w) * stats.ncx2.pdf(w, k, lam)
    val, _ = integrate.quad(f, c, np.inf, limit=400)
    return lam + val


def js_risk_scaled(n, sig2, c2):
    """Risk, under the loss sum_i (theta_hat_i - theta_i)^2, of James-Stein in the many-means model
    Y_i ~ N(theta_i, sig2/n), i = 1..n, at ||theta||^2 = c2 (the worst point of the ball)."""
    return sig2 / n * js_risk(n, n * c2 / sig2)


def js_mc(k, theta, R, seed, out_first=False):
    """Monte Carlo risks of X, James-Stein and its positive part with the demo's generator: R replicates,
    each of k consecutive DK.rng(seed).norm() draws. theta is the length-k mean vector."""
    theta = np.asarray(theta, dtype=float)
    Z = js_normals(seed, R * k).reshape(R, k)
    X = theta[None, :] + Z
    W = np.sum(X * X, axis=1)
    c = max(k - 2, 0)
    sh = 1 - c / W
    JS = sh[:, None] * X
    JP = np.maximum(sh, 0)[:, None] * X
    out = {"mle": float(np.mean(np.sum((X - theta) ** 2, 1))),
           "js": float(np.mean(np.sum((JS - theta) ** 2, 1))),
           "jsplus": float(np.mean(np.sum((JP - theta) ** 2, 1)))}
    if out_first:
        out["x0"] = X[0].tolist()
        out["js0"] = JP[0].tolist()
    return out


def theta_pattern(k, norm, pattern="equal"):
    """Mean vectors of a given length used by the demo: 'equal' (all coordinates equal), 'spike' (all the
    length on the first coordinate) or 'ramp' (proportional to 1, 2, ..., k)."""
    if pattern == "equal":
        v = np.ones(k)
    elif pattern == "spike":
        v = np.zeros(k)
        v[0] = 1.0
    else:
        v = np.arange(1, k + 1, dtype=float)
    return norm * v / np.linalg.norm(v)


def blyth_energy(d, n):
    """Smallest Dirichlet energy  int |grad h|^2  over radial h with h = 1 on the unit ball and h = 0 outside
    radius n, in dimension d (the harmonic profile): 2/(n - 1) for d = 1, 2 pi / log n for d = 2 and
    surface(d) (d - 2) / (1 - n^(2-d)) for d >= 3. It tends to 0 exactly when d <= 2."""
    if d == 1:
        return 2.0 / (n - 1)
    if d == 2:
        return 2 * math.pi / math.log(n)
    surf = 2 * math.pi ** (d / 2) / math.gamma(d / 2)
    return surf * (d - 2) / (1 - n ** (2 - d))


# =========================================================================== exercises
def ex1_bayes(model, **kw):
    """Exercise 12.1: Bayes estimator coefficients and Bayes risk.
    binom: X ~ Bin(n, p), p ~ Beta(al, be);  pois: X ~ Poisson(lam), lam ~ Gamma(al, be) with scale be
    (the book's parametrisation); normal: X ~ N(theta, s2), theta ~ N(a, b2)."""
    if model == "binom":
        n, al, be = kw["n"], kw["al"], kw["be"]
        s = al + be
        return al * be / (s * (s + 1) * (s + n))
    if model == "pois":
        al, be = kw["al"], kw["be"]
        return al * be ** 2 / (1 + be)
    s2, b2 = kw["s2"], kw["b2"]
    return s2 * b2 / (s2 + b2)


def ex5_risk(p, n, delta):
    """Exercise 12.5: risk of the estimator delta(x) (array over x = 0..n) under the loss (1 - delta/p)^2."""
    x = np.arange(n + 1)
    pmf = stats.binom.pmf(x, n, p)
    return float(np.sum((1 - np.asarray(delta) / p) ** 2 * pmf))


# =========================================================================== figures
def fig_12_1(out_dir=None):
    """Example 12.2: the risk functions of X and of the constant 3 cross."""
    fig, ax = common.panel_grid(1, 1, size=(4.2, 3.0))
    ax = ax[0, 0]
    th = np.linspace(0, 5, 501)
    ax.plot(th, np.ones_like(th), color=BLUE, label=r"$R(\theta,\widehat\theta_1)=1$, $\widehat\theta_1=X$")
    ax.plot(th, (3 - th) ** 2, color=RED, label=r"$R(\theta,\widehat\theta_2)=(3-\theta)^2$, $\widehat\theta_2=3$")
    ax.fill_between(th, 0, 3, where=(th > 2) & (th < 4), color=RED, alpha=0.07, lw=0)
    ax.set_xlim(0, 5)
    ax.set_ylim(0, 3)
    ax.set_xlabel(r"$\theta$")
    ax.set_ylabel("risk")
    ax.legend(fontsize=8, frameon=False, loc="upper center")
    ax.text(3, 0.12, r"$\widehat\theta_2$ better on $(2,4)$", ha="center", fontsize=8, color=RED)
    fig.tight_layout()
    return savefig(fig, CH, "fig12-1", out_dir)


def fig_12_2(out_dir=None):
    """Example 12.3: risk of the MLE and of the minimax (constant-risk) estimator of a Bernoulli p."""
    fig, axes = common.panel_grid(1, 2, size=(3.7, 2.9))
    ps = np.linspace(0, 1, 501)
    for ax, n, lab in ((axes[0, 0], 10, "a"), (axes[0, 1], 100, "b")):
        a = math.sqrt(n) / 2
        r1 = bern_risk(ps, n)
        r2 = bern_risk(ps, n, a, a)
        ax.plot(ps, r1, color=BLUE, label=r"MLE $\widehat p_1=\bar X$")
        ax.plot(ps, r2, color=RED, ls=":", lw=2.2, label=r"minimax $\widehat p_2$")
        h = minimax_better_halfwidth(n)
        ax.axvspan(0.5 - h, 0.5 + h, color=RED, alpha=0.07, lw=0)
        ax.set_ylim(0, 1.25 / (4 * n))
        ax.set_xlabel(r"$p$")
        ax.set_title(r"$n=%d$" % n)
        common.panel_label(ax, lab, loc="upper left")
        ax.ticklabel_format(axis="y", style="sci", scilimits=(-2, 2))
    axes[0, 0].set_ylabel("risk (MSE)")
    axes[0, 1].legend(fontsize=7.5, frameon=False, loc="lower center")
    fig.tight_layout()
    return savefig(fig, CH, "fig12-2", out_dir)


def fig_12_3(out_dir=None):
    """Example 12.15: risk of m tanh(m X) on [-m, m] for m = 0.5, with the two-point least favourable prior."""
    fig, axes = common.panel_grid(1, 1, size=(4.2, 3.0))
    ax = axes[0, 0]
    m = 0.5
    th = np.linspace(-m, m, 401)
    R = tanh_risk(th, m)
    ax.plot(th, R, color=BLUE, label=r"$R(\theta,\,m\tanh(mX))$")
    ax.axhline(R[-1], color=GREY, ls="--", lw=1, label=r"Bayes risk $r(f,\widehat\theta)=R(m)$")
    for x in (-m, m):
        ax.plot([x, x], [0, 0.5 * R[-1] * 0.9], color=RED, ls=(0, (3, 2)), lw=2.4)
    ax.text(m, 0.47 * R[-1], "mass 1/2", ha="center", va="bottom", fontsize=8, color=RED)
    ax.text(-m, 0.47 * R[-1], "mass 1/2", ha="center", va="bottom", fontsize=8, color=RED)
    ax.set_xlim(-0.6, 0.6)
    ax.set_ylim(0, 1.3 * R[-1])
    ax.set_xlabel(r"$\theta$")
    ax.set_ylabel("risk")
    ax.legend(fontsize=8, frameon=False, loc="upper center")
    fig.tight_layout()
    return savefig(fig, CH, "fig12-3", out_dir)


def fig_12_s1(out_dir=None):
    """Example 12.5: maximum risk and Bayes risk of the MLE and the minimax estimator as n grows."""
    fig, axes = common.panel_grid(1, 3, size=(3.5, 2.9))
    ns = np.unique(np.round(np.logspace(0, 3, 300)).astype(int))
    ax = axes[0, 0]
    ax.loglog(ns, 1 / (4 * ns), color=BLUE, label="max risk, MLE")
    ax.loglog(ns, ns / (4 * (ns + np.sqrt(ns)) ** 2), color=RED, label="max risk, minimax")
    ax.loglog(ns, 1 / (6 * ns), color=BLUE, ls="--", label="Bayes risk (uniform), MLE")
    ax.set_xlabel("$n$")
    ax.set_ylabel("risk")
    ax.legend(fontsize=7, frameon=False, loc="lower left")
    common.panel_label(ax, "a", loc="upper right")
    ax = axes[0, 1]
    ratio_max = (ns / (ns + np.sqrt(ns))) ** 2          # minimax / MLE, maximum risk
    ratio_bayes = 6 * ns / (4 * (np.sqrt(ns) + 1) ** 2)  # minimax / MLE, uniform Bayes risk
    ax.semilogx(ns, ratio_max, color=PURPLE, label="maximum risk")
    ax.semilogx(ns, ratio_bayes, color=GREEN, label="Bayes risk, uniform prior")
    ax.axhline(1, color=GREY, lw=0.8)
    nx = bayes_mle_crossing()
    ax.axvline(nx, color=GREY, ls=":", lw=1)
    ax.text(nx * 1.15, 0.25, r"$n=(2+\sqrt{6})^2\approx19.8$", fontsize=7.5)
    ax.set_ylim(0, 1.6)
    ax.set_xlabel("$n$")
    ax.set_ylabel("minimax / MLE")
    ax.legend(fontsize=7, frameon=False, loc="upper left")
    common.panel_label(ax, "b", loc="lower right")
    ax = axes[0, 2]
    hw = np.array([minimax_better_halfwidth(n) for n in ns])
    ax.loglog(ns, 2 * hw, color=RED, label="width of the set where\nminimax beats the MLE")
    ax.loglog(ns, math.sqrt(2) * ns ** -0.25, color=GREY, ls="--", label=r"$\sqrt{2}\,n^{-1/4}$")
    ax.set_xlabel("$n$")
    ax.legend(fontsize=7, frameon=False, loc="lower left")
    common.panel_label(ax, "c", loc="upper right")
    fig.tight_layout()
    return savefig(fig, CH, "fig12-S1", out_dir)


def fig_12_s2(out_dir=None):
    """Theorem 12.8: posterior mean, median and mode for a skewed Beta posterior, and the window loss."""
    fig, axes = common.panel_grid(1, 3, size=(3.5, 2.9))
    A, B, eps = 2.0, 10.0, 0.05      # uniform prior, 1 success in 10 trials
    s = beta_summary(A, B, eps)
    t = np.linspace(0, 1, 1001)
    ax = axes[0, 0]
    ax.plot(t, stats.beta.pdf(t, A, B), color="k", lw=1.4)
    a = s["window"]
    tt = np.linspace(a - eps, a + eps, 100)
    ax.fill_between(tt, 0, stats.beta.pdf(tt, A, B), color=PURPLE, alpha=0.18, lw=0)
    for key, col, lab in (("mean", RED, "mean"), ("median", BLUE, "median"), ("mode", GREEN, "mode")):
        ax.axvline(s[key], color=col, lw=1.4, label="%s %.3f" % (lab, s[key]))
    ax.set_xlim(0, 0.6)
    ax.set_xlabel(r"$p$")
    ax.set_title(r"posterior Beta$(2,10)$")
    ax.legend(fontsize=7.5, frameon=False, loc="upper right")
    common.panel_label(ax, "a", loc="upper left")
    ax = axes[0, 1]
    aa = np.linspace(0, 0.6, 601)
    for L, col, lab in (("sq", RED, "squared"), ("abs", BLUE, "absolute"), ("win", PURPLE, r"window, $\epsilon=0.05$")):
        v = np.array([post_loss(A, B, x, L, eps) for x in aa])
        v = (v - v.min()) / (v.max() - v.min())
        ax.plot(aa, v, color=col, label=lab)
        i = np.argmin(v)
        ax.plot(aa[i], 0, "o", color=col, ms=5)
    ax.set_xlabel(r"action $a$")
    ax.set_ylabel("posterior expected loss (rescaled)")
    ax.legend(fontsize=7.5, frameon=False, loc="upper center")
    common.panel_label(ax, "b", loc="upper left")
    ax = axes[0, 2]
    es = np.logspace(-3, np.log10(0.5), 80)
    acts = [beta_window_action(A, B, e) for e in es]
    ax.semilogx(es, acts, color=PURPLE, label="window-loss Bayes action")
    for key, col in (("mean", RED), ("median", BLUE), ("mode", GREEN)):
        ax.axhline(s[key], color=col, lw=1, ls="--", label=key)
    ax.set_xlabel(r"half-width $\epsilon$")
    ax.set_ylabel("action")
    ax.legend(fontsize=7, frameon=False, loc="upper left")
    common.panel_label(ax, "c", loc="lower right")
    fig.tight_layout()
    return savefig(fig, CH, "fig12-S2", out_dir)


def fig_12_s3(out_dir=None):
    """Example 12.15: risk of m tanh(mX) for several m, and where the two-point prior stops being least favourable."""
    fig, axes = common.panel_grid(1, 2, size=(3.9, 3.0))
    ax = axes[0, 0]
    m0 = two_point_threshold()
    for m, col in ((0.5, BLUE), (1.0, GREEN), (m0, ORANGE), (1.4, RED)):
        th = np.linspace(-m, m, 301)
        R = tanh_risk(th, m)
        ax.plot(th / m, R, color=col, label=r"$m=%.4g$" % m if m != m0 else r"$m=m_0=%.4f$" % m0)
    ax.set_xlabel(r"$\theta/m$")
    ax.set_ylabel(r"$R(\theta,\,m\tanh(mX))$")
    ax.legend(fontsize=7.5, frameon=False, loc="upper center", ncol=2)
    ax.set_ylim(0, 0.95)
    common.panel_label(ax, "a", loc="lower left")
    ax = axes[0, 1]
    ms = np.linspace(0.05, 1.5, 300)
    d = np.array([tanh_risk(0.0, m)[0] - tanh_risk(m, m)[0] for m in ms])
    ax.plot(ms, d, color=PURPLE)
    ax.axhline(0, color=GREY, lw=0.8)
    ax.axvline(m0, color=ORANGE, ls=":", lw=1.2)
    ax.axvspan(0, 0.668, color=GREEN, alpha=0.08, lw=0)
    ax.text(0.33, 0.02, "convexity\nproof", ha="center", fontsize=7.5, color=GREEN)
    ax.text(m0 + 0.03, -0.05, r"$m_0\approx%.4f$" % m0, fontsize=7.5)
    ax.set_xlabel(r"$m$")
    ax.set_ylabel(r"$R(0)-R(m)$")
    common.panel_label(ax, "b", loc="upper left")
    fig.tight_layout()
    return savefig(fig, CH, "fig12-S3", out_dir)


def fig_12_s4(out_dir=None):
    """Section 12.5: Hodges' estimator, the van Trees bound, and many Normal means."""
    fig, axes = common.panel_grid(1, 3, size=(3.5, 2.9))
    ax = axes[0, 0]
    for n, col in ((100, BLUE), (10 ** 4, GREEN), (10 ** 6, RED)):
        th = np.linspace(-1, 1, 4001) * 3 * n ** -0.25
        ax.plot(th * n ** 0.25, n * hodges_risk(th, n), color=col, label=r"$n=10^{%d}$" % round(math.log10(n)))
    ax.axhline(1, color="k", lw=1, ls="--", label=r"MLE: $nR=1$")
    ax.set_xlabel(r"$\theta\, n^{1/4}$")
    ax.set_ylabel(r"$n\,R(\theta,\cdot)$")
    ax.set_yscale("log")
    ax.legend(fontsize=7, frameon=False, loc="lower left")
    common.panel_label(ax, "a", loc="upper left")
    ax = axes[0, 1]
    ns = np.logspace(1, 7, 200)
    eps = 0.5
    sup_h = []
    for n in ns:
        th = np.linspace(0, eps, 3001)
        sup_h.append(n * hodges_risk(th, n).max())
    ax.loglog(ns, sup_h, color=RED, label=r"Hodges: $\sup_{|\theta|<0.5} nR$")
    ax.loglog(ns, [van_trees_bound(n, eps) for n in ns], color=BLUE, label="van Trees lower bound")
    ax.loglog(ns, np.ones_like(ns), color="k", ls="--", lw=1, label="MLE")
    ax.set_xlabel("$n$")
    ax.legend(fontsize=7, frameon=False, loc="upper left")
    common.panel_label(ax, "b", loc="lower right")
    ax = axes[0, 2]
    sig2, c2 = 1.0, 4.0
    nn = np.unique(np.round(np.logspace(0.5, 3, 60)).astype(int))
    nn = nn[nn >= 3]
    ax.semilogx(nn, sig2 + 0 * nn, color="k", ls="--", lw=1, label=r"MLE $\sigma^2$")
    ax.semilogx(nn, [js_risk_scaled(n, sig2, c2) for n in nn], color=RED, label="James-Stein (worst case)")
    ax.axhline(pinsker_risk(sig2, c2), color=BLUE, label=r"$\sigma^2c^2/(\sigma^2+c^2)$")
    ax.axhline(sig2 / (sig2 + c2), color=GREY, ls=":", label=r"printed $\sigma^2/(\sigma^2+c^2)$")
    ax.set_ylim(0, 1.1)
    ax.set_xlabel(r"$n$ (= number of means)")
    ax.set_ylabel("risk")
    ax.legend(fontsize=6.8, frameon=False, loc="lower right")
    common.panel_label(ax, "c", loc="upper left")
    fig.tight_layout()
    return savefig(fig, CH, "fig12-S4", out_dir)


def fig_12_s5(out_dir=None):
    """Section 12.7: risk of X, James-Stein and its positive part; Blyth energies in dimensions 1, 2, 3."""
    fig, axes = common.panel_grid(1, 3, size=(3.5, 2.9))
    ax = axes[0, 0]
    k = 10
    lens = np.linspace(0, 8, 81)
    ax.plot(lens, k + 0 * lens, color="k", ls="--", lw=1, label=r"$X$ (risk $k$)")
    ax.plot(lens, [js_risk(k, l * l) for l in lens], color=ORANGE, label="James-Stein")
    ax.plot(lens, [js_plus_risk(k, l * l) for l in lens], color=RED, label="positive part (12.12)")
    lm = np.array([0, 2, 4, 6, 8.0])
    mc = [js_mc(k, theta_pattern(k, l), 4000, 31 + i)["jsplus"] for i, l in enumerate(lm)]
    ax.plot(lm, mc, "o", ms=4.5, mfc="white", mec="k", label="Monte Carlo")
    ax.set_xlabel(r"$\|\theta\|$")
    ax.set_ylabel("risk")
    ax.set_ylim(0, 11)
    ax.set_title(r"$k=10$")
    ax.legend(fontsize=7, frameon=False, loc="lower right")
    common.panel_label(ax, "a", loc="upper left")
    ax = axes[0, 1]
    ks = np.arange(1, 101)
    for r, col in ((0.0, RED), (0.5, ORANGE), (1.0, GREEN), (2.0, BLUE)):
        ax.plot(ks, [js_plus_risk(k_, r * r * k_) / k_ for k_ in ks], color=col, label=r"$\|\theta\|^2/k=%g$" % (r * r))
    ax.axhline(1, color="k", ls="--", lw=1)
    ax.set_xlabel("dimension $k$")
    ax.set_ylabel("risk / risk of $X$")
    ax.set_ylim(0, 1.08)
    ax.legend(fontsize=7, frameon=False, loc="center right")
    common.panel_label(ax, "b", loc="lower left")
    ax = axes[0, 2]
    nr = np.logspace(0.3, 8, 200)
    for d, col in ((1, BLUE), (2, GREEN), (3, RED)):
        ax.loglog(nr, [blyth_energy(d, r) for r in nr], color=col, label="$d=%d$" % d)
    ax.set_xlabel(r"outer radius $n$")
    ax.set_ylabel(r"$\min\int|\nabla h|^2$")
    ax.legend(fontsize=7.5, frameon=False, loc="lower left")
    common.panel_label(ax, "c", loc="upper right")
    fig.tight_layout()
    return savefig(fig, CH, "fig12-S5", out_dir)


def ex6_table(ks=(3, 5, 10, 20, 50), ratios=(0.0, 0.25, 1.0, 4.0), R=4000, seed=600):
    """Exercise 12.6: simulated and exact risks of X and of the positive-part James-Stein estimator for
    theta with ||theta||^2 = ratio * k (all coordinates equal)."""
    rows = []
    for i, k in enumerate(ks):
        for j, q in enumerate(ratios):
            th = theta_pattern(k, math.sqrt(q * k))
            mc = js_mc(k, th, R, seed + 10 * i + j)
            rows.append((k, q, mc["mle"], mc["jsplus"], js_plus_risk(k, q * k)))
    return rows


def fig_12_s6(out_dir=None):
    """Exercise 12.6: risk of the positive-part James-Stein estimator relative to X, by simulation."""
    fig, axes = common.panel_grid(1, 2, size=(3.9, 3.0))
    ax = axes[0, 0]
    rows = ex6_table(ks=(3, 5, 10, 20, 50), ratios=(0.0, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0))
    cols = {3: BLUE, 5: GREEN, 10: ORANGE, 20: RED, 50: PURPLE}
    for k in (3, 5, 10, 20, 50):
        rr = [r for r in rows if r[0] == k]
        q = np.array([r[1] for r in rr])
        ax.plot(q, [r[3] / r[2] for r in rr], "o", color=cols[k], ms=4)
        qq = np.linspace(0, 8, 81)
        ax.plot(qq, [js_plus_risk(k, x * k) / k for x in qq], color=cols[k], lw=1.2, label="$k=%d$" % k)
    ax.axhline(1, color="k", ls="--", lw=1)
    ax.set_xlabel(r"$\|\theta\|^2/k$")
    ax.set_ylabel(r"risk(JS$^+$) / risk($X$)")
    ax.legend(fontsize=7, frameon=False, loc="lower right")
    common.panel_label(ax, "a", loc="upper left")
    ax = axes[0, 1]
    # sample size n per coordinate: X_j = mean of n draws from N(theta_j, 1), so X_j ~ N(theta_j, 1/n)
    k = 10
    th = theta_pattern(k, 1.0)
    for n, col in ((1, BLUE), (4, GREEN), (16, ORANGE), (64, RED)):
        lens = np.linspace(0, 3, 61)
        ax.plot(lens, [js_plus_risk(k, n * l * l) / k for l in lens], color=col, label="$n=%d$" % n)
    ax.axhline(1, color="k", ls="--", lw=1)
    ax.set_xlabel(r"$\|\theta\|$ (k = 10 means)")
    ax.set_ylabel("relative risk")
    ax.legend(fontsize=7, frameon=False, loc="lower right")
    common.panel_label(ax, "b", loc="upper left")
    fig.tight_layout()
    return savefig(fig, CH, "fig12-S6", out_dir)


FIGURES = {"12.1": fig_12_1, "12.2": fig_12_2, "12.3": fig_12_3, "12.S1": fig_12_s1, "12.S2": fig_12_s2,
           "12.S3": fig_12_s3, "12.S4": fig_12_s4, "12.S5": fig_12_s5, "12.S6": fig_12_s6}

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