Matthew Ford commited on
Commit ·
e7a7086
1
Parent(s): 1d7427e
pimt v8 statistical validation tests
Browse filesFormer-commit-id: 11d1f3f5fbdffafb166b541f0983f0d281725fe2
Former-commit-id: 7970ab2642b9568a58af48830595e60d7df7e980
- scripts/pimt_v8_stats_tests.py +229 -0
scripts/pimt_v8_stats_tests.py
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
PIMT v8 — statistical validation procedures.
|
| 4 |
+
|
| 5 |
+
Four tests that turn v8 point-estimate claims into tested claims:
|
| 6 |
+
|
| 7 |
+
1. paired_bootstrap -> is structure > baseline? (paired, same test molecules)
|
| 8 |
+
2. williams_test -> analytic cross-check for dependent correlations
|
| 9 |
+
3. character_permutation_null -> is 26-class genre separation above chance?
|
| 10 |
+
4. random_drop_control -> is the drop-10 gain specific to the flagged points?
|
| 11 |
+
|
| 12 |
+
Dependencies: numpy, scipy. No sklearn needed.
|
| 13 |
+
Plug your v8 arrays into the __main__ block (currently synthetic) and run.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import numpy as np
|
| 17 |
+
from scipy.stats import spearmanr, rankdata, t as tdist
|
| 18 |
+
from itertools import combinations
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# --------------------------------------------------------------------------- #
|
| 22 |
+
# helpers
|
| 23 |
+
# --------------------------------------------------------------------------- #
|
| 24 |
+
def _rho(a, b):
|
| 25 |
+
return spearmanr(a, b)[0]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _r2(y, yhat):
|
| 29 |
+
y = np.asarray(y, float)
|
| 30 |
+
yhat = np.asarray(yhat, float)
|
| 31 |
+
ss_res = np.sum((y - yhat) ** 2)
|
| 32 |
+
ss_tot = np.sum((y - y.mean()) ** 2)
|
| 33 |
+
return 1.0 - ss_res / ss_tot if ss_tot > 0 else np.nan
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# --------------------------------------------------------------------------- #
|
| 37 |
+
# 1. paired bootstrap: structure vs baseline on the SAME test molecules
|
| 38 |
+
# --------------------------------------------------------------------------- #
|
| 39 |
+
def paired_bootstrap(y_true, pred_struct, pred_base, n_boot=10000, seed=0):
|
| 40 |
+
"""
|
| 41 |
+
Both models are scored on the same held-out molecules, so the comparison is
|
| 42 |
+
paired. We resample TEST CASES with replacement and recompute the difference
|
| 43 |
+
each time. A 95% CI on the difference that excludes 0 supports 'structure >
|
| 44 |
+
baseline'. Comparing the two marginal CIs is the wrong test and is not used.
|
| 45 |
+
"""
|
| 46 |
+
rng = np.random.default_rng(seed)
|
| 47 |
+
y = np.asarray(y_true, float)
|
| 48 |
+
ps = np.asarray(pred_struct, float)
|
| 49 |
+
pb = np.asarray(pred_base, float)
|
| 50 |
+
n = len(y)
|
| 51 |
+
|
| 52 |
+
d_rho = np.empty(n_boot)
|
| 53 |
+
d_r2 = np.empty(n_boot)
|
| 54 |
+
for b in range(n_boot):
|
| 55 |
+
idx = rng.integers(0, n, n) # case resample, with replacement
|
| 56 |
+
d_rho[b] = _rho(y[idx], ps[idx]) - _rho(y[idx], pb[idx])
|
| 57 |
+
d_r2[b] = _r2(y[idx], ps[idx]) - _r2(y[idx], pb[idx])
|
| 58 |
+
|
| 59 |
+
def summarize(d, obs):
|
| 60 |
+
lo, hi = np.nanpercentile(d, [2.5, 97.5])
|
| 61 |
+
p = 2 * min(np.mean(d <= 0), np.mean(d >= 0)) # two-sided
|
| 62 |
+
return dict(observed=round(float(obs), 4),
|
| 63 |
+
ci95=(round(float(lo), 4), round(float(hi), 4)),
|
| 64 |
+
p_two_sided=round(float(p), 4),
|
| 65 |
+
excludes_zero=bool(lo > 0 or hi < 0))
|
| 66 |
+
|
| 67 |
+
return dict(
|
| 68 |
+
delta_rho=summarize(d_rho, _rho(y, ps) - _rho(y, pb)),
|
| 69 |
+
delta_r2=summarize(d_r2, _r2(y, ps) - _r2(y, pb)),
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# --------------------------------------------------------------------------- #
|
| 74 |
+
# 2. Williams / Hotelling test for two dependent correlations sharing a variable
|
| 75 |
+
# --------------------------------------------------------------------------- #
|
| 76 |
+
def williams_test(y_true, pred_struct, pred_base):
|
| 77 |
+
"""
|
| 78 |
+
Analytic cross-check. Compares r(y, struct) vs r(y, base) where both share y.
|
| 79 |
+
Applied to ranks, so it is the Spearman-flavoured version (Pearson on ranks).
|
| 80 |
+
The key quantity is r23 = corr(struct, base): the more the two models agree,
|
| 81 |
+
the more powerful the paired comparison — which is exactly why marginal CIs
|
| 82 |
+
mislead here.
|
| 83 |
+
"""
|
| 84 |
+
y = rankdata(y_true)
|
| 85 |
+
a = rankdata(pred_struct)
|
| 86 |
+
c = rankdata(pred_base)
|
| 87 |
+
n = len(y)
|
| 88 |
+
|
| 89 |
+
r12 = np.corrcoef(y, a)[0, 1] # y vs structure
|
| 90 |
+
r13 = np.corrcoef(y, c)[0, 1] # y vs baseline
|
| 91 |
+
r23 = np.corrcoef(a, c)[0, 1] # structure vs baseline
|
| 92 |
+
|
| 93 |
+
detR = 1 - r12**2 - r13**2 - r23**2 + 2 * r12 * r13 * r23
|
| 94 |
+
avg = (r12 + r13) / 2
|
| 95 |
+
num = (r12 - r13) * np.sqrt((n - 1) * (1 + r23))
|
| 96 |
+
den = np.sqrt(2 * ((n - 1) / (n - 3)) * detR + (avg**2) * (1 - r23) ** 3)
|
| 97 |
+
tstat = num / den
|
| 98 |
+
df = n - 3
|
| 99 |
+
p = 2 * tdist.sf(abs(tstat), df)
|
| 100 |
+
|
| 101 |
+
return dict(r_struct=round(float(r12), 4), r_base=round(float(r13), 4),
|
| 102 |
+
r_between_models=round(float(r23), 4),
|
| 103 |
+
t=round(float(tstat), 3), df=int(df), p=round(float(p), 4))
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
# --------------------------------------------------------------------------- #
|
| 107 |
+
# 3. character permutation null (26-class distributional separation)
|
| 108 |
+
# --------------------------------------------------------------------------- #
|
| 109 |
+
def _genre_prevalence(formula_genre, formula_class_vec, genres):
|
| 110 |
+
"""Genre prevalence vector = L1-normalised mean of its formulas' class vectors."""
|
| 111 |
+
K = formula_class_vec.shape[1]
|
| 112 |
+
P = np.zeros((len(genres), K))
|
| 113 |
+
for gi, g in enumerate(genres):
|
| 114 |
+
rows = formula_class_vec[formula_genre == g]
|
| 115 |
+
if len(rows):
|
| 116 |
+
v = rows.mean(axis=0)
|
| 117 |
+
s = v.sum()
|
| 118 |
+
P[gi] = v / s if s > 0 else v
|
| 119 |
+
return P
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def _mean_pairwise_cosine_distance(P):
|
| 123 |
+
def cd(u, v):
|
| 124 |
+
nu, nv = np.linalg.norm(u), np.linalg.norm(v)
|
| 125 |
+
if nu == 0 or nv == 0:
|
| 126 |
+
return 0.0
|
| 127 |
+
return 1 - np.dot(u, v) / (nu * nv)
|
| 128 |
+
return float(np.mean([cd(P[i], P[j]) for i, j in combinations(range(len(P)), 2)]))
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def character_permutation_null(formula_genre, formula_class_vec, genres,
|
| 132 |
+
n_perm=10000, seed=0, exclude=None):
|
| 133 |
+
"""
|
| 134 |
+
Null: genre labels carry no information about character profile. We shuffle
|
| 135 |
+
the genre assigned to each formula (label-preserving: each formula keeps its
|
| 136 |
+
own class vector, only its genre membership is permuted), rebuild genre
|
| 137 |
+
prevalence vectors, and recompute mean pairwise cosine distance.
|
| 138 |
+
|
| 139 |
+
exclude=('wildcard',) drops the catch-all so you can test whether the four
|
| 140 |
+
substantive genres separate above chance on their own — the claim that
|
| 141 |
+
actually matters, given wildcard dominates the observed 0.080.
|
| 142 |
+
"""
|
| 143 |
+
rng = np.random.default_rng(seed)
|
| 144 |
+
fg = np.asarray(formula_genre)
|
| 145 |
+
fcv = np.asarray(formula_class_vec, float)
|
| 146 |
+
gg = list(genres)
|
| 147 |
+
|
| 148 |
+
if exclude:
|
| 149 |
+
keep = ~np.isin(fg, list(exclude))
|
| 150 |
+
fg, fcv = fg[keep], fcv[keep]
|
| 151 |
+
gg = [g for g in gg if g not in exclude]
|
| 152 |
+
|
| 153 |
+
obs = _mean_pairwise_cosine_distance(_genre_prevalence(fg, fcv, gg))
|
| 154 |
+
null = np.empty(n_perm)
|
| 155 |
+
for i in range(n_perm):
|
| 156 |
+
perm = rng.permutation(fg)
|
| 157 |
+
null[i] = _mean_pairwise_cosine_distance(_genre_prevalence(perm, fcv, gg))
|
| 158 |
+
|
| 159 |
+
p = (np.sum(null >= obs) + 1) / (n_perm + 1) # one-sided: separation > chance
|
| 160 |
+
return dict(observed=round(obs, 4),
|
| 161 |
+
null_mean=round(float(null.mean()), 4),
|
| 162 |
+
null_95=round(float(np.percentile(null, 95)), 4),
|
| 163 |
+
p_value=round(float(p), 4),
|
| 164 |
+
genuine=bool(p < 0.05))
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
# --------------------------------------------------------------------------- #
|
| 168 |
+
# 4. random-drop control for the drop-10 stress test
|
| 169 |
+
# --------------------------------------------------------------------------- #
|
| 170 |
+
def random_drop_control(y_true, pred, k=10, n_iter=10000, seed=0, flagged_rho=None):
|
| 171 |
+
"""
|
| 172 |
+
Is the drop-10 improvement specific to the flagged materials, or does dropping
|
| 173 |
+
ANY k test points move rho comparably? Reports the distribution of rho under
|
| 174 |
+
random removal of k points, and where flagged_rho sits within it.
|
| 175 |
+
"""
|
| 176 |
+
rng = np.random.default_rng(seed)
|
| 177 |
+
y = np.asarray(y_true, float)
|
| 178 |
+
p = np.asarray(pred, float)
|
| 179 |
+
n = len(y)
|
| 180 |
+
|
| 181 |
+
dist = np.empty(n_iter)
|
| 182 |
+
for i in range(n_iter):
|
| 183 |
+
drop = rng.choice(n, k, replace=False)
|
| 184 |
+
mask = np.ones(n, bool)
|
| 185 |
+
mask[drop] = False
|
| 186 |
+
dist[i] = _rho(y[mask], p[mask])
|
| 187 |
+
|
| 188 |
+
out = dict(full_rho=round(float(_rho(y, p)), 4),
|
| 189 |
+
random_drop_mean=round(float(dist.mean()), 4),
|
| 190 |
+
random_drop_ci95=(round(float(np.percentile(dist, 2.5)), 4),
|
| 191 |
+
round(float(np.percentile(dist, 97.5)), 4)))
|
| 192 |
+
if flagged_rho is not None:
|
| 193 |
+
out['flagged_rho'] = flagged_rho
|
| 194 |
+
out['flagged_percentile'] = round(float(np.mean(dist <= flagged_rho) * 100), 1)
|
| 195 |
+
out['specific_to_flagged'] = bool(out['flagged_percentile'] >= 95)
|
| 196 |
+
return out
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
# --------------------------------------------------------------------------- #
|
| 200 |
+
# demo / smoke test — replace synthetic data with your v8 arrays
|
| 201 |
+
# --------------------------------------------------------------------------- #
|
| 202 |
+
if __name__ == "__main__":
|
| 203 |
+
rng = np.random.default_rng(20260710)
|
| 204 |
+
|
| 205 |
+
# --- Tracks B: 34 held-out molecules -----------------------------------
|
| 206 |
+
n_test = 34
|
| 207 |
+
y = rng.normal(size=n_test)
|
| 208 |
+
pred_struct = y * 0.66 + rng.normal(scale=0.75, size=n_test) # ~structure
|
| 209 |
+
pred_base = y * 0.55 + rng.normal(scale=0.9, size=n_test) # ~baseline
|
| 210 |
+
|
| 211 |
+
print("1. paired_bootstrap:")
|
| 212 |
+
print(" ", paired_bootstrap(y, pred_struct, pred_base, n_boot=2000))
|
| 213 |
+
print("2. williams_test:")
|
| 214 |
+
print(" ", williams_test(y, pred_struct, pred_base))
|
| 215 |
+
|
| 216 |
+
# drop-10: flagged result was rho=0.708 vs full 0.663 in v8
|
| 217 |
+
print("4. random_drop_control:")
|
| 218 |
+
print(" ", random_drop_control(y, pred_struct, k=10, n_iter=2000, flagged_rho=0.708))
|
| 219 |
+
|
| 220 |
+
# --- Track A: character permutation null -------------------------------
|
| 221 |
+
genres = ["amber_oriental", "citrus_cologne", "floral_woody", "fougere", "wildcard"]
|
| 222 |
+
M, K = 300, 26
|
| 223 |
+
formula_genre = rng.choice(genres, size=M)
|
| 224 |
+
formula_class_vec = rng.random((M, K)) # near-null by construction
|
| 225 |
+
print("3a. character_permutation_null (all 5 genres):")
|
| 226 |
+
print(" ", character_permutation_null(formula_genre, formula_class_vec, genres, n_perm=1000))
|
| 227 |
+
print("3b. character_permutation_null (drop wildcard):")
|
| 228 |
+
print(" ", character_permutation_null(formula_genre, formula_class_vec, genres,
|
| 229 |
+
n_perm=1000, exclude=("wildcard",)))
|