Estimate a logistic regression model to predict credit default.
Solution
Note: You will initially encounter a ConvergenceWarning, indicating that the maximum likelihood estimation has not fully converged. To address this, increase the max_iter parameter to give the optimization algorithm sufficient iterations to converge. In practice, scaling predictors can also improve convergence and model stability.
import pandas as pdcoef_df = pd.DataFrame({"feature": X.columns, "coefficient": model.coef_[0]})coef_df
feature
coefficient
0
income
-0.000517
1
debt_to_income
0.044377
2
missed_payments_12m
1.061270
3
credit_utilization
1.155647
4
months_with_company
-0.016245
NoteNote on interpretation
The coefficients shown here indicate the direction and relative strength of relationships. However, models from scikit-learn do not provide p-values or standard errors.
This means we cannot assess statistical significance directly. In applied research, p-values (e.g., from statsmodels) are typically used to evaluate whether effects are statistically reliable.
Therefore, the interpretation below focuses on magnitude and direction, not statistical significance.
Which variables increase the likelihood of default?
TipSolution
Variables with positive coefficients increase default risk.
In this model, the strongest positive effects are:
credit_utilization (1.16) → the strongest increase in default risk
missed_payments_12m (1.06) → substantial increase in default risk
Which variables decrease it?
TipSolution
Variables with negative coefficients decrease default risk.
In this model:
months_with_company (-0.02) → small decrease
income (-0.00) → very small (negligible) decrease
Which variable appears to have the strongest relationship with default?
TipSolution
Among the predictors, credit_utilization has the largest coefficient in absolute terms, followed closely by missed_payments_12m. This suggests that both variables are strongly associated with default risk in this model.
However, this comparison should be interpreted with caution. Because the predictors are measured on different scales (e.g., proportions, counts, income levels), the magnitude of coefficients is not directly comparable. A more rigorous comparison would require standardized variables or an analysis based on meaningful unit changes (e.g., odds ratios).
Task 1.4 — From coefficients to classification
Explain how the model moves from:
a linear combination of predictors
to a predicted probability
to a classification as default / non-default
Solution
Steps:
Linear combination (log-odds)
Logistic transformation → probability
Threshold (e.g., 0.5) → class label
# Example for one observationz = model.intercept_[0] + (X.iloc[0] * model.coef_[0]).sum()import numpy as npp =1/ (1+ np.exp(-z))# Apply thresholdy_class =int(p >=0.5)p, y_class
(np.float64(0.4677867020758892), 0)
With a predicted probability of 46.8%, the observation would be classified as “no default” under a 50% threshold.
For simplicity, model performance is evaluated on the same data used for estimation. In practice, a train/test split or cross-validation should be used.
Task 3.1 — Interpret the matrix
Solution
from sklearn.metrics import confusion_matrixcm = confusion_matrix(y, y_pred)cm
array([[94, 19],
[36, 44]])
TP: 44
TN: 94
FP: 19
FN: 36
Note: the ravel() function can be helpful to access the individual values:
Some predictors matter in both groups, but their strength differs.
For both early-career urban borrowers and platform workers, credit utilization is a strong positive predictor of default, indicating that higher usage of available credit increases risk in both groups.
Income and months with company have small negative effects in both groups, suggesting slightly lower default risk with higher income and longer tenure, though these effects are minor.
Key differences emerge for debt-to-income and missed payments. For platform workers, debt-to-income is much more influential, indicating that overall debt burden plays a larger role. In contrast, for early-career urban borrowers, missed payments are more important, suggesting recent repayment behavior is a stronger signal of default risk.
Task 5.3 — Compare ROC and AUC
Compare the ROC curves and AUC values of the two models.
The model for early-career urban borrowers achieves a higher AUC (~0.80) than the model for platform workers (~0.72).
This indicates that defaults are easier to classify for early-career urban borrowers, as the model can better distinguish between defaulters and non-defaulters.
A possible explanation is that risk patterns are more clear and consistent among early-career urban borrowers (e.g., strong signals like missed payments). In contrast, platform workers may have more heterogeneous or less stable financial situations, making default harder to predict with the available variables.