"""Murphy (2023), Chapter 36: Causality (book pp. 1185-1233; the supplement has only the chapter title page).

Companion code for Chapter 36 of K. P. Murphy, Probabilistic Machine Learning: Advanced Topics (MIT Press, 2023;
December 2025 draft), written from scratch with numpy, scipy and matplotlib, to be read next to the lecture notes
(../ch36-causality.html). Every estimator of the chapter is implemented by hand (ordinary least squares, Newton
fitting of a logistic propensity model, outcome adjustment, inverse probability weighting, the Hajek version,
the augmented estimator with and without cross fitting, the estimator of the average effect on the treated, matching
on a discrete covariate, the Wald estimator, two stage least squares, the double machine learning estimator of the
local average treatment effect, difference in differences) and the sensitivity analysis of Austen plots
(the bias formula, the trigamma formula for the outcome influence, the observed-covariate calibration).

Methods (all in this module)
* tools: ols, logistic_fit, features, sig;
* adjustment (Sections 36.3-36.4): rct_estimate, adjust_estimates, aiptw_crossfit, att_aiptw, matching_discrete;
* instruments (Section 36.5): wald, tsls, late_dml;
* difference in differences (Section 36.6): did_means, did_adjusted;
* sensitivity (Section 36.7): austen_bias, austen_r2y, austen_curve, observed_calibration, simulate_austen.

Figures reproduced: 36.1 (a stand-in simulation: the ice cream and drowning data are not distributed with the
book), 36.2 (the smoking graph and its mutilated graph, with the distributions they imply), 36.3 (the triangle graph
with the estimators of Section 36.4 on simulated data), 36.4 (M-bias), 36.5 (the instrumental variable graph with
ordinary and two stage least squares), 36.6 (difference in differences), 36.7 (an Austen plot on simulated data in
place of the blood pressure study). Own figures: x36.1 (the product-rate property of the augmented estimator),
x36.2 (overlap), x36.3 (the local average treatment effect, defiers and weak instruments), x36.4 (the Austen
bias formula against simulation), x36.5 (the front-door formula).

Skipped figures, with reasons: Figure 36.1(b) (smoking and lung cancer across countries, a plot from Our World in
Data: the data are not distributed with the book and are not bundled with any library; the panel (a) stand-in
shows the same logic), Figure 36.8 (a three-node diagram, redrawn inside x36.5).

Data. Every data set is simulated from the structural models written in the notes (the book's examples use data
that are not distributed with it: the ice cream and smoking data, the Card and Krueger fast food survey, the blood
pressure study). The samples used by the demonstrations are exported to ../data/ch36-*.json.
"""
import json
import os
import sys

import numpy as np
from scipy import special, stats

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

CH = 36
DATA_OUT = os.path.normpath(os.path.join(HERE, "..", "data"))
sig = common.sigmoid


# =========================================================================== basic tools
def ols(X, y):
    """Least squares coefficients (X is the full design matrix, with its intercept column)."""
    return np.linalg.lstsq(X, y, rcond=None)[0]


def ols_se(X, y):
    """Coefficients and homoskedastic standard errors."""
    n, p = X.shape
    b = ols(X, y)
    r = y - X @ b
    s2 = r @ r / (n - p)
    cov = s2 * np.linalg.inv(X.T @ X)
    return b, np.sqrt(np.diag(cov))


def logistic_fit(X, a, iters=60, tol=1e-10, ridge=1e-8):
    """Maximum likelihood logistic regression by Newton's method (Fisher scoring). X includes the intercept."""
    beta = np.zeros(X.shape[1])
    for _ in range(iters):
        p = sig(X @ beta)
        W = np.maximum(p * (1 - p), 1e-10)
        grad = X.T @ (a - p) - ridge * beta
        H = (X * W[:, None]).T @ X + ridge * np.eye(X.shape[1])
        step = np.linalg.solve(H, grad)
        beta = beta + step
        if np.max(np.abs(step)) < tol:
            break
    return beta


def features(x, kind="lin"):
    """Design columns for a covariate x of shape (n,) or (n,p): intercept, x, and x^2 when kind is 'quad'."""
    x = np.asarray(x, float)
    if x.ndim == 1:
        x = x[:, None]
    cols = [np.ones(len(x)), *x.T]
    if kind == "quad":
        cols += [c for c in (x ** 2).T]
    return np.column_stack(cols)


# =========================================================================== Section 36.3: randomised trials
def rct_estimate(y, a):
    """Difference in means (36.17) with the Neyman standard error and a 95 percent confidence interval."""
    y1, y0 = y[a == 1], y[a == 0]
    tau = y1.mean() - y0.mean()
    se = np.sqrt(y1.var(ddof=1) / len(y1) + y0.var(ddof=1) / len(y0))
    return tau, se, (tau - 1.96 * se, tau + 1.96 * se)


# =========================================================================== Section 36.4: adjustment
def fit_nuisance(x, a, y, qk="quad", gk="quad"):
    """Outcome model Q(a, x) = E[Y | A=a, X=x] by least squares (columns 1, a, features) and propensity g by logistic fit."""
    Fq = features(x, qk)
    Xq = np.column_stack([Fq[:, :1], a, Fq[:, 1:]])
    bq = ols(Xq, y)
    ones, zeros = np.ones(len(y)), np.zeros(len(y))
    Q1 = np.column_stack([Fq[:, :1], ones, Fq[:, 1:]]) @ bq
    Q0 = np.column_stack([Fq[:, :1], zeros, Fq[:, 1:]]) @ bq
    Fg = features(x, gk)
    bg = logistic_fit(Fg, a)
    g = sig(Fg @ bg)
    return Q1, Q0, g, bq, bg


def adjust_estimates(x, a, y, qk="quad", gk="quad", clip=0.0):
    """The estimators of Section 36.4 on one sample: naive difference in means, outcome regression (36.26), IPTW (36.28),
    Hajek (36.29) and AIPTW (36.30), each with a plug-in standard error; returns a dict name -> (estimate, se)."""
    n = len(y)
    Q1, Q0, g, bq, bg = fit_nuisance(x, a, y, qk, gk)
    if clip > 0:
        g = np.clip(g, clip, 1 - clip)
    Q = np.where(a == 1, Q1, Q0)
    out = {}
    tau0, se0, _ = rct_estimate(y, a)
    out["naive"] = (tau0, se0)
    # outcome regression: for a model without treatment-covariate interaction Q1 - Q0 is the coefficient of A
    Fq = features(x, qk)
    Xq = np.column_stack([Fq[:, :1], a, Fq[:, 1:]])
    b, se = ols_se(Xq, y)
    out["regression"] = (np.mean(Q1 - Q0), se[1])
    w1, w0 = a / g, (1 - a) / (1 - g)
    ipw = y * w1 - y * w0
    out["iptw"] = (ipw.mean(), np.sqrt(np.mean((ipw - ipw.mean()) ** 2) / n))
    haj = (y * w1).sum() / w1.sum() - (y * w0).sum() / w0.sum()
    phi_h = (a * (y - (y * w1).sum() / w1.sum()) / g) / np.mean(w1) - ((1 - a) * (y - (y * w0).sum() / w0.sum()) / (1 - g)) / np.mean(w0)
    out["hajek"] = (haj, np.sqrt(np.mean(phi_h ** 2) / n))
    psi = Q1 - Q0 + a * (y - Q1) / g - (1 - a) * (y - Q0) / (1 - g)
    ai = psi.mean()
    out["aiptw"] = (ai, np.sqrt(np.mean((psi - ai) ** 2) / n))
    return out


