5. 拒绝推断#
变量筛选与建模 中的模型只从获批申请人身上学习,但它将对每个申请人做决定,包括供应商评分卡过去会拒绝的那类申请人。本章推断拒绝申请人的结果,并在全部合格申请人上重新拟合模型; 拒绝推断 深入比较了各种方法。
前面各章的代码
>>> import numpy as np
>>> import pandas as pd
>>> from kivi.datasets import make_applications
>>> from kivi.selection import logistic_regression, stepwise
>>> from kivi.spec import fit_spec
>>> applications = make_applications(n_applicants=40000, rng=0)
>>> months = pd.period_range("2024-01", periods=12, freq="M")
>>> applications["month"] = months[np.arange(40000) * 12 // 40000]
>>> eligible = applications[~applications["policy_decline"]]
>>> accepts = eligible[eligible["approved"]]
>>> development = accepts[accepts["month"] <= "2024-09"]
>>> validation = accepts[accepts["month"] >= "2024-10"]
>>> rejects = eligible[~eligible["approved"] & (eligible["month"] <= "2024-09")]
>>> features = ["age", "income", "debt_to_income", "inquiries", "housing", "employment", "incumbent_pd"]
>>> method = {column: "monotonic" for column in ["age", "income", "debt_to_income", "incumbent_pd"]}
>>> method["employment"] = "chimerge"
>>> bins = {"inquiries": [-np.inf, 0, 1, 2, np.inf], "incumbent_pd": 8}
>>> spec = fit_spec(development[features], development["target"], method=method, bins=bins)
>>> selected = stepwise(spec.woe_transform(development), development["target"])
>>> accepts_model = logistic_regression(spec.woe_transform(development)[selected], development["target"])
>>> def predict(model, frame):
... slopes = model["coef"].drop("intercept")
... log_odds = model.loc["intercept", "coef"] + spec.woe_transform(frame)[slopes.index] @ slopes
... return 1 / (1 + np.exp(-log_odds))
为拒绝申请人评分#
仅基于获批申请人的模型给开发月份的拒绝申请人的平均 PD 约为 20%,是获批申请人违约率的两倍:
>>> reject_pd = predict(accepts_model, rejects)
>>> print(f"{reject_pd.mean():.3f}")
0.199
获批申请人无法说明这是否正确。供应商评分卡拒绝这些申请人,部分依据的是模型看不到的征信数据,因此他们很可能比特征相同的获批申请人风险更高。
模拟核对
拒绝申请人的违约率为 29.5%,因此仅基于获批申请人的模型把他们的风险低估了三分之一:
>>> print(f"{rejects['target'].mean():.3f}")
0.295
推断他们的结果#
fuzzy_augmentation() 把每个拒绝申请人拆成一行坏样本和一行好样本,坏样本的权重为其 PD 乘以 factor ,好样本取权重 1 的其余部分。 factor 大于 1 意味着假定拒绝申请人比其 PD 显示的风险更高。 parceling() 按每个评分段的坏账率乘以 factor 把该段的拒绝申请人标为坏, reweighting() 则不增加行,而是给每个评分段的获批申请人权重(获批 + 拒绝)/ 获批。推断出的行与权重为 1 的获批申请人一起加入开发样本:
>>> from kivi.reject_inference import fuzzy_augmentation, parceling, reweighting
>>> def refit(inferred):
... frame = pd.concat([development[features], rejects.loc[inferred.index, features]])
... target = pd.concat([development["target"], inferred["target"]])
... weights = pd.concat([pd.Series(1.0, index=development.index), inferred["weight"]])
... return logistic_regression(spec.woe_transform(frame)[selected], target, weights=weights)
每种方法都给出拒绝申请人的推断坏账率,以及重新拟合后的平均 PD:
>>> accept_pd = predict(accepts_model, development)
>>> candidates = {
... f"fuzzy {factor:g}": fuzzy_augmentation(reject_pd, factor=factor) for factor in [1, 1.5, 2]
... }
>>> candidates["parceling 1.5"] = parceling(
... accept_pd, development["target"], reject_pd, factor=1.5, expected=True
... )
>>> comparison = {
... name: {
... "inferred": np.average(inferred["target"], weights=inferred["weight"]),
... "reject_pd": predict(refit(inferred), rejects).mean(),
... }
... for name, inferred in candidates.items()
... }
>>> weighted = logistic_regression(
... spec.woe_transform(development)[selected],
... development["target"],
... weights=reweighting(accept_pd, reject_pd),
... )
>>> comparison["reweighting"] = {"inferred": np.nan, "reject_pd": predict(weighted, rejects).mean()}
>>> pd.DataFrame(comparison).T.round(3)
inferred reject_pd
fuzzy 1 0.199 0.199
fuzzy 1.5 0.299 0.265
fuzzy 2 0.399 0.333
parceling 1.5 0.297 0.263
reweighting NaN 0.206
factor 为 1 时,模糊扩充推断的正好是仅基于获批申请人的模型的预测,模型保持不变。重新加权无法让拒绝申请人比所在评分段的获批申请人风险更高。 factor 为 1.5 时,模糊扩充和打包法都把推断的坏账率提高到约 30%。
factor 是获批申请人无法验证的假设。项目假定拒绝申请人的违约率是其 PD 的 1.5 倍,即 factor 为 1.5,并选用模糊扩充,因为它使用每个拒绝申请人自己的 PD,而不是其所在评分段。
模拟核对
factor 为 1.5 时,推断的坏账率与拒绝申请人真实的 29.5% 相差不到半个百分点。在真实数据上, factor 只能凭判断确定,像 拒绝推断 中那样做敏感性分析,可以看出结果在多大程度上依赖于它。
重新拟合模型#
最终模型在获批申请人和推断出的拒绝申请人上共同拟合:
>>> model = refit(fuzzy_augmentation(reject_pd, factor=1.5))
>>> model.round(3)
coef se z p_value
variable
intercept -2.106 0.024 -89.555 0.0
age 0.745 0.104 7.176 0.0
income 0.733 0.111 6.602 0.0
debt_to_income 0.870 0.101 8.603 0.0
inquiries 0.940 0.085 11.022 0.0
housing 1.075 0.053 20.333 0.0
employment 0.998 0.138 7.212 0.0
incumbent_pd 1.003 0.051 19.669 0.0
每个系数都变大了:在全部合格申请人中,各变量对好坏的区分比只在获批申请人中更强。截距上升,因为样本现在包含了风险更高的拒绝申请人。不过标准误偏小:推断出的行被当作观测值,尽管它们并没有增加信息。这就是为什么 变量筛选与建模 在获批申请人上而不是在扩充后的样本上筛选变量。