| """A-priori, label-free probe-stability predictors evaluated by ProbeShift (G2). |
| |
| Every predictor maps IID training activations (+ labels, + optional augmentation |
| activations) to a scalar where **higher = predicted more OOD-stable**, so all can be |
| correlated against (negated) OOD drop under one convention. All are computed WITHOUT any |
| OOD data and WITHOUT new annotation, except `whitened_cosine_upper` which is flagged as an |
| OOD-using upper-bound reference (Truthfulness-Spectrum style). |
| |
| Implemented (each a verified competitor — see PRIOR_ART.md §四): |
| sip_eigengap SIP (2511.16288) — LDA eigengap / Fisher error |
| raptor_stability RAPTOR (2602.00158) — bootstrap direction mean|cos| (= dispersion) |
| fragility Fragility (2606.11375) — critical isotropic-noise collapse sigma |
| augmentation_robustness Probing-the-Probes — direction consistency under augmentation |
| xie_feature_dispersion Xie 2023 (2303.15488) — inter-class feature dispersion |
| pac ours (G3) — composite of dispersion + augmentation |
| whitened_cosine_upper Truth-Spectrum style — ID/OOD-whitened cosine (upper-bound ref) |
| """ |
| from __future__ import annotations |
|
|
| import numpy as np |
| from scipy import linalg |
|
|
| from config import STABILITY |
| from probes import make_probe |
| from metrics import accuracy |
| from stability_score import dispersion, augmentation_consistency, combine |
|
|
|
|
| |
| |
| |
| def sip_eigengap(X: np.ndarray, y: np.ndarray, num_labels: int, ridge: float = 1e-3, **_): |
| X = np.asarray(X, dtype=np.float64) |
| classes = np.unique(y) |
| mu = X.mean(0) |
| Sb = np.zeros((X.shape[1], X.shape[1])) |
| Sw = np.zeros_like(Sb) |
| for c in classes: |
| Xc = X[y == c] |
| d = (Xc.mean(0) - mu)[:, None] |
| Sb += len(Xc) * (d @ d.T) |
| Sw += np.cov(Xc, rowvar=False) * (len(Xc) - 1) |
| Sw += ridge * np.eye(X.shape[1]) |
| |
| eig = np.sort(linalg.eigvalsh(Sb, Sw))[::-1] |
| k = max(1, num_labels - 1) |
| if len(eig) <= k: |
| return float("nan") |
| gap = eig[k - 1] - eig[k] |
| fisher_err = np.sqrt(X.shape[1] / len(X)) |
| return float(gap / (eig[0] + 1e-12) / (fisher_err + 1e-12)) |
|
|
|
|
| |
| |
| |
| def raptor_stability(X, y, num_labels, probe_kind="logreg", seed=0, **kw): |
| disp = dispersion(np.asarray(X, np.float32), np.asarray(y), num_labels, |
| probe_kind=probe_kind, k=STABILITY.k_bootstrap, seed=seed, **kw) |
| return float(1.0 - disp) |
|
|
|
|
| |
| |
| |
| def fragility(X, y, num_labels, probe_kind="logreg", seed=0, |
| sigmas=(0.0, 0.25, 0.5, 1.0, 1.5, 2.0, 3.0), **kw): |
| X = np.asarray(X, dtype=np.float32) |
| rng = np.random.default_rng(seed) |
| p = make_probe(probe_kind, num_labels=num_labels, seed=seed, **kw).fit(X, y) |
| iid_acc = accuracy(y, p.predict(X)) |
| chance = 1.0 / num_labels |
| thresh = chance + 0.5 * (iid_acc - chance) |
| scale = X.std() |
| critical = sigmas[-1] |
| for s in sigmas: |
| Xn = X + s * scale * rng.standard_normal(X.shape).astype(np.float32) |
| if accuracy(y, p.predict(Xn)) < thresh: |
| critical = s |
| break |
| return float(critical) |
|
|
|
|
| |
| |
| |
| def augmentation_robustness(X, y, aug_X_list, num_labels, probe_kind="logreg", seed=0, **kw): |
| return float(augmentation_consistency( |
| np.asarray(X, np.float32), np.asarray(y), [np.asarray(a, np.float32) for a in aug_X_list], |
| num_labels, probe_kind=probe_kind, seed=seed, **kw)) |
|
|
|
|
| |
| |
| |
| def xie_feature_dispersion(X, y, num_labels, **_): |
| X = np.asarray(X, dtype=np.float64) |
| classes = np.unique(y) |
| mus = np.stack([X[y == c].mean(0) for c in classes]) |
| inter = np.mean([np.linalg.norm(mus[i] - mus[j]) |
| for i in range(len(mus)) for j in range(i + 1, len(mus))]) |
| return float(inter / (X.std() + 1e-12)) |
|
|
|
|
| |
| |
| |
| def pac(X, y, aug_X_list, num_labels, probe_kind="logreg", seed=0, **kw): |
| disp = dispersion(np.asarray(X, np.float32), np.asarray(y), num_labels, |
| probe_kind=probe_kind, k=STABILITY.k_bootstrap, seed=seed, **kw) |
| aug = augmentation_consistency( |
| np.asarray(X, np.float32), np.asarray(y), |
| [np.asarray(a, np.float32) for a in aug_X_list], num_labels, |
| probe_kind=probe_kind, seed=seed, **kw) |
| return float(combine(disp, aug, STABILITY)) |
|
|
|
|
| |
| |
| |
| def whitened_cosine_upper(X, y, num_labels, probe_kind="logreg", seed=0, cov=None, **kw): |
| """If `cov` (e.g. OOD covariance) is supplied, this becomes the OOD-using upper bound; |
| with cov=None it whitens by ID covariance (the metric-ablation variant).""" |
| X = np.asarray(X, dtype=np.float64) |
| C = np.cov(X, rowvar=False) if cov is None else np.asarray(cov, np.float64) |
| C += 1e-3 * np.eye(C.shape[0]) |
| W = linalg.fractional_matrix_power(C, -0.5).real |
| return raptor_stability((X @ W).astype(np.float32), y, num_labels, |
| probe_kind=probe_kind, seed=seed, **kw) |
|
|
|
|
| |
| PREDICTORS = { |
| "sip_eigengap": (sip_eigengap, False), |
| "raptor_stability": (raptor_stability, False), |
| "fragility": (fragility, False), |
| "augmentation_robustness": (augmentation_robustness, True), |
| "xie_feature_dispersion": (xie_feature_dispersion, False), |
| "pac": (pac, True), |
| "whitened_cosine_id": (whitened_cosine_upper, False), |
| } |
|
|
|
|
| def compute_all(X_iid, y_iid, aug_X_list, num_labels, probe_kind="logreg", seed=0) -> dict: |
| out = {} |
| for name, (fn, needs_aug) in PREDICTORS.items(): |
| try: |
| out[name] = fn(X_iid, y_iid, aug_X_list, num_labels, probe_kind=probe_kind, seed=seed) \ |
| if needs_aug else fn(X_iid, y_iid, num_labels, probe_kind=probe_kind, seed=seed) |
| except Exception as e: |
| out[name] = float("nan") |
| out[name + "__error"] = str(e) |
| return out |
|
|