def aiptw_crossfit(x, a, y, qk="quad", gk="quad", K=5, rng=None):
    """AIPTW with K-fold cross fitting (Section 36.4.2.4): the nuisance functions of the points in fold j are fitted
    on the other folds. Returns (estimate, standard error)."""
    n = len(y)
    rng = rng or np.random.default_rng(0)
    folds = rng.permutation(n) % K
    Q1 = np.zeros(n)
    Q0 = np.zeros(n)
    g = np.zeros(n)
    xx = np.asarray(x, float)
    for k in range(K):
        tr, te = folds != k, folds == k
        _, _, _, bq, bg = fit_nuisance(xx[tr], a[tr], y[tr], qk, gk)
        Fq = features(xx[te], qk)
        m = te.sum()
        Q1[te] = np.column_stack([Fq[:, :1], np.ones(m), Fq[:, 1:]]) @ bq
        Q0[te] = np.column_stack([Fq[:, :1], np.zeros(m), Fq[:, 1:]]) @ bq
        g[te] = sig(features(xx[te], gk) @ bg)
    psi = Q1 - Q0 + a * (y - Q1) / g - (1 - a) * (y - Q0) / (1 - g)
    return psi.mean(), np.sqrt(np.var(psi) / n)


def att_aiptw(a, y, Q0, g):
    """The double machine learning estimator of the effect on the treated (36.45); Q0 and g are fitted values."""
    pA = a.mean()
    r = y - Q0
    tau = np.mean(a / pA * r - (1 - a) * g / (pA * (1 - g)) * r)
    phi = a / pA * r - (1 - a) * g / (pA * (1 - g)) * r - a * tau / pA
    return tau, np.sqrt(np.mean(phi ** 2) / len(y))


def matching_discrete(x, a, y):
    """The matching estimator (36.41) for a discrete covariate x, weighting strata by their empirical frequency;
    strata without both treated and control units are skipped (and their weight renormalised)."""
    tot, wsum = 0.0, 0.0
    for v in np.unique(x):
        s = x == v
        t, c = s & (a == 1), s & (a == 0)
        if t.sum() and c.sum():
            w = s.mean()
            tot += w * (y[t].mean() - y[c].mean())
            wsum += w
    return tot / wsum


def dgp1(n, rng, c=1.5, d=0.0, k=1.5, tau=1.0, b=1.0, sigma=1.0):
    """One normal confounder X; logit g = c x + d (x^2 - 1); Y = tau A + b x + k (x^2 - 1) + sigma noise. The ATE is tau."""
    x = rng.standard_normal(n)
    g = sig(c * x + d * (x ** 2 - 1))
    a = (rng.random(n) < g).astype(float)
    y = tau * a + b * x + k * (x ** 2 - 1) + sigma * rng.standard_normal(n)
    return x, a, y, g


# =========================================================================== Section 36.5: instruments
def wald(y, a, z):
    """Wald estimator: intention-to-treat effect over first stage; returns (estimate, delta-method se, itt, first stage)."""
    n = len(y)
    itt = y[z == 1].mean() - y[z == 0].mean()
    fs = a[z == 1].mean() - a[z == 0].mean()
    tau = itt / fs
    # influence function of the ratio: (psi_itt - tau psi_fs) / fs
    p = z.mean()
    psi_itt = z * (y - y[z == 1].mean()) / p - (1 - z) * (y - y[z == 0].mean()) / (1 - p)
    psi_fs = z * (a - a[z == 1].mean()) / p - (1 - z) * (a - a[z == 0].mean()) / (1 - p)
    se = np.sqrt(np.mean((psi_itt - tau * psi_fs) ** 2) / n) / abs(fs)
    return tau, se, itt, fs


def tsls(y, a, Z, W=None):
    """Two stage least squares: regress a on (1, Z, W), then y on (1, fitted a, W). Z of shape (n,) or (n,k).
    Returns the coefficient of the treatment and its standard error (computed with the original treatment)."""
    n = len(y)
    Z = np.asarray(Z, float).reshape(n, -1)
    W = np.zeros((n, 0)) if W is None else np.asarray(W, float).reshape(n, -1)
    X1 = np.column_stack([np.ones(n), Z, W])
    ahat = X1 @ ols(X1, a)
    X2 = np.column_stack([np.ones(n), ahat, W])
    b = ols(X2, y)
    Xo = np.column_stack([np.ones(n), a, W])
    r = y - Xo @ b
    s2 = r @ r / (n - X2.shape[1])
    se = np.sqrt(s2 * np.linalg.inv(X2.T @ X2)[1, 1])
    return b[1], se


def late_dml(y, a, z, x, K=5, rng=None, pk="lin", mk="quad"):
    """Double machine learning estimator of the LATE (36.78) with cross fitting. Nuisances: mu(z,x) by least squares,
    m(z,x) by least squares on the binary treatment (a linear probability model keeps the code short), p(x) by a logistic fit."""
    n = len(y)
    rng = rng or np.random.default_rng(0)
    folds = rng.permutation(n) % K
    mu1, mu0, m1, m0, p = (np.zeros(n) for _ in range(5))
    for k in range(K):
        tr, te = folds != k, folds == k
        for tgt, o1, o0 in ((y, mu1, mu0), (a, m1, m0)):
            Ft = features(x[tr], mk)
            X = np.column_stack([Ft[:, :1], z[tr], Ft[:, 1:], z[tr, None] * Ft[:, 1:]])
            b = ols(X, tgt[tr])
            Fe = features(x[te], mk)
            for zz, o in ((1.0, o1), (0.0, o0)):
                zc = np.full(te.sum(), zz)
                o[te] = np.column_stack([Fe[:, :1], zc, Fe[:, 1:], zc[:, None] * Fe[:, 1:]]) @ b
        bp = logistic_fit(features(x[tr], pk), z[tr])
        p[te] = sig(features(x[te], pk) @ bp)
    phi_zy = mu1 - mu0 + z * (y - mu1) / p - (1 - z) * (y - mu0) / (1 - p)
    phi_za = m1 - m0 + z * (a - m1) / p - (1 - z) * (a - m0) / (1 - p)
    tau = phi_zy.mean() / phi_za.mean()
    se = np.sqrt(np.mean((phi_zy - tau * phi_za) ** 2) / n) / abs(phi_za.mean())
    return tau, se


def iv_types(n, rng, pc=0.5, pa=0.2, pd=0.0, m=1.0, theta_c=1.0, theta_o=2.0, rho=0.0, sigma=1.0):
    """Randomised encouragement Z with four principal strata (complier, always taker, never taker, defier).
    Y(a) = mu_type + theta_type a + rho z + noise; mu = +m for always takers, -m for never takers, 0 otherwise.
    The effect is theta_c for compliers and theta_o for all other types. Returns (z, a, y, type)."""
    pn = 1 - pc - pa - pd
    typ = rng.choice(4, size=n, p=[pc, pa, pn, pd])      # 0 complier, 1 always, 2 never, 3 defier
    z = (rng.random(n) < 0.5).astype(float)
    a = np.where(typ == 0, z, np.where(typ == 1, 1.0, np.where(typ == 2, 0.0, 1 - z)))
    mu = np.where(typ == 1, m, np.where(typ == 2, -m, 0.0))
    th = np.where(typ == 0, theta_c, theta_o)
    y = mu + th * a + rho * z + sigma * rng.standard_normal(n)
    return z, a, y, typ


