Portfolio Simulation Tutorial
Use portfolio simulation when you need a month-by-month expected path for balances, principal payments, prepayments, interest, and charge-offs under transition assumptions.
Inputs and first run
The portfolio snapshot needs loan_id, principal, annual_rate,
term_months, and status. The transition model must cover every live loan
status and identify its absorbing loss state. For a static transition matrix,
use StaticMatrixTransitionModel:
from cranalytics import (
StaticMatrixTransitionModel,
load_sample_transition_matrix,
make_mock_portfolio,
simulate_portfolio_cashflows,
)
portfolio = make_mock_portfolio(n_loans=300)
portfolio = portfolio[
portfolio["status"].isin(["Current", "Delinquent", "Charged Off"])
].copy()
model = StaticMatrixTransitionModel(load_sample_transition_matrix())
cashflows = simulate_portfolio_cashflows(portfolio, model, n_periods=12)
print(cashflows[[
"period", "scheduled_principal", "prepayments", "defaults", "ending_balance"
]].head())
The simulator applies transition probabilities deterministically, so the rows
represent an expected-value path rather than a random loan-level scenario.
Charge-off dollars appear in the defaults output column. prepayments includes
both CPR prepayments and transitions into a configured paid-off state.
How the balance path works
Each month's transition model distributes outstanding dollars across states.
Dollars reaching the loss or paid-off absorbing state leave the live balance.
For the current state (Current, when present), CPR prepayments are applied,
then scheduled principal and interest are calculated on the remaining amount.
Delinquent-state dollars do not prepay, amortize, or earn cash interest until
they transition back to the current state.
Set cpr_vector to an annual CPR scalar or a period-by-period Series. CPR is
converted to a monthly single mortality rate for each period. If no paid-off
state is configured, scheduled amortization and CPR still reduce the balance;
the model does not create a separate voluntary-payoff transition.
Dynamic transition models
A custom DynamicTransitionModel is queried for each loan and state holding
dollars. It receives the loan row with status set to that state and principal
and months_remaining updated. Other features, including months on book and
age, are not advanced automatically. If transition probabilities depend on
those features, update them in the model or provide a custom workflow that
advances them.
Output
The result has one row per simulated month. beginning_balance,
scheduled_principal, prepayments, defaults, interest, and
ending_balance are portfolio dollar amounts. balance_by_state maps each live
state to its ending outstanding dollars and each absorbing state to cumulative
dollars that reached it. The exact output column order is exposed as
cranalytics.simulation.PORTFOLIO_CASHFLOW_COLUMNS.
For a report or notebook, cranalytics.simulation.run takes the same inputs
(the transition model may also be a plain transition matrix) and returns one
workflow result:
from cranalytics.simulation import run
result = run(portfolio, transition_matrix, n_periods=12)
result.summary() # horizon totals and cumulative default rate, one row
result.tables() # cashflows, balance_by_state
result.assumptions() # periods, CPR, model, terminal states
result.plot() # ending balance and cumulative defaults
result.assumptions()["cpr"] records the annual CPR scalar, or a list of the
period-by-period rates actually used. A Series is read in positional order;
its index and any entries beyond n_periods do not affect the simulation.
For a runnable packaged example, use:
python -m cranalytics.examples.core_simulation