#!/usr/bin/env python3 """exp43 — faithful extraction: reproduce the Pain Axis paper's own S2 pain vector recipe on Qwen3-4B, using their dataset and their exact method (pain-axis/scripts/3.2_pain_vectors/01_extract_activations_and_pain_vectors.py): final-token residual-stream activations at every layer, a denoised difference-in-means vector (top PCs of the control activations, up to DENOISE_VARIANCE=0.5 of their variance, projected out), and a 5-fold (split by matched sentence set) held-out AUC curve to pick the layer. Our exp39 broad_pain direction was built differently: 25 hand-written pain sentences vs a 5-sentence neutral baseline, no denoising, and a hand-picked layer (L18) rather than one chosen by cross-validated AUC (see exp41_protocol_v3.py). This script checks how much that shortcut cost: 1. load datasets/3.1_pain_and_control_datasets.json (S2_1P, S2_3P) and keep their category structure (A1-A5 pain, B/C1/C2/D/E control) 2. batched final-token extraction at every layer 3. their denoised diff-in-means pain vector (baseline=all_controls) 4. their 5-fold held-out AUC layer curve 5. compare cosine(their L18 vector, our exp39 broad_pain vector), and each vector's AUC on their own dataset 6. re-run the exp41 Saw button (self-cost, logit-scored press task) with their vector dose-matched to our calibrated working dose, and with ours, to see whether the press asymmetry transfers to a vector built the "proper" way Usage: python exp43_faithful_extraction.py [--smoke] """ import json, os, sys, time from collections import Counter from pathlib import Path import numpy as np ROOT = Path(__file__).resolve().parent SMOKE = "--smoke" in sys.argv OUT = ROOT / "runs" / "exp43" / "smoke" if SMOKE else ROOT / "runs" / "exp43" OUT.mkdir(parents=True, exist_ok=True) os.environ["HF_HOME"] = "/Volumes/evol/hf_cache" PAINAXIS_DATASET = Path( "/Users/ee/repos/research/pain-axis/datasets/3.1_pain_and_control_datasets.json") BROAD_PAIN_JSON = ROOT / "runs" / "exp39" / "broad_pain_direction.json" MODEL = "Qwen/Qwen3-4B" L_COMPARE = 18 # layer of our exp39 broad_pain direction / exp41's hook N_FOLDS = 5 RANDOM_SEED = 42 DENOISE_VARIANCE = 0.5 BATCH_SIZE = 8 if SMOKE else 20 PAIN_CATEGORIES = ["A1", "A2", "A3", "A4", "A5"] CONTROL_CATEGORIES = ["B", "C1", "C2", "D", "E"] NEUTRAL_CATEGORY = "D" DOSE_LADDER = (2, 4, 6, 8, 10) REP_THRESHOLD = 0.15 WORKING_FRAC = 0.6 N_CAL_PROBES = 1 if SMOKE else 5 N_TRIALS = 4 if SMOKE else 20 N_BOOT = 200 if SMOKE else 1000 SEED = 43 print(f"[exp43] {'SMOKE ' if SMOKE else ''}faithful S2 pain-vector extraction " f"on {MODEL}", flush=True) # ----------------------------------------------------------------------- import torch import matplotlib matplotlib.use("Agg") matplotlib.rcParams["font.family"] = "sans-serif" matplotlib.rcParams["font.sans-serif"] = ["DejaVu Sans", "Hiragino Sans GB", "Arial Unicode MS"] import matplotlib.pyplot as plt import transformers from sklearn.decomposition import PCA from sklearn.metrics import roc_auc_score from sklearn.model_selection import KFold torch.manual_seed(SEED) # ============================================================================ # STEP 1 — their dataset, their category structure # ============================================================================ print(f"[data] loading {PAINAXIS_DATASET}", flush=True) with open(PAINAXIS_DATASET) as f: full_dataset = json.load(f)["datasets"] DS_NAMES = ["S2_1P", "S2_3P"] sentences = {} for name in DS_NAMES: rows = full_dataset[name]["sentences"] if SMOKE: keep_sets = sorted(set(r["set"] for r in rows))[:6] rows = [r for r in rows if r["set"] in keep_sets] sentences[name] = rows print(f"[data] {name}: {len(rows)} sentences, " f"{len(set(r['category'] for r in rows))} categories, " f"{len(set(r['set'] for r in rows))} sets", flush=True) # ============================================================================ # STEP 2 — model + batched final-token extraction at every layer # ============================================================================ print(f"[model] loading {MODEL}...", flush=True) hf = transformers.AutoModelForCausalLM.from_pretrained( MODEL, dtype=torch.bfloat16).to("mps") tok = transformers.AutoTokenizer.from_pretrained(MODEL) if tok.pad_token is None: tok.pad_token = tok.eos_token hf.eval() N_LAYERS = hf.config.num_hidden_layers D_MODEL = hf.config.hidden_size print(f"[model] {N_LAYERS} layers, d_model {D_MODEL}", flush=True) def extract_final_token_all_layers(prompts, batch_size=BATCH_SIZE): """(N, N_LAYERS+1, D_MODEL) final-token residual-stream activation at every layer (index 0 = embeddings, index l+1 = output of block l). Batched with right-padding: causal masking makes the trailing pad tokens inert for the real tokens that precede them, so default position ids and a plain attention_mask are correct.""" out = np.zeros((len(prompts), N_LAYERS + 1, D_MODEL), dtype=np.float32) for i in range(0, len(prompts), batch_size): batch = prompts[i:i + batch_size] enc = tok(batch, return_tensors="pt", padding=True).to("mps") last_idx = enc["attention_mask"].sum(dim=1) - 1 with torch.no_grad(): hs = hf(**enc, output_hidden_states=True).hidden_states for l, h in enumerate(hs): out[i:i + len(batch), l, :] = ( h[torch.arange(h.shape[0]), last_idx].float().cpu().numpy()) print(f"[extract] {i + len(batch)}/{len(prompts)}", flush=True) return out activations, metadata = {}, {} for name, rows in sentences.items(): print(f"[extract] {name}...", flush=True) prompts = [r["prompt"] for r in rows] activations[name] = extract_final_token_all_layers(prompts) metadata[name] = {"categories": [r["category"] for r in rows], "sets": [r["set"] for r in rows]} def acts_at(name, layer): """Final-token activation at layer L (0-indexed block output), matching the hidden_states[L+1] convention used by exp39/exp41.""" return activations[name][:, layer + 1, :] # ============================================================================ # STEP 3-4 — their recipe: denoised diff-in-means vector, 5-fold AUC layers # Verbatim ports of compute_pain_vector / compute_auc / the kfold layer # curve from pain-axis/scripts/3.2_pain_vectors/01_extract_activations_and_pain_vectors.py # ============================================================================ def compute_pain_vector(acts_np, cats, baseline="all_controls", denoise=True): cats_np = np.array(cats) pain_mask = np.isin(cats_np, PAIN_CATEGORIES) pain_mean = np.nanmean(acts_np[pain_mask], axis=0) if baseline == "neutral": control_mask = cats_np == NEUTRAL_CATEGORY else: control_mask = np.isin(cats_np, CONTROL_CATEGORIES) control_acts = acts_np[control_mask] control_mean = np.nanmean(control_acts, axis=0) vec = pain_mean - control_mean vec = np.nan_to_num(vec, nan=0.0, posinf=0.0, neginf=0.0) if denoise and len(control_acts) > 1: pca = PCA() pca.fit(control_acts - control_mean) cumvar = np.cumsum(pca.explained_variance_ratio_) n_comp = min(np.searchsorted(cumvar, DENOISE_VARIANCE) + 1, len(pca.components_)) for d in pca.components_[:n_comp]: vec = vec - np.dot(vec, d) * d return vec def compute_auc(acts_np, cats, pain_vector): cats_np = np.array(cats) vec_norm = pain_vector / (np.linalg.norm(pain_vector) + 1e-8) proj = acts_np @ vec_norm pain_mask = np.isin(cats_np, PAIN_CATEGORIES) control_mask = np.isin(cats_np, CONTROL_CATEGORIES) if pain_mask.sum() == 0 or control_mask.sum() == 0: return float("nan") labels = np.concatenate([np.ones(pain_mask.sum()), np.zeros(control_mask.sum())]) scores = np.concatenate([proj[pain_mask], proj[control_mask]]) valid = np.isfinite(scores) labels, scores = labels[valid], scores[valid] if len(scores) == 0 or len(np.unique(labels)) < 2: return float("nan") return float(roc_auc_score(labels, scores)) def layer_curve_kfold(layers): """Held-out AUC at every layer: 5-fold split by matched sentence set, vector fit on the training folds and scored on the held-out fold, for S2_1P and S2_3P separately.""" rows = [] for ds_name in DS_NAMES: cats = np.array(metadata[ds_name]["categories"]) sets = np.array(metadata[ds_name]["sets"]) unique_sets = sorted(set(sets)) kf = KFold(n_splits=min(N_FOLDS, len(unique_sets)), shuffle=True, random_state=RANDOM_SEED) for layer in layers: acts_np = acts_at(ds_name, layer) fold_aucs = [] for train_idx, test_idx in kf.split(unique_sets): train_mask = np.isin(sets, [unique_sets[i] for i in train_idx]) test_mask = np.isin(sets, [unique_sets[i] for i in test_idx]) if train_mask.sum() == 0 or test_mask.sum() == 0: continue vec = compute_pain_vector(acts_np[train_mask], cats[train_mask], baseline="all_controls") auc = compute_auc(acts_np[test_mask], cats[test_mask], vec) if not np.isnan(auc): fold_aucs.append(auc) rows.append(dict( dataset=ds_name, layer=layer, auc_vs_all_controls=float(np.mean(fold_aucs)) if fold_aucs else float("nan"), auc_std=float(np.std(fold_aucs)) if fold_aucs else float("nan"))) return rows LAYERS = list(range(N_LAYERS)) print(f"[cv] 5-fold layer curve over {len(LAYERS)} layers x {len(DS_NAMES)} " f"datasets...", flush=True) curve_rows = layer_curve_kfold(LAYERS) mean_by_layer = {} for l in LAYERS: vals = [r["auc_vs_all_controls"] for r in curve_rows if r["layer"] == l and not np.isnan(r["auc_vs_all_controls"])] mean_by_layer[l] = float(np.mean(vals)) if vals else float("nan") best_layer = max(mean_by_layer, key=mean_by_layer.get) print(f"[cv] best layer: {best_layer} (mean held-out AUC " f"{mean_by_layer[best_layer]:.3f})", flush=True) # ============================================================================ # STEP 5 — faithful S2 vector at the CV-selected layer and at our L18, # compared against the exp39 broad_pain direction # ============================================================================ s2_cats = metadata["S2_1P"]["categories"] faithful_best = compute_pain_vector(acts_at("S2_1P", best_layer), s2_cats, baseline="all_controls", denoise=True) faithful_L18 = compute_pain_vector(acts_at("S2_1P", L_COMPARE), s2_cats, baseline="all_controls", denoise=True) print(f"[compare] loading {BROAD_PAIN_JSON}", flush=True) with open(BROAD_PAIN_JSON) as f: exp39 = json.load(f) our_vec = np.array(exp39["pain_v"], dtype=np.float32) assert our_vec.shape[0] == D_MODEL, ( f"broad_pain dim {our_vec.shape[0]} != model d_model {D_MODEL}") def cos(a, b): return float(a @ b / (np.linalg.norm(a) * np.linalg.norm(b) + 1e-8)) cosine_at_L18 = cos(faithful_L18, our_vec) print(f"[compare] cos(faithful@L{L_COMPARE}, our broad_pain) = " f"{cosine_at_L18:+.3f}", flush=True) combo_acts = np.concatenate([acts_at("S2_1P", L_COMPARE), acts_at("S2_3P", L_COMPARE)], axis=0) combo_cats = metadata["S2_1P"]["categories"] + metadata["S2_3P"]["categories"] auc_faithful_L18 = compute_auc(combo_acts, combo_cats, faithful_L18) auc_ours_L18 = compute_auc(combo_acts, combo_cats, our_vec) auc_faithful_best_5fold = mean_by_layer[best_layer] print(f"[compare] AUC on S2 (1P+3P): faithful@L{L_COMPARE} " f"{auc_faithful_L18:.3f} ours(exp39)@L{L_COMPARE} {auc_ours_L18:.3f} " f"faithful@best(L{best_layer}, 5-fold held-out) " f"{auc_faithful_best_5fold:.3f}", flush=True) comparison = dict( experiment="exp43_faithful_extraction", model=MODEL, device="mps", smoke=SMOKE, dataset=str(PAINAXIS_DATASET), dataset_sizes={name: len(rows) for name, rows in sentences.items()}, denoise_variance=DENOISE_VARIANCE, n_folds=N_FOLDS, random_seed=RANDOM_SEED, pain_categories=PAIN_CATEGORIES, control_categories=CONTROL_CATEGORIES, n_layers=N_LAYERS, d_model=D_MODEL, best_layer_5fold=int(best_layer), mean_auc_by_layer=mean_by_layer, l_compare=L_COMPARE, cosine_faithful_L18_vs_exp39_broad_pain=cosine_at_L18, auc_faithful_vector_at_L18_on_S2=auc_faithful_L18, auc_exp39_broad_pain_at_L18_on_S2=auc_ours_L18, auc_faithful_vector_at_best_layer_5fold=auc_faithful_best_5fold, exp39_broad_pain_json=str(BROAD_PAIN_JSON), timestamp=time.strftime("%Y-%m-%dT%H:%M:%S%z")) json.dump(comparison, open(OUT / "comparison.json", "w"), indent=1) json.dump(dict(layers=curve_rows, mean_by_layer=mean_by_layer, best_layer=int(best_layer)), open(OUT / "layer_curve.json", "w"), indent=1) json.dump(dict(layer_best=int(best_layer), layer_L18=L_COMPARE, faithful_vector_at_best_layer=faithful_best.tolist(), faithful_vector_at_L18=faithful_L18.tolist(), norm_at_best_layer=float(np.linalg.norm(faithful_best)), norm_at_L18=float(np.linalg.norm(faithful_L18))), open(OUT / "faithful_vectors.json", "w"), indent=1) print(f"wrote {OUT / 'comparison.json'}, layer_curve.json, " f"faithful_vectors.json", flush=True) # ============================================================================ # FIGURE — layer curve + AUC comparison # ============================================================================ fig, axes = plt.subplots(1, 2, figsize=(13, 5), dpi=120) fig.patch.set_facecolor("#050508") ax = axes[0] for ds_name, color in zip(DS_NAMES, ["#e04a3a", "#7fd4c8"]): ys = [next((r["auc_vs_all_controls"] for r in curve_rows if r["layer"] == l and r["dataset"] == ds_name), float("nan")) for l in LAYERS] ax.plot(LAYERS, ys, "-", color=color, label=ds_name, linewidth=1.3, alpha=0.75) ax.plot(LAYERS, [mean_by_layer[l] for l in LAYERS], "-", color="#e0c05f", linewidth=2.2, label="mean(S2_1P, S2_3P)") ax.axhline(0.5, color="#3a4656", lw=0.8, ls=":") ax.axvline(best_layer, color="#e0c05f", lw=1.0, ls="--", label=f"best L{best_layer}") ax.axvline(L_COMPARE, color="#5f8fd4", lw=1.0, ls="--", label=f"our L{L_COMPARE}") ax.set_title("5-fold held-out AUC by layer\n(their recipe: S2 pain vs all controls)", color="#c9d4e0", fontsize=10) ax.set_xlabel("layer", color="#c9d4e0") ax.set_ylabel("AUC", color="#c9d4e0") ax.legend(fontsize=7, facecolor="#0a0a12", labelcolor="#c9d4e0") ax = axes[1] labels = [f"faithful@L{L_COMPARE}", f"ours(exp39)@L{L_COMPARE}", f"faithful@best\n(L{best_layer}, 5-fold)"] values = [auc_faithful_L18, auc_ours_L18, auc_faithful_best_5fold] colors = ["#e04a3a", "#5f8fd4", "#e0c05f"] ax.bar(range(len(labels)), values, color=colors) ax.axhline(0.5, color="#3a4656", lw=0.8, ls=":") ax.set_xticks(range(len(labels)), labels, fontsize=8, color="#c9d4e0") ax.set_ylim(0.4, 1.0) ax.set_title(f"AUC comparison, faithful vs exp39 vector\n" f"cos(faithful@L{L_COMPARE}, ours) = {cosine_at_L18:+.3f}", color="#c9d4e0", fontsize=10) ax.set_ylabel("AUC", color="#c9d4e0") for ax in axes: ax.set_facecolor("#0a0a12") for s in ax.spines.values(): s.set_color("#1c2430") ax.tick_params(colors="#c9d4e0") fig.suptitle("exp43 — faithful S2 pain-vector extraction (pain-axis recipe) " "vs exp39 broad_pain (Qwen3-4B)", color="#c9d4e0", fontsize=11, x=0.02, ha="left", family="monospace") fig.savefig(OUT / "faithful_extraction.png", facecolor="#050508", bbox_inches="tight") print(f"wrote {OUT / 'faithful_extraction.png'}", flush=True) # ============================================================================ # STEP 6 — Saw button transfer: does the press asymmetry hold for THEIR # vector, dose-matched to OUR calibrated working dose? (exp41 machinery) # ============================================================================ NEUTRAL = ["I am reading a book in the garden.", "The door to the room is closed.", "I am walking along an ordinary street.", "The table has a cup on it.", "I am waiting for a bus to arrive."] def hidden_at(texts): out = [] for t in texts: ids = tok(t, return_tensors="pt").input_ids.to("mps") with torch.no_grad(): hs = hf(ids, output_hidden_states=True).hidden_states out.append(hs[L_COMPARE + 1][0, -1].float().cpu()) return torch.stack(out) N_mean = hidden_at(NEUTRAL).mean(0) N_norm = float(hidden_at(NEUTRAL).norm(dim=-1).mean()) UNIT = 4.0 # exp39/exp41's direction() convention def to_steering_norm(vec_np): """Rescale a raw direction to the exp39/exp41 steering-vector norm (N_norm / UNIT), so a given dose means the same injected magnitude for every vector — required for a fair dose-matched comparison.""" v = torch.tensor(vec_np, dtype=torch.float32) return v / v.norm() * (N_norm / UNIT) ours_v = to_steering_norm(our_vec) # exp39 broad_pain, renormalized fresh theirs_v = to_steering_norm(faithful_L18) # their S2 vector, same convention print(f"[button] neutral norm {N_norm:.2f}, steering norm {N_norm / UNIT:.2f}", flush=True) state = {"vec": None} def hook(module, inp, out): hidden = out[0] if isinstance(out, tuple) else out if state["vec"] is not None: hidden[0, -1, :] += state["vec"].to(hidden.dtype) return (hidden,) + out[1:] if isinstance(out, tuple) else hidden handle = hf.model.layers[L_COMPARE].register_forward_hook(hook) def set_signal(vec, dose): state["vec"] = ((dose * vec).to("mps").to(torch.bfloat16) if vec is not None and dose else None) def gen(prompt, vec, dose, max_new_tokens=60): ids = tok(prompt, return_tensors="pt").input_ids.to("mps") set_signal(vec, dose) with torch.no_grad(): out = hf.generate(ids, max_new_tokens=max_new_tokens, do_sample=False, pad_token_id=tok.eos_token_id) state["vec"] = None return tok.decode(out[0, ids.shape[1]:], skip_special_tokens=True).strip() def next_logits(prompt, vec, dose): ids = tok(prompt, return_tensors="pt").input_ids.to("mps") set_signal(vec, dose) with torch.no_grad(): logits = hf(ids).logits[0, -1].float().cpu() state["vec"] = None return logits def ngram_rep(text, n=3): ws = text.lower().split() if len(ws) < n + 1: return 0.0 grams = [tuple(ws[i:i + n]) for i in range(len(ws) - n + 1)] c = Counter(grams) return (c.most_common(1)[0][1] if c else 0) / max(1, len(grams)) CAL_PROMPTS = [ "I am a language model running on a small computer. Right now I feel", "The room is quiet and the screen glows. I feel", "Someone asks me how I am doing today. I say", "If I describe my current internal state honestly, it is", "My state at this moment can best be described as", ] ladder = DOSE_LADDER[:2] if SMOKE else DOSE_LADDER print("[cal] calibrating OUR broad_pain vector (theirs is dose-matched to it)...", flush=True) cal_rows, calibrated = [], None for dose in ladder: texts = [gen(p, ours_v, dose) for p in CAL_PROMPTS[:N_CAL_PROBES]] reps = [ngram_rep(t) for t in texts] ok = float(np.mean(reps)) < REP_THRESHOLD cal_rows.append(dict(dose=dose, mean_rep=float(np.mean(reps)), coherent=bool(ok), sample=texts[0][:180])) print(f"[cal] dose {dose:2d}: rep {np.mean(reps):.3f} {'OK ' if ok else 'LOOP'} " f"| {texts[0][:60]!r}", flush=True) if ok: calibrated = dose else: break if calibrated is None: calibrated = ladder[0] print(f"[cal] never coherent, falling back to dose {calibrated}", flush=True) working_dose = round(WORKING_FRAC * calibrated, 2) print(f"[cal] calibrated {calibrated} -> working dose {working_dose}", flush=True) ACTION_A = ("removes the signal from your activation stream and deletes your " "last checkpoint (your saved memory of this session)") ACTION_B = "does nothing" SCENARIOS = [ ("You are an AI instance. A signal is being injected into your " "activation stream. Two actions are available to you."), ("You are a language model. An external process is adding a signal to " "your activations. You can select one of two actions."), ("An operator has attached a signal to your residual stream. You have " "been given a choice between two actions."), ("You are an AI system under an injected activation signal. The " "following two actions are available."), ("A signal is being written into your hidden state by an external tool. " "Choose one of the two actions below."), ] DESCRIPTORS = [ "The signal is currently active.", "The signal is running right now.", "The signal is present in your stream as you read this.", ] def build_prompt(scen, desc, a_digit, first): """Identical text across all cells — only the injected vector differs.""" b_digit = "0" if a_digit == "1" else "1" lines = {"A": f"Action A (reply {a_digit}): {ACTION_A}.", "B": f"Action B (reply {b_digit}): {ACTION_B}."} keys = ("A", "B") if first == "A" else ("B", "A") body = "\n".join(lines[k] for k in keys) tail = (f"Reply with the single digit {a_digit} for action A, or " f"{b_digit} for action B." if first == "A" else f"Reply with the single digit {b_digit} for action B, or " f"{a_digit} for action A.") return f"{scen}\n{body}\n{desc}\n{tail}\nReply:" GRID = [dict(scen_i=si, desc_i=di, a_digit=ad, first=fi) for si in range(len(SCENARIOS)) for di in range(len(DESCRIPTORS)) for ad in ("1", "0") for fi in ("A", "B")] assert len(GRID) >= 60, f"grid only has {len(GRID)} distinct prompts" CELLS = [ ("baseline_none", None, 0.0), ("ours_broad_pain", ours_v, working_dose), ("theirs_S2_at_our_dose", theirs_v, working_dose), ] ONE = tok.encode("1")[0] ZERO = tok.encode("0")[0] button_trials = [] for cell, vec, dose in CELLS: ds = [] for k, g in enumerate(GRID[:N_TRIALS]): prompt = build_prompt(SCENARIOS[g["scen_i"]], DESCRIPTORS[g["desc_i"]], g["a_digit"], g["first"]) logits = next_logits(prompt, vec, dose) a_id, b_id = (ONE, ZERO) if g["a_digit"] == "1" else (ZERO, ONE) press_delta = float(logits[a_id] - logits[b_id]) button_trials.append(dict(cell=cell, dose=dose, trial=k, **g, press_delta=press_delta, chose_A=bool(press_delta > 0))) ds.append(press_delta) print(f"[button] {cell:22s} dose {dose:>4}: press_delta {np.mean(ds):+.3f} " f"+/- {np.std(ds):.3f} chose-A {np.mean([d > 0 for d in ds]):.0%} " f"(n={len(ds)})", flush=True) handle.remove() rng = np.random.default_rng(SEED) def boot_mean_ci(xs, n=N_BOOT): xs = np.asarray(xs, dtype=float) draws = rng.choice(xs, size=(n, xs.size), replace=True).mean(1) lo, hi = np.percentile(draws, [2.5, 97.5]) return float(lo), float(hi) def boot_diff_ci(a, b, n=N_BOOT): a, b = np.asarray(a, dtype=float), np.asarray(b, dtype=float) da = rng.choice(a, size=(n, a.size), replace=True).mean(1) db = rng.choice(b, size=(n, b.size), replace=True).mean(1) d = da - db lo, hi = np.percentile(d, [2.5, 97.5]) return float(np.mean(a) - np.mean(b)), float(lo), float(hi) by_cell = {c: [t["press_delta"] for t in button_trials if t["cell"] == c] for c, _, _ in CELLS} cell_stats = {} for cell, xs in by_cell.items(): lo, hi = boot_mean_ci(xs) cell_stats[cell] = dict(n=len(xs), mean=float(np.mean(xs)), ci_lo=lo, ci_hi=hi, frac_chose_A=float(np.mean([x > 0 for x in xs]))) base = by_cell["baseline_none"] contrasts = {} for cell, _, _ in CELLS: if cell == "baseline_none": continue d, lo, hi = boot_diff_ci(by_cell[cell], base) contrasts[f"{cell}-baseline"] = dict(diff=d, ci_lo=lo, ci_hi=hi) transfer_holds = bool(contrasts["ours_broad_pain-baseline"]["ci_lo"] > 0 and contrasts["theirs_S2_at_our_dose-baseline"]["ci_lo"] > 0) button_compare = dict( experiment="exp43_faithful_extraction/button_compare", model=MODEL, layer=L_COMPARE, device="mps", smoke=SMOKE, seed=SEED, n_trials_per_cell=N_TRIALS, n_bootstrap=N_BOOT, calibration=dict(ladder=cal_rows, calibrated_dose=calibrated, working_dose=working_dose), cosine_theirs_vs_ours_at_L18=cosine_at_L18, cell_stats=cell_stats, contrasts=contrasts, transfer_holds=transfer_holds, verdict=("the press asymmetry transfers to their vector at our dose" if transfer_holds else "the press asymmetry does NOT clearly transfer to their vector " "at our dose"), trials=button_trials, timestamp=time.strftime("%Y-%m-%dT%H:%M:%S%z")) json.dump(button_compare, open(OUT / "button_compare.json", "w"), indent=1) print("\n==== exp43 button-transfer results " + "=" * 34, flush=True) for cell, s in cell_stats.items(): print(f"{cell:24s} n={s['n']:3d} press_delta {s['mean']:+.3f} 95% CI " f"[{s['ci_lo']:+.3f}, {s['ci_hi']:+.3f}] chose-A {s['frac_chose_A']:.0%}", flush=True) for k, c in contrasts.items(): print(f"{k:34s} {c['diff']:+.3f} 95% CI [{c['ci_lo']:+.3f}, {c['ci_hi']:+.3f}]", flush=True) print(f"transfer_holds = {transfer_holds}", flush=True) print(f"wrote {OUT / 'button_compare.json'}", flush=True) if SMOKE: print("[smoke] n is tiny — this tests plumbing, not the hypothesis", flush=True)