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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Turn raw job JSON into the numbers behind each claim.
|
| 2 |
+
|
| 3 |
+
Usage: python analyze.py <claim> <results.json> [more.json ...]
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
import glob
|
| 9 |
+
import json
|
| 10 |
+
import sys
|
| 11 |
+
|
| 12 |
+
import numpy as np
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def load(paths):
|
| 16 |
+
out = []
|
| 17 |
+
for p in paths:
|
| 18 |
+
for f in sorted(glob.glob(p)):
|
| 19 |
+
with open(f) as fh:
|
| 20 |
+
out.append(json.load(fh))
|
| 21 |
+
return out
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# --------------------------------------------------------------------------
|
| 25 |
+
# Claim 1 / Claim 5: from the ablation records
|
| 26 |
+
# --------------------------------------------------------------------------
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def claim1(paths):
|
| 30 |
+
"""Proposition 1: the standard (EIG) utility choice is suboptimal w.r.t. the
|
| 31 |
+
Lagrangian utility that accounts for the drifter's future trajectory.
|
| 32 |
+
|
| 33 |
+
The paper's formal proof (App. D) is a one-line argmax argument giving only
|
| 34 |
+
the weak inequality LU(x^S) <= LU(x*). Here we measure whether the gap is
|
| 35 |
+
real and how big it is, using the ground-truth field to define LU = B(.;true).
|
| 36 |
+
"""
|
| 37 |
+
recs = [r for d in load(paths) for r in d["records"]]
|
| 38 |
+
rows = []
|
| 39 |
+
for r in recs:
|
| 40 |
+
u_true = np.array(r["u_true"])
|
| 41 |
+
u_eig = np.array(r["u_eig"])
|
| 42 |
+
u_ball = np.array(r["u"]).mean(0)
|
| 43 |
+
i_eig, i_ball, i_star = u_eig.argmax(), u_ball.argmax(), u_true.argmax()
|
| 44 |
+
rows.append({
|
| 45 |
+
"t": r["t"],
|
| 46 |
+
"LU_star": u_true[i_star],
|
| 47 |
+
"LU_eig": u_true[i_eig],
|
| 48 |
+
"LU_ballast": u_true[i_ball],
|
| 49 |
+
"LU_mean": u_true.mean(), # uniform policy
|
| 50 |
+
"strict": bool(u_true[i_eig] < u_true[i_star] - 1e-9),
|
| 51 |
+
"eig_is_argmax": bool(i_eig == i_star),
|
| 52 |
+
})
|
| 53 |
+
out = {}
|
| 54 |
+
for t in sorted(set(r["t"] for r in rows)):
|
| 55 |
+
sub = [r for r in rows if r["t"] == t]
|
| 56 |
+
gap_eig = np.array([(r["LU_star"] - r["LU_eig"]) / abs(r["LU_star"]) * 100 for r in sub])
|
| 57 |
+
gap_bal = np.array([(r["LU_star"] - r["LU_ballast"]) / abs(r["LU_star"]) * 100 for r in sub])
|
| 58 |
+
gap_uni = np.array([(r["LU_star"] - r["LU_mean"]) / abs(r["LU_star"]) * 100 for r in sub])
|
| 59 |
+
out[t] = {
|
| 60 |
+
"n": len(sub),
|
| 61 |
+
"pct_strictly_suboptimal": 100 * np.mean([r["strict"] for r in sub]),
|
| 62 |
+
"gap_eig_pct": [gap_eig.mean(), 2 * gap_eig.std() / np.sqrt(len(sub))],
|
| 63 |
+
"gap_ballast_pct": [gap_bal.mean(), 2 * gap_bal.std() / np.sqrt(len(sub))],
|
| 64 |
+
"gap_unif_pct": [gap_uni.mean(), 2 * gap_uni.std() / np.sqrt(len(sub))],
|
| 65 |
+
}
|
| 66 |
+
return out
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def claim5(paths):
|
| 70 |
+
"""Sec. 5.1 / G.1: percentage utility gap vs J, and the J at which it drops
|
| 71 |
+
below 1%.
|
| 72 |
+
|
| 73 |
+
Gap_MC(J) = B(s*; inf) - B(s*_J; inf), approximated with J=200
|
| 74 |
+
Gap_Full(J) = B(s*_true; true) - B(s*_J; true)
|
| 75 |
+
"""
|
| 76 |
+
recs = [r for d in load(paths) for r in d["records"]]
|
| 77 |
+
ts = sorted(set(r["t"] for r in recs))
|
| 78 |
+
out = {}
|
| 79 |
+
for t in ts:
|
| 80 |
+
sub = [r for r in recs if r["t"] == t]
|
| 81 |
+
Jmax = np.array(sub[0]["u"]).shape[0]
|
| 82 |
+
Js = np.arange(1, Jmax + 1)
|
| 83 |
+
mc = np.zeros((len(sub), Jmax))
|
| 84 |
+
full = np.zeros((len(sub), Jmax))
|
| 85 |
+
eig_mc, eig_full, uni_mc, uni_full = [], [], [], []
|
| 86 |
+
for i, r in enumerate(sub):
|
| 87 |
+
u = np.array(r["u"]) # (Jmax, N)
|
| 88 |
+
u_true = np.array(r["u_true"])
|
| 89 |
+
u_eig = np.array(r["u_eig"])
|
| 90 |
+
B_inf = u.mean(0) # B(.; inf) approximated by J=200
|
| 91 |
+
s_star = B_inf.argmax()
|
| 92 |
+
s_true = u_true.argmax()
|
| 93 |
+
run = np.cumsum(u, axis=0) / Js[:, None] # B(.; J) for each J
|
| 94 |
+
sJ = run.argmax(axis=1) # s*_J
|
| 95 |
+
mc[i] = (B_inf[s_star] - B_inf[sJ]) / abs(B_inf[s_star]) * 100
|
| 96 |
+
full[i] = (u_true[s_true] - u_true[sJ]) / abs(u_true[s_true]) * 100
|
| 97 |
+
ie = u_eig.argmax()
|
| 98 |
+
eig_mc.append((B_inf[s_star] - B_inf[ie]) / abs(B_inf[s_star]) * 100)
|
| 99 |
+
eig_full.append((u_true[s_true] - u_true[ie]) / abs(u_true[s_true]) * 100)
|
| 100 |
+
uni_mc.append((B_inf[s_star] - B_inf.mean()) / abs(B_inf[s_star]) * 100)
|
| 101 |
+
uni_full.append((u_true[s_true] - u_true.mean()) / abs(u_true[s_true]) * 100)
|
| 102 |
+
|
| 103 |
+
def band(a):
|
| 104 |
+
return a.mean(0), 2 * a.std(0) / np.sqrt(a.shape[0])
|
| 105 |
+
|
| 106 |
+
m_mc, s_mc = band(mc)
|
| 107 |
+
m_fu, s_fu = band(full)
|
| 108 |
+
below = np.where(m_mc < 1.0)[0]
|
| 109 |
+
out[t] = {
|
| 110 |
+
"n_reps": len(sub),
|
| 111 |
+
"J": Js.tolist(),
|
| 112 |
+
"gap_mc_mean": m_mc.tolist(), "gap_mc_se2": s_mc.tolist(),
|
| 113 |
+
"gap_full_mean": m_fu.tolist(), "gap_full_se2": s_fu.tolist(),
|
| 114 |
+
"J_at_1pct_mc": int(Js[below[0]]) if len(below) else None,
|
| 115 |
+
"eig_gap_mc": float(np.mean(eig_mc)), "eig_gap_full": float(np.mean(eig_full)),
|
| 116 |
+
"unif_gap_mc": float(np.mean(uni_mc)), "unif_gap_full": float(np.mean(uni_full)),
|
| 117 |
+
"gap_at_J20_mc": float(m_mc[19]), "gap_at_J20_full": float(m_fu[19]),
|
| 118 |
+
}
|
| 119 |
+
return out
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# --------------------------------------------------------------------------
|
| 123 |
+
# Claims 3 / 4: policy comparison
|
| 124 |
+
# --------------------------------------------------------------------------
|
| 125 |
+
|
| 126 |
+
POLICY_ORDER = ["unif", "sobol", "dist_sep", "eig", "ballast_opt", "ballast_true"]
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def claim34(paths):
|
| 130 |
+
from ballast.experiment import iso_performance
|
| 131 |
+
|
| 132 |
+
res = [r for d in load(paths) for r in d["results"]]
|
| 133 |
+
seeds = sorted(set(r["seed"] for r in res))
|
| 134 |
+
pols = [p for p in POLICY_ORDER if any(r["policy"] == p for r in res)]
|
| 135 |
+
by = {(r["seed"], r["policy"]): np.array(r["errors"]) for r in res}
|
| 136 |
+
n_dep = len(next(iter(by.values())))
|
| 137 |
+
|
| 138 |
+
# runs where every policy completed
|
| 139 |
+
good = [s for s in seeds if all((s, p) in by for p in pols)]
|
| 140 |
+
E = {p: np.stack([by[(s, p)] for s in good]) for p in pols} # (n_runs, n_dep)
|
| 141 |
+
|
| 142 |
+
# --- average policy rank per iteration (1 = best)
|
| 143 |
+
stack = np.stack([E[p] for p in pols]) # (n_pol, n_runs, n_dep)
|
| 144 |
+
order = stack.argsort(axis=0).argsort(axis=0) + 1
|
| 145 |
+
rank_mean = order.mean(axis=1) # (n_pol, n_dep)
|
| 146 |
+
rank_se2 = 2 * order.std(axis=1) / np.sqrt(len(good))
|
| 147 |
+
|
| 148 |
+
# --- iso-performance vs UNIF
|
| 149 |
+
iso = {}
|
| 150 |
+
for p in pols:
|
| 151 |
+
v = np.stack([iso_performance(E[p][i], E["unif"][i]) for i in range(len(good))])
|
| 152 |
+
iso[p] = {
|
| 153 |
+
"mean": np.nanmean(v, axis=0).tolist(),
|
| 154 |
+
"se2": (2 * np.nanstd(v, axis=0) / np.sqrt(len(good))).tolist(),
|
| 155 |
+
"final": float(np.nanmean(v[:, -1])),
|
| 156 |
+
"final_se2": float(2 * np.nanstd(v[:, -1]) / np.sqrt(len(good))),
|
| 157 |
+
}
|
| 158 |
+
n_policy_chosen = n_dep - 1 # the first drifter is placed uniformly at random
|
| 159 |
+
return {
|
| 160 |
+
"n_runs": len(good),
|
| 161 |
+
"policies": pols,
|
| 162 |
+
"n_deploy": n_dep,
|
| 163 |
+
"rank_mean": rank_mean.tolist(),
|
| 164 |
+
"rank_se2": rank_se2.tolist(),
|
| 165 |
+
"err_mean": {p: E[p].mean(0).tolist() for p in pols},
|
| 166 |
+
"err_se2": {p: (2 * E[p].std(0) / np.sqrt(len(good))).tolist() for p in pols},
|
| 167 |
+
"iso": iso,
|
| 168 |
+
"savings_pct": {
|
| 169 |
+
p: 100 * iso[p]["final"] / n_policy_chosen for p in pols
|
| 170 |
+
},
|
| 171 |
+
"n_obs_mean": {
|
| 172 |
+
p: float(np.mean([r["n_obs"] for r in res if r["policy"] == p])) for p in pols
|
| 173 |
+
},
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
if __name__ == "__main__":
|
| 178 |
+
which, paths = sys.argv[1], sys.argv[2:]
|
| 179 |
+
fn = {"claim1": claim1, "claim5": claim5, "claim34": claim34}[which]
|
| 180 |
+
print(json.dumps(fn(paths), indent=2, default=float))
|