10. 投产与监控#

从 2025 年 1 月起,由新评分卡审批申请。本章为评分系统保存分箱方案,为一年的新申请评分,并逐月跟踪模型,直到申请人结构出现偏移; 模型监控 和 群体稳定性指标 介绍了这些指标。

前面各章的代码
>>> 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)
>>> from kivi.calibration import master_scale
>>> master = master_scale(np.geomspace(0.02, 0.32, 5), labels=list("ABCDE"))
>>> cutoff = master.loc["D", "upper"]

保存分箱#

评分系统必须完全按照开发样本的方式对每笔申请分组和编码。 select() 保留模型中的变量, to_json() 把它们的边界、分组和 WOE 表写成带版本号的 JSON;见 分箱方案 。 from_json() 把它读回为一个相等的分箱方案:

>>> from kivi.spec import BinningSpec
>>> production = spec.select(selected)
>>> text = production.to_json(indent=2)
>>> restored = BinningSpec.from_json(text)
>>> restored == production
True
>>> restored.groups["employment"]
{'permanent': 'permanent', 'self_employed': 'self_employed, temporary', 'temporary': 'self_employed, temporary'}

这份 JSON 与 评分卡 中的系数和刻度一起保存,旁边还保存了 交易流水特征 中的特征方案,它为每笔新申请计算交易流水特征,供下一版评分卡使用。下面的示例像评分系统一样,用 restored 来评分:

>>> def score_pd(frame):
...     slopes = model["coef"].drop("intercept")
...     log_odds = model.loc["intercept", "coef"] + restored.woe_transform(frame)[slopes.index] @ slopes
...     return 1 / (1 + np.exp(-log_odds))

为一年的申请评分#

2025 年的申请来自同一生成器的新样本,每月 2,000 笔。10 月,一家竞争对手推出了更便宜的贷款,大多数低风险申请人转投对方:从那时起,供应商 PD 低于 7% 的申请人中有 70% 不再申请。政策规则仍会拒绝部分申请,其余由新的截断点决定:

