"""Murphy (2023), Chapter 27: Discovery methods: an overview.

Companion code for Chapter 27 of K. P. Murphy, Probabilistic Machine Learning: Advanced Topics
(MIT Press, 2023; December 2025 draft), pp. 927-928 of the book, to be read next to the lecture notes
(../ch27-discovery-overview.html).

The chapter has no computed figure (Figure 27.1, "vision as inverse graphics", is a drawing).
This module makes one illustration of my own for the analysis-by-synthesis idea (27.1): a linear
Gaussian "renderer" x = A z + noise with a rank-deficient A (perceptual aliasing: a direction of
latent space that the sensor cannot see), where the prior alone decides what is recovered along the
invisible direction, and the MAP estimate is a ridge (Tikhonov) solution. Functions are reused by the
private numerical checks.

Skipped: Figure 27.1 (a drawing).

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

import numpy as np
from scipy import stats

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

CH = 27

A = np.array([[1.0, 1.0]])          # the renderer sees only z1 + z2
SIGMA = 0.2                         # sensor noise s.d.
TAU = 1.5                           # prior s.d. of each latent coordinate


def posterior_gaussian(x, A=A, sigma=SIGMA, tau=TAU, prior_mean=None):
    """Exact posterior of z for x = A z + eps, eps ~ N(0, sigma^2 I), z ~ N(m0, tau^2 I).
    Returns (mean, covariance). The mean is also the MAP estimate (ridge / Tikhonov solution)."""
    d = A.shape[1]
    m0 = np.zeros(d) if prior_mean is None else prior_mean
    P = A.T @ A / sigma ** 2 + np.eye(d) / tau ** 2
    cov = np.linalg.inv(P)
    mean = cov @ (A.T @ np.atleast_1d(x) / sigma ** 2 + m0 / tau ** 2)
    return mean, cov


def ridge_map(x, A=A, sigma=SIGMA, tau=TAU):
    """MAP = argmin ||x - A z||^2 + (sigma^2/tau^2) ||z||^2 (equation 27.1 for this model)."""
    lam = sigma ** 2 / tau ** 2
    return np.linalg.solve(A.T @ A + lam * np.eye(A.shape[1]), A.T @ np.atleast_1d(x))


def fig_own_27_1(out=None):
    """Own figure: prior, likelihood ridge and posterior for the aliased linear inverse problem."""
    x = 2.0
    m, C = posterior_gaussian(x)
    g = np.linspace(-4, 5, 300)
    Z1, Z2 = np.meshgrid(g, g)
    lik = stats.norm.pdf(x, Z1 + Z2, SIGMA)
    fig, ax = plt.subplots(1, 3, figsize=(10, 3.1), sharex=True, sharey=True)
    ax[0].contourf(Z1, Z2, stats.multivariate_normal(np.zeros(2), TAU ** 2 * np.eye(2)).pdf(np.dstack([Z1, Z2])), levels=8, cmap="Greys")
    ax[0].set_title("(a) prior")
    ax[1].contourf(Z1, Z2, lik, levels=8, cmap="Blues")
    ax[1].set_title("(b) likelihood: a ridge $z_1+z_2=x$")
    ax[2].contourf(Z1, Z2, stats.multivariate_normal(m, C).pdf(np.dstack([Z1, Z2])), levels=8, cmap="Greens")
    ax[2].plot(*m, "o", color=RED, ms=6)
    ax[2].set_title("(c) posterior, MAP at (%.2f, %.2f)" % tuple(m))
    for a in ax:
        a.set_xlabel("$z_1$")
    ax[0].set_ylabel("$z_2$")
    fig.tight_layout()
    savefig(fig, CH, "own27-1", out)


FIGURES = {"own27.1": fig_own_27_1}

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