Tutorial: Compare models¶
Suppose you want to see how a full model compares with a simple feature score. This example uses the same credit-risk data as Monitor model scores.
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 the same split as the monitoring tutorial: applications above a risk-estimate value of 63 are the reference group, and the remaining applications are under review.
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/compare_scorers_heloc.py
The split gives 7,683 reference applications and 2,188 applications under review.
2. Prepare scores for the same cases¶
Train the full model using only the reference group, then score the group under review. Compare those predictions with the reversed split feature on the same cases with known outcomes; inputs are matched by row position.
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()
ext_target = X_target[:, col_split].astype(float)
keep = ~np.isnan(ext_target)
y_ext = y_target[keep]
risk_ext = target_risk[keep]
ref_ext = -ext_target[keep]
3. Compare the full model with the feature-only baseline¶
Question: Does the full model rank cases with the outcome better than the reversed split-feature score?
Candidate: full model risk predictions.
Reference: the reversed ExternalRiskEstimate score, not a fitted model.
import numpy as np
import samesame as ss
test = ss.DeLong(y_ext, risk_ext, ref_ext)
win_auc = test(alternative="greater")
DeLong compares the scores' overall ranking of cases with and without the outcome. By default, it uses an asymptotic test. Set alternative="greater" to test whether the candidate ranks better, "less" if it ranks worse, or "two-sided" for either direction. To use permutations, construct the test with inference="permutation" and configure it with .set_permutation(...).
test = ss.Venkatraman(y_ext, risk_ext, ref_ext)
test = test.set_permutation(seed=np.random.default_rng(12345))
win_shape = test()
On these cases, the full model has a higher AUROC than the feature-only baseline (0.65 versus 0.54; DeLong, p < 0.0001). The Venkatraman test also finds that their curves differ (p = 0.0002), but does not say which score is better at every cutoff.
Use Paired AUROC versus shape to choose between the ranking and curve comparisons.