def wald_limit(pc=0.5, pa=0.2, pd=0.0, theta_c=1.0, theta_o=2.0, rho=0.0):
    """Probability limit of the Wald estimator in iv_types: (pc theta_c - pd theta_o + rho) / (pc - pd)."""
    return (pc * theta_c - pd * theta_o + rho) / (pc - pd)


# =========================================================================== Section 36.6: difference in differences
def did_means(y0, y1, a):
    """Classical estimator (36.88): mean change of the treated minus mean change of the controls."""
    d = y1 - y0
    return d[a == 1].mean() - d[a == 0].mean()


def did_adjusted(y0, y1, x, a, kind="quad"):
    """Estimator (36.93) of the effect on the treated, by outcome regression on the change: fit E[Y1 - Y0 | A=0, X]
    by least squares in the controls and average the treated units' residual change."""
    d = y1 - y0
    F = features(x, kind)
    b = ols(F[a == 0], d[a == 0])
    return np.mean(d[a == 1] - F[a == 1] @ b)


def dgp_did(n, rng, tau=2.0, shift=1.0, trend=(-0.5, -1.5), sigma=1.0):
    """Two periods; X is the type of restaurant (0 cheap, 1 expensive); expensive types are more often treated (the state
    that raises the wage) and have a worse trend: Y_t = W + t trend_X + tau A t + noise, W unit effect depending on state."""
    x = (rng.random(n) < 0.5).astype(float)
    pr = np.where(x == 1, 0.7, 0.3)
    a = (rng.random(n) < pr).astype(float)
    w = 3.0 + 1.0 * a + 0.5 * x + rng.standard_normal(n)
    tr = np.where(x == 1, trend[1], trend[0])
    y0 = w + sigma * rng.standard_normal(n)
    y1 = w + tr + tau * a + sigma * rng.standard_normal(n)
    return x, a, y0, y1


# =========================================================================== Section 36.7: Austen plots
def austen_bias(alpha, delta, g):
    """Theorem 6: |ATE - tau| = delta alpha/(1-alpha) E[1/g + 1/(1-g)] (the sign convention is discussed in the notes)."""
    return delta * alpha / (1 - alpha) * np.mean(1 / g + 1 / (1 - g))


def austen_v(alpha, g):
    """E[Var(logit g~ | A, X)] = E[ g (psi1(a+1) + psi1(b)) + (1-g) (psi1(a) + psi1(b+1)) ] with a = g (1/alpha - 1),
    b = (1-g)(1/alpha - 1): the trigamma formula of Theorem 7."""
    k = 1 / alpha - 1
    a, b = g * k, (1 - g) * k
    tg = lambda v: special.polygamma(1, v)
    return np.mean(g * (tg(a + 1) + tg(b)) + (1 - g) * (tg(a) + tg(b + 1)))


def austen_r2y(alpha, delta, g, resid_var):
    """Theorem 7: partial R^2 of the unobserved confounder for the outcome."""
    return delta ** 2 * austen_v(alpha, g) / resid_var


def austen_curve(bias, g, resid_var, alphas):
    """Pairs (alpha, R2) with |bias| equal to `bias`: delta(alpha) = bias (1-alpha)/(alpha K), K = E[1/(g(1-g))]."""
    K = np.mean(1 / g + 1 / (1 - g))
    r2 = []
    for al in alphas:
        d = bias * (1 - al) / (al * K)
        r2.append(austen_r2y(al, d, g, resid_var))
    return np.array(r2)


def observed_calibration(X, a, y, qk="lin"):
    """Section 36.7.2.1: for each covariate Z, alpha_Z = 1 - mean(g(1-g)) / mean(g_{-Z}(1-g_{-Z})) and
    R2_Z = 1 - mean((y - Q)^2) / mean((y - Q_{-Z})^2), with the reduced models refitted without Z."""
    n, p = X.shape

    def fit(cols):
        Fx = np.column_stack([np.ones(n), X[:, cols]]) if len(cols) else np.ones((n, 1))
        g = sig(Fx @ logistic_fit(Fx, a))
        Xq = np.column_stack([Fx[:, :1], a, Fx[:, 1:]])
        Q = Xq @ ols(Xq, y)
        return g, Q
    gF, QF = fit(list(range(p)))
    out = []
    for j in range(p):
        cols = [c for c in range(p) if c != j]
        gr, Qr = fit(cols)
        al = 1 - np.mean(gF * (1 - gF)) / np.mean(gr * (1 - gr))
        r2 = 1 - np.mean((y - QF) ** 2) / np.mean((y - Qr) ** 2)
        out.append((al, r2))
    return out, gF, QF


def simulate_austen(n, rng, g, alpha, delta, Q1, Q0):
    """Draw from the Austen sensitivity model (36.103)-(36.105) for given propensities g(x_i) and outcome functions
    Q(1, x_i), Q(0, x_i). Returns dict with the realised ATE (average of E[Y | A=1,X,U] - E[Y | A=0,X,U]) computed by the
    model's own formula, plus a, y and the latent propensity gt."""
    k = 1 / alpha - 1

    def log_gamma(shape):    # log of a Gamma(shape) draw, stable for shapes below 1: G(s) = G(s+1) U^(1/s)
        return np.log(rng.gamma(shape + 1.0)) + np.log(rng.random(np.shape(shape))) / shape
    L = log_gamma(g * k) - log_gamma((1 - g) * k)      # logit of a Beta(g k, (1-g) k) draw
    gt = sig(L)
    a = (rng.random(n) < gt).astype(float)
    # E[logit gt | A, X] in closed form: psi(a+A) - psi(b+1-A)
    ea = g * k
    eb = (1 - g) * k
    m1 = special.digamma(ea + 1) - special.digamma(eb)
    m0 = special.digamma(ea) - special.digamma(eb + 1)
    mA = np.where(a == 1, m1, m0)
    Q = np.where(a == 1, Q1, Q0)
    y = Q + delta * (L - mA) + 0.5 * rng.standard_normal(n)
    # model-implied causal contrast for every unit
    cate = (Q1 + delta * (L - m1)) - (Q0 + delta * (L - m0))
    return dict(a=a, y=y, gt=gt, L=L, ate=cate.mean(), m1=m1, m0=m0)


# =========================================================================== data export for the demonstrations
def obs_sample(seed=36, n=1000, **kw):
    rng = np.random.default_rng(seed)
    p = dict(c=1.0, d=0.6, k=1.2, tau=1.0)
    p.update(kw)
    x, a, y, g = dgp1(n, rng, **p)
    return p, x, a, y, g


def iv_sample(seed=3601, n=1500, **kw):
    rng = np.random.default_rng(seed)
    p = dict(pc=0.5, pa=0.2, pd=0.0, m=1.0, theta_c=1.0, theta_o=2.0, rho=0.0)
    p.update(kw)
    z, a, y, typ = iv_types(n, rng, **p)
    return p, z, a, y, typ


def austen_dataset(seed=3602, n=2000):
    """Stand-in for the blood pressure study: five covariates with different strengths for treatment and outcome."""
    rng = np.random.default_rng(seed)
    X = rng.standard_normal((n, 5))
    wa = np.array([0.9, 0.5, 0.2, 0.0, 0.35])
    wy = np.array([1.0, 0.2, 1.2, 0.0, 0.4])
    g = sig(-0.3 + X @ wa)
    a = (rng.random(n) < g).astype(float)
    tau = 0.6
    y = tau * a + X @ wy + 2.0 * rng.standard_normal(n)
    return X, a, y, tau, ["x1 (both)", "x2 (treatment)", "x3 (outcome)", "x4 (neither)", "x5 (both, weak)"]


