#!/usr/bin/env python3
"""
Render diagnostic plots for the Logistic distribution regression example.

Usage:
    Place dataset_logistic.csv in the same directory, then run:
        python render_logistic_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_logistic.csv")

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

control = {
    "compute": {
        "dic": True,
        "cpo": True,
        "mlik": True,
        "return_marginals": True,
    }
}

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

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

intercept = float(result.summary_fixed.loc["(Intercept)", "mean"])
slope = float(result.summary_fixed.loc["z", "mean"])

fitted_means = intercept + slope * df["z"].to_numpy()
residuals = df["y"].to_numpy() - fitted_means

density = result.marginals_hyperpar["precision for the logistic 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: Observations 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="#f97316", linewidth=2.4, label="pyINLA posterior mean")
ax.set_xlabel("z (covariate)")
ax.set_ylabel("y")
ax.set_title("Logistic regression: observed data and posterior mean trend")
ax.legend(loc="best")
ax.grid(alpha=0.25)
fig.tight_layout()
fig.savefig("logistic-regression-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="#a78bfa", s=42, edgecolor="white", linewidth=0.35)
ax.axhline(0.0, color="#f97316", linewidth=1.6, linestyle="--", label="zero residual")
ax.set_xlabel("Fitted mean")
ax.set_ylabel("Observed − fitted")
ax.set_title("Logistic regression: residuals vs fitted values")
ax.legend(loc="upper right")
ax.grid(alpha=0.25)
fig.tight_layout()
fig.savefig("logistic-regression-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="#f97316", linewidth=2.0)
ax.fill_between(density["x"], density["y"], color="#f97316", alpha=0.25)
ax.set_xlabel("Precision (τ)")
ax.set_ylabel("Density")
ax.set_title("Logistic regression: posterior density for precision τ")
ax.grid(alpha=0.25)
fig.tight_layout()
fig.savefig("logistic-regression-precision.png", dpi=160)
plt.close(fig)

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