"""Murphy (2023), Chapter 7: Inference algorithms: an overview.

Companion code for Chapter 7 of K. P. Murphy, Probabilistic Machine Learning: Advanced Topics
(MIT Press, 2023; December 2025 draft), pp. 345-357, written from scratch with numpy, scipy and
matplotlib, to be read next to the lecture notes (../ch07-inference-overview.html).

Methods (all on the one-dimensional beta-Bernoulli running example, 10 heads and 1 tail, uniform prior)
* Section 7.4.1: MAP versus mean (Figure 7.2) and the failure of reparameterisation invariance
  (own figure, the analogue of the book's Figure 2.12).
* Section 7.4.2: grid approximation (7.20)-(7.22), Figure 7.3a.
* Section 7.4.3: Laplace approximation (7.24)-(7.28), in theta and in the logit, Figure 7.3b.
* Section 7.4.4: automatic differentiation variational inference with the reparameterisation
  trick and Adam, Figure 7.4 (the gradients are derived by hand, no autodiff library).
* Section 7.4.5: Hamiltonian Monte Carlo on the logit, 4 chains, Figure 7.5 (own leapfrog code).
* Section 7.4.7: MAP, Gaussian, correlated and independent samples for a bimodal 2-d target
  (own version of Figure 7.6, whose original is a hand-made sketch adapted from the literature).

Skipped: Figure 7.1 (graphical models, a diagram with nothing computed).

Data: none; everything is simulated or analytic (seeds fixed in the code).
"""
import os
import sys

import numpy as np
from scipy import special, stats

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from common import BLUE, GREEN, GREY, ORANGE, PURPLE, RED, plt, savefig, run, sigmoid  # noqa: E402

CH = 7
N1, N0 = 10, 1                      # heads and tails of the running example
A_POST, B_POST = N1 + 1, N0 + 1     # exact posterior Beta(11, 2) under the uniform prior


# ------------------------------------------------------------------ exact and grid
def exact_posterior():
    return stats.beta(A_POST, B_POST)


def grid_posterior(K, n1=N1, n0=N0):
    """Grid approximation (7.20)-(7.22) on K cell centres of [0,1]; returns centres, p_k, delta."""
    delta = 1.0 / K
    th = (np.arange(K) + 0.5) * delta
    ptilde = th ** n1 * (1 - th) ** n0            # uniform prior
    pk = ptilde / ptilde.sum()
    return th, pk, delta                         # density estimate at cell k is pk / delta


# ------------------------------------------------------------------ Laplace
def laplace_theta(n1=N1, n0=N0):
    """Laplace approximation of the Beta(n1+1, n0+1) posterior directly in theta: (mode, variance)."""
    mode = n1 / (n1 + n0)
    H = n1 / mode ** 2 + n0 / (1 - mode) ** 2
    return mode, 1.0 / H


def laplace_logit(n1=N1, n0=N0):
    """Laplace approximation in alpha = logit(theta). The log joint in alpha is
    (n1+1) log sigma(alpha) + (n0+1) log(1-sigma(alpha)); mode alpha* = log((n1+1)/(n0+1)),
    Hessian H = (n1+n0+2) sigma(1-sigma) at the mode. Returns (mode, variance)."""
    a, b = n1 + 1, n0 + 1
    mode = np.log(a / b)
    s = a / (a + b)
    H = (a + b) * s * (1 - s)
    return mode, 1.0 / H


def laplace_logZ_theta(n1=N1, n0=N0):
    """log of the Laplace estimate (7.28) of p(D) for one particular sequence, in theta."""
    mode, var = laplace_theta(n1, n0)
    logjoint = n1 * np.log(mode) + n0 * np.log(1 - mode)
    return logjoint + 0.5 * np.log(2 * np.pi * var)


def exact_logZ(n1=N1, n0=N0):
    return special.betaln(n1 + 1, n0 + 1)