def export_obs_json(out=None):
    p, x, a, y, g = obs_sample()
    ref = {}
    for qk, gk in (("lin", "lin"), ("quad", "quad"), ("lin", "quad"), ("quad", "lin")):
        est = adjust_estimates(x, a, y, qk, gk)
        ref["%s,%s" % (qk, gk)] = {k: [round(float(v[0]), 6), round(float(v[1]), 6)] for k, v in est.items()}
    d = dict(params=p, n=len(y), x=np.round(x, 5).tolist(), a=a.astype(int).tolist(), y=np.round(y, 5).tolist(), reference=ref)
    json.dump(d, open(os.path.join(DATA_OUT, "ch36-observational.json"), "w"), separators=(",", ":"))
    return d


def export_iv_json(out=None):
    p, z, a, y, typ = iv_sample()
    w, se, itt, fs = wald(y, a, z)
    d = dict(params=p, n=len(y), z=z.astype(int).tolist(), a=a.astype(int).tolist(), y=np.round(y, 5).tolist(),
             type=typ.astype(int).tolist(),
             reference=dict(wald=round(float(w), 6), se=round(float(se), 6), itt=round(float(itt), 6), first_stage=round(float(fs), 6),
                            naive=round(float(y[a == 1].mean() - y[a == 0].mean()), 6)))
    json.dump(d, open(os.path.join(DATA_OUT, "ch36-iv.json"), "w"), separators=(",", ":"))
    return d


def export_austen_json(out=None):
    X, a, y, tau, names = austen_dataset()
    cal, g, Q = observed_calibration(X, a, y)
    n, p = X.shape
    Fx = np.column_stack([np.ones(n), X])
    Xq = np.column_stack([Fx[:, :1], a, Fx[:, 1:]])
    bq, se = ols_se(Xq, y)
    d = dict(tau=float(bq[1]), se=float(se[1]), n=n, resid_var=float(np.mean((y - Q) ** 2)),
             g=np.round(g, 5).tolist(), names=names,
             calibration=[[round(float(u), 5), round(float(v), 5)] for u, v in cal],
             K=float(np.mean(1 / g + 1 / (1 - g))))
    json.dump(d, open(os.path.join(DATA_OUT, "ch36-austen.json"), "w"), separators=(",", ":"))
    return d


def export_all(out=None):
    """Write the JSON files used by the demonstrations."""
    export_obs_json()
    export_iv_json()
    export_austen_json()
    print("wrote the data files in", DATA_OUT)


# =========================================================================== figure helpers
def draw_dag(ax, pos, edges, dashed=(), colors=None, node_colors=None, r=0.13, title=None, bidir=()):
    """Draw a causal graph: pos maps name -> (x, y); edges are (u, v) arrows; dashed edges are drawn dashed; bidir are
    undirected dashed curves (statistical dependence with an unspecified cause)."""
    from matplotlib.patches import FancyArrowPatch, Circle
    colors = colors or {}
    for (u, v) in edges:
        p0, p1 = np.array(pos[u]), np.array(pos[v])
        d = (p1 - p0) / np.linalg.norm(p1 - p0)
        ar = FancyArrowPatch(p0 + d * r, p1 - d * r, arrowstyle="-|>", mutation_scale=13, lw=1.4,
                             color=colors.get((u, v), "#222222"), linestyle="--" if (u, v) in dashed else "-", shrinkA=0, shrinkB=0)
        ax.add_patch(ar)
    for (u, v) in bidir:
        p0, p1 = np.array(pos[u]), np.array(pos[v])
        d = (p1 - p0) / np.linalg.norm(p1 - p0)
        ax.add_patch(FancyArrowPatch(p0 + d * r, p1 - d * r, arrowstyle="-", lw=1.2, color=GREY, linestyle="--", connectionstyle="arc3,rad=0.35"))
    for k, (x, y) in pos.items():
        nc = (node_colors or {}).get(k, "white")
        ax.add_patch(Circle((x, y), r, fc=nc, ec="#222222", lw=1.2, zorder=3))
        ax.text(x, y, k, ha="center", va="center", fontsize=10, zorder=4)
    xs = [p[0] for p in pos.values()]
    ys = [p[1] for p in pos.values()]
    ax.set_xlim(min(xs) - 0.45, max(xs) + 0.45)
    ax.set_ylim(min(ys) - 0.45, max(ys) + 0.45)
    ax.set_aspect("equal")
    ax.axis("off")
    if title:
        ax.set_title(title, fontsize=10)


def box_by(ax, data, labels, colors, truth, ylim=None, ylabel="estimate"):
    """Box plots (whiskers at the 5th and 95th percentile) of several estimators against the true value."""
    bp = ax.boxplot(data, whis=(5, 95), showfliers=False, patch_artist=True, widths=0.6)
    for patch, c in zip(bp["boxes"], colors):
        patch.set_facecolor(c)
        patch.set_alpha(0.6)
    for m in bp["medians"]:
        m.set_color("black")
    if truth is not None:
        ax.axhline(truth, color="black", ls="--", lw=1)
    ax.set_xticks(range(1, len(labels) + 1))
    ax.set_xticklabels(labels, rotation=30, ha="right", fontsize=8.5)
    ax.set_ylabel(ylabel)
    if ylim:
        ax.set_ylim(*ylim)


# =========================================================================== Figure 36.1
def fig_36_1(out=None):
    """Figure 36.1 (stand-in): ice cream production and drownings are associated through the temperature."""
    rng = np.random.default_rng(3601)
    months = np.tile(np.arange(12), 6)
    temp = 15 + 11 * np.sin(2 * np.pi * (months - 3.5) / 12) + rng.normal(0, 1.5, months.size)
    ice = 200 + 22 * temp + rng.normal(0, 25, months.size)          # production, millions of gallons
    drown = 55 + 1.5 * temp + rng.normal(0, 3.0, months.size)       # deaths, no effect of ice cream
    fig, axes = plt.subplots(1, 3, figsize=(9.6, 3.1))
    ax = axes[0]
    sc = ax.scatter(ice, drown, c=temp, cmap="coolwarm", s=22, edgecolor="none")
    b = np.polyfit(ice, drown, 1)
    xs = np.linspace(ice.min(), ice.max(), 2)
    ax.plot(xs, np.polyval(b, xs), color="black", lw=1.2)
    ax.set_xlabel("ice cream production")
    ax.set_ylabel("deaths by drowning")
    ax.set_title("(a) strong association (slope %.3f)" % b[0])
    cb = fig.colorbar(sc, ax=ax, pad=0.02)
    cb.set_label("temperature", fontsize=8)
    ax = axes[1]
    Xr = np.column_stack([np.ones_like(temp), temp])
    ri = ice - Xr @ ols(Xr, ice)
    rd = drown - Xr @ ols(Xr, drown)
    ax.scatter(ri, rd, c=temp, cmap="coolwarm", s=22, edgecolor="none")
    b2 = np.polyfit(ri, rd, 1)
    xs = np.linspace(ri.min(), ri.max(), 2)
    ax.plot(xs, np.polyval(b2, xs), color="black", lw=1.2)
    ax.set_xlabel("ice cream, residual given temperature")
    ax.set_ylabel("drownings, residual")
    ax.set_title("(b) adjusting for temperature (slope %.3f)" % b2[0])
    ax = axes[2]
    draw_dag(ax, {"T": (0.5, 1.0), "I": (0.0, 0.0), "D": (1.0, 0.0)}, [("T", "I"), ("T", "D")], title="(c) the assumed graph")
    ax.text(0.5, -0.35, "do(I = 0): D unchanged", ha="center", fontsize=8.5)
    fig.tight_layout()
    return savefig(fig, CH, "fig36-1", out)


