兼容性#

本页说明 kivi 的一个版本可以改变什么、保留什么:支持的版本、公共名称、返回表格的结构、异常类型,以及 BinningSpec 和 FeatureSpec 的文件。每个函数都遵循的规则(例如参数的顺序)见 约定 ,它们同样属于 API。

版本与弃用#

从 1.0 版起,kivi 遵循语义化版本:

  • 主版本(例如 2.0)可以移除或修改公共 API 的任何部分。

  • 次版本(例如 1.3)会新增函数、参数、表格列或格式版本,并可能弃用 API 的某一部分。

  • 补丁版本(例如 1.3.1)只修复错误。

  • 已弃用的函数、参数或行为在该主版本的其余版本中继续可用,并发出说明替代用法的 FutureWarning 。它在下一个主版本中才改变,因此 1.4 中的弃用在 2.0 中生效。版本说明在“变更”下列出每项弃用。

  • 对错误结果的修复可以出现在任何版本中,无论次版本还是补丁版本,即使已有代码依赖该错误结果。版本说明在“修复”下列出这类修复。

1.0 之前曾适用更短的规则:0.9 中弃用的内容已在 1.0 中改变,见 迁移指南 。要找出某个版本将会改变的调用,可以把 FutureWarning 变为错误来运行代码或其测试,例如 python -W error::FutureWarning 或 pytest -W error::FutureWarning 。

支持的版本#

kivi 支持 Python 3.10 或更高版本、NumPy 1.24 或更高版本,以及 pandas 2.0 或更高版本,其测试同时在这些最低版本和最新版本上运行。 kivi.plotting 的图表还需要 matplotlib 3.7 或更高版本,可用 pip install "kivi[plot]" 安装;其他模块都不导入它。从 1.0 起,最低版本只会在次版本中提高,并由前一个版本的版本说明预先公布。补丁版本从不提高最低版本。

哪些属于公共 API#

成对参数的结果是元组,例如 score_scaling() 的 (factor, offset) 、 platt_fit() 的 (slope, intercept) 、 isotonic_fit() 的 (edges, probability) 、 hosmer_lemeshow() 和 spiegelhalter_test() 的 (statistic, p_value) ,以及 vasicek_fit() 的 (probability, correlation) 。次版本可能把它们改为具名元组,其解包方式不变。

表格#

每个表格的索引名称、列名及列的顺序都是公共的。次版本可以在表格末尾追加列,但只有经过弃用才会重命名、重排或移除某列,因此请按名称而不是按位置选取列。 logistic_regression() 和 fractional_regression() 的 attrs 设置在返回的数据框上,pandas 在许多操作中会丢弃它们。

函数

索引

列

window_features()

observations 的索引,保持其顺序

按窗口依次给出 {prefix}events_{window} ,然后逐列、逐个聚合给出 {prefix}{column}_{aggregation}_{window} ,最后是比率 {numerator}_over_{window} 或 {numerator}_over_{denominator}

panel_features()

observations 的索引,保持其顺序

按窗口依次给出 {prefix}months_{window} 、 {prefix}bad_months_{window} 、 {prefix}longest_bad_run_{window} 、 {prefix}longest_good_run_{window} 、 {prefix}worst_status_{window} ,然后是 {prefix}months_since_bad

FeatureSpec.compute

observations 的索引,保持其顺序

先是 events 中每组的列,再是 panel 中每组的列,与上面两个函数给出的相同,最后是比率; FeatureSpec.columns 列出这些列

woe_table() 、 BinningSpec.tables

bin

non_event, event, total, non_event_share, event_share, total_share, event_rate, woe, iv

woe() ,计数为 Series 时

名为 woe 、索引与计数相同的 Series

psi_table()

bin

reference, current, reference_share, current_share, psi

csi_table()

bin

reference, current, reference_share, current_share, points, csi

calibration_table()

bin

total, event, event_rate, mean_probability

backtest_table()

bin

total 、 estimated 、 realized 、 difference 、 variance 、 statistic 、 p_value

brier_decomposition()

索引为 statistic 的 Series: brier 、 reliability 、 resolution 、 uncertainty 、 within_variance 、 within_covariance

concentration_test()

索引为 statistic 的 Series: hi 、 cv 、 initial_hi 、 initial_cv 、 statistic 、 p_value

expected_loss_table()

bin

total, exposure, exposure_weighted_probability, expected_loss, expected_loss_rate

gains_table()

bin ,以区间表示的评分区段

non_event, event, total, event_rate, lift, cum_non_event_share, cum_event_share, cum_total_share, ks, cum_lift

roc_table()

threshold

cum_non_event, cum_event, false_positive_rate, true_positive_rate, precision

roc_auc_interval()

statistic : auc 和 gini

estimate, se, lower, upper

delong_test()

索引为 current_auc 、 new_auc 、 difference 、 se 、 lower 、 upper 、 z 、 p_value 的 Series

