A candidate model runs in shadow mode: every live request goes to the production model, whose answer is served, and a copy goes to the candidate, whose answer is only logged. Labels for some requests arrive days later. From the log, summarise how the candidate behaved and decide whether to promote it, hold it for more data, or reject it.
Implement shadow_report(log, labels, rules).
log: a non-empty list of dicts, one per request, with keys request_id, segment (a string), prod_pred, cand_pred, prod_ms, cand_ms. When the candidate failed or timed out, cand_pred and cand_ms are None. Predictions are class labels compared with ==.labels: dict request_id -> true label for the requests whose label has arrived.rules: dict with max_error_rate, max_p95_ratio, min_labeled (an integer of at least 1) and min_gain.A request is scored when cand_pred is not None. Percentiles use the nearest-rank rule: for sorted values , the -th percentile is with (at least 1).
Return a dict with:
n: number of requests; cand_error_rate: share of requests not scored.agreement: share of scored requests where both predictions are equal (None if nothing was scored).disagreement_by_segment: {segment: share of that segment's scored requests where they differ}, keys sorted.prod_p50, prod_p95: over prod_ms of all requests. cand_p50, cand_p95: over cand_ms of scored requests (None if nothing was scored).n_labeled: scored requests that have a label. prod_acc, cand_acc: both models' accuracy on exactly those requests (None when n_labeled is 0).decision, the first rule that applies:
"reject" if nothing was scored, cand_error_rate > max_error_rate, or cand_p95 > max_p95_ratio * prod_p95;"hold" if n_labeled < min_labeled;"reject" if cand_acc < prod_acc;"promote" if cand_acc - prod_acc >= min_gain;"hold" otherwise.Round every float in the output to 4 decimals, and make every decision on the unrounded values.