Skip to content

Repository files navigation

📊 Expected Credit Loss (ECL) Prediction Machine


Python 3.10+ Streamlit App License: MIT

A production-grade credit risk modeling framework designed to calculate and predict Expected Credit Loss (ECL) in accordance with Basel II/III and IFRS 9 / Ind AS 109 financial reporting standards.

This repository implements a dual-layer risk evaluation pipeline trained on over 2.2 Million historical loan records, optimizing default boundaries while mitigating class imbalances and multi-collinear data leakage.

🌐 Live Interactive Interface

You can access and test the live interactive ECL Scenario Simulator here: 👉 Live Expected Credit Loss Calculator (Streamlit Sharing) (Note: If you customize your Streamlit subdomain during deployment, please update this link in your repository!)


Note

Portfolio Project Notice (Version 1.0.0): This project is structured as an iterative portfolio engine to showcase end-to-end data pipeline construction, statistical model building, and dynamic web deployment. As Version 1.0.0, it establishes the core predictive foundation. Subsequent releases will focus on advanced feature engineering, macroeconomic overlays, and reject inference models on the rejected applicant dataset.


🧠 Project Architecture & Repository Structure

To keep the repository clean and structured, the project is divided into dedicated notebooks representing sequential stages of the project lifecycle:

├── ECL_prediction/
│   ├── .gitignore                            # Standard and data-science specific git exclusions
│   ├── README.md                             # Detailed project documentation and guide
│   ├── requirements.txt                      # Project dependency definitions
│   │
│   ├── Data_Ingestion_and_Exploration.ipynb  # Step 01: Raw data acquisition via Kaggle & EDA scrap work
│   ├── ECL_Prediction_Pipeline.ipynb         # Step 02: Core model building, feature trimming, and evaluation
│   │
│   ├── app/
│   │   ├── app.py                            # Streamlit interactive scenario simulator dashboard
│   │   ├── ensemble_pd_model.joblib          # Saved model (Generated post-training)
│   │   ├── label_encoders.joblib             # Saved label encoders (Generated post-training)
│   │   └── optimized_features.joblib          # Saved feature lists (Generated post-training)

Notebook Breakdowns:

  1. Data_Ingestion_and_Exploration.ipynb: Handles automated dependency setup, Kaggle dataset downloading, file unpacking, and initial exploration of LendingClub's raw 150+ telemetry parameters.
  2. ECL_Prediction_Pipeline.ipynb: The core machine learning engine. It covers target engineering, imputation, class imbalance solutions, model training (Random Forest + HistGradientBoosting ensemble), Permutation Feature Importance, feature reduction (down to 11 high-value features), and ECL matrix forecasting.

🧮 Core Risk Theory

The standard regulatory risk equation for Expected Credit Loss is defined as:

$$\text{ECL} = \text{PD} \times \text{LGD} \times \text{EAD}$$

  1. Probability of Default (PD): Modeled using a soft-voting ensemble of RandomForestClassifier and HistGradientBoostingClassifier. Class imbalance is penalized dynamically (class_weight='balanced') to prevent the model from ignoring default cases (raising default recall from 0.00 to institutional standards).
  2. Loss Given Default (LGD): Modeled empirically based on historical recovery matrices, standardized at a standard institutional 80% loss rate post-default.I will look forward to be more precise in the further versions for these 2 features.2
  3. Exposure at Default (EAD): Modeled directly as the active outstanding loan capital (loan_amnt).

📊 Portfolio Provisioning Performance

After executing our validation engine against the testing portfolio, the framework generated the following financial provision metrics:

  • Total Outstanding Portfolio Exposure (EAD): $6,759,057,350.00
  • Total Required Capital Risk Provisions (ECL): $2,426,929,066.38
  • Portfolio Risk Provision Ratio: 35.91%
  • Validation Performance: Achieved a highly stable, generalized 0.7060 ROC-AUC validation score and 73% general accuracy despite stripping out highly collinear variables like int_rate, loan_amnt, and annual_inc.

⚙️ How to Run the Project Locally

1. Environment Setup

Clone the repository and install the required dependencies:

git clone https://github.com/b25cs1051-KUSH/ECL-Prediction-Machine.git
cd ECL-Prediction-Machine
pip install -r requirements.txt

2. Dataset Acquisition

The model relies on the LendingClub dataset. You can download it automatically through the first notebook or run the following Kaggle API commands:

pip install kaggle
kaggle datasets download -d wordsforthewise/lending-club --unzip

Note: Place the accepted_2007_to_2018Q4.csv.gz file directly into the repository root. All data files are excluded in .gitignore to prevent committing gigabyte-scale datasets.

3. Model Training & Export

Open the main pipeline notebook ECL_Prediction_Pipeline.ipynb in Jupyter and run all cells. The final cell will train the optimized ensemble engine and serialize the artifacts:

  • app/ensemble_pd_model.joblib
  • app/label_encoders.joblib
  • app/optimized_features.joblib

4. Running the Interactive Streamlit App

Launch the interactive web simulator dashboard to test borrower scenarios:

streamlit run app/app.py

Interactive Calculator: The app runs in a realistic Demo Heuristic Mode by default. Once you run the training pipeline and export the joblib files, it automatically transitions into a Live ML Inference Mode, querying your actual trained model in real time!


🔮 Version 1 Notes & Future Roadmap

This is Version 1.0.0 of the ECL Prediction Machine.

Handling the "Rejected Applications" Dataset

You may notice rejected_2007_to_2018Q4.csv.gz in the LendingClub data download. In this first version, we have focused exclusively on accepted_2007_to_2018Q4.csv.gz to model the default probability of active borrowers. Pushing rejected loans directly into this model is not feasible because they lack a target variable (they were never issued, so they could never "default" or "pay back").

However, the rejected dataset is highly valuable for the next phase:

  • Reject Inference: We can model selection bias (the fact that we only train default models on borrowers who were accepted) using Heckman’s two-stage correction.
  • Credit Decisioning Classifier: Training a classifier on accepted vs. rejected applications to model the bank's automated approval boundaries.

Feature Engineering Roadmap

For Version 2.0, we will shift focus from pure feature exclusion to deep feature engineering:

  • Bureau Telemetry Interaction: Engineering credit utilisation ratios (revol_bal / total credit limits) and inquiry density.
  • Macroeconomic Overlay: Injecting interest rate cycle indicators and GDP growth trends to model "Point-in-Time" (PIT) PD instead of purely historical "Through-the-Cycle" (TTC) PD.
  • Employment Length Encoding: Converting categorical string categories into numerical representations reflecting occupational stability.

🛠️ Tech Stack

  • Languages & Core: Python 3.10+, HTML, CSS
  • Data Science: pandas, numpy, scikit-learn, joblib
  • Exploratory Data Analysis: ydata-profiling / fg-data-profiling
  • Web App: Streamlit
  • Visualizations: matplotlib, seaborn
  • Formulas: LaTeX / KaTeX rendering

🏷️ Repository Topics

credit-riskmachine-learningfintechifrs9basel-iiiprobability-of-defaultensemble-learningfinancial-engineeringdata-science


📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

About

An enterprise-grade Expected Credit Loss (ECL) engine built on 2.2M+ loan records. Utilizes advanced machine learning ensembles (Random Forest & Gradient Boosting) to model Probability of Default (PD) in strict compliance with Basel II/III and IFRS 9 financial risk standards.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages