Predicting the probability of default

Credit Risk Modeling in Python

Michael Crabtree

Data Scientist, Ford Motor Company

Logistic regression coefficients

# Model Intercept
array([-3.30582292e-10])
# Coefficients for ['loan_int_rate','person_emp_length','person_income']
array([[ 1.28517496e-09, -2.27622202e-09, -2.17211991e-05]])

Formula for probability of default using logistic regression

# Calculating probability of default
int_coef_sum = -3.3e-10 +
    (1.29e-09 * loan_int_rate) + (-2.28e-09 * person_emp_length) + (-2.17e-05 * person_income)
prob_default = 1 / (1 + np.exp(-int_coef_sum))
prob_nondefault = 1 - (1 / (1 + np.exp(-int_coef_sum)))
Credit Risk Modeling in Python

Interpreting coefficients

# Intercept
intercept = -1.02
# Coefficient for employment length
person_emp_length_coef = -0.056
  • For every 1 year increase in person_emp_length, the person is less likely to default
Credit Risk Modeling in Python

Interpreting coefficients

# Intercept
intercept = -1.02
# Coefficient for employment length
person_emp_length_coef = -0.056
  • For every 1 year increase in person_emp_length, the person is less likely to default
intercept person_emp_length value * coef probability of default
-1.02 10 (10 * -0.06) .17
-1.02 11 (11 * -0.06) .16
-1.02 12 (12 * -0.06) .15
Credit Risk Modeling in Python

Using non-numeric columns

  • Numeric: loan_int_rate, person_emp_length, person_income

  • Non-numeric:

    cr_loan_clean['loan_intent']
    
EDUCATION            
MEDICAL              
VENTURE              
PERSONAL             
DEBTCONSOLIDATION   
HOMEIMPROVEMENT
  • Will cause errors with machine learning models in Python unless processed
Credit Risk Modeling in Python

One-hot encoding

  • Represent a string with a number

Example of loan intent in the data

Credit Risk Modeling in Python

One-hot encoding

  • Represent a string with a number
  • 0 or 1 in a new column column_VALUE

Example of one-hot encoding the loan intent column

Credit Risk Modeling in Python

Get dummies

  • Utilize the get_dummies() within pandas
# Separate the numeric columns
cred_num = cr_loan.select_dtypes(exclude=['object'])
# Separate non-numeric columns
cred_cat = cr_loan.select_dtypes(include=['object'])
# One-hot encode the non-numeric columns only
cred_cat_onehot = pd.get_dummies(cred_cat)
# Union the numeric columns with the one-hot encoded columns
cr_loan = pd.concat([cred_num, cred_cat_onehot], axis=1)
Credit Risk Modeling in Python

Predicting the future, probably

  • Use the .predict_proba() method within scikit-learn
# Train the model
clf_logistic.fit(X_train, np.ravel(y_train))
# Predict using the model
clf_logistic.predict_proba(X_test)
  • Creates array of probabilities of default
# Probabilities: [[non-default, default]]
array([[0.55, 0.45]])
Credit Risk Modeling in Python

Let's practice!

Credit Risk Modeling in Python

Preparing Video For Download...