#!/usr/bin/env python3 """Paired seed-block statistical comparison of validation macro-F1 across architectures.""" from pathlib import Path import csv,collections,json import numpy as np from scipy.stats import friedmanchisquare,wilcoxon ROOT=Path(__file__).resolve().parents[1] rows=list(csv.DictReader(open(ROOT/"experiments"/"validation_runs.csv"))) by=collections.defaultdict(dict) for r in rows:by[r["model"]][int(r["seed"])]=float(r["val_macro_f1"]) models=sorted(by) common=sorted(set.intersection(*(set(by[m]) for m in models))) if models else [] if len(models)<2 or len(common)<3:raise SystemExit("Need at least two models and three common seeds for paired tests.") ranked=sorted(models,key=lambda m:np.mean([by[m][s] for s in common]),reverse=True) fried=friedmanchisquare(*[[by[m][s] for s in common] for m in ranked]) best=ranked[0] comparisons=[] for model in ranked[1:]: try: stat,p=wilcoxon([by[best][s] for s in common],[by[model][s] for s in common],zero_method="wilcox",alternative="two-sided") except ValueError:stat,p=float("nan"),1.0 comparisons.append({"reference":best,"comparison":model,"wilcoxon_statistic":float(stat),"p_raw":float(p)}) # Holm step-down correction order=sorted(range(len(comparisons)),key=lambda i:comparisons[i]["p_raw"]) running=0.0;m=len(order) for rank,i in enumerate(order): adj=min(1.0,(m-rank)*comparisons[i]["p_raw"]) running=max(running,adj);comparisons[i]["p_holm"]=running out={"metric":"validation macro-F1 after per-run validation-only threshold tuning","models":ranked,"paired_seeds":common,"friedman_statistic":float(fried.statistic),"friedman_p":float(fried.pvalue),"posthoc_wilcoxon_vs_top_mean_model":comparisons,"interpretation":"Exploratory paired-seed inference. Seeds are the blocks; validation set is fixed. Do not treat these p-values as independent-sample inference or use test data for model selection."} (ROOT/"experiments"/"validation_statistical_tests.json").write_text(json.dumps(out,indent=2)) print(json.dumps(out,indent=2))