#!/usr/bin/env python3
"""
Render diagnostic plots for the Gaussian log-gamma prior example.

Usage:
    Place dataset_gaussian_loggamma.csv in the same directory, then run:
        python render_gaussian_plots.py

Requires: pyinla, pandas, numpy, matplotlib
"""

from __future__ import annotations

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pyinla import pyinla

# ── Fit the model ────────────────────────────────────────────────────────────

df = pd.read_csv("dataset_gaussian_loggamma.csv")

model = {
    "response": "y",
    "fixed": ["1", "z"],
}

control = {
    "family": {
        "hyper": [
            {
                "prior": "loggamma",
                "param": [1.0, 0.01],
                "initial": 2.0,
            }
        ]
    },
    "predictor": {"compute": True},
}

result = pyinla(model=model, family="gaussian", data=df, control=control)

# ── Extract quantities ───────────────────────────────────────────────────────

intercept = float(result.summary_fixed.loc["(Intercept)", "mean"])
slope = float(result.summary_fixed.loc["z", "mean"])
fitted_means = result.summary_fitted_values["mean"].to_numpy()
residuals = df["y"].to_numpy() - fitted_means

density = result.marginals_hyperpar["Precision for the Gaussian observations"]
density = density.rename(columns={density.columns[0]: "x", density.columns[1]: "y"})

z_grid = np.linspace(df["z"].min(), df["z"].max(), 200)
fitted_line = intercept + slope * z_grid

# ── Plot 1: Observed data and posterior mean ─────────────────────────────────

fig, ax = plt.subplots(figsize=(6.2, 4.1))
ax.scatter(df["z"], df["y"], color="#38bdf8", s=46, edgecolor="white", linewidth=0.4, label="observations")
ax.plot(z_grid, fitted_line, color="#facc15", linewidth=2.4, label="posterior mean")
ax.set_xlabel("z (covariate)")
ax.set_ylabel("y")
ax.set_title("Gaussian log-gamma prior: observed data and posterior mean")
ax.legend(loc="best")
ax.grid(alpha=0.25)
fig.tight_layout()
fig.savefig("gaussian-pc-fit.png", dpi=160)
plt.close(fig)

# ── Plot 2: Residuals vs fitted values ───────────────────────────────────────

fig, ax = plt.subplots(figsize=(6.2, 4.1))
ax.scatter(fitted_means, residuals, color="#38bdf8", s=42, edgecolor="white", linewidth=0.35)
ax.axhline(0.0, color="#facc15", linewidth=1.6, linestyle="--", label="zero residual")
ax.set_xlabel("Fitted mean")
ax.set_ylabel("Observed - fitted")
ax.set_title("Gaussian log-gamma prior: residuals vs fitted values")
ax.legend(loc="upper right")
ax.grid(alpha=0.25)
fig.tight_layout()
fig.savefig("gaussian-pc-residuals.png", dpi=160)
plt.close(fig)

# ── Plot 3: Posterior density for precision ──────────────────────────────────

fig, ax = plt.subplots(figsize=(6.2, 4.1))
ax.plot(density["x"], density["y"], color="#f59e0b", linewidth=2.0)
ax.fill_between(density["x"], density["y"], color="#f59e0b", alpha=0.25)
ax.set_xlabel("Precision (τ)")
ax.set_ylabel("Density")
ax.set_title("Gaussian log-gamma prior: posterior density for precision τ")
ax.grid(alpha=0.25)
fig.tight_layout()
fig.savefig("gaussian-pc-precision.png", dpi=160)
plt.close(fig)

print("Plots saved: gaussian-pc-fit.png, gaussian-pc-residuals.png, gaussian-pc-precision.png")
print(f"Recovered: intercept = {intercept:.4f}, slope = {slope:.4f}")
print(result.summary_fixed)
print(result.summary_hyperpar)
