机器学习正影响着许多不同的行业,金融业是受其影响最大的行业之一。 欺诈检测、客户细分、员工或客户保留等功能是机器学习的主要目标,本文的关注点为信用风险评分。
如何才能让从事该领域的专业人员更加容易地应对工作?软件包Credit R就这样诞生了!它允许你在机器学习应用程序之前轻松创建信用风险评分的基本模型。此外,它还包含一些可用于验证评分进程的函数。 该软件包旨在促进变量分析、变量选择、模型开发、模型校准、评定量表开发和模型验证等方法的应用。通过已定义的函数,这些方法可以快速应用于所有建模数据或特定变量中。 在本文中,我们首先将了解Credit R软件包的细节。然后,我们将列举一个使用Credit R软件包的完整例子,深入了解R软件包。 这个软件包是为信贷风险专业人士提供的。使用该软件包需要具备信贷风险评分方法的基本知识。 目录 1. 为什么要使用Credit R? 2. 开始使用Credit R 3. Credit R中的函数列表 4. Credit R软件包的应用 1. 为什么要使用Credit R? 随着信贷领域机器学习模型的需求不断增加,人们对信贷风险模型的认识正在迅速转变。然而,许多管理者对过渡到机器学习技术仍然非常谨慎。因此,一个可能的推测是,在这个转换阶段,机器学习算法将与传统方法并行。 一旦确定机器学习算法,在挑战该领域传统方法的同时,也产生了比传统方法更有效的结果,这样就可以获得监管者的信任。此外,解释机器学习算法的新方法可能有助于创建更透明的学习过程。 Credit R软件包提供了自动使用传统方法的可能性,也为传统和机器学习模型的验证提供了可能性。 2. 开始使用 Credit R 要安装credit R软件包,你首先需要安装devtools软件包。可以通过运行以下代码来安装devtools软件包: install.packages('devtools', dependencies = TRUE) 可以使用devtools软件包中的“install_github”函数安装creditR软件包: library(devtools) devtools::install_github('ayhandis/creditR) library(creditR) 3. Credit R中的函数列表 下面列出了该软件包中可用的函数。 ls('package:creditR') 输出: 4. Credit R 软件包的应用 我们已经说了很多理论知识了,现在开始深入了解R软件包吧! 下面列举了一个Credit R的应用实例,它研究了如何使用软件包中提供的函数执行信贷风险评分中的一些常见步骤。 在完成本例时,我们做了一些实践。 一般应用程序在两个主要标题下构建,即建模和模型验证,在注释行中可以看到相应代码的详细信息。 本文仅列举了重要的输出。 此R脚本旨在使Credit R软件包更容易理解。获得高精度模型不在本研究范围内。 # Attaching the library library(creditR) #Model data and data structure data('germancredit') str(germancredit) #Preparing a sample data set sample_data <- germancredit[,c('duration.in.month','credit.amount','installment.rate.in.percentage.of.disposable.income', 'age.in.years','creditability')] #Converting the ‘Creditability’ (default flag) variable into numeric type sample_data$creditability <- ifelse(sample_data$creditability == 'bad',1,0) #Calculating the missing ratios missing_ratio(sample_data) 输出: #Splitting the data into train and test sets traintest <- train_test_split(sample_data,123,0.70) train <- traintest$train test <- traintest$test WOE变换是一种通过变量与目标变量的关系将变量转换为分类变量的方法。以下“woerules”对象包含WOE规则。 在woe.binning.deploy函数的帮助下,WOE规则能够在数据集上运行。 我们需要的变量在“woe.get.clear.data”函数的帮助下分配给“train_woe”对象。 #Applying WOE transformation on the variables woerules <- woe.binning(df = train,target.var = 'creditability',pred.var = train,event.class = 1) train_woe <- woe.binning.deploy(train, woerules, add.woe.or.dum.var='woe') #Creating a dataset with the transformed variables and default flag train_woe <- woe.get.clear.data(train_woe,default_flag = 'creditability',prefix = 'woe') #Applying the WOE rules used on the train data to the test data test_woe <- woe.binning.deploy(test, woerules, add.woe.or.dum.var='woe') test_woe <- woe.get.clear.data(test_woe,default_flag = 'creditability',prefix = 'woe') 信息值和单变量基尼系数可以用作选择变量的方法。一般来说,IV的阈值为0.30,单变量基尼的阈值为0.10。 #Performing the IV and Gini calculations for the whole data set IV.calc.data(train_woe,'creditability') 输出: Gini.univariate.data(train_woe,'creditability') 输出: #Creating a new dataset by Gini elimination. IV elimination is also possible eliminated_data <- Gini_elimination(train_woe,'creditability',0.10) str(eliminated_data) 输出: 现实生活中有太多的变量无法用相关矩阵来管理。因此,可以将它们进行聚类以确定具有相似特征的变量。这里由于变量的数量很少,这种特殊的聚类示例没有意义,但该方法通常在具有大量变量的数据集中非常有用。 #A demonstration of the functions useful in performing Clustering clustering_data <- variable.clustering(eliminated_data,'creditability', 2) clustering_data 输出: # Returns the data for variables that have the maximum gini value in the dataset selected_data <- variable.clustering.gini(eliminated_data,'creditability', 2) 在某些情况下,集群的平均相关性很重要,因为集群的数量设置可能不正确。因此,如果集群具有较高的平均相关性,则应该对其进行详细检查。相关性值(在集群1中只有一个变量)为NaN。 correlation.cluster(eliminated_data,clustering_data,variables = 'variable',clusters = 'Group') 输出: 通过包含在数据集中的变量形成模型。当模型摘要检查变量时,这些变量似乎是有意义的。然后,借助“woe.glm.feature.importance”函数,计算出变量的权重。实际上,权重是根据单个单位变化对概率的影响来计算的。 #Creating a logistic regression model of the data model= glm(formula = creditability ~ ., family = binomial(link = 'logit'), data = eliminated_data) summary(model) 输出: #Calculating variable weights woe.glm.feature.importance(eliminated_data,model,'creditability') 输出: #Generating the PD values for the train and test data ms_train_data <- cbind(eliminated_data,model$fitted.values) ms_test_data <- cbind(test_woe[,colnames(eliminated_data)], predict(model,type = 'response', newdata = test_woe)) colnames(ms_train_data) <- c('woe.duration.in.month.binned','woe.age.in.years.binned','woe.installment.rate.in.percentage.of.disposable.income.binned','creditability','PD') colnames(ms_test_data) <- c('woe.duration.in.month.binned','woe.age.in.years.binned','woe.installment.rate.in.percentage.of.disposable.income.binned','creditability','PD') 在现实生活中,相关机构使用的是评定量表而不是连续的PD值。由于一些监管问题或为了适应不断变化的市场/投资组合条件,模型会根据不同的中心趋势进行校准。 回归和贝叶斯校正方法包含在软件包中。通过嵌入企业系统,并借助代码“calibration object$calibration_formula”,可以获得可执行校准的数值函数作为输出。 #An example application of the Regression calibration method. The model is calibrated to the test_woe data regression_calibration <- regression.calibration(model,test_woe,'creditability') regression_calibration$calibration_data regression_calibration$calibration_model regression_calibration$calibration_formula 输出: 在评定量表上采用贝叶斯校正方法。借助“master.scale”函数,我们可以轻松创建评定量表。然而,在现实生活中,只有在多次尝试之后才能创建评定量表。 将摘要添加到输出中。运行R脚本可以看到详细信息。此外,本示例的目的仅仅是在本研究范围内引入函数,因此PD值不会单独增加。 #Creating a master scale master_scale <- master.scale(ms_train_data,'creditability','PD') master_scale 输出: 为了采用贝叶斯校正法,我们在数据集中创建了分数变量,然后将评定量表的集中趋势校准到5%。 #Calibrating the master scale and the modeling data to the default rate of 5% using the bayesian calibration method ms_train_data$Score = log(ms_train_data$PD/(1-ms_train_data$PD)) ms_test_data$Score = log(ms_test_data$PD/(1-ms_test_data$PD)) bayesian_method <- bayesian.calibration(data = master_scale,average_score ='Score',total_observations = 'Total.Observations',PD = 'PD',central_tendency = 0.05,calibration_data = ms_train_data,calibration_data_score ='Score') #After calibration, the information and data related to the calibration process can be obtained as follows bayesian_method$Calibration.model bayesian_method$Calibration.formula 输出: 在实际应用中,对于不熟悉风险管理的员工来说,理解可能性的概念很难。因此,需要创建标准分。标准分可以通过使用“scalled.score”函数来创建。 #The Scaled score can be created using the following function scaled.score(bayesian_method$calibration_data, 'calibrated_pd', 3000, 15) 在建模阶段之后,执行模型验证以验证不同的期望,例如模型的准确性和稳定性。在现实生活中还会应用定性验证过程。 注意:模型校准仅用于例证。模型验证测试按照以下原始标准进行。 在逻辑回归创建的模型中,我们应考虑多重共线性问题。尽管使用了不同的阈值,但大于5的vif值表示存在这个问题。 #Calculating the Vif values of the variables. vif.calc(model) 输出: 一般来说,基尼系数的可接受下限为0.40。但是,这可能会因模型类型而异。 #Calculating the Gini for the model Gini(model$fitted.values,ms_train_data$creditability) 输出: 0.3577422 #Performing the 5 Fold cross validation k.fold.cross.validation.glm(ms_train_data,'creditability',5,1) 输出: #The KS test is performed on the distributions of the estimates for good and bad observations Kolmogorov.Smirnov(ms_train_data,'creditability','PD') Kolmogorov.Smirnov(ms_test_data,'creditability','PD') 记分卡通常要很长时间后才会进行修订,因为修订过程会产生巨大的运营成本。因此,模型的稳定性降低了修改的必要性。此外,相关机构需要稳定的模型,因为这些模型被用作许多计算的输入,如减值、资本、风险加权资产等。 系统稳定性指数(System Stability Index)是用来衡量模型和变量稳定性的一种测试。若ssi值大于0.25,则表明变量稳定性受到损害。 #Variable stabilities are measured SSI.calc.data(train_woe,test_woe,'creditability') 输出: 由于主量表的主要目的是区分风险,所以HHI测试用于测量主量表的浓度。HHI值超过0.30表示浓度高。这可能是因为处于建模阶段或主量表的创建方式不正确。 #The HHI test is performed to measure the concentration of the master scale Herfindahl.Hirschman.Index(master_scale,'Total.Observations') 输出: 0.1463665 通过“anchor.point”函数,我们测试了默认速率是否与预期水平的平均PD兼容。 #Performing the Anchor point test Anchor.point(master_scale,'PD','Total.Observations',0.30) 输出: 卡方检验也可以用作校准检验。“chi square.test”函数可用于在指定的置信度级别下执行此测试。 #The Chi-square test is applied on the master scale chisquare.test(master_scale,'PD','Bad.Count','Total.Observations',0.90) 输出: 二项式测试也可以用作校准测试。单尾二项检验通常用于IRB模型,而双尾二项检验用于IFRS 9模型。但除了IRB之外,双尾二项检验对于一般用途更方便。 #The Binomial test is applied on the master scale master_scale$DR <- master_scale$Bad.Count/master_scale$Total.Observations Binomial.test(master_scale,'Total.Observations','PD','DR',0.90,'one') 输出: 为了确保连续性,需要对建模和模型验证进行管理。当正确管理R环境时,机构可以轻松提供这种可管理的建模和验证环境。 相关机构正在使用开放源代码环境(如R或具有大数据技术的Python)设计更高效的业务流程。从这个方面来看,Credit R为建模和验证方法的应用带来了组织上的便利。 结束语 Credit R软件包为用户提供了许多执行传统信用风险评分的方法,以及一些用于测试模型有效性的方法,这些方法也可应用于ML算法。此外,由于该软件包在传统方法的应用中提供了自动化功能,因此可以降低传统过程的操作成本。 此外,可以将这些模型与机器学习模型进行比较,以证明ML模型也符合法规要求,满足这些要求是应用ML模型的前提。 |
|