Outcome-label sensitivity is the share of reference-positive records flagged by production; specificity is the share of reference-negative records left unflagged.
Estimate outcome-label sensitivity and specificity by group
Keep the reference label on the correct axis
For a deadline-breach flag, a false negative is an adjudicated breach that production missed. A false positive is a production breach flag on an adjudicated nonbreach. Sensitivity divides true production positives by all adjudicated positives. Specificity divides true production negatives by all adjudicated negatives. Precision uses a different denominator and cannot replace either quantity in a rate-correction formula. The validation design supplies paired labels.
Estimate within meaningful groups
If a new routing rule changes logging behavior, the flag may become easier to trigger after launch. Estimate error rates separately for the periods or groups whose breach rates will be contrasted, provided each cell has enough reviewed positives and negatives. Pooling all periods can mask differential error. A group with no adjudicated positive cases has undefined sensitivity, not zero sensitivity. Record the denominator beside every reported proportion.
Respect the validation sampling design
The fixture computes conditional rates from paired records when sampling is uniform within each adjudicated class. A real audit may stratify on the production label instead. Then an unweighted confusion table can distort sensitivity and specificity; use known inclusion probabilities or a sampling-aware model. The code supports inverse-inclusion weighting for each reviewed record. Those weights do not correct adjudication failures or a biased reference rubric.
Do not infer a stable error process from one month
A software deployment may change event timestamps and flag behavior. Compare error rates over time, branch and ticket type before applying a pooled estimate. If validation positives are scarce, uncertainty can be large even when the full production dataset has millions of tickets. Extra unreviewed production records do not identify false negatives. Group-specific correction makes the consequence visible.
Use the rates only for the population they describe
Sensitivity and specificity estimated in a high-volume urban queue may not transfer to low-volume queues with manual overrides. A validation sample must cover the target mix or support a transparent model of error differences. A correction based on unsupported cells is extrapolation. The correction lesson states the algebra and its assumptions.
Implementation
def weighted_label_accuracy(reviewed_records):
cells = {"true_positive": 0.0, "false_negative": 0.0,
"true_negative": 0.0, "false_positive": 0.0}
for production_label, reference_label, inclusion_probability in reviewed_records:
if production_label not in (0, 1) or reference_label not in (0, 1):
raise ValueError("labels must be binary")
if not 0 < inclusion_probability <= 1:
raise ValueError("invalid inclusion probability")
weight = 1 / inclusion_probability
if reference_label == 1:
key = "true_positive" if production_label == 1 else "false_negative"
else:
key = "true_negative" if production_label == 0 else "false_positive"
cells[key] += weight
positives = cells["true_positive"] + cells["false_negative"]
negatives = cells["true_negative"] + cells["false_positive"]
if positives == 0 or negatives == 0:
raise ValueError("both reference classes are needed")
return (cells["true_positive"] / positives,
cells["true_negative"] / negatives)
reviewed = ([(1, 1, .5)] * 18 + [(0, 1, .5)] * 2 +
[(0, 0, .5)] * 37 + [(1, 0, .5)] * 3)
sensitivity, specificity = weighted_label_accuracy(reviewed)
assert abs(sensitivity - .9) < 1e-12
assert abs(specificity - .925) < 1e-12Performance and operating cost
For V reviewed records, weighted cell accumulation is O(V) time and O(1) auxiliary space. Precision of the rates depends on reviewed reference-positive and reference-negative counts, not the size of the unreviewed production table.
Common Mistakes
- Do not substitute precision for sensitivity or specificity.
- Do not assign a numeric sensitivity when a group has no reviewed positive records.
- Do not ignore unequal selection probabilities in a stratified audit.