# ------------------------------------------------------------------ ADVI
def advi(n_iter=1000, lr=0.05, seed=0, n1=N1, n0=N0, S=10):
    """ADVI for the beta-Bernoulli posterior. theta = sigmoid(z), q(z) = N(mu, sigma^2).
    The log joint of z (including the Jacobian) is a*log s(z) + b*log(1-s(z)) with a=n1+1, b=n0+1.
    The ELBO is E_q[log joint(z)] + H[q]; gradients by the reparameterisation z = mu + sigma*eps;
    optimiser Adam. Returns dict with the trace of the negative ELBO (estimated on the minibatch),
    mu_t, sigma_t and the final parameters."""
    rng = np.random.default_rng(seed)
    a, b = n1 + 1, n0 + 1
    params = np.array([0.0, np.log(1.0)])      # mu, log sigma
    m = np.zeros(2)
    v = np.zeros(2)
    out = {"nelbo": [], "mu": [], "sigma": []}
    for t in range(1, n_iter + 1):
        mu, ls = params
        sig = np.exp(ls)
        eps = rng.standard_normal(S)
        z = mu + sig * eps
        s = sigmoid(z)
        logjoint = a * np.log(s) + b * np.log1p(-s)
        gz = a * (1 - s) - b * s                    # d logjoint / dz
        elbo = logjoint.mean() + ls + 0.5 * np.log(2 * np.pi * np.e)
        grad = np.array([gz.mean(), (gz * eps * sig).mean() + 1.0])
        # Adam ascent on the ELBO
        m = 0.9 * m + 0.1 * grad
        v = 0.999 * v + 0.001 * grad ** 2
        step = lr * (m / (1 - 0.9 ** t)) / (np.sqrt(v / (1 - 0.999 ** t)) + 1e-8)
        params = params + step
        out["nelbo"].append(-elbo)
        out["mu"].append(params[0])
        out["sigma"].append(np.exp(params[1]))
    out["final"] = (float(np.mean(out["mu"][-200:])), float(np.mean(out["sigma"][-200:])))   # tail average of the iterates
    return out


def elbo_quadrature(mu, sigma, n1=N1, n0=N0, n=4001):
    """Exact (quadrature) ELBO of q(z)=N(mu,sigma^2) for the beta-Bernoulli model, in the sense of
    the log joint of theta = log p(D|theta)p(theta) = n1 log th + n0 log(1-th), with density of z."""
    z = np.linspace(mu - 10 * sigma, mu + 10 * sigma, n)
    qz = stats.norm.pdf(z, mu, sigma)
    s = sigmoid(z)
    logjoint_z = (n1 + 1) * np.log(s) + (n0 + 1) * np.log1p(-s)
    return np.trapz(qz * (logjoint_z - np.log(qz)), z)


def advi_density(mu, sigma, theta):
    """Density of theta = sigmoid(z), z ~ N(mu, sigma^2)."""
    z = np.log(theta / (1 - theta))
    return stats.norm.pdf(z, mu, sigma) / (theta * (1 - theta))


# ------------------------------------------------------------------ HMC
def hmc_chain(logp_grad, x0, n_samples, step, n_leap, rng):
    """Hamiltonian Monte Carlo for a scalar or vector target. logp_grad(x) -> (log p, grad)."""
    x = np.atleast_1d(np.array(x0, float))
    lp, g = logp_grad(x)
    out = np.empty((n_samples, x.size))
    acc = 0
    for i in range(n_samples):
        p = rng.standard_normal(x.size)
        xn, pn, gn = x.copy(), p.copy(), g.copy()
        pn = pn + 0.5 * step * gn
        for l in range(n_leap):
            xn = xn + step * pn
            lpn, gn = logp_grad(xn)
            if l < n_leap - 1:
                pn = pn + step * gn
        pn = pn + 0.5 * step * gn
        logr = (lpn - 0.5 * pn @ pn) - (lp - 0.5 * p @ p)
        if np.log(rng.uniform()) < logr:
            x, lp, g = xn, lpn, gn
            acc += 1
        out[i] = x
    return out, acc / n_samples


def logit_target(z, n1=N1, n0=N0):
    """Log density (unnormalised) and gradient of alpha = logit(theta) for the posterior."""
    z = np.asarray(z, float)
    s = sigmoid(z)
    a, b = n1 + 1, n0 + 1
    lp = a * np.log(s) + b * np.log1p(-s)
    g = a * (1 - s) - b * s
    return float(np.sum(lp)), np.atleast_1d(g)


def hmc_beta_binomial(n_chains=4, n_samples=1000, seed=1):
    rng = np.random.default_rng(seed)
    starts = [-2.0, -0.5, 1.0, 3.0]
    chains, accs = [], []
    for c in range(n_chains):
        ch, acc = hmc_chain(logit_target, starts[c % 4], n_samples, 0.3, 5, rng)
        chains.append(ch[:, 0])
        accs.append(acc)
    return np.array(chains), accs


# ------------------------------------------------------------------ Metropolis-Hastings (for the proof check)
def random_walk_mh(logp, x0, n, scale, rng):
    x = np.atleast_1d(np.array(x0, float))
    lp = logp(x)
    out = np.empty((n, x.size))
    acc = 0
    for i in range(n):
        y = x + scale * rng.standard_normal(x.size)
        lq = logp(y)
        if np.log(rng.uniform()) < lq - lp:
            x, lp = y, lq
            acc += 1
        out[i] = x
    return out, acc / n


