Skip to content

Scenario-Conditioned Loss Forecasting Tutorial

This guide shows how to run baseline, adverse, and severe loss forecasts side by side using time-varying transition matrices, reversion to long-run averages, scenario-specific LGD stress paths, probability weights, and a macroeconomic link hook.

Problem

A single static transition matrix produces one point-in-time reserve estimate. In credit risk management and loss forecasting, you typically need to:

  • derive stressed transition matrices from explicit, auditable levers rather than hand-editing individual cells
  • hold a stressed forecast for a reasonable-and-supportable (R&S) window, then revert smoothly to long-run historical averages
  • stress loss severity (LGD) alongside roll rates, including per-month LGD paths
  • combine multiple named scenarios with probability weights into one tidy table
  • plug in a macroeconomic link mapping economic paths (for example, unemployment) to monthly transition matrices

1. Baseline, Adverse, and Severe Scenarios with Reversion

Start from load_sample_transition_matrix() as the long-run baseline, derive adverse and severe matrices using build_stressed_transition_matrix(...), and revert each stressed matrix back to the long-run baseline after a 12-month R&S window using 6-month straight-line reversion.

import pandas as pd

from cranalytics import (
    load_sample_transition_matrix,
    make_mock_portfolio,
)
from cranalytics.scenarios import (
    build_reversion_schedule,
    build_stressed_transition_matrix,
    run_loss_scenarios,
    summarize_loss_scenarios,
)

portfolio = make_mock_portfolio(n_loans=250, seed=42)
as_of_date = pd.Timestamp("2024-01-01")
horizon = 36

# Long-run average transition matrix (Current, Delinquent, Charged Off, Paid Off)
long_run = load_sample_transition_matrix()

# Explicit stress levers: forward-roll multiplier, cure-rate haircut, prepayment slowdown
adverse_rs = build_stressed_transition_matrix(
    long_run,
    forward_roll_multiplier=1.35,
    cure_rate_haircut=0.25,
    prepayment_slowdown=0.15,
)
severe_rs = build_stressed_transition_matrix(
    long_run,
    forward_roll_multiplier=1.80,
    cure_rate_haircut=0.50,
    prepayment_slowdown=0.35,
)

# 12-month R&S window + 6-month straight-line reversion to long-run averages
adverse_schedule = build_reversion_schedule(
    adverse_rs,
    long_run,
    n_periods=horizon,
    reasonable_and_supportable_months=12,
    reversion_months=6,
    method="straight_line",
)
severe_schedule = build_reversion_schedule(
    severe_rs,
    long_run,
    n_periods=horizon,
    reasonable_and_supportable_months=12,
    reversion_months=6,
    method="straight_line",
)

# Scenario-specific LGD paths (elevated severity during downturn, reverting later)
adverse_lgd = [0.60] * 12 + [0.55] * 6 + [0.50] * (horizon - 18)
severe_lgd = [0.70] * 12 + [0.60] * 6 + [0.50] * (horizon - 18)

scenario_results = run_loss_scenarios(
    portfolio,
    scenarios={
        "baseline": long_run,
        "adverse": adverse_schedule,
        "severe": severe_schedule,
    },
    lgd={
        "baseline": 0.50,
        "adverse": adverse_lgd,
        "severe": severe_lgd,
    },
    scenario_weights={
        "baseline": 0.50,
        "adverse": 0.35,
        "severe": 0.15,
    },
    as_of_date=as_of_date,
    n_periods=horizon,
    amortize=True,
    cpr={"baseline": 0.08, "adverse": 0.05, "severe": 0.02},
)

print(summarize_loss_scenarios(scenario_results))
print(scenario_results.loc[scenario_results["scenario"] == "Weighted"].head())

Immediate Reversion

Pass method="immediate" (or reversion_months=0) to jump directly from the R&S matrix to long_run on month reasonable_and_supportable_months + 1:

adverse_immediate = build_reversion_schedule(
    adverse_rs,
    long_run,
    n_periods=horizon,
    reasonable_and_supportable_months=12,
    method="immediate",
)

2. Time-Varying Schedules in Core Entry Points

