4. 变量筛选与建模#
评分卡是违约标记对各变量 WOE 的逻辑回归。本章对开发样本做 WOE 编码,检查变量之间没有重复信息,筛选变量,并在获批申请人上拟合模型。
前面各章的代码
>>> import numpy as np
>>> import pandas as pd
>>> from kivi.datasets import make_applications
>>> 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)
WOE 编码#
woe_transform() 对样本的每一列分组,并把每个组替换为它的 WOE:
>>> X = spec.woe_transform(development)
>>> y = development["target"]
>>> X.head(3).round(3)
age income debt_to_income inquiries housing employment incumbent_pd
0 -0.033 -0.038 -0.03 -0.188 0.341 0.186 0.490
2 -0.189 0.067 -0.03 0.066 -0.472 -0.081 0.660
3 -0.033 -0.038 -0.03 0.222 0.341 -0.081 -0.243
WOE 越高风险越高,因此模型的每个系数都应为正。
检查相关性#
两个高度相关的变量携带相同的信息,还会让系数不稳定。 correlation_filter() 在每对相关系数超过 0.7 的变量中剔除较弱的一个, vif() 衡量其他变量合起来能在多大程度上解释每个变量;见 变量筛选 :
>>> from kivi.selection import correlation_filter, logistic_regression, stepwise, vif
>>> iv = pd.Series({column: table["iv"].sum() for column, table in spec.tables.items()})
>>> kept = correlation_filter(X, priority=iv)
>>> X.columns.difference(kept).tolist()
[]
>>> vif(X[kept]).round(2)
variable
age 1.15
income 1.19
debt_to_income 1.33
inquiries 1.22
housing 1.00
employment 1.15
incumbent_pd 1.73
Name: vif, dtype: float64
没有哪一对接近阈值。供应商的 PD 方差膨胀因子最高,因为它也用到了申请数据,但 1.7 远低于通常 5 到 10 的上限。
筛选变量并拟合模型#
stepwise() 按逻辑回归的 p 值逐步加入和剔除变量,并且在使用 WOE 输入时只保留系数为正的变量。随后由 logistic_regression() 拟合模型:
>>> selected = stepwise(X[kept], y)
>>> selected.tolist()
['age', 'income', 'debt_to_income', 'inquiries', 'housing', 'employment', 'incumbent_pd']
>>> accepts_model = logistic_regression(X[selected], y)
>>> accepts_model.round(3)
coef se z p_value
variable
intercept -2.191 0.025 -88.805 0.000
age 0.529 0.151 3.495 0.000
income 0.466 0.159 2.931 0.003
debt_to_income 0.507 0.143 3.546 0.000
inquiries 0.617 0.134 4.607 0.000
housing 1.016 0.073 13.961 0.000
employment 0.615 0.204 3.009 0.003
incumbent_pd 0.805 0.059 13.643 0.000
所有变量都被保留,每个系数都为正且显著。供应商的 PD 和供应商评分卡忽略的 housing 的 z 统计量最大。
申请人的 PD 是截距加上 WOE 值乘以系数之和的逻辑函数值。后面各章用下面这个函数计算它:
>>> 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))
>>> print(f"{predict(accepts_model, development).mean():.4f}, {y.mean():.4f}")
0.1008, 0.1008
在开发样本上,平均 PD 等于违约率,任何带截距的逻辑回归都是如此。这个模型叫作 accepts_model ,因为它只见过获批申请人。 拒绝推断 将考察它对其他申请人的预测。