How to weight for common support¶
Use overlap weighting when you want to check whether a score difference remains among applicant profiles found in both groups. It gives these shared profiles more influence, but it cannot create overlap where none exists.
Reuse X_source, X_target, source_risk, and target_risk from Monitor model scores.
source is the reference group. target is the group under review. A group probability P(target|x) estimates how likely a profile is to come from target, based on its features. Use these probabilities to build overlap weights; keep them separate from the values being tested.
1. Estimate which group each applicant resembles¶
The domain classifier estimates each applicant's probability of belonging to target. This is not the risk score being tested. Estimate each probability out of fold, so the applicant was not used to fit the classifier that predicted it.
Fit and calibrate a classifier to distinguish the groups. Leave out ExternalRiskEstimate, which defined the split.
from sklearn.calibration import CalibratedClassifierCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_predict
labels = np.repeat([0, 1], [len(X_source), len(X_target)])
X_concat = np.vstack([X_source, X_target])
domain_probabilities = {}
for label, X_domain in [
("all features", X_concat),
("excluding split feature", np.delete(X_concat, col_split, axis=1)),
]:
rf_domain = CalibratedClassifierCV(
estimator=RandomForestClassifier(
n_estimators=500,
random_state=12345,
min_samples_leaf=10,
),
method="sigmoid",
cv=5,
)
domain_probabilities[label] = cross_val_predict(
rf_domain,
X_domain,
labels,
cv=5,
method="predict_proba",
)[:, 1]
Use the group probabilities to calculate weights, not as the scores being tested.
2. Build weights and check how much information remains¶
Pass the out-of-fold probabilities to .overlap() with shrinkage=0.5. Check the effective sample size (ESS) for each group before reading the p-value.
import samesame as ss
weight_sets = {}
two = ss.Dsos(source_risk, target_risk)
for label, domain_prob in domain_probabilities.items():
source_prob = domain_prob[labels == 0]
target_prob = domain_prob[labels == 1]
weighted = two.overlap(
source_prob,
target_prob,
shrinkage=0.5,
)
weight_sets[label] = {"Common-support": weighted}
print(f"\n{label}")
ess_source, ess_target = weighted.ess or (float("nan"), float("nan"))
print(
f"Common-support: ESS source {ess_source:.0f}/{len(source_prob)}, "
f"target {ess_target:.0f}/{len(target_prob)}"
)
all features
Common-support: ESS source 2587/7683, target 22/2188
excluding split feature
Common-support: ESS source 2990/7683, target 856/2188
With all features, the target ESS is only 22 out of 2,188. Do not rely on that result: it has very low precision. Excluding the split feature retains 856 effective observations in the target group.
3. Test for the higher-risk tail after weighting¶
import numpy as np
import samesame as ss
test = ss.Dsos(source_risk, target_risk)
test = test.set_permutation(seed=np.random.default_rng(12345))
result = test()
print(f"Unweighted p-value: {result.pvalue:.4f}")
for label, methods in weight_sets.items():
print(f"\n{label}")
for method, weighted in methods.items():
weighted = weighted.set_permutation(seed=np.random.default_rng(12345))
result = weighted()
print(f"{method}: p-value {result.pvalue:.4f}")
Unweighted p-value: 0.0001
all features
Common-support: p-value 0.0020
excluding split feature
Common-support: p-value 0.0001
After excluding the split feature, the test still finds evidence that high-risk scores stand out in target among shared profiles (p = 0.0001). This compares score tails among overlapping profiles; it does not compare average risk or prove worse outcomes. See API: Weighting for the exact weighting behavior and ESS formula.