Log pairwise calibration ECE for learned selector
Browse files
workspace/scripts/eval_learned_dominance_selector.py
CHANGED
|
@@ -19,7 +19,7 @@ if str(PROJECT_ROOT) not in sys.path:
|
|
| 19 |
import numpy as np # noqa: E402
|
| 20 |
|
| 21 |
from cil.chart_features import CHART_FEATURE_MODES, OBJECT_LAYOUT_EMBED_DIM, OBSERVATION_EMBED_DIM # noqa: E402
|
| 22 |
-
from cil.metrics import macro_micro_summary # noqa: E402
|
| 23 |
from scripts.eval_dominance_selector import _DominanceScorer, _chart_map, _first_train_seed, _rows # noqa: E402
|
| 24 |
from scripts.eval_ctt_generated_rollout import load_chart_items # noqa: E402
|
| 25 |
|
|
@@ -256,13 +256,28 @@ def main(argv: list[str] | None = None) -> int:
|
|
| 256 |
fit_objective=args.fit_objective,
|
| 257 |
pairwise_weight=args.pairwise_weight,
|
| 258 |
)
|
| 259 |
-
|
| 260 |
-
|
| 261 |
calibration_dataset,
|
| 262 |
best["weights"],
|
| 263 |
best["mean"],
|
| 264 |
best["std"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
tau=best["tau"],
|
|
|
|
|
|
|
| 266 |
)
|
| 267 |
metric_names = sorted(
|
| 268 |
{
|
|
@@ -326,8 +341,12 @@ def main(argv: list[str] | None = None) -> int:
|
|
| 326 |
"num_calibration_candidates": len(calibration_dataset["samples"]),
|
| 327 |
"num_eval_candidates": len(eval_dataset["samples"]),
|
| 328 |
"calibration_model_selection": best["selection"],
|
| 329 |
-
"calibration_summary":
|
| 330 |
-
"eval_summary":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
"summary": summary,
|
| 332 |
"rows": eval_cases,
|
| 333 |
}
|
|
@@ -1079,11 +1098,27 @@ def _evaluate_dataset(
|
|
| 1079 |
std: np.ndarray,
|
| 1080 |
*,
|
| 1081 |
tau: float | dict[str, float],
|
|
|
|
| 1082 |
) -> list[dict[str, Any]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1083 |
x = np.stack([sample["feature"] for sample in dataset["samples"]], axis=0)
|
| 1084 |
x_norm = (x - mean) / std
|
| 1085 |
x_norm[:, 0] = 1.0
|
| 1086 |
-
return
|
| 1087 |
|
| 1088 |
|
| 1089 |
def _evaluate_predictions(
|
|
@@ -1091,8 +1126,13 @@ def _evaluate_predictions(
|
|
| 1091 |
predictions: np.ndarray,
|
| 1092 |
*,
|
| 1093 |
tau: float | dict[str, float],
|
|
|
|
|
|
|
| 1094 |
) -> list[dict[str, Any]]:
|
| 1095 |
samples = dataset["samples"]
|
|
|
|
|
|
|
|
|
|
| 1096 |
rows: list[dict[str, Any]] = []
|
| 1097 |
for row_index, sample_indices in sorted(dataset["by_row"].items()):
|
| 1098 |
best_index = max(sample_indices, key=lambda index: float(predictions[index]))
|
|
@@ -1108,40 +1148,138 @@ def _evaluate_predictions(
|
|
| 1108 |
)
|
| 1109 |
hidden_utility = float(sample["hidden_chart_oracle_utility"])
|
| 1110 |
hidden_success = float(sample["hidden_chart_oracle_success"])
|
| 1111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1112 |
{
|
| 1113 |
-
"
|
| 1114 |
-
"
|
| 1115 |
-
"
|
| 1116 |
-
"
|
| 1117 |
-
"
|
| 1118 |
-
"
|
| 1119 |
-
"tau": row_tau,
|
| 1120 |
-
"execute_generated": float(execute),
|
| 1121 |
-
"coverage": float(execute),
|
| 1122 |
-
"fallback_rate": float(not execute),
|
| 1123 |
-
"base_utility": float(sample["base_utility"]),
|
| 1124 |
-
"base_success": float(sample["base_success"]),
|
| 1125 |
-
"selected_utility": selected_utility,
|
| 1126 |
-
"selected_success": selected_success,
|
| 1127 |
-
"selected_utility_gain_over_base": selected_utility - float(sample["base_utility"]),
|
| 1128 |
-
"selected_success_gain_over_base": selected_success - float(sample["base_success"]),
|
| 1129 |
-
"proposal_oracle_utility": float(sample["proposal_oracle_utility"]),
|
| 1130 |
-
"proposal_oracle_success": float(sample["proposal_oracle_success"]),
|
| 1131 |
-
"hidden_chart_oracle_utility": hidden_utility,
|
| 1132 |
-
"hidden_chart_oracle_success": hidden_success,
|
| 1133 |
-
"outcome_ptr": float(sample["outcome_ptr"]),
|
| 1134 |
-
"selector_regret": max(0.0, float(sample["proposal_oracle_utility"]) - selected_utility),
|
| 1135 |
-
"success_selector_gap": max(0.0, float(sample["proposal_oracle_success"]) - selected_success),
|
| 1136 |
-
"support_gap": max(0.0, hidden_utility - float(sample["proposal_oracle_utility"]))
|
| 1137 |
-
if math.isfinite(hidden_utility)
|
| 1138 |
-
else math.nan,
|
| 1139 |
-
"success_support_gap": max(0.0, hidden_success - float(sample["proposal_oracle_success"]))
|
| 1140 |
-
if math.isfinite(hidden_success)
|
| 1141 |
-
else math.nan,
|
| 1142 |
}
|
| 1143 |
)
|
| 1144 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1145 |
|
| 1146 |
|
| 1147 |
def _tau_for_sample(sample: dict[str, Any], tau: float | dict[str, float]) -> float:
|
|
@@ -1244,6 +1382,10 @@ def _mean(values: list[Any]) -> float | None:
|
|
| 1244 |
return sum(clean) / len(clean) if clean else None
|
| 1245 |
|
| 1246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1247 |
def _source_rank(value: Any) -> float:
|
| 1248 |
match = re.search(r"rank(\d+)", str(value))
|
| 1249 |
return float(match.group(1)) if match else 0.0
|
|
@@ -1253,15 +1395,16 @@ def _table(metrics: dict[str, Any]) -> str:
|
|
| 1253 |
summary = metrics["eval_summary"]
|
| 1254 |
lines = [
|
| 1255 |
"% Auto-generated by scripts/eval_learned_dominance_selector.py",
|
| 1256 |
-
"\\begin{tabular}{
|
| 1257 |
"\\toprule",
|
| 1258 |
-
"Rows & Coverage & Fallback & Base succ. & Selected succ. & Oracle succ. & OutcomePTR & Succ. support gap & Succ. selector gap \\\\",
|
| 1259 |
"\\midrule",
|
| 1260 |
f"{metrics['num_eval_rows']} & {_fmt(summary.get('coverage'))} & "
|
| 1261 |
f"{_fmt(summary.get('fallback_rate'))} & {_fmt(summary.get('base_success'))} & "
|
| 1262 |
f"{_fmt(summary.get('selected_success'))} & {_fmt(summary.get('proposal_oracle_success'))} & "
|
| 1263 |
f"{_fmt(summary.get('outcome_ptr'))} & {_fmt(summary.get('success_support_gap'))} & "
|
| 1264 |
-
f"{_fmt(summary.get('success_selector_gap'))}
|
|
|
|
| 1265 |
"\\bottomrule",
|
| 1266 |
"\\end{tabular}",
|
| 1267 |
]
|
|
@@ -1285,16 +1428,18 @@ def _report(metrics: dict[str, Any]) -> str:
|
|
| 1285 |
"",
|
| 1286 |
"The ridge calibrator and threshold are fit on calibration measured rows only. Eval outcomes are used only for reporting.",
|
| 1287 |
"",
|
| 1288 |
-
"| Split | Coverage | Fallback | Base success | Selected success | Proposal oracle | OutcomePTR | Success support gap | Success selector gap |",
|
| 1289 |
-
"| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |",
|
| 1290 |
f"| calibration | {_fmt(calibration.get('coverage'))} | {_fmt(calibration.get('fallback_rate'))} | "
|
| 1291 |
f"{_fmt(calibration.get('base_success'))} | {_fmt(calibration.get('selected_success'))} | "
|
| 1292 |
f"{_fmt(calibration.get('proposal_oracle_success'))} | {_fmt(calibration.get('outcome_ptr'))} | "
|
| 1293 |
-
f"{_fmt(calibration.get('success_support_gap'))} | {_fmt(calibration.get('success_selector_gap'))} |"
|
|
|
|
| 1294 |
f"| eval | {_fmt(summary.get('coverage'))} | {_fmt(summary.get('fallback_rate'))} | "
|
| 1295 |
f"{_fmt(summary.get('base_success'))} | {_fmt(summary.get('selected_success'))} | "
|
| 1296 |
f"{_fmt(summary.get('proposal_oracle_success'))} | {_fmt(summary.get('outcome_ptr'))} | "
|
| 1297 |
-
f"{_fmt(summary.get('success_support_gap'))} | {_fmt(summary.get('success_selector_gap'))} |"
|
|
|
|
| 1298 |
"",
|
| 1299 |
"This is a selector diagnostic over already measured candidates, not a new rollout.",
|
| 1300 |
]
|
|
|
|
| 19 |
import numpy as np # noqa: E402
|
| 20 |
|
| 21 |
from cil.chart_features import CHART_FEATURE_MODES, OBJECT_LAYOUT_EMBED_DIM, OBSERVATION_EMBED_DIM # noqa: E402
|
| 22 |
+
from cil.metrics import macro_micro_summary, pairwise_causal_dominance_ece # noqa: E402
|
| 23 |
from scripts.eval_dominance_selector import _DominanceScorer, _chart_map, _first_train_seed, _rows # noqa: E402
|
| 24 |
from scripts.eval_ctt_generated_rollout import load_chart_items # noqa: E402
|
| 25 |
|
|
|
|
| 256 |
fit_objective=args.fit_objective,
|
| 257 |
pairwise_weight=args.pairwise_weight,
|
| 258 |
)
|
| 259 |
+
eval_predictions = _linear_predictions(eval_dataset, best["weights"], best["mean"], best["std"])
|
| 260 |
+
calibration_predictions = _linear_predictions(
|
| 261 |
calibration_dataset,
|
| 262 |
best["weights"],
|
| 263 |
best["mean"],
|
| 264 |
best["std"],
|
| 265 |
+
)
|
| 266 |
+
eval_pairwise = _pairwise_calibration_summary(eval_dataset, eval_predictions)
|
| 267 |
+
calibration_pairwise = _pairwise_calibration_summary(calibration_dataset, calibration_predictions)
|
| 268 |
+
eval_cases = _evaluate_predictions(
|
| 269 |
+
eval_dataset,
|
| 270 |
+
eval_predictions,
|
| 271 |
+
tau=best["tau"],
|
| 272 |
+
include_pairwise_calibration=True,
|
| 273 |
+
pairwise_calibration=eval_pairwise,
|
| 274 |
+
)
|
| 275 |
+
calibration_cases = _evaluate_predictions(
|
| 276 |
+
calibration_dataset,
|
| 277 |
+
calibration_predictions,
|
| 278 |
tau=best["tau"],
|
| 279 |
+
include_pairwise_calibration=True,
|
| 280 |
+
pairwise_calibration=calibration_pairwise,
|
| 281 |
)
|
| 282 |
metric_names = sorted(
|
| 283 |
{
|
|
|
|
| 341 |
"num_calibration_candidates": len(calibration_dataset["samples"]),
|
| 342 |
"num_eval_candidates": len(eval_dataset["samples"]),
|
| 343 |
"calibration_model_selection": best["selection"],
|
| 344 |
+
"calibration_summary": _summary_with_pairwise(calibration_cases, calibration_pairwise),
|
| 345 |
+
"eval_summary": _summary_with_pairwise(eval_cases, eval_pairwise),
|
| 346 |
+
"pairwise_causal_calibration": {
|
| 347 |
+
"calibration": _pairwise_calibration_global(calibration_pairwise),
|
| 348 |
+
"eval": _pairwise_calibration_global(eval_pairwise),
|
| 349 |
+
},
|
| 350 |
"summary": summary,
|
| 351 |
"rows": eval_cases,
|
| 352 |
}
|
|
|
|
| 1098 |
std: np.ndarray,
|
| 1099 |
*,
|
| 1100 |
tau: float | dict[str, float],
|
| 1101 |
+
include_pairwise_calibration: bool = False,
|
| 1102 |
) -> list[dict[str, Any]]:
|
| 1103 |
+
predictions = _linear_predictions(dataset, weights, mean, std)
|
| 1104 |
+
return _evaluate_predictions(
|
| 1105 |
+
dataset,
|
| 1106 |
+
predictions,
|
| 1107 |
+
tau=tau,
|
| 1108 |
+
include_pairwise_calibration=include_pairwise_calibration,
|
| 1109 |
+
)
|
| 1110 |
+
|
| 1111 |
+
|
| 1112 |
+
def _linear_predictions(
|
| 1113 |
+
dataset: dict[str, Any],
|
| 1114 |
+
weights: np.ndarray,
|
| 1115 |
+
mean: np.ndarray,
|
| 1116 |
+
std: np.ndarray,
|
| 1117 |
+
) -> np.ndarray:
|
| 1118 |
x = np.stack([sample["feature"] for sample in dataset["samples"]], axis=0)
|
| 1119 |
x_norm = (x - mean) / std
|
| 1120 |
x_norm[:, 0] = 1.0
|
| 1121 |
+
return x_norm @ weights
|
| 1122 |
|
| 1123 |
|
| 1124 |
def _evaluate_predictions(
|
|
|
|
| 1126 |
predictions: np.ndarray,
|
| 1127 |
*,
|
| 1128 |
tau: float | dict[str, float],
|
| 1129 |
+
include_pairwise_calibration: bool = False,
|
| 1130 |
+
pairwise_calibration: dict[str, Any] | None = None,
|
| 1131 |
) -> list[dict[str, Any]]:
|
| 1132 |
samples = dataset["samples"]
|
| 1133 |
+
if include_pairwise_calibration and pairwise_calibration is None:
|
| 1134 |
+
pairwise_calibration = _pairwise_calibration_summary(dataset, predictions)
|
| 1135 |
+
pairwise_rows = (pairwise_calibration or {}).get("rows", {})
|
| 1136 |
rows: list[dict[str, Any]] = []
|
| 1137 |
for row_index, sample_indices in sorted(dataset["by_row"].items()):
|
| 1138 |
best_index = max(sample_indices, key=lambda index: float(predictions[index]))
|
|
|
|
| 1148 |
)
|
| 1149 |
hidden_utility = float(sample["hidden_chart_oracle_utility"])
|
| 1150 |
hidden_success = float(sample["hidden_chart_oracle_success"])
|
| 1151 |
+
row = {
|
| 1152 |
+
"chart_id": sample["chart_id"],
|
| 1153 |
+
"task_id": sample["task_id"],
|
| 1154 |
+
"seed": sample["seed"],
|
| 1155 |
+
"train_seed": sample["train_seed"],
|
| 1156 |
+
"selected_candidate_index": int(sample["candidate_index"]),
|
| 1157 |
+
"predicted_margin": predicted_margin,
|
| 1158 |
+
"tau": row_tau,
|
| 1159 |
+
"execute_generated": float(execute),
|
| 1160 |
+
"coverage": float(execute),
|
| 1161 |
+
"fallback_rate": float(not execute),
|
| 1162 |
+
"base_utility": float(sample["base_utility"]),
|
| 1163 |
+
"base_success": float(sample["base_success"]),
|
| 1164 |
+
"selected_utility": selected_utility,
|
| 1165 |
+
"selected_success": selected_success,
|
| 1166 |
+
"selected_utility_gain_over_base": selected_utility - float(sample["base_utility"]),
|
| 1167 |
+
"selected_success_gain_over_base": selected_success - float(sample["base_success"]),
|
| 1168 |
+
"proposal_oracle_utility": float(sample["proposal_oracle_utility"]),
|
| 1169 |
+
"proposal_oracle_success": float(sample["proposal_oracle_success"]),
|
| 1170 |
+
"hidden_chart_oracle_utility": hidden_utility,
|
| 1171 |
+
"hidden_chart_oracle_success": hidden_success,
|
| 1172 |
+
"outcome_ptr": float(sample["outcome_ptr"]),
|
| 1173 |
+
"selector_regret": max(0.0, float(sample["proposal_oracle_utility"]) - selected_utility),
|
| 1174 |
+
"success_selector_gap": max(0.0, float(sample["proposal_oracle_success"]) - selected_success),
|
| 1175 |
+
"support_gap": max(0.0, hidden_utility - float(sample["proposal_oracle_utility"]))
|
| 1176 |
+
if math.isfinite(hidden_utility)
|
| 1177 |
+
else math.nan,
|
| 1178 |
+
"success_support_gap": max(0.0, hidden_success - float(sample["proposal_oracle_success"]))
|
| 1179 |
+
if math.isfinite(hidden_success)
|
| 1180 |
+
else math.nan,
|
| 1181 |
+
}
|
| 1182 |
+
if include_pairwise_calibration:
|
| 1183 |
+
calibration = pairwise_rows.get(row_index) or pairwise_rows.get(str(row_index), {})
|
| 1184 |
+
row.update(_pairwise_calibration_scalars(calibration))
|
| 1185 |
+
rows.append(row)
|
| 1186 |
+
return rows
|
| 1187 |
+
|
| 1188 |
+
|
| 1189 |
+
def _pairwise_calibration_summary(
|
| 1190 |
+
dataset: dict[str, Any],
|
| 1191 |
+
predictions: np.ndarray,
|
| 1192 |
+
*,
|
| 1193 |
+
n_bins: int = 10,
|
| 1194 |
+
) -> dict[str, Any]:
|
| 1195 |
+
if len(predictions) != len(dataset["samples"]):
|
| 1196 |
+
raise ValueError("predictions must align with dataset samples")
|
| 1197 |
+
bins = [
|
| 1198 |
+
{
|
| 1199 |
+
"count": 0,
|
| 1200 |
+
"accuracy_sum": 0.0,
|
| 1201 |
+
"confidence_sum": 0.0,
|
| 1202 |
+
"lower": index / n_bins,
|
| 1203 |
+
"upper": (index + 1) / n_bins,
|
| 1204 |
+
}
|
| 1205 |
+
for index in range(n_bins)
|
| 1206 |
+
]
|
| 1207 |
+
by_row: dict[int, dict[str, Any]] = {}
|
| 1208 |
+
total_pairs = 0
|
| 1209 |
+
correct_sum = 0.0
|
| 1210 |
+
confidence_sum = 0.0
|
| 1211 |
+
samples = dataset["samples"]
|
| 1212 |
+
for row_index, sample_indices in sorted(dataset["by_row"].items()):
|
| 1213 |
+
row_scores = [float(predictions[index]) for index in sample_indices]
|
| 1214 |
+
row_utilities = [float(samples[index]["candidate_utility"]) for index in sample_indices]
|
| 1215 |
+
row_metrics = pairwise_causal_dominance_ece(row_scores, row_utilities, n_bins=n_bins)
|
| 1216 |
+
by_row[int(row_index)] = row_metrics
|
| 1217 |
+
row_pairs = int(row_metrics.get("num_pairs") or 0)
|
| 1218 |
+
if row_pairs <= 0:
|
| 1219 |
+
continue
|
| 1220 |
+
total_pairs += row_pairs
|
| 1221 |
+
correct_sum += float(row_metrics.get("accuracy") or 0.0) * row_pairs
|
| 1222 |
+
confidence_sum += float(row_metrics.get("mean_confidence") or 0.0) * row_pairs
|
| 1223 |
+
for index, row_bin in enumerate(row_metrics.get("bins", [])):
|
| 1224 |
+
if index >= len(bins):
|
| 1225 |
+
break
|
| 1226 |
+
count = int(row_bin.get("count") or 0)
|
| 1227 |
+
bins[index]["count"] += count
|
| 1228 |
+
bins[index]["accuracy_sum"] += float(row_bin.get("accuracy") or 0.0) * count
|
| 1229 |
+
bins[index]["confidence_sum"] += float(row_bin.get("confidence") or 0.0) * count
|
| 1230 |
+
|
| 1231 |
+
ece = 0.0
|
| 1232 |
+
rendered_bins: list[dict[str, float | int]] = []
|
| 1233 |
+
for bucket in bins:
|
| 1234 |
+
count = int(bucket["count"])
|
| 1235 |
+
accuracy = bucket["accuracy_sum"] / count if count else 0.0
|
| 1236 |
+
confidence = bucket["confidence_sum"] / count if count else 0.0
|
| 1237 |
+
if total_pairs:
|
| 1238 |
+
ece += (count / total_pairs) * abs(accuracy - confidence)
|
| 1239 |
+
rendered_bins.append(
|
| 1240 |
{
|
| 1241 |
+
"lower": float(bucket["lower"]),
|
| 1242 |
+
"upper": float(bucket["upper"]),
|
| 1243 |
+
"count": count,
|
| 1244 |
+
"accuracy": accuracy,
|
| 1245 |
+
"confidence": confidence,
|
| 1246 |
+
"abs_gap": abs(accuracy - confidence),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1247 |
}
|
| 1248 |
)
|
| 1249 |
+
return {
|
| 1250 |
+
"n_bins": int(n_bins),
|
| 1251 |
+
"num_rows": len(dataset["by_row"]),
|
| 1252 |
+
"ece": ece if total_pairs else math.nan,
|
| 1253 |
+
"num_pairs": int(total_pairs),
|
| 1254 |
+
"accuracy": correct_sum / total_pairs if total_pairs else math.nan,
|
| 1255 |
+
"mean_confidence": confidence_sum / total_pairs if total_pairs else math.nan,
|
| 1256 |
+
"bins": rendered_bins,
|
| 1257 |
+
"rows": by_row,
|
| 1258 |
+
}
|
| 1259 |
+
|
| 1260 |
+
|
| 1261 |
+
def _pairwise_calibration_scalars(calibration: dict[str, Any]) -> dict[str, float]:
|
| 1262 |
+
return {
|
| 1263 |
+
"pairwise_causal_calibration_ece": _finite_or_nan(calibration.get("ece")),
|
| 1264 |
+
"pairwise_causal_calibration_pairs": float(calibration.get("num_pairs") or 0),
|
| 1265 |
+
"pairwise_causal_calibration_accuracy": _finite_or_nan(calibration.get("accuracy")),
|
| 1266 |
+
"pairwise_causal_calibration_confidence": _finite_or_nan(
|
| 1267 |
+
calibration.get("mean_confidence")
|
| 1268 |
+
),
|
| 1269 |
+
}
|
| 1270 |
+
|
| 1271 |
+
|
| 1272 |
+
def _pairwise_calibration_global(calibration: dict[str, Any]) -> dict[str, Any]:
|
| 1273 |
+
return {key: value for key, value in calibration.items() if key != "rows"}
|
| 1274 |
+
|
| 1275 |
+
|
| 1276 |
+
def _summary_with_pairwise(
|
| 1277 |
+
rows: list[dict[str, Any]],
|
| 1278 |
+
pairwise_calibration: dict[str, Any],
|
| 1279 |
+
) -> dict[str, float | None]:
|
| 1280 |
+
summary = _simple_summary(rows)
|
| 1281 |
+
summary.update(_pairwise_calibration_scalars(pairwise_calibration))
|
| 1282 |
+
return summary
|
| 1283 |
|
| 1284 |
|
| 1285 |
def _tau_for_sample(sample: dict[str, Any], tau: float | dict[str, float]) -> float:
|
|
|
|
| 1382 |
return sum(clean) / len(clean) if clean else None
|
| 1383 |
|
| 1384 |
|
| 1385 |
+
def _finite_or_nan(value: Any) -> float:
|
| 1386 |
+
return float(value) if isinstance(value, (int, float)) and math.isfinite(float(value)) else math.nan
|
| 1387 |
+
|
| 1388 |
+
|
| 1389 |
def _source_rank(value: Any) -> float:
|
| 1390 |
match = re.search(r"rank(\d+)", str(value))
|
| 1391 |
return float(match.group(1)) if match else 0.0
|
|
|
|
| 1395 |
summary = metrics["eval_summary"]
|
| 1396 |
lines = [
|
| 1397 |
"% Auto-generated by scripts/eval_learned_dominance_selector.py",
|
| 1398 |
+
"\\begin{tabular}{lrrrrrrrrr}",
|
| 1399 |
"\\toprule",
|
| 1400 |
+
"Rows & Coverage & Fallback & Base succ. & Selected succ. & Oracle succ. & OutcomePTR & Succ. support gap & Succ. selector gap & Cal. ECE \\\\",
|
| 1401 |
"\\midrule",
|
| 1402 |
f"{metrics['num_eval_rows']} & {_fmt(summary.get('coverage'))} & "
|
| 1403 |
f"{_fmt(summary.get('fallback_rate'))} & {_fmt(summary.get('base_success'))} & "
|
| 1404 |
f"{_fmt(summary.get('selected_success'))} & {_fmt(summary.get('proposal_oracle_success'))} & "
|
| 1405 |
f"{_fmt(summary.get('outcome_ptr'))} & {_fmt(summary.get('success_support_gap'))} & "
|
| 1406 |
+
f"{_fmt(summary.get('success_selector_gap'))} & "
|
| 1407 |
+
f"{_fmt(summary.get('pairwise_causal_calibration_ece'))} \\\\",
|
| 1408 |
"\\bottomrule",
|
| 1409 |
"\\end{tabular}",
|
| 1410 |
]
|
|
|
|
| 1428 |
"",
|
| 1429 |
"The ridge calibrator and threshold are fit on calibration measured rows only. Eval outcomes are used only for reporting.",
|
| 1430 |
"",
|
| 1431 |
+
"| Split | Coverage | Fallback | Base success | Selected success | Proposal oracle | OutcomePTR | Success support gap | Success selector gap | Calibration ECE |",
|
| 1432 |
+
"| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |",
|
| 1433 |
f"| calibration | {_fmt(calibration.get('coverage'))} | {_fmt(calibration.get('fallback_rate'))} | "
|
| 1434 |
f"{_fmt(calibration.get('base_success'))} | {_fmt(calibration.get('selected_success'))} | "
|
| 1435 |
f"{_fmt(calibration.get('proposal_oracle_success'))} | {_fmt(calibration.get('outcome_ptr'))} | "
|
| 1436 |
+
f"{_fmt(calibration.get('success_support_gap'))} | {_fmt(calibration.get('success_selector_gap'))} | "
|
| 1437 |
+
f"{_fmt(calibration.get('pairwise_causal_calibration_ece'))} |",
|
| 1438 |
f"| eval | {_fmt(summary.get('coverage'))} | {_fmt(summary.get('fallback_rate'))} | "
|
| 1439 |
f"{_fmt(summary.get('base_success'))} | {_fmt(summary.get('selected_success'))} | "
|
| 1440 |
f"{_fmt(summary.get('proposal_oracle_success'))} | {_fmt(summary.get('outcome_ptr'))} | "
|
| 1441 |
+
f"{_fmt(summary.get('success_support_gap'))} | {_fmt(summary.get('success_selector_gap'))} | "
|
| 1442 |
+
f"{_fmt(summary.get('pairwise_causal_calibration_ece'))} |",
|
| 1443 |
"",
|
| 1444 |
"This is a selector diagnostic over already measured candidates, not a new rollout.",
|
| 1445 |
]
|