"""Murphy (2023), Chapter 1: Introduction.

Companion code for Chapter 1 of K. P. Murphy, Probabilistic Machine Learning: Advanced Topics
(MIT Press, 2023; December 2025 draft), pp. 1-2 of the book, to be read next to the lecture notes
(../ch01-introduction.html).

The chapter has no figure and no computation. This module therefore makes one illustration of my
own for the sentence in the chapter's overview that inference means "inverting a function and
working backwards from outputs to causes", as opposed to applying it: an ill-posed inverse problem
whose forwards map z -> x = z^2 is many-to-one, so that the likelihood has two equal peaks (the
"perceptual aliasing" of Chapter 27) and a prior is what makes the posterior, and the MAP estimate,
well defined. Functions are reused by the private numerical checks.

Data: none (analytic; the grid is deterministic).
"""
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  # noqa: E402

CH = 1
SIGMA = 0.3      # observation noise s.d.
X_OBS = 4.0      # observed value; the forward map is x = z^2 + noise


def forward(z):
    """Deterministic forward map z -> x (many-to-one: z and -z give the same x)."""
    return z ** 2


def posterior_grid(prior_mu=1.0, prior_sd=1.5, x_obs=X_OBS, sigma=SIGMA, n=4001, lim=5.0):
    """Likelihood, prior and normalised posterior of z on a grid."""
    z = np.linspace(-lim, lim, n)
    lik = stats.norm.pdf(x_obs, forward(z), sigma)
    prior = stats.norm.pdf(z, prior_mu, prior_sd)
    post = lik * prior
    post /= np.trapz(post, z)
    return z, lik, prior, post


def fig_own_1_1(out=None):
    """Own figure: inverting a many-to-one map. Likelihood with two peaks, a prior, and the posterior."""
    z, lik, prior, post = posterior_grid()
    fig, ax = plt.subplots(1, 3, figsize=(10, 2.8))
    ax[0].plot(z, forward(z), color=BLUE)
    ax[0].axhline(X_OBS, color=RED, ls="--", lw=1)
    ax[0].set_xlabel("$z$"); ax[0].set_ylabel("$x=z^2$"); ax[0].set_title("(a) forward map")
    ax[1].plot(z, lik / np.trapz(lik, z), color=BLUE, label="likelihood (rescaled)")
    ax[1].plot(z, prior, color=GREY, label="prior $\\mathcal{N}(1,1.5^2)$")
    ax[1].set_xlabel("$z$"); ax[1].legend(fontsize=7.5); ax[1].set_title("(b) two equally good explanations")
    ax[2].plot(z, post, color=GREEN)
    zm = z[np.argmax(post)]
    ax[2].axvline(zm, color="k", lw=1, label="MAP $\\hat z=%.2f$" % zm)
    ax[2].set_xlabel("$z$"); ax[2].legend(fontsize=8); ax[2].set_title("(c) posterior")
    fig.tight_layout()
    savefig(fig, CH, "own1-1", out)


FIGURES = {"own1.1": fig_own_1_1}

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