Before a retrained model replaces the one in production, continuous integration (CI) evaluates both on the same data and decides whether the release may go ahead. Write the gate: compare the candidate with the production baseline overall and on each data slice, and return the decision with the reasons.
Implement release_gate(baseline, candidate, required, min_count, tolerance, higher_is_better=True):
baseline: {"overall": float, "slices": {slice_name: float}}, the production model's metric.candidate: {"overall": float, "slices": {slice_name: (value, n)}}, the candidate's metric and n, the number of evaluation examples in that slice.required: slice names that must be in the candidate's evaluation (may repeat).min_count: a slice needs at least this many examples to be judged.tolerance: the largest allowed drop on a slice, in metric units.The drop from an old value to a new one is old - new when higher_is_better, and new - old otherwise. Comparisons allow a slack of 1e-9 for floating-point error.
The release fails for each of these, collected as reasons in this order:
("overall", drop) if the overall drop is greater than 1e-9.("missing", name) if the candidate has no such slice, or ("too_few", name, n) if its n is below min_count.n >= min_count and a baseline value: ("regressed", name, drop) if its drop is greater than tolerance + 1e-9.Drops in reasons are rounded to 4 decimals. Return {"passed": bool, "reasons": [...], "checked": [...], "skipped": [...]}, where checked lists the candidate slices compared in step 3 and skipped the other candidate slices (too few examples or no baseline), both sorted. passed is True exactly when there are no reasons.