generalized_auc()

statistic : gauc 和 somers_d

estimate, se, lower, upper

profile_table()

variable

kind 、 count 、 missing 、 missing_share 、 distinct 、 top 、 top_share 、 special 、 special_share 、 mean 、 std 、 min 、 q01 、 q50 、 q99 、 max ,给出目标变量时接着是 event_rate 、 missing_event_rate 、 non_missing_event_rate 、 missing_lift 、 non_missing_lift 、 iv 、 tied_bins 、 gini ,然后是 skewness 、 kurtosis 、 cv ,给出目标变量时最后是 ks 、 chi2_p_value

logistic_regression() 、 fractional_regression()

variable :先是 intercept ,然后是 X 的各列

coef, se, z, p_value

vif()

名为 vif 、索引为 variable 的 Series

correlation_filter() 、 vif_filter()

由 X 中保留的列组成的 pandas.Index ,按原顺序排列;当 X 不是 DataFrame 时为列的位置

selection_path()

step :1 到 order 中的变量个数

variable 、 auc 、 gini 、 ks 、 max_p_value 、 min_coefficient ,给出 validation 时还有 validation_auc 、 validation_gini 、 validation_ks

points_table()

variable

bin, woe, points

woe_to_points()

woe 的索引

coefficients 的每个变量一列,列名称取自其索引的名称

reason_codes()

points 的索引

reason_1 到 reason_<top>

ahp_weights()

名为 weight 、索引为 factor 的 Series

ahp_consistency()

索引为 statistic 的 Series: lambda_max 、 ci 、 ri 、 cr

combine_weights()

名为 weight 、索引为 factor 和 sub_factor 的 Series

master_scale()

grade

lower, probability, upper

cutoff_table()

cutoff

approved 、 approval_rate 、 approved_event 、 approved_event_rate 、 rejected_event_share 、 rejected_non_event_share ,给出 loss 时还有 approved_loss

confusion_table()

cutoff

true_positive 、 false_positive 、 true_negative 、 false_negative 、 precision 、 recall 、 f1 、 accuracy

swap_set()

current 和 new ,共四行

total 、 share ,给出目标变量时还有 known 、 event 、 event_rate

fuzzy_augmentation()

probability 的索引,每个拒绝样本出现两次

target, weight

parceling()

reject_score 的索引,在 expected=True 时每个拒绝样本出现两次

target, weight

reweighting()

名为 weight 、索引与 score 相同的 Series

monitoring_table()

period

total, known, event, event_rate, auc, gini, ks, psi

stability_table()

period

groups 的每列一列,列的名称为 variable

workout_lgd()

loan : loan 的取值,保持其顺序

ead 、 recovery 、 cost 、 lgd

lgd_table()

bin

total 、 total_share 、 mean 、 std 、 min 、 max 、 zero_share 、 full_share

most_prudent_pd()

名为 most_prudent_pd 的 Series,索引为 Series event 或 total 的索引;否则为数组

delinquency_panel()

范围索引,每笔贷款每月一行

loan_id 、 cohort 、 month 、 month_on_book 、 days_past_due 、 overdue_amount 、 outstanding ,传入 principal 时还有 balance ,以及 status

vintage_table()

cohort

每个账龄月份一列,列的名称为 month_on_book

transition_matrix()

before

每个状态一列,列的名称为 after

roll_rates()

名为 roll_rate 、索引为 before 的 Series

migration_bandwidth()

索引为 statistic 的 Series: upper 、 lower

migration_test()

before 和 after , grades 的每个非对角单元格一行

total 、 share 、 statistic 、 p_value

transition_pd()

state

每个期限一列,列的名称为 horizon

survival_table()

period :从 1 到最大的 duration

at_risk 、 event 、 competing (仅在给出 competing 时)、 censored 、 hazard 、 survival 、 cumulative_pd

stage_table()

stage :1、2 和 3

total, exposure, exposure_share, expected_credit_loss, coverage

make_loan_panel()

整数范围

loan_id, cohort, month, month_on_book, term, amount, balance, status

make_applications()

整数范围

age 、 income 、 debt_to_income 、 inquiries 、 housing 、 employment 、 incumbent_pd 、 policy_decline 、 approved 、 target ,使用 include_risk=True 时还有 risk

make_transactions()

范围索引,按申请人和时间排序

applicant_id 、 timestamp 、 amount 、 kind 、 merchant_category

make_defaults()

范围索引,按违约日期排序

loan_id 、 segment 、 default_date 、 ead 、 limit 、 drawn 、 collateral_value 、 discount_rate 、 closing_date

make_recoveries()

范围索引,按 defaults 中贷款的顺序、再按日期排列

loan_id 、 date 、 amount 、 kind

load_bank()

整数范围

UCI 数据的 16 列和 target

