Skip to content

Latest commit

 

History

184 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AdvancedAnalyticsLabs

Analytics labs notebooks, supporting analytics teaching for BSc and MSc courses. I've taught these at a business school and a statistics department, so I think they fit both reasonably well. Currently, there are 20 labs uploaded divided into five topics:

Intro to Python

  1. Introduction to Python: First few steps. Simple intro for people who might be already familiar with other languages, not meant for people with no programming experience!

  2. Functions and Revenue Management: Implementation of simple algorithms (Littlewood, EMSR-a and EMSR-b). Covers function creation and an introduction to PyPlot. Taught until 2019 in Southampton University as part of Advanced Analytics course.

Banking Regulation

  1. Basel Capital Requirements: Covers Lambda functions and an introduction to Pandas in the context of the Basel capital requirements formulas.

  2. Bond Pricing: Teaches bond pricing, yields and clean/dirty prices. Taught from 2019 at Western University, as part of the Banking Analytics course I created. Replaces Revenue Management lab above, and also covers function creation and an introduction to PyPlot.

Credit Risk Modelling

  1. Data Preprocessing: Simple data preprocessing using polars and scikit-learn.

  2. Weight of evidence transformation: How to calculate Weight of Evidence transformations in Python. It is optimized for Polars datasets, allowing for manual adjustment.

  3. Logistic Regression and Scorecards: Intro to scikit-learn, how to run a Lasso and Ridge regression, and how to calculate a scorecard. It includes larger-than-memory training using SGD.

  4. Random Forest and XGBoosting: How to run a Random Forest, an XGBoost model, tune parameters over a grid, use Shapley values to explain predictions, and compare ROC curves. It also includes larger-than-memory training.

  5. LGD Modelling: How to model LGD using either a GLM or an XGB model.

  6. PD / LGD Calibration: How to define ratings by segmenting the AUC curve and calibrate a long-run PD / downturn LGD adjusted by macroeconomic factors using the Vasicek model.

  7. Model Validation: Out-of-time validation of the PD and LGD models from the previous labs. Level 0 stability (SSI, Jensen-Shannon, Herfindahl, adversarial validation), Level 1 discrimination (DeLong AUC tests, SHAP sign check), Level 2 calibration (binomial, Jeffreys, Hosmer-Lemeshow, normal, Vasicek, Brier decomposition, Spiegelhalter), the traffic-light dashboard and action scheme, LGD backtests (t, Wilcoxon, F, Ansari-Bradley, Loterman bootstrap, gAUC, loss shortfall, MAD) and benchmarking (Spearman, Kendall, gamma, champion-challenger).

  8. Fairness in Credit Scoring: Fairness audit of a credit scoring model with Fairlearn. Independence, separation and sufficiency by group (selection rates, equalized odds, precision and calibration by group, ROC curves with the feasible region), confounding through the credit score, bootstrap, permutation and Wald tests with traffic lights, post-processing with ThresholdOptimizer, the impossibility theorem in the data, and a D-hacking demonstration. Part A is adapted from the Chapter 8 lab of Deep Learning in Banking (Bravo, Maldonado and Óskarsdóttir, 2025), written by María Óskarsdóttir.

  9. Stress Testing: The bank's own stress tests on a simulated credit portfolio, in the order of the lecture. Sensitivity tests at Level 0, 1 and 2 and the Basel-formula stress at Z = -1.75; macroeconomic scenarios through the Vasicek systematic factor with a satellite regression on GDP and unemployment; the Pillar 1 capital path; migration matrices under stress with the Z-shift; IFRS 9 lifetime PD under a scenario path, the probability-weighted ECL and the convexity gap against a Monte Carlo benchmark; SICR staging with the EBA threefold trigger, the threshold matrix and the cliff-effect curve; and a reverse stress test that returns the Z and the GDP path that use up the buffer, double the provisions, or breach the minimum.

  10. Prompt Engineering with an Open-Weight LLM: Prompting Gemma 4 E2B-it (open weights, Apache 2.0, runs on the free Colab GPU) with Hugging Face transformers. Roles and the chat template, decoding parameters (temperature, top-p, repetition penalty, seed), structured instructions, and a banking use case where the LLM turns the SHAP values of an XGBoost delinquency model into a reason statement.

Deep Learning

See our new book's for the most up-to-date versions of these labs. They are also available in the book's GitHub.

Other labs

  1. SQL Refresher: Refresher on SQL, how to access it from Python, and a very light introduction to SQLAlchemy.

  2. Primer on Visualization: A few plots using pyplot, seaborn and plotly. Very introductory primer.

  3. Explainability and Confounding: How to use the Shap package to explain XGB models and a couple of confounding factors examples. Taught as part of the DS3000 - Intro to Machine Learning course at Western.

These labs are available under the GPL v3, feel free to use them as you wish. I'll be grateful if you can point to the Github, as I'll keep these updated in subsequent iterations of the modules where I teach this. As always, these notebooks are provided with no guarantees.

Comments are welcome!

Releases

Packages

Contributors

Languages