# =========================================================================== Figure 36.2
def smoking_scm(grid=4001):
    """The SCM of (36.5)-(36.8) with G ~ Bern(0.3), H ~ N(0,1), A = 1{xi < -1.2 H}, Y ~ Bern(sigmoid(-1.5 + 0.8A - 0.9H + G)).
    Returns exact (quadrature) quantities: observational risk difference, the ATE, the adjusted estimand (36.12)."""
    h = np.linspace(-8, 8, grid)
    dh = h[1] - h[0]
    ph = stats.norm.pdf(h)
    pA1 = stats.norm.cdf(-1.2 * h)
    out = {}
    def risk(a, hh):
        return 0.7 * sig(-1.5 + 0.8 * a - 0.9 * hh) + 0.3 * sig(-1.5 + 0.8 * a - 0.9 * hh + 1.0)
    pa1 = np.sum(pA1 * ph) * dh
    ph_given = {1: pA1 * ph / pa1, 0: (1 - pA1) * ph / (1 - pa1)}
    out["h"] = h
    out["ph"] = ph
    out["ph_given"] = ph_given
    out["obs"] = {a: np.sum(risk(a, h) * ph_given[a]) * dh for a in (0, 1)}
    out["do"] = {a: np.sum(risk(a, h) * ph) * dh for a in (0, 1)}
    out["pA1"] = pa1
    return out


def fig_36_2(out=None):
    """Figure 36.2: the smoking graph, its mutilated graph, and the distributions of health consciousness H that the
    conditional and the interventional distributions use."""
    S = smoking_scm()
    pos = {"A": (0.0, 0.6), "Y": (2.0, 0.6), "H": (0.5, 1.5), "G": (1.5, 1.5)}
    edges = [("H", "A"), ("H", "Y"), ("A", "Y"), ("G", "Y")]
    fig = plt.figure(figsize=(9.8, 2.9))
    gs = fig.add_gridspec(1, 3, width_ratios=[1, 1, 1.45])
    ax = fig.add_subplot(gs[0])
    draw_dag(ax, pos, edges, title="(a) causal graph")
    ax = fig.add_subplot(gs[1])
    draw_dag(ax, pos, [e for e in edges if e != ("H", "A")], title="(b) mutilated graph, do(A = a)")
    ax = fig.add_subplot(gs[2])
    h = S["h"]
    m = (h > -3.5) & (h < 3.5)
    ax.plot(h[m], S["ph"][m], color="black", lw=1.6, label="P(H): used by do(A = a)")
    ax.plot(h[m], S["ph_given"][1][m], color=RED, lw=1.6, label="P(H | A = 1), smokers")
    ax.plot(h[m], S["ph_given"][0][m], color=BLUE, lw=1.6, label="P(H | A = 0)")
    ax.set_xlabel("health consciousness H")
    ax.set_ylabel("density")
    ax.set_ylim(0, 0.75)
    ax.legend(fontsize=7.5, frameon=False, loc="upper left")
    ax.set_title("(c) risk difference: conditional %.3f, do %.3f" % (S["obs"][1] - S["obs"][0], S["do"][1] - S["do"][0]), fontsize=9.5)
    fig.tight_layout()
    return savefig(fig, CH, "fig36-2", out)


# =========================================================================== Figure 36.3
SCEN = dict(c=1.0, d=0.6, k=1.2, tau=1.0)


def sim_adjust(R, n, qk, gk, seed, names=("naive", "regression", "iptw", "hajek", "aiptw"), **kw):
    p = dict(SCEN)
    p.update(kw)
    rng = np.random.default_rng(seed)
    res = {k: [] for k in names}
    for _ in range(R):
        x, a, y, g = dgp1(n, rng, **p)
        e = adjust_estimates(x, a, y, qk, gk)
        for k in names:
            res[k].append(e[k][0])
    return {k: np.array(v) for k, v in res.items()}


def fig_36_3(out=None):
    """Figure 36.3: the triangle graph, and the estimators of Section 36.4 when the outcome model, or the propensity model, is wrong."""
    names = ["naive", "regression", "iptw", "hajek", "aiptw"]
    labs = ["naive", "outcome\nregression", "IPTW", "Hajek", "AIPTW"]
    cols = [GREY, BLUE, ORANGE, "#e8b04a", GREEN]
    fig = plt.figure(figsize=(10.2, 3.3))
    gs = fig.add_gridspec(1, 3, width_ratios=[0.8, 1.2, 1.2])
    ax = fig.add_subplot(gs[0])
    draw_dag(ax, {"X": (1.0, 1.0), "A": (0.0, 0.0), "Y": (2.0, 0.0)}, [("X", "A"), ("X", "Y"), ("A", "Y")], title="(a) observed confounders")
    r1 = sim_adjust(500, 500, "lin", "quad", 1)
    r2 = sim_adjust(500, 500, "quad", "lin", 2)
    for k, (r, ttl) in enumerate(((r1, "(b) outcome model wrong (linear Q)"), (r2, "(c) propensity model wrong (linear logit)"))):
        ax = fig.add_subplot(gs[k + 1])
        box_by(ax, [r[n] for n in names], labs, cols, SCEN["tau"], ylim=(-0.5, 4.0))
        ax.set_title(ttl, fontsize=9.5)
    fig.tight_layout()
    return savefig(fig, CH, "fig36-3", out)


# =========================================================================== Figure 36.4
def mbias_sim(R, n, seed, tau=1.0):
    rng = np.random.default_rng(seed)
    un, ad = [], []
    for _ in range(R):
        u1, u2 = rng.standard_normal(n), rng.standard_normal(n)
        a = u1 + rng.standard_normal(n)
        x = u1 + u2 + rng.standard_normal(n)
        y = tau * a + u2 + rng.standard_normal(n)
        un.append(ols(np.column_stack([np.ones(n), a]), y)[1])
        ad.append(ols(np.column_stack([np.ones(n), a, x]), y)[1])
    return np.array(un), np.array(ad)


def other_bias_sim(n, rng):
    """Mediator and collider examples (Section 36.4.5.1): total effect 1.3 through A -> M -> Y and a direct edge 0.5;
    collider C = A + Y + noise with A -> Y of size 1."""
    a = rng.standard_normal(n)
    m = 0.8 * a + rng.standard_normal(n)
    y = 0.5 * a + 1.0 * m + rng.standard_normal(n)
    un = ols(np.column_stack([np.ones(n), a]), y)[1]
    ad = ols(np.column_stack([np.ones(n), a, m]), y)[1]
    a2 = rng.standard_normal(n)
    y2 = 1.0 * a2 + rng.standard_normal(n)
    c = a2 + y2 + rng.standard_normal(n)
    un2 = ols(np.column_stack([np.ones(n), a2]), y2)[1]
    ad2 = ols(np.column_stack([np.ones(n), a2, c]), y2)[1]
    return un, ad, un2, ad2


