- P2: SMC tie posterior (numpy + torch), RML/soft-DTW losses, bank training, D2 synthetic generator, scripts 09-13, protocol_p2 (locked) and its outcome (G-B failed; H1 not rejected, H3 rejected, H4 not rejected). - Diagnosis on dev realizations 900/901 only (scripts 14-17, run_p3_dev.sh): posterior screen, SMC stability (P = 1024, K = 1), sigma_a misspecification stress and training, H3 with the SMC posterior, F3 placement on sigma_post. - protocol_p3: H2 split into H2a (posterior-mean labels vs point ties) and H2b (RML vs posterior mean as a function of sigma_post); H3 descriptive only; sigma_a arm; new D2 config d2_synth_p3.yaml; main grid on realizations 8-15. Locked by configs/protocol_p3.sha256 and tag protocol-p3. - runlog: per-run logs and runtime index (logs/index.csv). - Docs: plan §12 items 10-11, theory, technical overview, report; wording aligned with Gunning & Glinsky (2006): the tie posterior is not claimed as new. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
61 lines
2.3 KiB
Python
61 lines
2.3 KiB
Python
"""Basic pointwise metrics; samples that are NaN on either side are ignored."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
|
|
|
|
def _valid(y_true, y_pred):
|
|
y_true = np.asarray(y_true, dtype=float)
|
|
y_pred = np.asarray(y_pred, dtype=float)
|
|
ok = np.isfinite(y_true) & np.isfinite(y_pred)
|
|
return y_true[ok], y_pred[ok]
|
|
|
|
|
|
def point_metrics(y_true, y_pred) -> dict[str, float]:
|
|
t, p = _valid(y_true, y_pred)
|
|
err = p - t
|
|
var_t = float(np.var(t))
|
|
return {
|
|
"n": int(t.size),
|
|
"mae": float(np.mean(np.abs(err))),
|
|
"rmse": float(np.sqrt(np.mean(err**2))),
|
|
"pcc": float(np.corrcoef(t, p)[0, 1]) if t.size > 1 else float("nan"),
|
|
"r2": float(1.0 - np.mean(err**2) / var_t) if var_t > 0 else float("nan"),
|
|
"std_ratio": float(np.std(p) / np.std(t)) if np.std(t) > 0 else float("nan"),
|
|
}
|
|
|
|
|
|
def split_bands(y: np.ndarray, f_c_hz: float, dt_ms: float = 4.0, order: int = 4) -> tuple[np.ndarray, np.ndarray]:
|
|
"""Split a series into a low band (< f_c, zero-phase Butterworth) and the remainder (seismic band)."""
|
|
from scipy import signal
|
|
|
|
y = np.asarray(y, dtype=float)
|
|
sos = signal.butter(order, f_c_hz, fs=1000.0 / dt_ms, output="sos")
|
|
low = signal.sosfiltfilt(sos, y, padtype="even")
|
|
return low, y - low
|
|
|
|
|
|
def band_metrics(y_true, y_pred, f_c_hz: float = 6.0, dt_ms: float = 4.0) -> dict[str, float]:
|
|
lt, ht = split_bands(y_true, f_c_hz, dt_ms)
|
|
lp, hp = split_bands(y_pred, f_c_hz, dt_ms)
|
|
lo, hi = point_metrics(lt, lp), point_metrics(ht, hp)
|
|
return {f"low_{k}": lo[k] for k in ("rmse", "pcc", "std_ratio")} | {
|
|
f"band_{k}": hi[k] for k in ("rmse", "pcc", "std_ratio")
|
|
}
|
|
|
|
|
|
def gaussian_metrics(y_true, mu, sigma) -> dict[str, float]:
|
|
"""NLL, CRPS (Gaussian closed form) and coverage of central intervals."""
|
|
from scipy import stats
|
|
|
|
y, mu, s = (np.asarray(a, dtype=float) for a in (y_true, mu, sigma))
|
|
z = (y - mu) / s
|
|
nll = float(np.mean(0.5 * np.log(2 * np.pi * s**2) + 0.5 * z**2))
|
|
crps = float(np.mean(s * (z * (2 * stats.norm.cdf(z) - 1) + 2 * stats.norm.pdf(z) - 1 / np.sqrt(np.pi))))
|
|
out = {"nll": nll, "crps": crps, "mean_sigma": float(np.mean(s))}
|
|
for level in (50, 80, 90, 95):
|
|
q = stats.norm.ppf(0.5 + level / 200)
|
|
out[f"coverage_{level}"] = float(np.mean(np.abs(z) <= q))
|
|
return out
|