Sub topic 5
Censoring and Selection Bias in Partially Labeled Data
This chapter assumes you know binary classification metrics, train/validation/test splits, and how a deployed model logs predictions. It also assumes basic pandas and scikit-learn.
A production prediction can have no label yet because the outcome takes time to become observable. This is censoring: the value exists, but your monitoring system cannot observe it within the current reporting period. A fraud label may arrive after an investigation. A churn label may require waiting through the retention window.
The danger is treating the labeled subset as a random sample. It often is not.
How partial labels become biased
Suppose a fraud system labels transactions only after customers dispute them or investigators select them for review. The labeled rows may contain more suspicious transactions than the full population. Their precision will not represent production precision.
Selection can also favor easy cases. For example, automatic checks may label obvious negatives while ambiguous cases remain pending. The observed accuracy then looks better than the eventual accuracy.
| Situation | Likely distortion |
|---|---|
| Investigators review high-risk predictions first | Observed event rate is too high |
| Only completed purchases receive outcomes | Recent or abandoned cases are missing |
| Customers report some outcomes but not others | Labels depend on customer behavior |
| Missing labels are filled with zero | Recall and precision become misleading |
A missing label is not automatically a negative label. It may mean “not observed yet,” “not selected for review,” or “the outcome is impossible to measure.”
A useful data model
Keep these fields separate:
predicted_at: when the model made the prediction.label: the outcome, when known.label_observed: whether the outcome is currently available.maturity_age: how long the prediction has had to receive a label.selection_features: variables that may explain why a label was observed.
The following diagram shows why the labeled path can differ from the full prediction stream.
Correcting selection with inverse probability weighting
If label observation depends on recorded features, estimate each row’s probability of being labeled. This probability is called a propensity. Weight labeled rows by the inverse of their propensity, so frequently observed rows count less and rarely observed rows count more.
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
rng = np.random.default_rng(7)
n = 2000
x = rng.normal(size=n)
score = 1 / (1 + np.exp(-x))
y = rng.binomial(1, 0.2 + 0.6 * score)
prediction = (score >= 0.5).astype(int)
# In this simulation, high-x cases are more likely to be labeled.
p_seen = 0.15 + 0.65 / (1 + np.exp(-2 * x))
seen = rng.random(n) < p_seen
df = pd.DataFrame({
"x": x, "prediction": prediction, "y": y, "seen": seen
})
propensity_model = LogisticRegression().fit(
df[["x"]], df["seen"].astype(int)
)
p_hat = propensity_model.predict_proba(df[["x"]])[:, 1]
p_hat = np.clip(p_hat, 0.05, 0.95)
labeled = df[df["seen"]].copy()
labeled["weight"] = 1 / p_hat[df["seen"]]
labeled["correct"] = (
labeled["prediction"] == labeled["y"]
).astype(float)
observed_accuracy = labeled["correct"].mean()
weighted_accuracy = np.average(
labeled["correct"], weights=labeled["weight"]
)
print(f"Observed accuracy: {observed_accuracy:.3f}")
print(f"Weighted accuracy: {weighted_accuracy:.3f}")This approach relies on an important assumption: after conditioning on the recorded features, label observation is unrelated to the unknown outcome. This is often called missing at random. Include useful predictors of selection, such as risk score, region, product, age of the case, and review queue.
Practical checks
Compare labeled and unlabeled rows before trusting any metric:
- Compare feature distributions and prediction-score distributions.
- Measure the labeling rate by score band, cohort, region, and time.
- Check whether the labeling rate changes as cases mature.
- Compare weighted estimates with later outcomes from fully matured cohorts.
- Cap extreme weights, then report the cap and effective sample size.
Exercise
Why can filling every missing production label with zero make accuracy appear higher than it is?
Show answer
Pending cases may contain positive outcomes or difficult cases. Calling them negative changes the target rather than measuring it, which can inflate accuracy and distort precision, recall, and calibration.