3. 分箱与 WOE#
评分卡会对每个变量分组,以便解读每个组的作用、向业务方解释并持续监控。本章在一个变量上比较各种分箱方法,为每个变量选定分箱方式,再把所有变量一次拟合成一个分箱方案,供后续章节复用。这些变量是 数据与样本 中的七个候选变量; 交易流水特征 中的交易流水特征留待评分卡的下一个版本。
前面各章的代码
>>> import numpy as np
>>> import pandas as pd
>>> from kivi.datasets import make_applications
>>> 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"]
比较分箱方法#
负债收入比是典型的连续型变量。 bin_edges() 只根据取值拟合等频边界,而 chimerge_edges() 、 tree_edges() 和 monotonic_edges() 还会用到目标变量; 分箱 介绍了每一种方法。每种方法最多分五箱,再用 woe_table() 衡量结果:
>>> from kivi.binning import bin_edges, chimerge_edges, monotonic_edges, tree_edges
>>> from kivi.woe import woe_table
>>> x, y = development["debt_to_income"], development["target"]
>>> candidates = {
... "quantile": bin_edges(x, bins=5),
... "chimerge": chimerge_edges(x, y, max_bins=5),
... "tree": tree_edges(x, y, max_bins=5),
... "monotonic": monotonic_edges(x, y, max_bins=5),
... }
>>> tables = {name: woe_table(pd.cut(x, edges), y) for name, edges in candidates.items()}
>>> pd.DataFrame(
... {
... "bins": {name: len(table) for name, table in tables.items()},
... "iv": {name: table["iv"].sum() for name, table in tables.items()},
... "rising": {name: table["woe"].is_monotonic_increasing for name, table in tables.items()},
... }
... ).round(3)
bins iv rising
quantile 5 0.031 True
chimerge 4 0.036 True
tree 5 0.039 True
monotonic 5 0.038 True
各方法下负债收入比各分箱的 WOE。有监督的方法把边界放在事件率变化的地方,四种方法得到的 WOE 都随负债收入比上升。#
有监督的方法比等频分箱多找到一点信息,而且四种方法的 WOE 都随负债收入比上升,这正符合债务负担越重风险越高的预期。只有 monotonic_edges() 能保证这一趋势,因此项目对四个连续变量 age 、 income 、 debt_to_income 和 incumbent_pd 都采用它。符合业务预期的趋势,比略高一点的 IV 更容易站得住脚。
为查询次数指定边界#
概况把 inquiries 标为存在并列值:将近一半的申请人没有查询记录,因此它的等频分箱会塌缩。它只有少数几个取值,因此改用指定的边界,三次及以上的查询归入一箱:
>>> inquiry_edges = [-np.inf, 0, 1, 2, np.inf]
>>> woe_table(pd.cut(development["inquiries"], inquiry_edges), y)[["total", "event_rate", "woe"]].round(3)
total event_rate woe
bin
(-inf, 0.0] 9524 0.085 -0.188
(0.0, 1.0] 7136 0.107 0.066
(1.0, 2.0] 2738 0.123 0.222
(2.0, inf] 829 0.157 0.507
事件率随查询次数逐级上升,从没有查询时的 8.5% 升到三次及以上时的 15.7%。
合并就业类别#
类别型变量保留原有标签作为分组,除非某些标签太小或彼此太相似,不值得分开:
>>> woe_table(development["employment"], y)[["total", "event_rate", "woe"]].round(3)
total event_rate woe
bin
permanent 14552 0.094 -0.081
temporary 3031 0.126 0.255
self_employed 2644 0.110 0.103
临时雇员和自雇人士的违约率相近,都高于长期雇员。 chimerge_groups() 合并事件率差异不显著的相邻类别,并以 dict 返回分组; group_categories() 把它应用到任意样本上:
>>> from kivi.binning import chimerge_groups, group_categories
>>> employment_groups = chimerge_groups(development["employment"], y)
>>> employment_groups
{'permanent': 'permanent', 'self_employed': 'self_employed, temporary', 'temporary': 'self_employed, temporary'}
>>> group_categories(validation["employment"], employment_groups).value_counts()
employment
permanent 4839
self_employed, temporary 1946
Name: count, dtype: int64
housing 保留三个标签,它们的事件率差别明显。
一次拟合所有变量#
fit_spec() 按上面选定的方法拟合每一列的分箱,并计算每个组的 WOE;见 分箱方案 。供应商的 PD 携带了大部分信息,因此最多分八箱:
>>> from kivi.spec import fit_spec
>>> method = {column: "monotonic" for column in ["age", "income", "debt_to_income", "incumbent_pd"]}
>>> method["employment"] = "chimerge"
>>> spec = fit_spec(
... development[features], y, method=method, bins={"inquiries": inquiry_edges, "incumbent_pd": 8}
... )
>>> pd.Series({column: table["iv"].sum() for column, table in spec.tables.items()}).round(3)
age 0.028
income 0.027
debt_to_income 0.038
inquiries 0.037
housing 0.110
employment 0.015
incumbent_pd 0.299
dtype: float64
分箱方案保存了每一列的边界、类别分组和 WOE 表。供应商 PD 的表显示,WOE 在八个分箱中逐级上升:
>>> spec.tables["incumbent_pd"][["total", "event_rate", "woe", "iv"]].round(3)
total event_rate woe iv
bin
(-inf, 0.0304] 1017 0.022 -1.623 0.071
(0.0304, 0.0377] 1008 0.035 -1.136 0.041
(0.0377, 0.0541] 3051 0.053 -0.692 0.055
(0.0541, 0.0793] 5038 0.081 -0.243 0.013
(0.0793, 0.0961] 3033 0.104 0.037 0.000
(0.0961, 0.109] 2029 0.120 0.199 0.004
(0.109, 0.123] 2025 0.155 0.490 0.029
(0.123, inf] 3026 0.178 0.660 0.084
开发样本上供应商 PD 的八个分箱。违约率从最低一箱的 2% 升到最高一箱的 18%,最高一箱是 PD 刚好低于供应商 15% 截断点的申请人。#
分箱只在开发样本上拟合。之后的每个样本,从验证样本到投产后的申请,都用相同的边界和分组划分,这样它们的 WOE 和稳定性才可比。