Every transition-matrix schedule returned by build_reversion_schedule or build_macro_transition_schedule plugs directly into the core loss forecasting and cashflow simulation APIs:

from cranalytics import (
    forecast_lifetime_loss,
    forecast_portfolio_states,
    simulate_portfolio_cashflows,
)

# 1. Lifetime loss with time-varying matrices and a per-month LGD path
stressed_loss = forecast_lifetime_loss(
    portfolio,
    adverse_schedule,
    lgd=adverse_lgd,
    as_of_date=as_of_date,
    amortize=True,
    cpr=0.05,
)

# 2. Portfolio state migration under the reversion schedule
initial_states = (
    portfolio["status"]
    .value_counts(normalize=True)
    .reindex(long_run.index, fill_value=0.0)
)
state_path = forecast_portfolio_states(
    adverse_schedule,
    initial_states,
    n_periods=horizon,
)

# 3. Full monthly cashflow simulation under the same schedule
cashflows = simulate_portfolio_cashflows(
    portfolio,
    adverse_schedule,
    cpr_vector=0.05,
    n_periods=horizon,
    paid_off_state="Paid Off",
)

If a forecast runs longer than the supplied matrix schedule or LGD path, the final matrix (or final LGD value) is held constant for all subsequent months.

Use build_macro_transition_schedule(macro_path, macro_link) to map a monthly DataFrame of macroeconomic variables to one validated transition matrix per month.

cranalytics.scenarios ships a simple baseline reference link (make_logit_macro_link and fit_logit_macro_link) that applies a linear log-odds shift to forward-roll probabilities. Treat it as a transparent baseline; supply your own callable when deploying a custom econometric model.

from cranalytics.scenarios import (
    build_macro_transition_schedule,
    make_logit_macro_link,
)

macro_adverse = pd.DataFrame(
    {
        "month": range(1, 13),
        "unemployment_rate": [4.2, 4.6, 5.1, 5.7, 6.2, 6.5, 6.4, 6.1, 5.8, 5.4, 5.0, 4.7],
    }
)

# Built-in baseline reference link (positive coefficient => higher unemployment increases roll rates)
baseline_link = make_logit_macro_link(
    long_run,
    coefficients={"unemployment_rate": 0.25},
    baseline_values={"unemployment_rate": 4.0},
    cure_sensitivity=0.20,
)
macro_rs_schedule = build_macro_transition_schedule(macro_adverse, baseline_link)

# Combine the 12-month macro R&S schedule with 6-month reversion to long-run averages
full_macro_schedule = build_reversion_schedule(
    macro_rs_schedule,
    long_run,
    n_periods=horizon,
    reasonable_and_supportable_months=12,
    reversion_months=6,
    method="straight_line",
)

You can also fit the baseline logit-shift coefficients directly from historical monthly transitions and a matching historical macro table using fit_logit_macro_link(transition_history, macro_history, macro_cols=...), or pass any custom callable (macro_path: pd.DataFrame) -> Sequence[pd.DataFrame] to build_macro_transition_schedule(...):

def custom_policy_link(macro_df: pd.DataFrame) -> list[pd.DataFrame]:
    return [
        build_stressed_transition_matrix(
            long_run,
            forward_roll_multiplier=1.0 + 0.20 * max(row.unemployment_rate - 4.0, 0.0),
            cure_rate_haircut=min(0.10 * max(row.unemployment_rate - 4.0, 0.0), 0.75),
        )
        for row in macro_df.itertuples(index=False)
    ]

custom_schedule = build_macro_transition_schedule(macro_adverse, custom_policy_link)

Governance Notes

  • Every matrix produced by build_stressed_transition_matrix, build_reversion_schedule, and build_macro_transition_schedule is validated via validate_transition_matrix so row sums equal 1.0 and probabilities stay within [0, 1].
  • run_loss_scenarios validates that scenario_weights are non-negative and sum to 1.0. The "Weighted" rows equal the exact probability-weighted sum of the individual scenario results in every month.
  • BaselineLogitMacroLink (make_logit_macro_link / fit_logit_macro_link) is a baseline reference link intended for prototyping, benchmarking, and sensitivity analysis—not a substitute for a validated macroeconomic satellite model.