def fig_36_4(out=None):
    """Figure 36.4: M-bias, with two further ways to adjust for the wrong variable (a mediator and a collider)."""
    fig = plt.figure(figsize=(10.0, 3.2))
    gs = fig.add_gridspec(1, 3, width_ratios=[1.0, 1.15, 1.15])
    ax = fig.add_subplot(gs[0])
    draw_dag(ax, {"U1": (0.0, 1.2), "U2": (2.0, 1.2), "X": (1.0, 1.2), "A": (0.0, 0.0), "Y": (2.0, 0.0)},
             [("U1", "A"), ("U2", "Y"), ("U1", "X"), ("U2", "X"), ("A", "Y")], r=0.15, title="(a) the M-bias graph")
    un, ad = mbias_sim(300, 2000, 11)
    ax = fig.add_subplot(gs[1])
    box_by(ax, [un, ad], ["no adjustment", "adjust for X"], [GREEN, RED], 1.0, ylim=(0.5, 1.2))
    ax.set_title("(b) M-bias (truth 1, n = 2000)", fontsize=9.5)
    rng = np.random.default_rng(12)
    res = np.array([other_bias_sim(2000, rng) for _ in range(300)])
    ax = fig.add_subplot(gs[2])
    box_by(ax, [res[:, 0], res[:, 1], res[:, 2], res[:, 3]],
           ["mediator:\nnone", "mediator:\nadjust M", "collider:\nnone", "collider:\nadjust C"], [GREEN, RED, GREEN, RED], None, ylim=(0.0, 1.6))
    ax.plot([0.55, 2.45], [1.3, 1.3], color="black", ls="--", lw=1)
    ax.plot([2.55, 4.45], [1.0, 1.0], color="black", ls="--", lw=1)
    ax.set_title("(c) dashes: truth 1.3 (total), 1 (collider)", fontsize=9.5)
    fig.tight_layout()
    return savefig(fig, CH, "fig36-4", out)


# =========================================================================== Figure 36.5
def iv_linear(n, rng, alpha, beta=1.0, gamma=2.0):
    u = rng.standard_normal(n)
    z = rng.standard_normal(n)
    a = alpha * z + u + rng.standard_normal(n)
    y = beta * a + gamma * u + rng.standard_normal(n)
    return z, a, y


def fig_36_5(out=None):
    """Figure 36.5: the instrumental variable graph, and ordinary and two stage least squares as the instrument weakens."""
    fig = plt.figure(figsize=(10.2, 3.2))
    gs = fig.add_gridspec(1, 3, width_ratios=[0.9, 1.2, 1.1])
    ax = fig.add_subplot(gs[0])
    draw_dag(ax, {"Z": (0.0, 0.0), "A": (1.0, 0.0), "Y": (2.0, 0.0), "U": (1.5, 1.1)}, [("Z", "A"), ("A", "Y"), ("U", "A"), ("U", "Y")],
             node_colors={"U": "#dddddd"}, title="(a) instrument Z, hidden confounder U")
    rng = np.random.default_rng(21)
    als = np.array([0.05, 0.1, 0.2, 0.4, 0.7, 1.0, 1.5])
    n = 500
    ols_m, q10, q50, q90 = [], [], [], []
    for al in als:
        o, t = [], []
        for _ in range(400):
            z, a, y = iv_linear(n, rng, al)
            o.append(ols(np.column_stack([np.ones(n), a]), y)[1])
            t.append(tsls(y, a, z)[0])
        ols_m.append(np.mean(o))
        lo, md, hi = np.percentile(t, [10, 50, 90])
        q10.append(lo)
        q50.append(md)
        q90.append(hi)
    ax = fig.add_subplot(gs[1])
    ax.fill_between(als, q10, q90, color=GREEN, alpha=0.25, label="2SLS, 10-90%")
    ax.plot(als, q50, color=GREEN, marker="o", ms=3.5, label="2SLS median")
    ax.plot(als, ols_m, color=RED, marker="s", ms=3.5, label="OLS mean")
    ax.axhline(1.0, color="black", ls="--", lw=1)
    ax.set_ylim(-1.5, 3.2)
    ax.set_xlabel("instrument strength alpha")
    ax.set_ylabel("estimate of beta (truth 1)")
    ax.legend(fontsize=7.5, frameon=False, loc="upper right")
    ax.set_title("(b) n = 500, 400 studies per point", fontsize=9.5)
    ax = fig.add_subplot(gs[2])
    for al, col in ((0.1, MAGENTA), (1.0, BLUE)):
        t = np.array([tsls(*(lambda s: (s[2], s[1], s[0]))(iv_linear(n, rng, al)))[0] for _ in range(400)])
        ax.hist(np.clip(t, -5, 7), bins=np.linspace(-5, 7, 61), alpha=0.55, color=col, label="alpha = %.1f" % al, density=True)
    ax.axvline(1.0, color="black", ls="--", lw=1)
    ax.set_xlabel("2SLS estimate (clipped to [-5, 7])")
    ax.set_yticks([])
    ax.legend(fontsize=8, frameon=False)
    ax.set_title("(c) weak instruments: wide, off-centre", fontsize=9.5)
    fig.tight_layout()
    return savefig(fig, CH, "fig36-5", out)


# =========================================================================== Figure 36.6
def fig_36_6(out=None):
    """Figure 36.6: difference in differences, its graph, parallel trends and conditional parallel trends."""
    rng = np.random.default_rng(31)
    fig = plt.figure(figsize=(10.4, 3.3))
    gs = fig.add_gridspec(1, 3, width_ratios=[0.8, 1.1, 1.2])
    ax = fig.add_subplot(gs[0])
    draw_dag(ax, {"A": (0.0, 1.0), "dY": (1.3, 0.0), "X": (1.3, 2.0), "U": (2.6, 1.0)}, [("A", "dY"), ("X", "dY"), ("X", "A"), ("U", "dY")],
             node_colors={"U": "#dddddd"}, bidir=[("U", "A")], r=0.2, title="(a) graph of the change dY = Y1 - Y0")
    # (b): equal trends in both groups
    x, a, y0, y1 = dgp_did(200000, rng, trend=(-1.0, -1.0))
    ax = fig.add_subplot(gs[1])
    mt0, mt1 = y0[a == 1].mean(), y1[a == 1].mean()
    mc0, mc1 = y0[a == 0].mean(), y1[a == 0].mean()
    ax.plot([0, 1], [mc0, mc1], color=BLUE, marker="o", label="control (PA)")
    ax.plot([0, 1], [mt0, mt1], color=RED, marker="o", label="treated (NJ)")
    ax.plot([0, 1], [mt0, mt0 + (mc1 - mc0)], color=RED, ls="--", marker="o", mfc="white", label="treated, no treatment")
    ax.annotate("", xy=(1.04, mt1), xytext=(1.04, mt0 + (mc1 - mc0)), arrowprops=dict(arrowstyle="<->", color="black"))
    ax.text(1.07, 0.5 * (mt1 + mt0 + mc1 - mc0), "tau = %.2f" % did_means(y0, y1, a), va="center", fontsize=8.5)
    ax.set_xlim(-0.1, 1.45)
    ax.set_xticks([0, 1])
    ax.set_xticklabels(["before", "after"])
    ax.set_ylabel("mean outcome")
    ax.legend(fontsize=7.5, frameon=False, loc="lower left")
    ax.set_title("(b) parallel trends (true tau = 2)", fontsize=9.5)
    # (c): conditional parallel trends
    nn, R = 800, 400
    naive, adj = [], []
    for _ in range(R):
        x, a, y0, y1 = dgp_did(nn, rng)
        naive.append(did_means(y0, y1, a))
        adj.append(did_adjusted(y0, y1, x, a, "lin"))
    ax = fig.add_subplot(gs[2])
    box_by(ax, [naive, adj], ["difference in\ndifferences", "adjusted for\nrestaurant type"], [RED, GREEN], 2.0, ylim=(0.5, 3.2))
    ax.set_title("(c) trends depend on type (n = 800)", fontsize=9.5)
    fig.tight_layout()
    return savefig(fig, CH, "fig36-6", out)


