6. 评分卡#

评分卡给每个变量的每个组一个分值,使申请人的评分成为任何人都能复核的加总。本章把 拒绝推断 中的模型转换为分值,为验证样本评分,并给出低分的原因。

前面各章的代码
>>> import numpy as np
>>> import pandas as pd
>>> from kivi.datasets import make_applications
>>> from kivi.reject_inference import fuzzy_augmentation
>>> 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))
>>> inferred = fuzzy_augmentation(predict(accepts_model, rejects), factor=1.5)
>>> 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"]])
>>> model = logistic_regression(spec.woe_transform(frame)[selected], target, weights=weights)

选择刻度#

评分与好坏比的对数成线性关系。贷款机构沿用其他评分卡的刻度:600 分对应 50 比 1 的好坏比,每增加 20 分好坏比翻一倍。 score_scaling() 给出该刻度的因子和偏移量;见 评分卡 :

>>> from kivi.scorecard import points_table, reason_codes, score_scaling, score_to_pd, woe_to_points
>>> scale = dict(pdo=20, base_score=600, base_odds=50)
>>> factor, offset = score_scaling(**scale)
>>> print(f"factor = {factor:.2f}, offset = {offset:.2f}")
factor = 28.85, offset = 487.12

构建分值表#

points_table() 把 WOE 表和系数转换为分值。WOE 越高风险越大,分值也就越低:

>>> coefficients = model["coef"].drop("intercept")
>>> intercept = model.loc["intercept", "coef"]
>>> card = points_table({column: spec.tables[column] for column in selected}, coefficients, intercept, **scale)
>>> card.loc[["housing", "employment", "incumbent_pd"]].round({"woe": 3, "points": 1})
                                   bin    woe  points
variable
housing                            own -0.472    92.9
housing                       mortgage -0.174    83.7
housing                           rent  0.341    67.7
employment                   permanent -0.081    80.6
employment    self_employed, temporary  0.186    72.9
incumbent_pd            (-inf, 0.0304] -1.623   125.2
incumbent_pd          (0.0304, 0.0377] -1.136   111.1
incumbent_pd          (0.0377, 0.0541] -0.692    98.3
incumbent_pd          (0.0541, 0.0793] -0.243    85.3
incumbent_pd          (0.0793, 0.0961]  0.037    77.2
incumbent_pd           (0.0961, 0.109]  0.199    72.5
incumbent_pd            (0.109, 0.123]  0.490    64.1
incumbent_pd              (0.123, inf]  0.660    59.2

在同一变量内,两个组的分值之差等于因子乘以系数再乘以它们的 WOE 之差。供应商的 PD 从最好的分箱到最差的分箱相差 66 分, housing 相差 25 分, employment 相差 8 分。

为申请人评分#

woe_to_points() 给每个申请人所在各组的分值,各分值相加即为评分:

>>> points = woe_to_points(spec.woe_transform(validation)[selected], coefficients, intercept, **scale)
>>> score = points.sum(axis=1)
>>> points.assign(score=score).head(3).T.round(1)
                30000  30001  30003
variable
age              75.5   82.3   82.3
income           76.0   70.1   76.9
debt_to_income   82.6   79.0   74.8
inquiries        76.5   76.5   76.5
housing          83.7   67.7   92.9
employment       72.9   80.6   80.6
incumbent_pd     72.5   59.2   72.5
score           539.7  515.4  556.5

评分是模型对数几率经刻度转换的结果,因此 score_to_pd() 能把它还原为模型的 PD:

>>> bool(np.allclose(score_to_pd(score, **scale), predict(model, validation)))
True
>>> score.describe()[["min", "25%", "50%", "75%", "max"]].round(1)
min    491.4
25%    536.0
50%    552.6
75%    569.6
max    637.3
dtype: float64

解释低分#

被拒绝的申请人会被告知哪些变量让评分降低最多。 reason_codes() 把每个变量的分值与该变量最佳分组的分值相比较,列出差距最大的变量。以验证样本中评分最低的三人为例:

>>> lowest = score.nsmallest(3).index
>>> points.loc[lowest].T.round(1)
                35013  34278  36020
variable
age              69.0   69.0   75.5
income           79.1   76.0   70.1
debt_to_income   79.0   79.0   79.0
inquiries        64.5   64.5   72.3
housing          67.7   67.7   67.7
employment       72.9   80.6   72.9
incumbent_pd     59.2   59.2   59.2
>>> reason_codes(points.loc[lowest], card)
           reason_1 reason_2   reason_3
35013  incumbent_pd  housing  inquiries
34278  incumbent_pd  housing  inquiries
36020  incumbent_pd  housing     income

三人的供应商 PD 都在最差的分箱,而且都是租房,因此 incumbent_pd 和 housing 排在前面;第三个原因各不相同。原因只取决于分值表,因此在任何应用该评分卡的系统中都相同。