Practice · browser-run Python

Runnable credit-risk Python examples

Run every published sample, inspect its output, and edit the code. Examples use synthetic data and execute in your browser; your code and inputs are not uploaded to the site.

Scorecards and probability of default

Reproduce WOE and IV, convert a logistic score to PD, and build a cumulative PD term structure from conditional hazards.

Runnable Python · local in your browser

Calculate WOE and IV from grouped counts

Use synthetic good and bad counts for three ordered scorecard bins. The 0.5 adjustment avoids a zero distribution in this small example.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

Runnable Python · local in your browser

Convert a logistic model logit to PD

Apply the logistic function to an illustrative intercept plus two feature contributions.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

Runnable Python · local in your browser

Calculate year-by-year marginal and cumulative PD

Follow years 1–3 separately: conditional PD is the rate among survivors, marginal PD is the share of the original cohort defaulting in that year, and cumulative PD adds those marginal shares.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

LGD, EAD, and IFRS 9 ECL

Discount recovery cash flows, estimate undrawn exposure, and combine survival-weighted losses across scenarios.

Runnable Python · local in your browser

Discount recovery cash flows for workout LGD

Discount synthetic recoveries and costs to default date and calculate a capped loss fraction.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

Runnable Python · local in your browser

Estimate revolving EAD with a CCF

Apply a hypothetical conversion factor to the currently undrawn facility limit.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

Runnable Python · local in your browser

Calculate survival-weighted, discounted ECL

Combine period marginal PD, LGD, EAD, discount factors, and two scenario weights in a small illustrative horizon.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

Feature engineering

Build numeric, categorical, and date features with training-only fit rules, explicit missingness handling, fixed encoding columns, and a clear point-in-time cutoff.

Runnable Python · local in your browser

Engineer numeric credit-risk features

Use a training-only median, explicit missingness flags, a log transform, a payment-to-income ratio, and a delinquency indicator. Amounts are synthetic CAD; transforms are illustrative and should be validated for the intended population.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

Runnable Python · local in your browser

Encode categorical variables with stable columns

Create one-hot features using a declared category schema, with separate missing and other buckets so unknown values do not shift the model columns.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

Runnable Python · local in your browser

Build point-in-time date features

Derive account age and days since the last payment relative to a fixed scoring date. Future-dated source values fail closed instead of leaking into features.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

Data preparation, validation, and monitoring

Check point-in-time leakage, rank and calibrate PD predictions, measure distribution drift, and summarize portfolio outcomes.

Runnable Python · local in your browser

Detect a point-in-time feature leak

A miniature data preparation check compares each feature's observation date with the prediction date.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

Runnable Python · local in your browser

Monitor observed-to-expected and Brier score

Compare a small set of matured binary outcomes against predicted PD values.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

Runnable Python · local in your browser

Calculate ROC-AUC, Gini, KS, precision, and recall

Use synthetic out-of-sample scores and matured labels. Higher scores indicate higher risk; the example computes pairwise ROC-AUC and a threshold-specific operating point.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

Runnable Python · local in your browser

Compare expected defaults, Brier score, and log loss

Review several probability metrics on the same synthetic, matured population. Small samples are noisy; this output is not an alert rule.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

Runnable Python · local in your browser

Calculate PSI, feature drift, and missingness

Compare synthetic reference and current bin counts with a small smoothing constant. Inspect the bins and data-quality indicators as well as the summary index.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

Runnable Python · local in your browser

Summarize rating migration, arrears roll, and recovery LGD

Use synthetic account paths to calculate grade transitions, a delinquency roll rate, and a simple discounted recovery outcome.

Output

Run the code to see the output.

Runtime loads only after you press Run. Code runs in a separate browser worker with a 10-second limit and a 20,000-character output cap. Use synthetic examples only; browser execution is not a secure environment for secrets or sensitive records.

These samples illustrate methods; they are not production model code or accounting advice. Confirm target definitions, horizons, data quality, and institutional policy before applying a method.