评分卡#
评分卡把基于 WOE 的逻辑回归转换为分值。每个申请人在每个变量上获得其所属分组的分值,这些分值相加得到一个代表好结果几率的分数。kivi 把系数和截距当作普通数值接收,因此模型可以来自任何库。
评分卡位于 变量筛选 和 拒绝推断 之后(它们给出系数),位于 模型表现 之前(后者评估评分)。违约太少、无法拟合模型时, 专家评分卡 改为根据判断确定各因子的权重。 score_scaling() 、 pd_to_score() 和 score_to_pd() 设定刻度, points_table() 和 woe_to_points() 给出分值, reason_codes() 解释评分。
刻度#
分数与好坏比 \(o = (1 - \mathrm{PD}) / \mathrm{PD}\) 的对数呈线性关系。刻度由三个数决定:基准分数、基准分数对应的几率,以及几率翻倍所需增加的分数(PDO):
kivi.scorecard 的每个函数都以关键字参数接收这三个数,因此可以把它们放在同一个字典里:
>>> from kivi.scorecard import pd_to_score, score_scaling, score_to_pd
>>> 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
在这一刻度下,几率 50:1(即 PD 为 1/51)对应 600 分,几率 100:1 则多 20 分。 score_to_pd() 进行反向转换:
>>> pd_to_score([1 / 51, 1 / 101, 0.1], **scale).round(1)
array([600. , 620. , 550.5])
>>> score_to_pd([560, 600, 640], **scale).round(4)
array([0.0741, 0.0196, 0.005 ])
备注
kivi 的 WOE 为 \(\ln(p_i / q_i)\) ,即事件占比与非事件占比之比的对数,并以坏结果作为事件(参见 证据权重与信息值 )。因此 WOE 越高表示风险越高,以坏标记为因变量、以 WOE 为自变量的逻辑回归系数为正,WOE 越高得到的分值越低。有些教材的 WOE 定义方向相反;此时系数和 WOE 值都会变号,而分值保持不变。
每个分组的分值#
下面的示例在随包附带的银行数据上构建一张小型评分卡。这份数据记录的是营销响应而非违约,因此与 风控建模流程 一样,把 target 为 1 视为坏结果。先对开发样本分组,并拟合每个变量的 WOE:
>>> import numpy as np
>>> import pandas as pd
>>> from kivi.datasets import load_bank
>>> from kivi.woe import woe_table, woe_transform
>>> data = load_bank()
>>> development, monitoring = data.iloc[:3000], data.iloc[3000:]
>>> age_edges = np.array([-np.inf, 25, 35, 45, 55, 65, np.inf])
>>> def to_groups(frame):
... groups = frame[["poutcome", "contact", "housing"]].copy()
... groups["age"] = pd.cut(frame["age"], age_edges)
... return groups
>>> dev_groups = to_groups(development)
>>> target = development["target"]
>>> tables = {column: woe_table(dev_groups[column], target) for column in dev_groups}
在开发样本上以 target 为因变量、以四个 WOE 列为自变量做逻辑回归,得到下面的系数和截距(保留三位小数)。 points_table() 把它们转换为分值:
>>> from kivi.scorecard import points_table
>>> coefficients = {"poutcome": 0.899, "contact": 0.658, "housing": 0.691, "age": 0.714}
>>> intercept = -2.028
>>> table = points_table(tables, coefficients, intercept, **scale)
>>> table.round({"woe": 4, "points": 1})
bin woe points
variable
poutcome unknown -0.2571 143.1
poutcome failure 0.2135 130.9
poutcome other 0.5917 121.1
poutcome success 2.5481 70.3
contact cellular 0.2409 131.8
contact unknown -0.9172 153.8
contact telephone 0.2063 132.5
housing no 0.3329 129.8
housing yes -0.3273 142.9
age (-inf, 25.0] 0.7941 120.0
age (25.0, 35.0] -0.0324 137.1
age (35.0, 45.0] -0.1664 139.8
age (45.0, 55.0] -0.0001 136.4
age (55.0, 65.0] 0.0384 135.6
age (65.0, inf] 1.1146 113.4
共有 \(m\) 个变量时,变量 \(j\) 的第 \(i\) 组得到
截距 \(\beta_0\) 和偏移量平均分摊到各个变量,因此上面四个变量各自承担 \(487.12 + 28.85 \times 2.028 = 545.64\) 分的四分之一。在同一变量内,两个分组的分值只在 WOE 项上有差别。WOE 为零的分组,其事件率与整个样本相同,恰好得到该变量分摊的份额。
小技巧
负的系数会使变量的方向反转,让风险更高的分组得到更多分值。这通常说明变量之间存在相关性;在对模型做刻度转换之前,请检查系数的符号。
为申请人评分#
按 证据权重与信息值 中的方法,用开发样本的 WOE 表对新申请人编码,再用 woe_to_points() 给出每个变量的分值。每行之和即为分数:
>>> from kivi.scorecard import woe_to_points
>>> mon_groups = to_groups(monitoring)
>>> woe = pd.DataFrame(
... {column: woe_transform(mon_groups[column], tables[column]) for column in mon_groups}
... )
>>> points = woe_to_points(woe, coefficients, intercept, **scale)
>>> points.assign(score=points.sum(axis=1)).head(3).round(1)
poutcome contact housing age score
3000 143.1 132.5 129.8 139.8 545.2
3001 143.1 153.8 142.9 139.8 579.7
3002 143.1 131.8 142.9 137.1 554.9
为索引为 3001 的申请人评分:每个取值先分组,再编码为所在组的 WOE 并换算成分值,各分值相加即为评分。#
分数就是模型对数几率经刻度转换后的结果,因此等于 pd_to_score() 对模型概率给出的分数:
>>> probability = 1 / (1 + np.exp(-(intercept + woe @ pd.Series(coefficients))))
>>> bool(np.allclose(points.sum(axis=1), pd_to_score(probability, **scale)))
True
上线的评分卡通常把分值取整。请用 table.round() 和 points.round() 以同样的方式对分值表和申请人分值取整: woe_to_points 的每个分值与 points_table() 的计算方式完全一致,因此取整后的结果也相同。取整后的总分与精确分数之差,每个变量最多半分。
原因码#
被拒绝的申请人会被告知哪些变量让分数降低最多。 reason_codes() 把每个变量的分值匹配到分值表中的某个分组,与该变量最佳分组的分值比较,并按差距从大到小列出变量:
>>> from kivi.scorecard import reason_codes
>>> reason_codes(points, table).head(3)
reason_1 reason_2 reason_3
3000 contact housing None
3001 None None None
3002 contact age None
申请人 3000 通过固定电话联系,比 contact 的最佳分组少 21.3 分;没有住房贷款,比 housing 的最佳分组少 13.2 分。申请人 3001 在每个变量上都处于最佳分组,因此没有变量拉低其分数。
刻度不会改变原因的顺序,因为每个差距都等于 \(\mathrm{factor} \cdot \beta_j\) 乘以 WOE 之差。在 atol 范围内无法匹配分值表中任何分组的分值(例如用其他截距算出的分值)会引发错误,而不会对照错误的分组给出原因。
开发样本中没有观测的分组(例如新类别)里的申请人,在分值表中没有自己的分组。其 WOE 来自 woe_transform() 中的 unseen ,这是为该分组选定的取值,而不是关于申请人的证据,因此默认情况下这些分值同样会引发错误。把它们的分值作为 unseen_points 传入,即可视为没有造成损失:此时该变量不会成为这些申请人的原因。 unseen_points 是分值而不是 WOE:对包含 unseen 所给 WOE 的单行数据调用 woe_to_points() 即可得到;WOE 为 0 时得到基础分值,每个变量都相同。下例中,第一个申请人的住房状况在开发样本中从未出现:
>>> new = pd.DataFrame({"housing": ["rented", "no"], "contact": ["telephone", "unknown"]})
>>> new_woe = pd.DataFrame({
... "housing": woe_transform(new["housing"], tables["housing"], unseen=0.0),
... "contact": woe_transform(new["contact"], tables["contact"]),
... })
>>> two = {"housing": coefficients["housing"], "contact": coefficients["contact"]}
>>> new_table = points_table(tables, two, intercept, **scale)
>>> new_points = woe_to_points(new_woe, two, intercept, **scale)
>>> zero = woe_to_points(pd.DataFrame({"housing": [0.0], "contact": [0.0]}), two, intercept, **scale)
>>> zero.round(2)
housing contact
0 272.82 272.82
>>> reason_codes(new_points, new_table, unseen_points=zero.iloc[0])
reason_1 reason_2 reason_3
0 contact None None
1 housing None None
第一个申请人的 housing 分值就是 WOE 为 0 时的分值,因此只有电话联系方式(telephone)是原因。第二个申请人没有住房贷款,属于较不利的住房分组,并且处于最佳的联系方式分组 unknown 。只有接近 unseen_points 的分值才会豁免:来自其他模型的分值,或舍入比 atol 允许的更粗的 WOE,仍会引发错误。
备注
如果开发样本对坏结果做了过采样或欠采样,模型的截距只适用于该样本。在构建分值表之前,请用 kivi.calibration.corrected_intercept() 校正截距;参见 校准 。