Tutorial: Monitor model scores¶
Imagine a lender's model starts seeing applicants from a new channel. Their predicted risk is higher. That could mean the applicants are riskier, the model's predictions are less accurate, or both. We check predicted risk and prediction error separately so a change in one is not mistaken for a change in the other.
1. Load data and create the split¶
The tutorial uses HELOC applications from the FICO Challenge. The source is OpenML 45554. The set holds 9,871 rows. The label marks 90+ days past due. Load with sklearn.datasets.fetch_openml(data_id=45554, as_frame=False, parser="liac-arff"). You need scikit-learn >= 1.4, numpy, scipy, and samesame. The split uses 63 on ExternalRiskEstimate. source holds 7,683 applications above 63 with 43.5% bad rate. target holds 2,188 applications at or below 63 with 81.9% bad rate. Expect these counts after the split. Models use 500 trees with random_state=12345 and min_samples_leaf=10. Reports use seed=np.random.default_rng(12345).
The example uses a public credit-risk dataset. It divides applications at a risk-estimate value of 63: higher values form the reference group (source), and lower values form the group under review (target).
import numpy as np
from sklearn.datasets import fetch_openml
fico = fetch_openml(data_id=45554, as_frame=False, parser="liac-arff")
X, y = fico.data, (fico.target == "Bad").astype(int)
col_split = fico.feature_names.index("ExternalRiskEstimate")
mask_high = X[:, col_split] > 63
X_source, y_source = X[mask_high], y[mask_high]
X_target, y_target = X[~mask_high], y[~mask_high]
Run the full script. The first run downloads the dataset; later runs use the cached copy.
uv run python docs/code/credit/common_support_heloc.py
The split gives 7,683 reference applications and 2,188 applications under review.
2. Produce out-of-sample risk scores¶
Fit the model on the reference group. Use out-of-bag predictions for its cases and the fitted model to score the group under review.
import numpy as np
import samesame as ss
from sklearn.ensemble import RandomForestClassifier
rf_bad = RandomForestClassifier(
n_estimators=500,
oob_score=True,
random_state=12345,
min_samples_leaf=10,
)
rf_bad.fit(X_source, y_source)
source_risk = rf_bad.oob_decision_function_[:, 1].ravel()
target_risk = rf_bad.predict_proba(X_target)[:, 1].ravel()
Neither group is scored using predictions from a model trained on those same cases. This matters because scores from training cases can look too good. Check each output against the question it can answer.
3. Test scores¶
Each output is a separate warning sign. Check them one at a time, and decide what would count as worse before running a test.
Question: Do the new applications have higher predicted risk? Higher risk is the concern here, so use the values as-is.
test = ss.Dsos(source_risk, target_risk)
test = test.set_permutation(seed=np.random.default_rng(12345))
harm = test()
The sample mean predicted risk rises from 44% in the reference group to 73% in the group under review. This is a risk warning, not proof that the model is less accurate.
Question: Is the model making larger errors? Higher error is the concern, so use the values as-is.
The Brier score is the squared difference between a predicted probability and the known outcome. Larger values mean a larger prediction error.
brier_source = (y_source - source_risk) ** 2
brier_target = (y_target - target_risk) ** 2
test = ss.Dsos(brier_source, brier_target)
test = test.set_permutation(seed=np.random.default_rng(12345))
harm = test()
This test finds no evidence of higher error in this split. Predicted risk rises without a matching rise in prediction error.
Predicted risk is higher in the new group, but the error check does not show the same warning. Higher risk alone does not show that the model has become less accurate.
The key point: predicted risk and prediction error answer different questions. This example found higher risk estimates, but no matching rise in prediction errors.
Next, compare models or weight for common support when the groups have different mixes of applicants.