# ============================================================================= # Project : modelmap # File : tests/test_stats_and_probes.py # Purpose : Correctness tests for stats + probe modules (tiny references) # Author : Simon-Pierre Boucher # Contact : contact@spboucher.ai # Website : https://modelmap.io # Created : 2026-08-12 # Modified : 2026-08-12 # Platform : macOS / Apple Silicon (arm64) # License : All rights reserved (research code) # ============================================================================= """Charter §0.3: numerical probes get correctness tests against tiny references — a probe must separate real structure from noise, FDR must control false discoveries on known nulls, map cards must gate publication.""" import numpy as np from modelmap.atlas.mapcard import MapCard from modelmap.probes.linear import probe_with_control from modelmap.stats.replication import bh_fdr, bootstrap_ci, jaccard, replication_rate def test_bootstrap_ci_covers_mean(): rng = np.random.default_rng(1) point, lo, hi = bootstrap_ci(rng.normal(5.0, 1.0, 500)) assert lo < 5.0 < hi and abs(point - 5.0) < 0.2 def test_bh_fdr_null_and_signal(): rng = np.random.default_rng(2) null_p = rng.uniform(0, 1, 1000) assert bh_fdr(null_p, q=0.05).mean() < 0.01 # near-zero false discoveries mixed = np.concatenate([null_p, np.full(50, 1e-8)]) disc = bh_fdr(mixed, q=0.05) assert disc[-50:].all() # all planted signals found def test_jaccard_and_replication(): assert jaccard({1, 2}, {1, 2}) == 1.0 assert jaccard({1}, {2}) == 0.0 point, lo, hi = replication_rate([{1, 2, 3}, {2, 3, 4}, {1, 2, 3}]) assert 0 < lo <= point <= hi <= 1 def test_probe_separates_structure_from_noise(): """On separable data: high selectivity. On pure noise labels: ~0.""" rng = np.random.default_rng(3) n, d = 600, 32 y = rng.integers(0, 2, n) x = rng.normal(0, 1, (n, d)) x[:, 0] += 3.0 * y # planted signal in one dimension r = probe_with_control(x, y, seed=0) assert r.task_accuracy > 0.9 assert r.selectivity > 0.3 y_noise = rng.integers(0, 2, n) x_noise = rng.normal(0, 1, (n, d)) r0 = probe_with_control(x_noise, y_noise, seed=0) assert abs(r0.selectivity) < 0.15 # noise floor: no fake structure def test_mapcard_gates_publication(): card = MapCard() assert card.validate() # empty card blocked card = MapCard( map_id="atlas/demo/probes/v0", map_type="probes", model_id="demo", model_hash="abc", quantization="fp16", commit="deadbeef", config="{}", created="2026-08-12", hardware_manifest={"chip": "test"}, confidence_level=1, regenerate_command="make demo", seeds=[0, 1, 2], prompt_sets=["a#sha", "b#sha"], controls=["shuffled-labels"], replication_rate=0.8, ) assert card.validate() == [] card.seeds = [0] # Level 1 with 1 seed must fail assert any("3 seeds" in e for e in card.validate())