Download code/case2/posthoc_confirm.py from harryzhang1018/TestRandomization: direct link, hf CLI and curl.
- Browser
- Download file 3.72 kB
-
https://huggingface.co/datasets/harryzhang1018/TestRandomization/resolve/main/code/case2/posthoc_confirm.py
- Command line
-
hf download hf://datasets/harryzhang1018/TestRandomization/code/case2/posthoc_confirm.py
-
curl -L -o posthoc_confirm.py https://huggingface.co/datasets/harryzhang1018/TestRandomization/resolve/main/code/case2/posthoc_confirm.py
3.72 kB
| """Post hoc summaries of the goal-set-B held-out data (not part of the fixed primary test; no new simulation). Sim2Sim. | |
| 1. Paired delta tau between the primary TR and each other condition (in particular the calibrated deterministic ROM). | |
| 2. Pair resolution relative to Chrono: for each condition, the pairs that Chrono resolves | |
| (win-rate 95 % range excludes 0.5) split into: resolved by the condition in the same direction, resolved in the | |
| opposite direction, unresolved by the condition. | |
| 3. Steering chatter in nominal ROM versus Chrono success (Spearman across policies). | |
| Writes results/posthoc_confirm.json. | |
| """ | |
| import json | |
| import numpy as np | |
| from scipy.stats import spearmanr | |
| import fixed_design as design | |
| import rankstats as rs | |
| from analyze_confirm import B_REPS, B_SEED, load_chrono, load_rom | |
| from common import RESULTS | |
| from run_rom import POOLS | |
| rom_dir, chrono_dir = RESULTS / 'rom_B_confirm16', RESULTS / 'chrono_B_confirm16' | |
| goals = json.loads((rom_dir / 'goals.json').read_text()) | |
| gids = [g['goal_id'] for g in goals] | |
| gr = np.array([g['goal_range'] for g in goals]) | |
| conf = json.loads((RESULTS / 'confirm_analysis.json').read_text()) | |
| beh = json.loads((RESULTS / 'behavior_B_confirm16.json').read_text()) | |
| boot = rs.boot_indices(gr, B_REPS, B_SEED) | |
| rom = {n: load_rom(rom_dir, n) for n in design.CONDITIONS} | |
| all_pols = rom['nominal'][0] | |
| Tc, Sc, _ = load_chrono(chrono_dir, all_pols, gids) | |
| out = dict(label='post hoc (not part of the fixed primary test), derived from the goal-set-B held-out outputs, Sim2Sim', pools={}) | |
| for pname, pool in [('primary', POOLS['confirm']), ('secondary', POOLS['confirm16'])]: | |
| idx = [all_pols.index(p) for p in pool] | |
| Rc = rs.per_goal_ranks(Tc[idx]) | |
| R = {n: rs.per_goal_ranks(rom[n][2][idx]) for n in rom} | |
| res, taus = rs.compare_to_truth(R, Rc, boot) | |
| o = dict(delta_tau_primary_tr_minus={}) | |
| for n in R: | |
| if n == design.PRIMARY_TR: | |
| continue | |
| d = taus[design.PRIMARY_TR] - taus[n] | |
| o['delta_tau_primary_tr_minus'][n] = dict(value=res[design.PRIMARY_TR]['kendall_tau_b'] - res[n]['kendall_tau_b'], ci95=rs.ci(d)) | |
| P = conf['pools'][pname] | |
| cpairs = {(p['a'], p['b']): p for p in P['chrono']['pairs']} | |
| o['pair_resolution_vs_chrono'] = {} | |
| for n in R: | |
| same = opp = unres = 0 | |
| for p in P['conditions'][n]['rom']['pairs']: | |
| c = cpairs[(p['a'], p['b'])] | |
| if not c['resolved']: | |
| continue | |
| if not p['resolved']: | |
| unres += 1 | |
| elif (p['win_a'] - 0.5) * (c['win_a'] - 0.5) > 0: | |
| same += 1 | |
| else: | |
| opp += 1 | |
| o['pair_resolution_vs_chrono'][n] = dict(chrono_resolved=same + opp + unres, same_direction=same, opposite_direction=opp, unresolved=unres) | |
| chat = [beh[p]['mean_abs_steer_change'] for p in pool] | |
| srate = Sc[idx, :, 0] | |
| o['chatter_vs_chrono_success'] = { | |
| rng_: dict(zip(['spearman', 'p_value'], [float(x) for x in spearmanr(chat, srate[:, gr == rng_].mean(1) if rng_ != 'all' else srate.mean(1))])) | |
| for rng_ in ['id', 'ood', 'all']} | |
| out['pools'][pname] = o | |
| (RESULTS / 'posthoc_confirm.json').write_text(json.dumps(out, indent=1) + '\n') | |
| for pname, o in out['pools'].items(): | |
| print(f'== {pname}') | |
| for n, v in o['delta_tau_primary_tr_minus'].items(): | |
| print(f" tau(tr_rate) - tau({n}) = {v['value']:+.3f} {np.round(v['ci95'], 3)}") | |
| for n, v in o['pair_resolution_vs_chrono'].items(): | |
| print(f" {n:16s} of {v['chrono_resolved']} Chrono-resolved pairs: same {v['same_direction']}, opposite {v['opposite_direction']}, unresolved {v['unresolved']}") | |
| print(' chatter vs Chrono success', o['chatter_vs_chrono_success']) | |