# =========================================================================== Figure 36.7
def fig_36_7(out=None):
    """Figure 36.7: an Austen plot on a simulated study with five covariates (stand-in for the blood pressure data)."""
    X, a, y, tau, names = austen_dataset()
    cal, g, Q = observed_calibration(X, a, y)
    n = len(y)
    Xq = np.column_stack([np.ones(n), a, X])
    b, se = ols_se(Xq, y)
    th = b[1]
    rv = np.mean((y - Q) ** 2)
    als = np.linspace(0.003, 0.997, 400)
    fig, ax = plt.subplots(figsize=(4.9, 4.2))
    c0 = austen_curve(th, g, rv, als)
    cl = austen_curve(th - 1.96 * se[1], g, rv, als)
    ch = austen_curve(th + 1.96 * se[1], g, rv, als)
    ax.fill_between(als, np.minimum(cl, ch), np.maximum(cl, ch), color="#bbbbbb", alpha=0.7, lw=0)
    ax.plot(als, c0, color="black", lw=1.6)
    cols = [RED, BLUE, GREEN, ORANGE, PURPLE]
    for (al, r2), nm, col in zip(cal, names, cols):
        ax.scatter([al], [r2], color=col, s=34, zorder=5, label=nm)
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    ax.set_xlabel("influence on treatment (alpha)")
    ax.set_ylabel("influence on outcome (partial R$^2$)")
    ax.set_title("bias = %.2f (the estimated effect)" % th, fontsize=10)
    ax.legend(fontsize=7.5, frameon=False, loc="upper right")
    fig.tight_layout()
    return savefig(fig, CH, "fig36-7", out)


# =========================================================================== own figures
def fig_x36_1(out=None):
    """Figure x36.1 (own): the product-rate property. Nuisance functions are perturbed by n^(-1/4) times a fixed bias
    function; the plug-in and weighting estimators then have error of order n^(-1/4), the augmented estimator n^(-1/2)."""
    p = dict(c=1.0, d=0.6, k=1.2, tau=1.0)
    ns = np.array([250, 500, 1000, 2000, 4000, 8000, 16000])
    R = 300
    rng = np.random.default_rng(41)
    names = ["regression", "iptw", "aiptw"]
    rm = {k: [] for k in names}
    for n in ns:
        err = {k: [] for k in names}
        eps = n ** -0.25
        for _ in range(R):
            x, a, y, g = dgp1(int(n), rng, **p)
            Q1 = p["tau"] + 1.0 * x + p["k"] * (x ** 2 - 1) + eps * (1.0 + 0.5 * np.cos(x))
            Q0 = 0.0 + 1.0 * x + p["k"] * (x ** 2 - 1) + eps * (0.0 + 0.5 * np.cos(x))
            gh = np.clip(g + 0.5 * eps * np.cos(2 * x) * g * (1 - g) * 4 * 0.5, 0.02, 0.98)
            ipw = y * a / gh - y * (1 - a) / (1 - gh)
            psi = Q1 - Q0 + a * (y - Q1) / gh - (1 - a) * (y - Q0) / (1 - gh)
            err["regression"].append(np.mean(Q1 - Q0) - p["tau"])
            err["iptw"].append(ipw.mean() - p["tau"])
            err["aiptw"].append(psi.mean() - p["tau"])
        for k in names:
            rm[k].append(np.sqrt(np.mean(np.square(err[k]))))
    fig, ax = plt.subplots(figsize=(5.2, 3.6))
    for k, col, lab in (("regression", BLUE, "outcome regression"), ("iptw", ORANGE, "IPTW"), ("aiptw", GREEN, "AIPTW")):
        sl = np.polyfit(np.log(ns), np.log(rm[k]), 1)[0]
        ax.loglog(ns, rm[k], marker="o", ms=4, color=col, label="%s (slope %.2f)" % (lab, sl))
    ax.loglog(ns, 0.9 * (ns / ns[0]) ** -0.5 * rm["aiptw"][0] / 0.9, color="black", ls=":", lw=1, label="slope -1/2")
    ax.loglog(ns, rm["regression"][0] * (ns / ns[0]) ** -0.25, color="black", ls="--", lw=1, label="slope -1/4")
    ax.set_xlabel("sample size n")
    ax.set_ylabel("RMSE of the estimate of tau")
    ax.legend(fontsize=7.5, frameon=False)
    fig.tight_layout()
    return savefig(fig, CH, "figx36-1", out)


def fig_x36_2(out=None):
    """Figure x36.2 (own): overlap. The root mean square error of the weighting estimators grows with the confounding
    strength c (more extreme propensities), the efficient standard deviation sigma sqrt(E[1/(g(1-g))]/n) with it."""
    cs = np.array([0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5])
    rng = np.random.default_rng(42)
    n, R, sg = 1000, 300, 1.0
    out_ = {k: [] for k in ("regression", "iptw", "hajek", "aiptw")}
    bound = []
    for c in cs:
        errs = {k: [] for k in out_}
        for _ in range(R):
            x, a, y, g = dgp1(n, rng, c=c, d=0.0, k=0.8, sigma=sg)
            e = adjust_estimates(x, a, y, "quad", "lin")
            for k in errs:
                errs[k].append(e[k][0] - 1.0)
        for k in out_:
            out_[k].append(np.sqrt(np.mean(np.square(errs[k]))))
        xs = rng.standard_normal(400000)
        gg = sig(c * xs)
        bound.append(sg * np.sqrt(np.mean(1 / (gg * (1 - gg))) / n))
    fig, axes = plt.subplots(1, 2, figsize=(8.2, 3.2))
    ax = axes[0]
    for k, col, lab in (("regression", BLUE, "outcome regression"), ("iptw", ORANGE, "IPTW"), ("hajek", "#e8b04a", "Hajek"), ("aiptw", GREEN, "AIPTW")):
        ax.plot(cs, out_[k], marker="o", ms=3.5, color=col, label=lab)
    ax.plot(cs, bound, color="black", ls="--", label="efficient sd")
    ax.set_yscale("log")
    ax.set_xlabel("confounding strength c (logit g = c x)")
    ax.set_ylabel("RMSE (n = 1000)")
    ax.legend(fontsize=7.5, frameon=False)
    ax = axes[1]
    for c, col in ((0.5, BLUE), (2.0, ORANGE), (3.5, RED)):
        x, a, y, g = dgp1(5000, rng, c=c)
        ax.hist(g, bins=np.linspace(0, 1, 41), alpha=0.5, color=col, density=True, label="c = %.1f" % c)
    ax.set_xlabel("true propensity score g(X)")
    ax.set_yticks([])
    ax.legend(fontsize=8, frameon=False)
    fig.tight_layout()
    return savefig(fig, CH, "figx36-2", out)


