SPB Git

spb/wp27_uqo

Public
2.5 KB · 40 lines python
1"""Pre-registered sensitivity grid for the bubble/crash definitions: bubble 3/5/8 % (>= 10 ticks), crash 7/10/15 % (20-tick window).2Writes results/tables/threshold_sensitivity.csv and a LaTeX fragment with the control-vs-treatment paired effects per threshold."""3import sys4from pathlib import Path5import pandas as pd6ROOT = Path(__file__).resolve().parents[1]7sys.path.insert(0, str(ROOT / "src"))8from market_agents.analysis.reload import recompute_design  # noqa: E4029from market_agents.analysis.estimators import paired_effects  # noqa: E4021011rows = []12for design, control, cells in (("classical_confirmatory", "control", ["leverage_heavy", "leverage_heavy_funding_shock", "trend_heavy", "mock_llm_50_indep"]),13                               ("confirmatory_openai", "dose_000", ["dose_025", "dose_050", "dose_100", "leverage_025"])):14    p = ROOT / "results/runs" / design15    if not p.exists():16        continue17    for b in (0.03, 0.05, 0.08):18        for c in (0.07, 0.10, 0.15):19            df = recompute_design(p, write=False, bubble_thr=b, crash_thr=c)20            df["cell"] = df.name.str.split("/").str[-1]21            for cell in cells:22                if cell not in set(df.cell):23                    continue24                for oc in ("n_bubbles", "bubble_amp", "n_crashes"):25                    e = paired_effects(df, oc, "cell", cell, control)26                    rows.append({"design": design, "bubble_thr": b, "crash_thr": c, "cell": cell, "outcome": oc, "control": e.get("control_mean"),27                                 "effect": e.get("diff"), "p": e.get("p"), "n": e.get("n")})28t = pd.DataFrame(rows)29out = ROOT / "results/tables"30out.mkdir(parents=True, exist_ok=True)31t.to_csv(out / "threshold_sensitivity.csv", index=False)32if len(t):33    piv = t[t.outcome.isin(["n_bubbles", "n_crashes"])].pivot_table(index=["design", "cell", "outcome"], columns=["bubble_thr", "crash_thr"], values="effect")34    lines = ["\\begin{tabular}{lll" + "r" * piv.shape[1] + "}", "\\toprule", "Design & Cell & Outcome & " + " & ".join(f"b{int(b*100)}/c{int(c*100)}" for b, c in piv.columns) + " \\\\", "\\midrule"]35    for (d, cell, oc), r in piv.iterrows():36        lines.append(f"{d.replace('_', ' ')} & {cell.replace('_', ' ')} & {oc.replace('_', ' ')} & " + " & ".join(f"{v:+.2f}" for v in r.values) + " \\\\")37    lines += ["\\bottomrule", "\\end{tabular}"]38    (out / "tab_threshold_sensitivity.tex").write_text("\n".join(lines))39print(t.groupby(["design", "cell", "outcome"])["effect"].agg(["min", "max"]).round(2).to_string() if len(t) else "nothing")40
Files · c1fdf6ca460749ff23b61fccd1b74aa8618ff683
Menu

Theme