>>> new = make_applications(n_applicants=24000, rng=1)
>>> new["month"] = pd.period_range("2025-01", periods=12, freq="M")[np.arange(24000) * 12 // 24000]
>>> rng = np.random.default_rng(2)
>>> lost = (new["month"] >= "2025-10") & (new["incumbent_pd"] < 0.07) & (rng.random(24000) < 0.7)
>>> stream = new[~lost & ~new["policy_decline"]]
>>> stream_pd = score_pd(stream)
>>> approved = stream_pd <= cutoff

到 2025 年末,9 月及之前发放的贷款结果已知,之后的还不知道。未知的结果以缺失值传入。

逐月跟踪模型#

monitoring_table() 每月给出一行:申请数、已知结果数及其违约率、已知结果上的 AUC、Gini 和 KS,以及 PD 相对于开发月份合格申请人的 PSI:

>>> from kivi.monitoring import monitoring_table, stability_table
>>> outcome = stream["target"].where(approved & (stream["month"] <= "2025-09"))
>>> reference = eligible[eligible["month"] <= "2024-09"]
>>> report = monitoring_table(stream_pd, stream["month"], outcome, reference_score=score_pd(reference))
>>> report.round(3)
         total  known  event  event_rate    auc   gini     ks    psi
period
2025-01   1852   1406    143       0.102  0.697  0.395  0.329  0.002
2025-02   1837   1406    143       0.102  0.678  0.355  0.290  0.006
2025-03   1858   1452    153       0.105  0.664  0.329  0.269  0.004
2025-04   1857   1406    150       0.107  0.667  0.334  0.282  0.007
2025-05   1865   1385    131       0.095  0.669  0.339  0.273  0.006
2025-06   1846   1377    151       0.110  0.644  0.288  0.211  0.004
2025-07   1863   1387    140       0.101  0.651  0.301  0.270  0.005
2025-08   1848   1374    133       0.097  0.703  0.405  0.333  0.004
2025-09   1874   1406    176       0.125  0.659  0.318  0.243  0.012
2025-10   1460      0      0         NaN    NaN    NaN    NaN  0.139
2025-11   1483      0      0         NaN    NaN    NaN    NaN  0.150
2025-12   1487      0      0         NaN    NaN    NaN    NaN  0.134
每月的申请数与已知结果数、Gini 系数及其开发样本值,以及带 0.1 和 0.25 阈值的 PD 的 PSI

2025 年的监控报告。已知结果的 Gini 在开发样本值附近波动;从 10 月起,PD 的 PSI 升至 0.1 以上。#

到 9 月为止,PSI 一直低于 0.02,已知结果的 Gini 在其开发样本值附近波动:

>>> from kivi.metrics import gini
>>> print(f"{gini(score_pd(development), development['target']):.3f}")
0.353

2025 年的获批申请人是由新截断点而不是供应商的截断点挑选的,因此其 Gini 不必与开发样本完全一致;持续下降才是警示信号。从 10 月起,申请量减少,PSI 升到 0.13 到 0.15 之间:高于通常需要进一步检查的 0.1,但低于 0.25。

找出原因#

stability_table() 按分箱方案的分组,给出每个变量每个月的 PSI:

>>> stability = stability_table(
...     restored.cut(stream), stream["month"], reference_groups=restored.cut(reference)
... )
>>> stability.round(3).tail(4).T
period          2025-09  2025-10  2025-11  2025-12
variable
age               0.005    0.022    0.011    0.027
income            0.003    0.016    0.026    0.021
debt_to_income    0.001    0.030    0.041    0.021
inquiries         0.002    0.016    0.014    0.015
housing           0.001    0.002    0.000    0.001
employment        0.002    0.007    0.006    0.006
incumbent_pd      0.006    0.182    0.189    0.192
2025 年各变量逐月 PSI 的热力图

各变量逐月的 PSI。只有 incumbent_pd 从 10 月起发生了变化。#

只有供应商的 PD 发生了偏移。 psi_table() 显示 12 月哪些分箱发生了变化, csi_table() 则用 评分卡 中的分值显示每处变化使平均评分移动了多少:

>>> from kivi.scorecard import points_table
>>> from kivi.stability import csi_table, psi_table
>>> december = stream[stream["month"] == "2025-12"]
>>> before = restored.cut(reference)["incumbent_pd"]
>>> after = restored.cut(december)["incumbent_pd"]
>>> psi_table(before, after).round(3)
                  reference  current  reference_share  current_share    psi
bin
(-inf, 0.0304]         1017       14            0.037          0.009  0.037
(0.0304, 0.0377]       1008       25            0.036          0.017  0.015
(0.0377, 0.0541]       3051       56            0.110          0.038  0.077
(0.0541, 0.0793]       5038      189            0.181          0.127  0.019
(0.0793, 0.0961]       3033      222            0.109          0.149  0.013
(0.0961, 0.109]        2029      150            0.073          0.101  0.009
(0.109, 0.123]         2025      130            0.073          0.087  0.003
(0.123, inf]          10571      701            0.381          0.471  0.019
>>> coefficients = model["coef"].drop("intercept")
>>> card = points_table(
...     restored.tables, coefficients, model.loc["intercept", "coef"], pdo=20, base_score=600, base_odds=50
... )
>>> shift = csi_table(before, after, card.loc[["incumbent_pd"]])
>>> print(f"{shift['csi'].sum():.2f}")
-5.89

最低的三个分箱,即供应商 PD 低于 5.4% 的申请人,占比减少了约三分之二;仅通过供应商 PD 这一项,12 月的平均评分就降低了 5.9 分。模型并没有错:它为现在看到的申请人评分。这一偏移是业务的变化,贷款机构应当通过定价和营销来应对,而不是重新拟合评分卡。最后一个季度的结果将显示 PD 是否依然成立。