1from __future__ import annotations23import numpy as np4from scipy import stats567def pit_histogram(u: np.ndarray, bins: int = 20) -> dict:8 h, edges = np.histogram(np.clip(u, 0, 1), bins=bins, range=(0, 1))9 n = len(u)10 expected = n / bins11 chi2 = float(((h - expected) ** 2 / expected).sum())12 ks = stats.kstest(u, "uniform")13 return {"hist": (h / n).tolist(), "edges": edges.tolist(), "chi2": chi2, "chi2_p": float(1 - stats.chi2.cdf(chi2, bins - 1)),14 "ks_stat": float(ks.statistic), "ks_p": float(ks.pvalue), "mean": float(u.mean()), "var": float(u.var()),15 "n": int(n)}161718def reliability(y: np.ndarray, s: np.ndarray, levels=(0.5, 0.8, 0.9, 0.95, 0.99)) -> list[dict]:19 out = []20 for lv in levels:21 a = 1 - lv22 lo, hi = np.quantile(s, [a / 2, 1 - a / 2], axis=0)23 cov = float(((y >= lo) & (y <= hi)).mean())24 out.append({"level": lv, "coverage": cov, "width": float((hi - lo).mean()), "gap": cov - lv})25 return out26