特征名称由参数决定。列名和前缀保持原样,kivi 自己的部分,即聚合名、窗口和面板特征的名称,为小写,窗口写作 3m 、 90d 或 6m_3m 。数据类型同样是公共的: events 、 count 和 nunique 特征以及 panel_features() 的各项计数为 int64, worst_status 的数据类型与状态列相同,其余特征为 float64。窗口内没有取值时,这些计数和 sum 为 0, worst_status 为缺失的状态,其余特征为 NaN。

load_bank() 总是返回相同的数据。 make_loan_panel() 、 make_applications() 、 make_transactions() 、 make_defaults() 和 make_recoveries() 保持其列和数据类型不变,包括 kind 、 merchant_category 和 segment 的类别,但对给定的 rng 模拟出的取值只在同一个次版本内保持不变,以便次版本可以改进模拟。

woe_trend() 返回六个标签之一: "increasing" 、 "decreasing" 、 "u_shaped" 、 "inverted_u" 、 "irregular" 和 "flat" 。它们与列名一样属于公开接口;它的规则同样如此:WOE 的相对差不超过 \(10^{-10}\) 的相邻分箱既不算上升也不算下降。

ahp_consistency() 的随机一致性指标取自 Tone(1986)3 至 15 个因子的表,是常量。发布版本只会为纠正错误的数值而修改它们,并作为修复列在发布说明的 Fixed 下,因此同一矩阵的一致性比率在各版本之间保持不变。

图表#

kivi.plotting 的函数保持以下内容不变:从每张表绘制的数据、哪一列画在哪条轴上、参数、引发的错误以及返回值。每个函数都返回它所绘制的 matplotlib.axes.Axes ,只有 woe_chart() 例外:它是唯一有两个纵轴的图表,返回柱状图和折线各自的 Axes。 distribution_chart() 绘制原始 non_event 和 event 计数的占比,而不是 WOE 表中经过平滑的占比列; precision_recall_chart() 绘制 ROC 表中有精确率的行,并在最后一行的精确率(即事件率)处画一条线。颜色、线型、图形尺寸、图例和标注的位置、刻度格式以及组成图表的 matplotlib 图元数量可能在任何版本中改变,以便改进图表的外观。需要进一步美化图表的代码应基于返回的 Axes,而不是其中的图元。

错误与警告#

  • 异常类型是公共的。无效输入引发 ValueError ,无法拟合的模型引发 ValueError 的子类 SeparationError 。Python 本身会拒绝的调用(例如缺少参数或含有未知参数)引发 TypeError 。

  • 警告类别是公共的: UserWarning 表示结果与请求不同, RuntimeWarning 表示拟合未收敛, FutureWarning 表示 API 的某一部分已被弃用。

  • 消息的措辞不是公共的,可能在任何版本中改变。消息指出参数名称和所收到的值,这一规则保持不变。

已保存的分箱方案#

BinningSpec.to_json 和 FeatureSpec.to_json 写出的文件可以被以后的版本读取; 分箱方案 和 时间窗特征 说明其格式。

  • format 总是 "kivi.BinningSpec" 或 "kivi.FeatureSpec" , format_version 是整数。分箱方案没有类别分组时为版本 1,对某列做了分组时为版本 2。特征方案为版本 1,即 kivi 1.3.0 的格式,其 kivi_version 之后的键就是 FeatureSpec 接受的参数: id 、 observation_time 、 events 、 ratios 和 panel ,每组都写出全部的键。

  • 每个 1.x 版本都能读取 kivi 0.6.0 及以后版本写出的分箱方案的每种格式版本,以及 kivi 1.3.0 及以后版本写出的特征方案。

  • 分箱方案以能够容纳它的最低格式版本写出,因此不使用较新功能的文件仍可被较早的版本读取。新的格式版本只会增加内容,在同一个主版本内,已有键的含义从不改变。

  • 格式版本较新的文件会引发指出该版本的 ValueError ,而不是只读取其中一部分。未知的键同样会引发错误,因此新键总是伴随新的格式版本出现。特征方案在缺少键或同一个 JSON 对象中键重复时也会引发错误。

  • kivi_version 记录写出该方案的版本,从不影响文件的读取方式。相等比较和哈希像包含每个字段一样包含它,因此由较早版本保存的分箱方案与今天的同一拟合结果不相等; 保存和加载分箱方案 说明如何比较二者的内容。

  • 用分箱方案编码时只读取表格每行的 bin 和 woe 。其他列描述拟合结果,按写入时的内容加载。

kivi 的测试保存了由 0.6.0 写出的格式版本 1 分箱方案和由 0.8.0 写出的格式版本 2 分箱方案,并检查每个文件都能加载、对一个固定样本的编码与写出它的版本完全相同,并且除 kivi_version 和末位数字的舍入外与重新拟合的结果一致。测试以同样的方式保存了一个 1.3.0 的特征方案,其中有时间窗组、面板组和比率,并检查它能加载、再次写出相同的 JSON,并对一个固定样本计算出相同的特征。