Financial Calculations & Valuation Intermediate

Basel III Credit Risk Expected Loss (EL = PD x LGD x EAD) & Risk-Weighted Assets in Python

Calculate loan portfolio Expected Loss (EL), Unexpected Loss (UL), and Credit Risk-Weighted Assets across borrower rating grades using PD, LGD, and EAD in Python.

#pandas#numpy

Problem Statement

Bank risk officers and fintech lenders need to quantify Expected Loss (provisioned via pricing) and Unexpected Loss (buffered by capital) across retail and corporate loan books.

Formula / Core Logic

Expected Loss (EL) = PD * LGD * EAD Unexpected Loss (UL) = EAD * sqrt(PD * (1 - PD) * LGD^2)

Installation

pip install pandas numpy
credit-risk-expected-loss-pd-lgd-ead-basel-python.py
import pandas as pd
import numpy as np

def calculate_credit_expected_loss(df):
    # Expected cols: Loan_ID, EAD, PD_Pct, LGD_Pct
    df = df.copy()
    pd_dec = df['PD_Pct'] / 100.0
    lgd_dec = df['LGD_Pct'] / 100.0
    
    df['Expected_Loss'] = (df['EAD'] * pd_dec * lgd_dec).round(2)
    df['Unexpected_Loss'] = (df['EAD'] * np.sqrt(pd_dec * (1 - pd_dec) * (lgd_dec ** 2))).round(2)
    return df, {'Total_Portfolio_EL': round(df['Expected_Loss'].sum(), 2), 'Total_Portfolio_UL': round(df['Unexpected_Loss'].sum(), 2)}

Get the Complete 100+ Python Quant & Automation Bundle

Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).

Buy Full Bundle on Gumroad ($19) →

Frequently Asked Questions

Q: How is LGD (Loss Given Default) estimated for collateralized loans?

LGD = 1 - Recovery Rate, where Recovery Rate accounts for distressed collateral haircut and legal enforcement timelines.