def fig_x36_3(out=None):
    """Figure x36.3 (own): the Wald estimator when monotonicity fails (defiers) and when the instrument is weak."""
    rng = np.random.default_rng(43)
    pds = np.linspace(0, 0.2, 9)
    est, lim, nai = [], [], []
    for pd in pds:
        z, a, y, typ = iv_types(400000, rng, pc=0.5, pa=0.2, pd=pd)
        est.append(wald(y, a, z)[0])
        lim.append(wald_limit(0.5, 0.2, pd))
        nai.append(y[a == 1].mean() - y[a == 0].mean())
    fig, axes = plt.subplots(1, 2, figsize=(8.4, 3.2))
    ax = axes[0]
    ax.plot(pds, lim, color=GREEN, label="limit of Wald")
    ax.plot(pds, est, color=GREEN, ls="none", marker="o", ms=4, label="Wald, n = 400000")
    ax.plot(pds, nai, color=RED, marker="s", ms=3.5, label="as-treated difference")
    ax.axhline(1.0, color="black", ls="--", lw=1, label="LATE = 1 (compliers)")
    ax.set_xlabel("share of defiers")
    ax.set_ylabel("estimate")
    ax.legend(fontsize=7.5, frameon=False)
    ax.set_title("(a) monotonicity", fontsize=9.5)
    ax = axes[1]
    for pc, col in ((0.5, BLUE), (0.15, ORANGE), (0.05, RED)):
        t = np.array([wald(*(lambda s: (s[2], s[1], s[0]))(iv_types(2000, rng, pc=pc, pa=0.2)))[0] for _ in range(1000)])
        ax.hist(np.clip(t, -8, 10), bins=np.linspace(-8, 10, 73), alpha=0.5, color=col, density=True, label="compliers %.0f%%" % (100 * pc))
    ax.axvline(1.0, color="black", ls="--", lw=1)
    ax.set_xlabel("Wald estimate (clipped to [-8, 10])")
    ax.set_yticks([])
    ax.legend(fontsize=7.5, frameon=False)
    ax.set_title("(b) weak instruments, n = 2000", fontsize=9.5)
    fig.tight_layout()
    return savefig(fig, CH, "figx36-3", out)


def fig_x36_4(out=None):
    """Figure x36.4 (own): the bias of the Austen model against simulation, and the latent propensity score for three values of alpha."""
    X, a0, y0, tau, names = austen_dataset()
    cal, g, Q = observed_calibration(X, a0, y0)
    n = len(g)
    Q1, Q0 = np.full(n, 0.6), np.zeros(n)
    rng = np.random.default_rng(44)
    als = np.array([0.05, 0.1, 0.2, 0.35, 0.5, 0.7])
    fig, axes = plt.subplots(1, 2, figsize=(8.4, 3.3))
    ax = axes[0]
    fine = np.linspace(0.02, 0.8, 100)
    for dl, col in ((0.25, BLUE), (0.5, GREEN), (1.0, RED)):
        ax.plot(fine, [austen_bias(al, dl, g) for al in fine], color=col, label="delta = %.2f (formula)" % dl)
        sim = []
        for al in als:
            r = [simulate_austen(n, rng, g, al, dl, Q1, Q0) for _ in range(20)]
            sim.append(np.mean([0.6 - q["ate"] for q in r]))
        ax.plot(als, sim, color=col, ls="none", marker="o", ms=4)
    ax.set_ylim(0, 4)
    ax.set_xlabel("alpha")
    ax.set_ylabel("tau minus the average treatment effect")
    ax.legend(fontsize=7.5, frameon=False)
    ax.set_title("(a) bias: formula (lines), simulation (dots)", fontsize=9.5)
    ax = axes[1]
    u = np.linspace(0.001, 0.999, 500)
    for al, col in ((0.1, BLUE), (0.3, GREEN), (0.6, RED)):
        k = 1 / al - 1
        ax.plot(u, stats.beta.pdf(u, 0.3 * k, 0.7 * k), color=col, label="alpha = %.1f" % al)
    ax.axvline(0.3, color="black", ls=":", lw=1)
    ax.set_ylim(0, 8)
    ax.set_xlabel("latent propensity score g~(X, U), with g(X) = 0.3")
    ax.set_yticks([])
    ax.legend(fontsize=8, frameon=False)
    ax.set_title("(b) Beta(g (1/alpha - 1), (1 - g)(1/alpha - 1))", fontsize=9.5)
    fig.tight_layout()
    return savefig(fig, CH, "figx36-4", out)


def frontdoor_example():
    """Binary front-door example: U ~ Bern(.5); A | U ~ Bern(.2 + .6 U); M | A ~ Bern(.1 + .7 A); Y | M, U ~ Bern(.1 + .5 M + .3 U).
    Returns exact P(Y=1 | do(A=a)), the front-door value (36.114) and the naive P(Y=1 | A=a)."""
    pu = np.array([0.5, 0.5])
    pa_u = lambda a, u: (0.2 + 0.6 * u) if a == 1 else 1 - (0.2 + 0.6 * u)
    pm_a = lambda m, a: (0.1 + 0.7 * a) if m == 1 else 1 - (0.1 + 0.7 * a)
    py_mu = lambda m, u: 0.1 + 0.5 * m + 0.3 * u
    joint = {}
    for u in (0, 1):
        for a in (0, 1):
            for m in (0, 1):
                for y in (0, 1):
                    py = py_mu(m, u) if y == 1 else 1 - py_mu(m, u)
                    joint[(u, a, m, y)] = pu[u] * pa_u(a, u) * pm_a(m, a) * py
    def marg(**kw):
        return sum(v for k, v in joint.items() if all(k["uamy".index(c)] == val for c, val in kw.items()))
    true_do = {a: sum(pm_a(m, a) * sum(pu[u] * py_mu(m, u) for u in (0, 1)) for m in (0, 1)) for a in (0, 1)}
    pA = {a: marg(a=a) for a in (0, 1)}
    pMgA = {(m, a): marg(a=a, m=m) / pA[a] for a in (0, 1) for m in (0, 1)}
    pYgMA = {(m, a): marg(a=a, m=m, y=1) / marg(a=a, m=m) for a in (0, 1) for m in (0, 1)}
    front = {a: sum(pMgA[(m, a)] * sum(pA[a2] * pYgMA[(m, a2)] for a2 in (0, 1)) for m in (0, 1)) for a in (0, 1)}
    naive = {a: marg(a=a, y=1) / pA[a] for a in (0, 1)}
    return true_do, front, naive


def fig_x36_5(out=None):
    """Figure x36.5 (own): the front-door graph (Figure 36.8) and the front-door formula on a binary example."""
    td, fr, nv = frontdoor_example()
    fig, axes = plt.subplots(1, 2, figsize=(8.2, 3.0), gridspec_kw=dict(width_ratios=[1, 1.1]))
    draw_dag(axes[0], {"A": (0.0, 0.0), "M": (1.0, 0.0), "Y": (2.0, 0.0), "U": (1.0, 1.1)}, [("A", "M"), ("M", "Y"), ("U", "A"), ("U", "Y")],
             node_colors={"U": "#dddddd"}, title="(a) front-door graph")
    ax = axes[1]
    w = 0.26
    for j, (d, col, lab) in enumerate(((td, BLUE, "truth P(Y=1 | do(A=a))"), (fr, GREEN, "front-door formula"), (nv, RED, "naive P(Y=1 | A=a)"))):
        ax.bar(np.array([0, 1]) + (j - 1) * w, [d[0], d[1]], width=w, color=col, alpha=0.8, label=lab)
    ax.set_xticks([0, 1])
    ax.set_xticklabels(["a = 0", "a = 1"])
    ax.set_ylim(0, 0.85)
    ax.legend(fontsize=7.5, frameon=False, loc="upper left")
    ax.set_title("(b) risk of Y = 1", fontsize=9.5)
    fig.tight_layout()
    return savefig(fig, CH, "figx36-5", out)


FIGURES = {
    "36.1": fig_36_1,
    "36.2": fig_36_2,
    "36.3": fig_36_3,
    "36.4": fig_36_4,
    "36.5": fig_36_5,
    "36.6": fig_36_6,
    "36.7": fig_36_7,
    "x36.1": fig_x36_1,
    "x36.2": fig_x36_2,
    "x36.3": fig_x36_3,
    "x36.4": fig_x36_4,
    "x36.5": fig_x36_5,
    "data": export_all,
}

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