File size: 1,402 Bytes
f36cc9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Forward-minus-reverse MLP effect on sampled tokens (B - H) - (R - C), registered confirm.py draws.

    uv run --no-project --with numpy --with zstandard python code/two_way_interaction.py

Output equals stats/audit/two_way_interaction.json.
"""
import json
import sys
from pathlib import Path

import numpy as np

sys.path.insert(0, str(Path(__file__).resolve().parent))
import confirm as cf  # noqa: E402
from registered_draws_all_arms import PATHS, RAW  # noqa: E402

dataset = RAW / "confirmation256/panel.jsonl"
expected = {cf.item_key(r): r for r in cf.read_rows(dataset)}
keys = sorted(expected)
h = cf.fingerprint(dataset)
arms = {k: cf.as_arrays(cf.verified_arm(PATHS[k], expected, h)[0], keys) for k in ("base", "all32", "reverse", "concise")}
rng = np.random.Generator(np.random.PCG64(cf.SEED))
strata = {a: [i for i, k in enumerate(keys) if expected[k]["area"] == a] for a in cf.AREAS}
draws = np.column_stack([rng.choice(m, size=(cf.DRAWS, len(m))) for m in strata.values()])
out = {}
for metric in ("generated", "thinking"):
    b, f, r, c = (arms[k][metric].astype(float) for k in ("base", "all32", "reverse", "concise"))
    d = (b - f) - (r - c)
    out[metric] = {"forward_effect": float((b - f).mean()), "reverse_effect": float((r - c).mean()),
                   "forward_minus_reverse": float(d.mean()), "ci": cf.interval(d[draws].mean(1))}
json.dump(out, sys.stdout, indent=1)