"""Sequential Bayesian linear regression: the bridge from Bishop (2006) Section 3.3 to
Murphy's sequential updating (PML1 Section 11.6.1, Figure 11.20).

Model: y = w0 + w1 x + noise, noise ~ N(0, 1/beta), prior w ~ N(0, I/alpha), basis (1, x).

Sequential update after observing (x_n, y_n) (Bishop eqs (3.50)-(3.51) with Phi_n = (1, x_n)):
    S_n^{-1} = S_{n-1}^{-1} + beta * phi_n phi_n^T
    m_n      = S_n (S_{n-1}^{-1} m_{n-1} + beta * phi_n * y_n)

Note on a bug in an earlier draft of this script: it wrote m_n = S_n (S_n^{-1} m_{n-1} + ...), which
uses the NEW precision with the OLD mean and gives the wrong posterior mean. The correct form uses the
previous precision S_{n-1}^{-1} m_{n-1}. check() below verifies the sequential result against the batch
closed form, which would have caught the error.

Data come from the same seeded generator as the browser dashboard (JSRandom, the DK.rng of demo-kit.js),
so the Python figure and the web page show identical points.
"""
import os
import sys

import numpy as np

import matplotlib.pyplot as plt

import common

from ch11_sampling import JSRandom

TRUE_W = np.array([-0.3, 0.5])
ALPHA = 2.0
BETA = 25.0
SEED = 42
N_POINTS = 30


def make_stream(n, seed=SEED, beta=BETA):
    """n points (x, y): x uniform on [-1, 1], y = w0 + w1 x + N(0, 1/beta)."""
    rng = JSRandom(seed)
    xs, ys = [], []
    for _ in range(n):
        x = 2.0 * rng.u() - 1.0
        y = TRUE_W[0] + TRUE_W[1] * x + rng.standard_normal() / np.sqrt(beta)
        xs.append(x)
        ys.append(y)
    return np.array(xs), np.array(ys)


class SequentialBayesLR:
    """Gaussian posterior over (w0, w1), updated one observation at a time."""

    def __init__(self, alpha=ALPHA, beta=BETA):
        self.alpha, self.beta = alpha, beta
        self.reset()

    def reset(self):
        self.mu = np.zeros(2)
        self.Lam = self.alpha * np.eye(2)          # precision S_n^{-1}
        self.Sigma = np.eye(2) / self.alpha        # covariance S_n

    def update(self, x, y):
        phi = np.array([1.0, x])
        Lam_prev, mu_prev = self.Lam, self.mu
        self.Lam = Lam_prev + self.beta * np.outer(phi, phi)
        self.Sigma = np.linalg.inv(self.Lam)
        self.mu = self.Sigma @ (Lam_prev @ mu_prev + self.beta * phi * y)
        return self.mu, self.Sigma

    def sample(self, rng, k=10):
        """k draws from the current posterior, using the seeded JSRandom stream."""
        L = np.linalg.cholesky(self.Sigma)
        return np.array([self.mu + L @ np.array([rng.standard_normal(), rng.standard_normal()])
                         for _ in range(k)])


def batch_posterior(xs, ys, alpha=ALPHA, beta=BETA):
    """Closed-form posterior after all points at once (Bishop (3.53)-(3.54))."""
    Phi = np.column_stack([np.ones_like(xs), xs])
    Lam = alpha * np.eye(2) + beta * Phi.T @ Phi
    Sigma = np.linalg.inv(Lam)
    mu = beta * Sigma @ Phi.T @ ys
    return mu, Sigma


def check(n=N_POINTS):
    """Sequential and batch posteriors must agree to rounding error."""
    xs, ys = make_stream(n)
    eng = SequentialBayesLR()
    for x, y in zip(xs, ys):
        eng.update(x, y)
    mu_b, S_b = batch_posterior(xs, ys)
    err_mu = np.max(np.abs(eng.mu - mu_b))
    err_S = np.max(np.abs(eng.Sigma - S_b))
    ok = err_mu < 1e-10 and err_S < 1e-10
    print("%s sequential == batch posterior: max |dmu| = %.2e, max |dSigma| = %.2e"
          % ("PASS" if ok else "FAIL", err_mu, err_S))
    return ok


def fig_sequential(out_dir=None):
    """Figure 11.20 (PML1) / Figure 3.7 (Bishop): posterior and data-space predictions after 0, 1, 2, 5, 10, 30 points."""
    xs, ys = make_stream(N_POINTS)
    g = np.linspace(-1, 1, 120)
    W0, W1 = np.meshgrid(g, g)
    stages = [0, 1, 2, 5, 10, N_POINTS]
    fig, axes = plt.subplots(len(stages), 2, figsize=(7.2, 2.6 * len(stages)), squeeze=False)
    eng = SequentialBayesLR()
    rng = JSRandom(7)
    xl = np.linspace(-1, 1, 50)
    done = 0
    for row, n in enumerate(stages):
        while done < n:
            eng.update(xs[done], ys[done])
            done += 1
        mu, S = eng.mu, eng.Sigma
        # posterior density on the grid
        d = np.stack([W0 - mu[0], W1 - mu[1]], -1)
        q = np.einsum("...i,ij,...j->...", d, np.linalg.inv(S), d)
        dens = np.exp(-0.5 * q) / (2 * np.pi * np.sqrt(np.linalg.det(S)))
        ax = axes[row, 0]
        ax.contourf(W0, W1, dens, levels=20, cmap="viridis")
        ax.plot(TRUE_W[0], TRUE_W[1], "r+", ms=12, mew=2)
        ax.set_xlim(-1, 1); ax.set_ylim(-1, 1)
        ax.set_ylabel("$w_1$")
        ax.set_title("posterior after %d point%s" % (n, "" if n == 1 else "s"), fontsize=9)
        ax = axes[row, 1]
        for w in eng.sample(rng, 10):
            ax.plot(xl, w[0] + w[1] * xl, color="#1f5fbf", alpha=0.3, lw=1)
        ax.plot(xl, TRUE_W[0] + TRUE_W[1] * xl, "r-", lw=2)
        ax.scatter(xs[:n], ys[:n], s=14, color="black", zorder=5)
        ax.set_xlim(-1, 1); ax.set_ylim(-1.2, 1.2)
        ax.set_title("samples from the posterior", fontsize=9)
        if row == len(stages) - 1:
            axes[row, 0].set_xlabel("$w_0$"); axes[row, 1].set_xlabel("$x$")
    fig.suptitle("Sequential Bayesian linear regression (Bishop Fig. 3.7; Murphy PML1 Fig. 11.20)", fontsize=10)
    folder = out_dir or os.path.join(common.HERE, "..", "img", "bridge")
    os.makedirs(folder, exist_ok=True)
    for ext in ("png", "svg", "pdf"):
        fig.savefig(os.path.join(folder, "bridge-sequential-bayes." + ext))
    plt.close(fig)
    print("wrote", os.path.normpath(os.path.join(folder, "bridge-sequential-bayes.png")))


def run_all(out_dir=None):
    ok = check()
    if not ok:
        sys.exit("sequential update disagrees with the batch posterior")
    fig_sequential(out_dir)


FIGURES = {"bridge-sequential-bayes": fig_sequential}

if __name__ == "__main__":
    if "--check" in sys.argv:
        sys.exit(0 if check() else 1)
    common.run(FIGURES, 3)