# ------------------------------------------------------------------ figures
def fig_7_2(out=None):
    """Figure 7.2: the mode is untypical of the distribution; the mean is a better summary."""
    fig, ax = plt.subplots(1, 2, figsize=(7.6, 2.8))
    x = np.linspace(-3, 5, 2000)
    p = 0.5 * stats.norm.pdf(x, 0, np.sqrt(2)) + 0.5 * stats.norm.pdf(x, 2, np.sqrt(0.05))
    ax[0].plot(x, p, color=BLUE)
    ax[0].axvline(2.0, color="k", lw=1.2, label="mode")
    ax[0].axvline(1.0, color=RED, lw=1.2, label="mean")
    ax[0].set_xlabel("$x$"); ax[0].set_ylabel("$p(x)$"); ax[0].legend(); ax[0].set_title("bimodal mixture")
    x2 = np.linspace(0, 6, 600)
    ax[1].plot(x2, stats.gamma.pdf(x2, 1), color=BLUE)
    ax[1].axvline(0, color="k", lw=1.2, label="mode")
    ax[1].axvline(1, color=RED, lw=1.2, label="mean")
    ax[1].set_xlabel("$x$"); ax[1].set_title("Ga(1, 1)"); ax[1].legend()
    fig.tight_layout()
    savefig(fig, CH, "fig7-2", out)


def fig_own_7_1(out=None):
    """Own figure: the MAP estimate is not invariant to reparameterisation (the book's Figure 2.12 idea)."""
    rng = np.random.default_rng(0)
    x = rng.normal(6, 1, 200000)
    y = 1 / (1 + np.exp(-x + 5))
    fig, ax = plt.subplots(1, 2, figsize=(7.6, 2.8))
    xs = np.linspace(2, 10, 400)
    ax[0].plot(xs, stats.norm.pdf(xs, 6, 1), color=BLUE)
    ax[0].axvline(6, color="k", lw=1)
    ax[0].set_xlabel("$x$"); ax[0].set_title("$x\\sim\\mathcal{N}(6,1)$, mode 6")
    ax[1].hist(y, bins=120, density=True, color=BLUE, alpha=.6)
    yy = np.linspace(0.005, 0.995, 400)
    xin = 5 + np.log(yy / (1 - yy))
    py = stats.norm.pdf(xin, 6, 1) / (yy * (1 - yy))
    ax[1].plot(yy, py, color=GREY, lw=1)
    mode_y = yy[np.argmax(py)]
    f6 = 1 / (1 + np.exp(-1.0))
    ax[1].axvline(mode_y, color="k", lw=1.2, label="mode of $p_y$ (%.3f)" % mode_y)
    ax[1].axvline(f6, color=RED, lw=1.2, ls="--", label="$f(6)$ (%.3f)" % f6)
    ax[1].set_xlabel("$y=f(x)=\\sigma(x-5)$"); ax[1].legend(); ax[1].set_title("transformed density")
    fig.tight_layout()
    savefig(fig, CH, "own7-1", out)


def fig_7_3(out=None):
    """Figure 7.3: grid approximation and Laplace approximation of the beta-Bernoulli posterior."""
    fig, ax = plt.subplots(1, 2, figsize=(7.6, 2.8), sharey=True)
    th = np.linspace(0.001, 0.999, 800)
    post = exact_posterior()
    for a in ax:
        a.plot(th, post.pdf(th), color=BLUE, label="true posterior")
        a.set_xlabel("$\\theta$")
    c, pk, d = grid_posterior(20)
    ax[0].plot(c, pk / d, "o", color=RED, ms=4, label="grid, 20 points")
    ax[0].plot(c, pk / d, "-", color=RED, lw=0.8)
    ax[0].legend(); ax[0].set_ylabel("$p(\\theta)$"); ax[0].set_title("(a) grid approximation")
    mode, var = laplace_theta()
    ax[1].plot(th, stats.norm.pdf(th, mode, np.sqrt(var)), color=RED, label="Laplace in $\\theta$")
    zm, zv = laplace_logit()
    zz = np.log(th / (1 - th))
    ax[1].plot(th, stats.norm.pdf(zz, zm, np.sqrt(zv)) / (th * (1 - th)), color=GREEN, ls="--", label="Laplace in logit($\\theta$)")
    ax[1].set_ylim(0, 6.5); ax[1].legend(fontsize=8); ax[1].set_title("(b) Laplace approximation")
    fig.tight_layout()
    savefig(fig, CH, "fig7-3", out)


