"""Figures for Claim 3 from claim3_results.json."""
import json
import os
import plotly.graph_objects as go
from plotly.subplots import make_subplots
R = json.load(open("outputs/claim3/claim3_results.json", encoding="utf-8"))
os.makedirs("outputs/claim3", exist_ok=True)
curve = R["D_empirical_summary"]["curve"]
full = R["D_empirical_summary"]["mean_gap_full_horizon"]
fig = make_subplots(
rows=1, cols=2,
subplot_titles=(
"Case study (γ=0.99, k=10, n=2): paper vs recomputed",
"Empirical: value-gap error vs rollout length (200 tabular MDPs)",
),
)
# --- left: case-study constants ---
rows = R["A_case_study"]["rows"]
labels = {
"thm41_pi": "Thm 4.1
ε_π coef",
"thm41_m": "Thm 4.1
ε_m coef",
"thm42_pi": "Thm 4.2
ε_π coef",
"thm42_m": "Thm 4.2
ε_m coef",
}
names = [labels[r["quantity"]] for r in rows]
fig.add_trace(go.Bar(x=names, y=[r["paper"] for r in rows], name="Paper",
marker_color="#5B8FF9",
text=[f"{r['paper']:.0f}" for r in rows], textposition="outside"),
row=1, col=1)
fig.add_trace(go.Bar(x=names, y=[r["recomputed"] for r in rows], name="Recomputed",
marker_color="#F6BD16",
text=[f"{r['recomputed']:.1f}" for r in rows], textposition="outside"),
row=1, col=1)
fig.update_yaxes(type="log", title_text="bound coefficient (log)", row=1, col=1)
# --- right: empirical compounding curve ---
fig.add_trace(go.Scatter(
x=[c["n"] for c in curve], y=[c["mean_gap_branch"] for c in curve],
mode="lines+markers", name="n-chunk branched rollout",
line=dict(color="#5AD8A6", width=3), marker=dict(size=8)), row=1, col=2)
fig.add_hline(y=full, line_dash="dash", line_color="#E8684A",
annotation_text=f"full-horizon rollout ({full:.4f})",
annotation_position="bottom right", row=1, col=2)
fig.add_vline(x=2, line_dash="dot", line_color="#888",
annotation_text="paper uses n=2", row=1, col=2)
fig.update_xaxes(title_text="branched rollout length n (chunks)", row=1, col=2)
fig.update_yaxes(title_text="mean |V̂ − V| value-estimation error", row=1, col=2)
fig.update_layout(
title="Claim 3 — chunk-level branched rollout tightens the value gap",
barmode="group", height=460, width=1180,
legend=dict(orientation="h", yanchor="bottom", y=-0.22, xanchor="center", x=0.5),
template="plotly_white",
)
fig.write_html("outputs/claim3/claim3_figure.html", include_plotlyjs="cdn")
raw = {
"case_study": rows,
"unit_fairness": R["C_unit_fairness"],
"empirical_curve": curve,
"mean_gap_full_horizon": full,
"bounds_held": {
"thm41": f'{R["D_empirical_summary"]["thm41_bound_held"]}/{R["D_empirical_summary"]["n_seeds"]}',
"thm42": f'{R["D_empirical_summary"]["thm42_bound_held"]}/{R["D_empirical_summary"]["n_seeds"]}',
},
}
json.dump(raw, open("outputs/claim3/claim3_figure_raw.json", "w", encoding="utf-8"), indent=2)
print("wrote outputs/claim3/claim3_figure.html and claim3_figure_raw.json")