by Alejandro Correa Bahnsen & Iván Torroledo
version 1.2, Feb 2018
This notebook is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
In order to mitigate the impact of credit risk and make more objective and accurate decisions, financial institutions use credit scores to predict and control their losses. The objective in credit scoring is to classify which potential customers are likely to default a contracted financial obligation based on the customer's past financial experience, and with that information decide whether to approve or decline a loan [1]. This tool has become a standard practice among financial institutions around the world in order to predict and control their loans portfolios. When constructing credit scores, it is a common practice to use standard cost-insensitive binary classification algorithms such as logistic regression, neural networks, discriminant analysis, genetic programing, decision trees, among others [2,3].
Formally, a credit score is a statistical model that allows the estimation of the probability ˆpi=P(yi=1|xi) of a customer i defaulting a contracted debt. Additionally, since the objective of credit scoring is to estimate a classifier ci to decide whether or not to grant a loan to a customer i, a threshold t is defined such that if ˆpi<t, then the loan is granted, i.e., ci(t)=0, and denied otherwise, i.e., ci(t)=1.
Improve on the state of the art in credit scoring by predicting the probability that somebody will experience financial distress in the next two years.
https://www.kaggle.com/c/GiveMeSomeCredit
import pandas as pd
import numpy as np
from costcla.datasets import load_creditscoring1
data = load_creditscoring1()
# Elements of the data file
print(data.keys())
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20. "This module will be removed in 0.20.", DeprecationWarning)
dict_keys(['data', 'target', 'cost_mat', 'target_names', 'DESCR', 'feature_names', 'name'])
pd.DataFrame(data.data, columns=data.feature_names).head()
RevolvingUtilizationOfUnsecuredLines | age | NumberOfTime30-59DaysPastDueNotWorse | DebtRatio | MonthlyIncome | NumberOfOpenCreditLinesAndLoans | NumberOfTimes90DaysLate | NumberRealEstateLoansOrLines | NumberOfTime60-89DaysPastDueNotWorse | NumberOfDependents | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 0.766127 | 45.0 | 2.0 | 0.802982 | 9120.0 | 13.0 | 0.0 | 6.0 | 0.0 | 2.0 |
1 | 0.957151 | 40.0 | 0.0 | 0.121876 | 2600.0 | 4.0 | 0.0 | 0.0 | 0.0 | 1.0 |
2 | 0.658180 | 38.0 | 1.0 | 0.085113 | 3042.0 | 2.0 | 1.0 | 0.0 | 0.0 | 0.0 |
3 | 0.233810 | 30.0 | 0.0 | 0.036050 | 3300.0 | 5.0 | 0.0 | 0.0 | 0.0 | 0.0 |
4 | 0.907239 | 49.0 | 1.0 | 0.024926 | 63588.0 | 7.0 | 0.0 | 1.0 | 0.0 | 0.0 |
# Class label
target = pd.DataFrame(pd.Series(data.target).value_counts(), columns=('Frequency',))
target['Percentage'] = target['Frequency'] / target['Frequency'].sum()
target.index = ['Negative (Good Customers)', 'Positive (Bad Customers)']
print(target)
Frequency Percentage Negative (Good Customers) 105299 0.932551 Positive (Bad Customers) 7616 0.067449
# Full description of the dataset
# print data.DESCR
# Number of features
pd.DataFrame(data.feature_names, columns=('Features',))
Features | |
---|---|
0 | RevolvingUtilizationOfUnsecuredLines |
1 | age |
2 | NumberOfTime30-59DaysPastDueNotWorse |
3 | DebtRatio |
4 | MonthlyIncome |
5 | NumberOfOpenCreditLinesAndLoans |
6 | NumberOfTimes90DaysLate |
7 | NumberRealEstateLoansOrLines |
8 | NumberOfTime60-89DaysPastDueNotWorse |
9 | NumberOfDependents |
Using three classifiers, a model is learned to classify customers in good and bad
# Load classifiers and split dataset in training and testing
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test, cost_mat_train, cost_mat_test = \
train_test_split(data.data, data.target, data.cost_mat)
# Fit the classifiers using the training dataset
classifiers = {"RF": {"f": RandomForestClassifier()},
"DT": {"f": DecisionTreeClassifier()},
"LR": {"f": LogisticRegression()}}
for model in classifiers.keys():
# Fit
classifiers[model]["f"].fit(X_train, y_train)
# Predict
classifiers[model]["c"] = classifiers[model]["f"].predict(X_test)
classifiers[model]["p"] = classifiers[model]["f"].predict_proba(X_test)
classifiers[model]["p_train"] = classifiers[model]["f"].predict_proba(X_train)
After the classifier ci is estimated, there is a need to evaluate its performance. In practice, many statistical evaluation measures are used to assess the performance of a credit scoring model. Measures such as the area under the receiver operating characteristic curve (AUC), Brier score, Kolmogorov-Smirnoff (K-S) statistic, F1-Score, and misclassification are among the most common [4].
# Evaluate the performance
from sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score
measures = {"f1": f1_score, "pre": precision_score,
"rec": recall_score, "acc": accuracy_score}
results = pd.DataFrame(columns=measures.keys())
# Evaluate each model in classifiers
for model in classifiers.keys():
results.loc[model] = [measures[measure](y_test, classifiers[model]["c"]) for measure in measures.keys()]
results
f1 | pre | rec | acc | |
---|---|---|---|---|
RF | 0.221516 | 0.484375 | 0.143592 | 0.930532 |
DT | 0.244554 | 0.235575 | 0.254246 | 0.891884 |
LR | 0.022189 | 0.550000 | 0.011323 | 0.931312 |
# Plot the results
%matplotlib inline
from IPython.core.pylabtools import figsize
import matplotlib.pyplot as plt
plt.style.use('ggplot')
figsize(10, 5)
ax = plt.subplot(111)
ind = np.arange(results.shape[0])
width = 0.2
l = ax.plot(ind, results, "-o")
plt.legend(iter(l), results.columns.tolist(), loc='center left', bbox_to_anchor=(1, 0.5))
ax.set_xlim([-0.25, ind[-1]+.25])
ax.set_xticks(ind)
ax.set_xticklabels(results.index)
plt.show()
Nevertheless, none of these measures takes into account the business and economical realities that take place in credit scoring. Costs that the financial institution had incurred to acquire customers, or the expected profit due to a particular client, are not considered in the evaluation of the different models.
Typically, a credit risk model is evaluated using standard cost-insensitive measures. However, in practice, the cost associated with approving what is known as a bad customer, i.e., a customer who default his credit loan, is quite different from the cost associated with declining a good customer, i.e., a customer who successfully repay his credit loan. Furthermore, the costs are not constant among customers. This is because loans have different credit line amounts, terms, and even interest rates. Some authors have proposed methods that include the misclassification costs in the credit scoring context [4,5,6,7].
In order to take into account the varying costs that each example carries, we proposed in [8], a cost matrix with example-dependent misclassification costs as given in the following table.
| | Actual Positive (yi=1) | Actual Negative (yi=0)| |--- |:-: |:-: | | Predicted Positive (ci=1) | CTPi=0 | CFPi=ri+CaFP | | Predicted Negative (ci=0) | CFNi=Cli⋅Lgd | CTNi=0 |
First, we assume that the costs of a correct classification, CTPi and CTNi, are zero for every customer i. We define CFNi to be the losses if the customer i defaults to be proportional to his credit line Cli. We define the cost of a false positive per customer CFPi as the sum of two real financial costs ri and CaFP, where ri is the loss in profit by rejecting what would have been a good customer.
The profit per customer ri is calculated as the present value of the difference between the financial institution gains and expenses, given the credit line Cli, the term li and the financial institution lending rate intri for customer i, and the financial institution of cost funds intcf.
ri=PV(A(Cli,intri,li),intcf,li)−Cli,
with A being the customer monthly payment and PV the present value of the monthly payments, which are calculated using the time value of money equations [9],
A(Cli,intri,li)=Cliintri(1+intri)li(1+intri)li−1,
PV(A,intcf,li)=Aintcf(1−1(1+intcf)li).
The second term CaFP, is related to the assumption that the financial institution will not keep the money of the declined customer idle. It will instead give a loan to an alternative customer [10]. Since no further information is known about the alternative customer, it is assumed to have an average credit line ¯Cl and an average profit ¯r. Given that,
CaFP=−¯r⋅π0+¯Cl⋅Lgd⋅π1,
in other words minus the profit of an average alternative customer plus the expected loss, taking into account that the alternative customer will pay his debt with a probability equal to the prior negative rate, and similarly will default with probability equal to the prior positive rate.
One key parameter of our model is the credit limit. There exists several strategies to calculate the Cli depending on the type of loans, the state of the economy, the current portfolio, among others [1,9]. Nevertheless, given the lack of information regarding the specific business environments of the considered datasets, we simply define Cli as
Cli=min{q⋅Inci,Clmax,Clmax(debti)},where Inci and debti are the monthly income and debt ratio of the customer i, respectively, q is a parameter that defines the maximum Cli in times Inci, and Clmax the maximum overall credit line. Lastly, the maximum credit line given the current debt is calculated as the maximum credit limit such that the current debt ratio plus the new monthly payment does not surpass the customer monthly income. It is calculated as
Clmax(debti)=PV(Inci⋅Pm(debti),intri,li),
Let S be a set of N examples i, N=|S|, where each example is
represented by the augmented feature vector
x∗i=[xi,CTPi,CFPi,CFNi,CTNi]
and labeled using the class label yi∈{0,1}.
A classifier f which generates the predicted label ci for each element i is trained
using the set S. Then the cost of using f on S is calculated by
Cost(f(S))=N∑i=1Cost(f(x∗i)),
where
Cost(f(x∗i))=yi(ciCTPi+(1−ci)CFNi)+(1−yi)(ciCFPi+(1−ci)CTNi).
However, the total cost may not be easy to interpret. We proposed an approach in [8], where the savings of using an algorithm are defined as the cost of the algorithm versus the cost of using no algorithm at all. To do that, the cost of the costless class is defined as
Costl(S)=min{Cost(f0(S)),Cost(f1(S))},
where
fa(S)=a, with a∈{0,1}.
The cost improvement can be expressed as the cost savings as compared with Costl(S).
Savings(f(S))=Costl(S)−Cost(f(S))Costl(S).
As this database contain information regarding the features, and more importantly about the income of each example, from which an estimated credit limit Cli can be calculated. Since no specific information regarding the datasets is provided, we assume that they belong to an average Portuguese financial institution. This enabled us to find the different parameters needed to calculate the cost measure.
| Parameter | Value | |--- |:-: | |Interest rate (intr) | 4.79% | | Cost of funds (intcf) | 2.94% | | Term (l) in months | 24 | | Loss given default (Lgd) | 75% | | Times income (q) | 3 | | Maximum credit line (Clmax) | 25,000|
In particular, we obtain the average interest rates in Europe during 2013 from the European Central Bank [11]. Additionally, we use a fixed loan term l to two years, considering that in the Kaggle Credit dataset the class was constructed to predict two years of credit behavior. Moreover, we set the loss given default Lgd using information from the Basel II standard, q to 3 since it is the average personal loan requests related to monthly income, and the maximum credit limit Clmax to 25,000 Euros.
# The cost matrix is already calculated for the dataset
# cost_mat[C_FP,C_FN,C_TP,C_TN]
print(data.cost_mat[[10, 17, 50]])
[[ 1023.73054104 18750. 0. 0. ] [ 717.25781516 6749.25 0. 0. ] [ 866.65393177 12599.25 0. 0. ]]
# Calculation of the cost and savings
from costcla.metrics import savings_score, cost_loss
# Evaluate the savings for each model
results["sav"] = np.zeros(results.shape[0])
for model in classifiers.keys():
results["sav"].loc[model] = savings_score(y_test, classifiers[model]["c"], cost_mat_test)
# TODO: plot results
print(results)
f1 pre rec acc sav RF 0.221516 0.484375 0.143592 0.930532 0.126101 DT 0.244554 0.235575 0.254246 0.891884 0.177077 LR 0.022189 0.550000 0.011323 0.931312 0.005574
# Plot the results
figsize(10, 5)
ax = plt.subplot(111)
l = ax.plot(ind, results["f1"], "-o", label='F1Score', c='C1')
b = ax.bar(ind, results['sav'], 0.6, label='Savings')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
ax.set_xlim([-0.5, ind[-1]+.5])
ax.set_xticks(ind)
ax.set_xticklabels(results.index)
plt.show()
There are significant differences in the results when evaluating a model using a traditional cost-insensitive measure such as the accuracy or F1Score, than when using the savings, leading to the conclusion of the importance of using the real practical financial costs of each context.
As these methods (RF, LR and DT) are not performing well we then move to use cost-sensitive methods. The first model we used is the Bayes minimum risk model (BMR) [8]. As defined in [12], the BMR classifier is a decision model based on quantifying tradeoffs between various decisions using probabilities and the costs that accompany such decisions. This is done in a way that for each example the expected losses are minimized. In what follows, we consider the probability estimates ˆpi as known, regardless of the algorithm used to calculate them. The risk that accompanies each decision is calculated using the cost matrix described above. In the specific framework of binary classification, the risk of predicting the example i as negative is
R(ci=0|xi)=CTNi(1−ˆpi)+CFNi⋅ˆpi,and R(ci=1|xi)=CTPi⋅ˆpi+CFPi(1−ˆpi),
is the risk when predicting the example as positive, where ˆpi is the estimated positive probability for example i. Subsequently, if
R(ci=0|xi)≤R(ci=1|xi),then the example i is classified as negative. This means that the risk associated with the decision ci is lower than the risk associated with classifying it as positive.
from costcla.models import BayesMinimumRiskClassifier
ci_models = list(classifiers.keys())
for model in ci_models:
classifiers[model+"-BMR"] = {"f": BayesMinimumRiskClassifier()}
# Fit
classifiers[model+"-BMR"]["f"].fit(y_test, classifiers[model]["p"])
# Calibration must be made in a validation set
# Predict
classifiers[model+"-BMR"]["c"] = classifiers[model+"-BMR"]["f"].predict(classifiers[model]["p"], cost_mat_test)
# Evaluate
results.loc[model+"-BMR"] = 0
results.loc[model+"-BMR", measures.keys()] = \
[measures[measure](y_test, classifiers[model+"-BMR"]["c"]) for measure in measures.keys()]
results["sav"].loc[model+"-BMR"] = savings_score(y_test, classifiers[model+"-BMR"]["c"], cost_mat_test)
results
f1 | pre | rec | acc | sav | |
---|---|---|---|---|---|
RF | 0.221516 | 0.484375 | 0.143592 | 0.930532 | 0.126101 |
DT | 0.244554 | 0.235575 | 0.254246 | 0.891884 | 0.177077 |
LR | 0.022189 | 0.550000 | 0.011323 | 0.931312 | 0.005574 |
RF-BMR | 0.277842 | 0.179456 | 0.615028 | 0.779943 | 0.399326 |
DT-BMR | 0.130096 | 0.076634 | 0.430262 | 0.603953 | 0.077424 |
LR-BMR | 0.170002 | 0.100082 | 0.564076 | 0.620886 | 0.188954 |
# Plot the results
ind = np.arange(results.shape[0])
figsize(10, 5)
ax = plt.subplot(111)
l = ax.plot(ind, results["f1"], "-o", label='F1Score', c='C1')
b = ax.bar(ind, results['sav'], 0.6, label='Savings')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
ax.set_xlim([-0.5, ind[-1]+.5])
ax.set_xticks(ind)
ax.set_xticklabels(results.index)
plt.show()
The next algorithm that is evaluated is the cost-sensitive decision trees algorithm [13].
Decision trees are one of the most widely used machine learning algorithms [14]. The technique is considered to be white box, in the sense that is easy to interpret, and has a very low computational cost, while maintaining a good performance as compared with more complex techniques [15]. There are two types of decision tree depending on the objective of the model. They work either for classification or regression.
Classification trees is one of the most common types of decision tree, in which the objective is to find the Tree that best discriminates between classes. In general the decision tree represents a set of splitting rules organized in levels in a flowchart structure.
In the Tree, each rule is shown as a node, and it is represented as (xj,lj), meaning that the set S is split in two sets Sl and Sr according to xj and lj:
Sl={x∗i|x∗i∈S∧xji≤lj},andSr={x∗i|x∗i∈S∧xji>lj},where xj is the jth feature represented in the vector xj=[xj1,xj2,...,xjN], and lj is a value such that min(xj)≤lj<max(xj). Moreover, S=Sl∪Sr.
After the training set have been split, the percentage of positives in the different sets is calculated. First, the number of positives in each set is estimated by
S1={x∗i|x∗i∈S∧yi=1},and the percentage of positives is calculates as π1=|S1|/|S|.
Then, the impurity of each leaf is calculated using either a misclassification error, entropy or Gini measures:
a) Misclassification: Im(π1)=1−max(π1,1−π1)
b) Entropy: Ie(π1)=−π1logπ1−(1−π1)log(1−π1)
c) Gini: Ig(π1)=2π1(1−π1)
Finally the gain of the splitting criteria using the rule (xj,lj) is calculated as the impurity of S minus the weighted impurity of each leaf:
Gain(xj,lj)=I(π1)−|Sl||S|I(πl1)−|Sr||S|I(πr1),where I(π1) can be either of the impurity measures Ie(π1) or Ig(π1).
Subsequently, the gain of all possible splitting rules is calculated. The rule with maximal gain is selected
(bestx,bestl)=argmax(xj,lj)Gain(xj,lj),and the set S is split into Sl and Sr according to that rule.
In order to grow a tree typical algorithms use a top-down induction using a greedy search in each iteration [16]. In each iteration, the algorithms evaluates all possible splitting rules and pick the one that maximizes the splitting criteria. After the selection of a splitting rule, each leaf is further selected and it is subdivides into smaller leafs, until one of the stopping criteria is meet.
Standard impurity measures such as misclassification, entropy or Gini, take into account the distribution of classes of each leaf to evaluate the predictive power of a splitting rule, leading to an impurity measure that is based on minimizing the misclassification rate. However, as has been previously shown [8], minimizing misclassification does not lead to the same results than minimizing cost. Instead, we are interested in measuring how good is a splitting rule in terms of cost not only accuracy. For doing that, we propose a new example-dependent cost based impurity measure that takes into account the cost matrix of each example.
We define a new cost-based impurity measure taking into account the costs when all the examples in a leaf are classified both as negative using f0 and positive using f1
Ic(S)=min{Cost(f0(S)),Cost(f1(S))}.The objective of this measure is to evaluate the lowest expected cost of a splitting rule. Following the same logic, the classification of each set is calculated as the prediction that leads to the lowest cost
f(S)={−0−if−Cost(f0(S))≤Cost(f1(S))−1−otherwiseFinally, using the cost-based impurity, the splitting criteria cost based gain of using the splitting rule (xj,lj) is calculated.
from costcla.models import CostSensitiveDecisionTreeClassifier
classifiers["CSDT"] = {"f": CostSensitiveDecisionTreeClassifier()}
# Fit
classifiers["CSDT"]["f"].fit(X_train, y_train, cost_mat_train)
# Predict
classifiers["CSDT"]["c"] = classifiers["CSDT"]["f"].predict(X_test)
# Evaluate
results.loc["CSDT"] = 0
results.loc["CSDT", measures.keys()] = \
[measures[measure](y_test, classifiers["CSDT"]["c"]) for measure in measures.keys()]
results["sav"].loc["CSDT"] = savings_score(y_test, classifiers["CSDT"]["c"], cost_mat_test)
results
f1 | pre | rec | acc | sav | |
---|---|---|---|---|---|
RF | 0.221516 | 0.484375 | 0.143592 | 0.930532 | 0.126101 |
DT | 0.244554 | 0.235575 | 0.254246 | 0.891884 | 0.177077 |
LR | 0.022189 | 0.550000 | 0.011323 | 0.931312 | 0.005574 |
RF-BMR | 0.277842 | 0.179456 | 0.615028 | 0.779943 | 0.399326 |
DT-BMR | 0.130096 | 0.076634 | 0.430262 | 0.603953 | 0.077424 |
LR-BMR | 0.170002 | 0.100082 | 0.564076 | 0.620886 | 0.188954 |
CSDT | 0.276488 | 0.167133 | 0.799794 | 0.711892 | 0.480551 |
# Plot the results
ind = np.arange(results.shape[0])
figsize(10, 5)
ax = plt.subplot(111)
l = ax.plot(ind, results["f1"], "-o", label='F1Score', c='C1')
b = ax.bar(ind, results['sav'], 0.6, label='Savings')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
ax.set_xlim([-0.5, ind[-1]+.5])
ax.set_xticks(ind)
ax.set_xticklabels(results.index)
plt.show()
CostCla is a easy to use Python library for example-dependent cost-sensitive classification problems. It includes many example-dependent cost-sensitive algorithms. Since it is part of the scientific Python ecosystem, it can be easily integrated with other machine learning libraries. Future work includes adding more cost-sensitive databases and algorithms, and support for Python ≥ 3.4.