def fig_7_4(out=None):
    """Figure 7.4: ADVI applied to the beta-Bernoulli model."""
    r = advi()
    fig, ax = plt.subplots(2, 2, figsize=(7.6, 5.2))
    th = np.linspace(0.001, 0.999, 800)
    mu, sg = r["final"]
    ax[0, 0].plot(th, exact_posterior().pdf(th), color=BLUE, label="true posterior")
    ax[0, 0].plot(th, advi_density(mu, sg, th), color=RED, label="variational $q$")
    ax[0, 0].set_xlabel("$\\theta$"); ax[0, 0].legend(fontsize=8); ax[0, 0].set_title("(a) approximate vs true")
    ax[0, 1].plot(r["nelbo"], color=BLUE, lw=0.8)
    ax[0, 1].set_xlabel("iteration"); ax[0, 1].set_title("(b) negative ELBO (10 samples per step)")
    ax[1, 0].plot(r["mu"], color=BLUE); ax[1, 0].set_xlabel("iteration"); ax[1, 0].set_title("(c) $\\mu$")
    ax[1, 1].plot(r["sigma"], color=BLUE); ax[1, 1].set_xlabel("iteration"); ax[1, 1].set_title("(d) $\\sigma$")
    fig.tight_layout()
    savefig(fig, CH, "fig7-4", out)


def fig_7_5(out=None):
    """Figure 7.5: HMC for the beta-Bernoulli posterior, 4 chains."""
    chains, _ = hmc_beta_binomial()
    th = sigmoid(chains)
    fig, ax = plt.subplots(1, 2, figsize=(7.6, 2.8))
    g = np.linspace(0.3, 1, 400)
    for i, c in enumerate(th):
        k = stats.gaussian_kde(c[100:])
        ax[0].plot(g, k(g), lw=1.0, label="chain %d" % (i + 1))
    ax[0].plot(g, exact_posterior().pdf(g), "k--", lw=1.0, label="exact")
    ax[0].set_xlabel("$\\theta$"); ax[0].legend(fontsize=7); ax[0].set_title("(a) density of samples")
    for i, c in enumerate(th):
        ax[1].plot(c, lw=0.6)
    ax[1].set_xlabel("iteration"); ax[1].set_ylabel("$\\theta$"); ax[1].set_title("(b) trace plot")
    fig.tight_layout()
    savefig(fig, CH, "fig7-5", out)


def bimodal_2d():
    """A 2-d mixture of two correlated Gaussians (weights 0.55 / 0.45): returns logp, grad and sampler."""
    mus = [np.array([-2.0, -1.0]), np.array([2.0, 1.5])]
    covs = [np.array([[1.0, 0.7], [0.7, 1.0]]) * 0.6, np.array([[1.0, -0.5], [-0.5, 0.8]]) * 0.6]
    w = [0.55, 0.45]

    def logp(x):
        x = np.atleast_2d(x)
        return special.logsumexp([np.log(w[k]) + stats.multivariate_normal.logpdf(x, mus[k], covs[k]) for k in range(2)], axis=0)

    def sample(n, rng):
        k = rng.choice(2, size=n, p=w)
        return np.array([rng.multivariate_normal(mus[j], covs[j]) for j in k])

    return logp, sample, mus, covs, w


def fig_7_6(out=None):
    """Own version of Figure 7.6: four ways to summarise a bimodal 2-d posterior."""
    logp, sample, mus, covs, w = bimodal_2d()
    rng = np.random.default_rng(3)
    g1 = np.linspace(-5, 5, 200)
    X, Y = np.meshgrid(g1, np.linspace(-4, 5, 200))
    P = np.exp(logp(np.c_[X.ravel(), Y.ravel()])).reshape(X.shape)
    ind = sample(300, rng)
    mh, _ = random_walk_mh(lambda x: float(np.ravel(logp(x))[0]), mus[1], 300, 0.5, rng)
    mom = ind.mean(0); cov = np.cov(ind.T)
    fig, ax = plt.subplots(1, 4, figsize=(11, 2.9), sharex=True, sharey=True)
    for a in ax:
        a.contour(X, Y, P, levels=6, colors=GREY, linewidths=0.7)
        a.set_xticks([]); a.set_yticks([])
    ax[0].plot(*mus[0], "o", color=RED, ms=8); ax[0].set_title("(a) local MAP")
    from common import ellipse_xy
    for ns in (1, 2):
        ax[1].plot(*ellipse_xy(mom, cov, ns), color=RED)
    ax[1].set_title("(b) Gaussian fit")
    ax[2].plot(mh[:, 0], mh[:, 1], ".", color=RED, ms=3); ax[2].set_title("(c) correlated MCMC samples")
    ax[3].plot(ind[:, 0], ind[:, 1], ".", color=RED, ms=3); ax[3].set_title("(d) independent samples")
    fig.tight_layout()
    savefig(fig, CH, "own7-2", out)


FIGURES = {"7.2": fig_7_2, "7.3": fig_7_3, "7.4": fig_7_4, "7.5": fig_7_5, "own7.1": fig_own_7_1, "own7.2": fig_7_6}

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