Vintage and roll-rate analysis#
Analyse a loan panel as the loans age. delinquency_panel() builds the panel from instalment schedules and payments, vintage_table() gives the cumulative bad rate of each cohort by month on book, and transition_matrix() and roll_rates() show how loans move between delinquency buckets or grades. migration_bandwidth() and migration_test() summarize and test the migrations between rating grades.
See Vintage and roll-rate analysis for examples on a synthetic loan panel, on a few repayment schedules and on simulated rating migrations.
|
Build a monthly delinquency panel from instalment schedules and payments. |
|
Tabulate the cumulative bad rate of each cohort by month on book. |
|
Count the movements of entities between two states. |
|
Compute the share of each state's entities that rolled to the next state. |
|
Measure how far customers migrate between rating grades, upwards and downwards. |
|
Test whether the migration frequencies fall away from the diagonal of the matrix. |
- kivi.vintage.delinquency_panel(loan, due_date, amount_due, payment_loan, payment_date, payment_amount, *, end, origination=None, principal=None, tolerance=0.0)#
Build a monthly delinquency panel from instalment schedules and payments.
At every month end, a loan is behind by the instalments that have fallen due and are not paid yet. This function applies the payments of each loan to its instalments, the oldest first, and gives one row per loan and month end with the days past due, the delinquency status and the amounts past due and outstanding: the loan panel that vintage_table, transition_matrix and roll_rates take.
- Parameters:
- loanarray_like
Loan of each instalment of the schedule. The instalments need not be sorted.
- due_datearray_like
Due date of each instalment, as dates or as strings that pandas.to_datetime reads. A time of day is ignored.
- amount_duearray_like
Amount of each instalment, finite and non-negative, such as the principal and the interest due on that date.
- payment_loanarray_like
Loan of each payment. Every one must be a loan of the schedule, and a loan without payments has paid nothing. The payments need not be sorted, and their Series may have any index.
- payment_datearray_like
Date of each payment, read like due_date.
- payment_amountarray_like
Amount of each payment, finite and non-negative. Net a reversal against the payment that it reverses.
- enddate or month
Last month of the panel, such as
"2026-01", a date in that month or a pandas.Period. A string is read as pandas.Period reads it, and a period longer than a month, such as"2025Q4", ends in its last month. The payments must be complete up to its last day.- originationpandas.Series or mapping, optional
Origination date of each loan, as a Series indexed by loan or a mapping from loan to date, read like due_date. It must give a date for every loan of the schedule and may hold other loans too. The cohorts are then the origination months, and the months on book count from them. By default, both come from the first due dates.
- principalarray_like, optional
Principal part of each instalment, between 0 and its amount due, matched to the schedule like amount_due. The panel then gets a
balancecolumn.- tolerancefloat, default 0.0
Largest unpaid amount of an instalment that still counts as paid, such as a few cents of rounding. It decides only which instalments count as paid; the amounts in the panel stay exact.
- Returns:
- pandas.DataFrame
One row per loan and month end, sorted by loan and then by month, with the columns described in the Notes: those of kivi.datasets.make_loan_panel that a schedule gives, in the same order, and the days past due and the amounts owed.
- Raises:
- ValueError
If the inputs of the schedule, or those of the payments, are not one-dimensional, have different lengths or are Series with different indices, or if the schedule is empty. Also if a loan or a date is missing, a date cannot be read, an amount is missing, negative, infinite or not a number, principal exceeds amount_due, a payment belongs to a loan that is not in the schedule, end is not a date or a month or comes before the month of every due date, origination is not a Series or a mapping, repeats a loan, lacks a loan of the schedule or its date, or comes after the first due date of its loan, or tolerance is not a finite, non-negative number.
See also
vintage_tableCumulative bad rates by cohort and month on book.
transition_matrixMovements between delinquency buckets.
kivi.datasets.make_loan_panelSynthetic loan panel with the same statuses.
Notes
A loan has a row at the end of every month from the month of its first due date to end. It leaves the panel after the month end at which all its instalments count as paid, also when it is repaid early, while a loan that is still behind after its last due date keeps rolling until end. Payments after end are not used, and neither are amounts paid beyond the total due, such as an overpayment or a payment after the loan was repaid: there is no credit balance.
At each month end \(t\), only the payments made on or before \(t\) count. They go to the instalments in the order of their due dates, the oldest unpaid one first and instalments due on the same day in the order of the schedule, so an amount paid before an instalment falls due counts towards it. With \(C_k\) the total amount due by instalment \(k\) of a loan in that order, and \(P_t\) the total paid by \(t\), instalment \(k\) counts as paid at \(t\) if
\[C_k - P_t \le \mathrm{tolerance},\]that is, once at most tolerance remains unpaid of it and the instalments before it together. A partial payment leaves an instalment unpaid until then, and an unpaid remainder within tolerance is still owed: the next payments go to it first. A loan therefore leaves the panel once at most tolerance of it is unpaid, so a tolerance of at least its total due makes a loan that pays nothing leave in its first month. Amounts within a relative \(10^{-10}\) of \(C_k\) count as paid, so that the rounding of sums does not leave unpaid an instalment paid in parts. Instalments due on the same day add up, so a schedule row that a faulty join repeats doubles the amount due.
If \(d\) is the due date of the oldest instalment that is not paid at \(t\), the loan is \(t - d\) days past due when \(d\) is before \(t\), and 0 days otherwise; an instalment is not past due on its due date.
The columns are:
loan_id: the loan, with the labels of loan;cohort: the origination month of the loan with origination, and otherwise the month before its first due date, a monthly pandas.Period;month: the month, a monthly pandas.Period, observed on its last day;month_on_book:month - cohortin months. Without origination, it is 1 in the month of the first due date, as in kivi.datasets.make_loan_panel, whose first instalment falls due one month after origination. With origination, a loan whose first instalment falls due later, after a grace period, has its first row at a later month on book;days_past_due: the days past due, as defined above;overdue_amount: the unpaid amount of the instalments due before the month end;outstanding: the unpaid amount of all instalments, past due or not;balance, only with principal: the unpaid principal of all instalments. A payment pays the interest of an instalment, its amount due less its principal, before its principal;status: the days past due as an ordered categorical with the categoriescurrent(0 days),1-29,30-59,60-89and90+(90 days or more), the statuses of kivi.datasets.make_loan_panel.
Without origination, loans originated in the same month but with grace periods of different lengths fall into different cohorts. With it, a cohort whose loans all have a grace period starts at a later month on book in vintage_table, which leaves the earlier ones NaN.
Days past due at month ends do not always move one bucket a month. An instalment due on 31 January and still unpaid is 29 days past due at the end of February and 60 days at the end of March, so it skips
30-59.Examples
>>> from kivi.vintage import delinquency_panel >>> loan = ["A"] * 3 + ["B"] * 3 >>> due_date = ["2024-01-15", "2024-02-15", "2024-03-15"] * 2 >>> amount_due = [100.0] * 6 >>> payment_loan = ["A", "A", "B"] >>> payment_date = ["2024-01-15", "2024-03-20", "2024-01-12"] >>> payment_amount = [100.0, 200.0, 200.0] >>> panel = delinquency_panel( ... loan, due_date, amount_due, payment_loan, payment_date, payment_amount, end="2024-05" ... ) >>> panel.drop(columns=["cohort", "month_on_book"]) loan_id month days_past_due overdue_amount outstanding status 0 A 2024-01 0 0.0 200.0 current 1 A 2024-02 14 100.0 200.0 1-29 2 A 2024-03 0 0.0 0.0 current 3 B 2024-01 0 0.0 100.0 current 4 B 2024-02 0 0.0 100.0 current 5 B 2024-03 16 100.0 100.0 1-29 6 B 2024-04 46 100.0 100.0 30-59 7 B 2024-05 77 100.0 100.0 60-89
Loan A missed its February instalment and paid it with the March one on 20 March, after the end of February, so it was 14 days past due at the end of February and is repaid at the end of March. Loan B paid two instalments in January, which keeps it current in February, and has not paid the last one, so it keeps rolling after its last due date.
- kivi.vintage.vintage_table(loan, cohort, month_on_book, bad, *, weights=None)#
Tabulate the cumulative bad rate of each cohort by month on book.
A vintage table follows each cohort of loans, usually the loans originated in the same month, as it ages. Its entry for a cohort and a month on book is the share of the cohort’s loans that were bad at or before that month on book, so every row is non-decreasing.
- Parameters:
- loanarray_like
Identifier of the loan of each row of the panel. A loan has at most one row per month on book, and its rows need not be sorted.
- cohortarray_like
Cohort of each row, such as the month of origination. It must be the same on every row of a loan. Cohorts are sorted, and categorical cohorts follow the order of their categories.
- month_on_bookarray_like
Whole, non-negative number of months since origination of each row.
- badarray_like
Whether the loan is bad at that month on book, as booleans or 0 and 1. Pass the bad definition as a condition on the status, such as
status == "90+", orstatus >= "60-89"for an ordered categorical.- weightsarray_like, optional
Non-negative weight of each loan, such as the amount lent, repeated on every row of the loan. The table then reports the share of the cohort’s weight in loans that were bad, and cohorts whose weights sum to zero are left out.
- Returns:
- pandas.DataFrame
Cumulative bad rates with one row per cohort and one column per distinct month on book of the panel. An entry is NaN outside the months on book at which the cohort is observed, from the smallest to the largest month on book of its rows.
- Raises:
- ValueError
If the inputs are empty, not one-dimensional or of different lengths, or Series with different indices. Also if loan or cohort has a missing value, a loan has more than one cohort or more than one row for a month on book, month_on_book has a value that is not a whole, non-negative number, bad has a value other than 0 and 1, or weights is not valid or differs between the rows of a loan.
See also
transition_matrixMovements between states from one month to another.
roll_ratesShare of each delinquency bucket that rolls to the next.
kivi.datasets.make_loan_panelSynthetic loan panel to try this on.
Notes
Let \(L_c\) be the loans of cohort \(c\), \(\tau_i\) the first month on book at which loan \(i\) is bad (infinite if it never is) and \(w_i\) its weight, 1 without weights. The entry for month on book \(k\) is
\[V_{c,k} = \frac{\sum_{i \in L_c} w_i \, [\tau_i \le k]} {\sum_{i \in L_c} w_i}.\]The denominator includes every loan of the cohort, also loans that left the panel early, for example because they were repaid; such a loan counts as bad only if it was bad before it left. A loan that is bad once stays counted as bad, even if it later cures.
A cohort is observed from the smallest to the largest month on book of its rows, and its entries outside that range are NaN, not 0. Later months on book are missing because the cohort is too young to show whether its loans will go bad, and earlier ones because the panel starts after the cohort was originated. Loans that were bad only before the panel starts are not counted, so start the panel at origination where possible. Comparing the cohorts at the same month on book compares them at the same age, which separates a change in the quality of new lending from the seasoning of the portfolio.
Two cases need care. If every loan of a cohort leaves the panel before it ends, for example because all of them were repaid, the cohort’s last entry is final, yet its later months on book are NaN; to fill them, keep a row for each month end after a loan left, with a status that is not bad, such as
"closed". And a cohort that spans several origination months, such as a quarter, mixes loans of different ages: at its largest months on book, its youngest loans count in the denominator although they have not been observed that long, which biases those entries down. Monthly cohorts avoid this.Examples
>>> from kivi.vintage import vintage_table >>> loan = [1, 1, 1, 2, 2, 2, 3, 3, 4, 4] >>> cohort = ["2024-01"] * 6 + ["2024-02"] * 4 >>> month_on_book = [1, 2, 3, 1, 2, 3, 1, 2, 1, 2] >>> status = ["current", "90+", "current", "current", "current", "current", ... "90+", "90+", "current", "current"] >>> bad = [value == "90+" for value in status] >>> vintage_table(loan, cohort, month_on_book, bad) month_on_book 1 2 3 cohort 2024-01 0.0 0.5 0.5 2024-02 0.5 0.5 NaN
Loan 1 cured after month on book 2 but stays counted as bad, and the 2024-02 cohort has not reached month on book 3.
- kivi.vintage.transition_matrix(before, after, *, states=None, normalize=False, weights=None)#
Count the movements of entities between two states.
Each entity, such as a loan or an obligor, is observed in one state before and one state after, for example its delinquency bucket at two consecutive month ends or its rating grade at two annual reviews. Row \(i\), column \(j\) counts the entities that moved from state \(i\) to state \(j\). With normalize, each row holds the shares of the states after, the migration matrix of a rating system.
- Parameters:
- before, afterarray_like
State of each entity at the first and at the second observation. If both are Series, they must have identical indices; otherwise they are matched by position. Neither may contain missing values: drop the entities that were not observed twice, or give the entities that left a state such as
"closed".- statesarray_like, optional
All states in order, for delinquency buckets and rating grades from the best to the worst. States without entities still get a row and a column. By default, the categories of categorical inputs, which must then be the same for both, or else the sorted distinct values. Sorting puts labels such as
"current"after"90+", so pass states or ordered categoricals for delinquency buckets. A state must not be missing.- normalizebool, default False
If True, divide each row by its total, so that it holds the shares of the state’s entities that moved to each state. A row without entities is then NaN.
- weightsarray_like, optional
Non-negative weight of each entity, such as its balance at the first observation, matched like after. The counts are then sums of weights.
- Returns:
- pandas.DataFrame
One row per state before and one column per state after, in the order of states. Without weights and normalize, the entries are integer counts.
- Raises:
- ValueError
If the inputs are empty, not one-dimensional, of different lengths or Series with different indices, have a missing value or a value that is not one of states, or weights is not valid. Also if states is not one-dimensional or has duplicates or missing values, or is not given while before and after are categoricals with different categories, or if normalize is not a boolean.
See also
roll_ratesShare of each state that moved to the next worse state.
vintage_tableCumulative bad rates by cohort and month on book.
Notes
With \(n_{ij}\) entities moving from state \(i\) to state \(j\), the normalized entry
\[p_{ij} = \frac{n_{ij}}{\sum_k n_{ik}}\]estimates the probability of moving to \(j\) from \(i\). If the movements follow a time-homogeneous Markov chain, the \(h\)-th power of the normalized matrix gives the probabilities over \(h\) periods.
Examples
>>> from kivi.vintage import transition_matrix >>> before = ["A", "A", "A", "B", "B", "C"] >>> after = ["A", "A", "B", "B", "C", "C"] >>> transition_matrix(before, after, states=["A", "B", "C", "D"]) after A B C D before A 2 1 0 0 B 0 1 1 0 C 0 0 1 0 D 0 0 0 0 >>> transition_matrix(before, after, normalize=True).round(3) after A B C before A 0.667 0.333 0.0 B 0.000 0.500 0.5 C 0.000 0.000 1.0
- kivi.vintage.roll_rates(before, after, *, states=None, weights=None)#
Compute the share of each state’s entities that rolled to the next state.
Roll rates summarize the movements of delinquency between two month ends: the roll rate of
30-59days past due is the share of the loans at 30 to 59 days that are at 60 to 89 days a month later. The states must be ordered from the best to the worst, so that the next state is the next worse one.- Parameters:
- before, afterarray_like
State of each entity at the first and at the second observation, as in transition_matrix. Neither may contain missing values.
- statesarray_like, optional
All states from the best to the worst. Required unless before and after are ordered categoricals with the same categories, whose order is then used. A sorted order would be wrong for most labels, for example
"current"would come after"90+".- weightsarray_like, optional
Non-negative weight of each entity, such as its balance at the first observation, matched like after. The rates are then shares of weight, the balance roll rates.
- Returns:
- pandas.Series
Roll rate of every state except the worst, which has no next state, indexed by the state. A state without entities has a NaN rate.
- Raises:
- ValueError
In the same cases as transition_matrix, and if states is not given while before and after are not ordered categoricals.
See also
transition_matrixThe movements between all pairs of states.
vintage_tableCumulative bad rates by cohort and month on book.
Notes
With \(n_{ij}\) entities moving from state \(i\) to state \(j\), the roll rate of state \(i\) is
\[r_i = \frac{n_{i,i+1}}{\sum_k n_{ik}}.\]Entities that improved, stayed or moved more than one state worse count in the denominator only. A loan can fall at most one delinquency bucket further behind from one month end to the next, so the product of the roll rates estimates the share of current loans that reach the worst bucket after one month per roll, if the movements follow a Markov chain.
Examples
>>> from kivi.vintage import roll_rates >>> states = ["current", "30", "60", "90+"] >>> before = ["current"] * 4 + ["30"] * 3 + ["60"] * 2 + ["90+"] >>> after = ["current", "current", "current", "30", ... "current", "30", "60", "60", "90+", "90+"] >>> roll_rates(before, after, states=states).round(3) before current 0.250 30 0.333 60 0.500 Name: roll_rate, dtype: float64
- kivi.vintage.migration_bandwidth(before, after, *, grades, weights=None)#
Measure how far customers migrate between rating grades, upwards and downwards.
The matrix weighted bandwidth (MWB) of the European Central Bank [1] summarizes a migration matrix in two numbers: how far the customers who moved to worse grades moved, and how far those who moved to better grades moved, each relative to the furthest move from their grade in either direction. A stable rating system has small values.
- Parameters:
- beforearray_like
Grade of each customer at the beginning of the observation period. Every value must be one of grades.
- afterarray_like
Status of each customer at the end of the period: a grade, or any other label, such as
"default"or"closed", for a customer who left the grades. If before and after are both Series, they must have identical indices; otherwise they are matched by position. Neither may contain missing values.- gradesarray_like
The \(K\) grades of the rating scale for non-defaulted customers, from the best to the worst, at least two.
- weightsarray_like, optional
Non-negative frequency weight of each customer, matched like after: integer weights give the same result as repeating rows.
- Returns:
- pandas.Series
upper, the MWB of the migrations to worse grades, above the diagonal of the migration matrix, andlower, that of the migrations to better grades, below it, in an index namedstatistic. Each is between 0 and 1, and NaN when no customer moved in its direction.
- Raises:
- ValueError
If the inputs are empty, not one-dimensional, of different lengths or Series with different indices, or have a missing value, if before has a value that is not one of grades, or if weights is not valid. Also if grades is not one-dimensional, has fewer than two grades, duplicates or missing values.
See also
migration_testTest the monotonicity of the migration matrix.
transition_matrixThe migration matrix itself.
Notes
Number the grades from 1, the best, to \(K\). With \(N_i\) customers in grade \(i\) at the beginning and \(N_{ij}\) of them in grade \(j\) at the end, \(p_{ij} = N_{ij} / N_i\), and
\[\mathrm{MWB}_\mathrm{upper} = \frac{1}{M_u} \sum_{i=1}^{K-1} \sum_{j=i+1}^{K} |i - j| \, N_i \, p_{ij}, \qquad M_u = \sum_{i=1}^{K-1} \max(|i - K|, |i - 1|) \, N_i \sum_{j=i+1}^{K} p_{ij},\]with \(\mathrm{MWB}_\mathrm{lower}\) and \(M_l\) defined in the same way over \(j < i\), for \(i\) from 2 to \(K\). \(N_i\) counts the customers who defaulted or left as well, but \(N_i \, p_{ij} = N_{ij}\) counts only those who reached a grade, so the customers who left change neither bandwidth. With weights, the counts are sums of weights.
References
[1]European Central Bank, “Instructions for reporting the validation results of internal models: IRB Pillar I models for credit risk”, February 2019, section 2.5.5.1.
Examples
>>> from kivi.vintage import migration_bandwidth >>> before = ["A", "A", "A", "B", "B", "B", "C", "C"] >>> after = ["A", "B", "C", "A", "B", "default", "B", "C"] >>> migration_bandwidth(before, after, grades=["A", "B", "C"]).round(4) statistic upper 0.7500 lower 0.6667 dtype: float64
Of grade A, one customer moved one grade down and one two grades, where each could have moved two, so the upper MWB is (1 + 2) / (2 + 2). The customer of grade B who defaulted counts in neither bandwidth.
- kivi.vintage.migration_test(before, after, *, grades, weights=None)#
Test whether the migration frequencies fall away from the diagonal of the matrix.
In a stable rating system, few customers move far: the share of a grade that moves to another grade should fall the further that grade lies from it. The z tests of the European Central Bank [1] compare each off-diagonal cell of the migration matrix with its neighbour towards the diagonal. A small p-value means that more customers reached the cell than its neighbour, a sign that the portfolio or the rating system shifted.
- Parameters:
- beforearray_like
Grade of each customer at the beginning of the observation period. Every value must be one of grades.
- afterarray_like
Status of each customer at the end of the period: a grade, or any other label, such as
"default"or"closed", for a customer who left the grades. If before and after are both Series, they must have identical indices; otherwise they are matched by position. Neither may contain missing values.- gradesarray_like
The \(K\) grades of the rating scale for non-defaulted customers, from the best to the worst, at least two.
- weightsarray_like, optional
Non-negative frequency weight of each customer, matched like after: integer weights give the same result as repeating rows.
- Returns:
- pandas.DataFrame
One row for each of the \(K (K - 1)\) off-diagonal cells, in the order of grades, indexed by the grades
beforeandafter.totalis the number of customers in the grade before, those who left included;sharethe share of them in the grade after;statisticthe z statistic; andp_valueits one-sided p-value.
- Raises:
- ValueError
In the same cases as migration_bandwidth.
See also
migration_bandwidthHow far the customers migrate.
transition_matrixThe migration matrix itself.
Notes
With \(N_i\) customers in grade \(i\) at the beginning, those who defaulted or left included, and the shares \(p_{ij}\) of them in grade \(j\) at the end, each cell is compared with its neighbour \(n\) towards the diagonal, \(n = j + 1\) below the diagonal (\(j < i\)) and \(n = j - 1\) above it (\(j > i\)):
\[z_{ij} = \frac{p_{in} - p_{ij}} {\sqrt{\left[p_{ij} (1 - p_{ij}) + p_{in} (1 - p_{in}) + 2 p_{ij} p_{in}\right] / N_i}},\]whose denominator is the standard deviation of \(p_{in} - p_{ij}\) when the customers of grade \(i\) migrate independently. Under the null hypothesis \(p_{ij} \le p_{in}\), the p-value is \(\Phi(z_{ij})\), so the cells that are fuller than their neighbour get small p-values. As the European Central Bank asks, the statistic and the p-value are NaN where they are not defined: for a grade without customers, and where both shares are 0 or one of them is 1. With weights, the counts are sums of weights, so weights that are not frequencies, such as exposures, do not give standard normal statistics.
References
[1]European Central Bank, “Instructions for reporting the validation results of internal models: IRB Pillar I models for credit risk”, February 2019, section 2.5.5.2.
Examples
>>> from kivi.vintage import migration_test >>> before = ["A", "A", "A", "B", "B", "B", "C", "C"] >>> after = ["A", "B", "C", "A", "B", "default", "B", "C"] >>> migration_test(before, after, grades=["A", "B", "C"]).round(4) total share statistic p_value before after A B 3 0.3333 0.0000 0.5000 C 3 0.3333 0.0000 0.5000 B A 3 0.3333 0.0000 0.5000 C 3 0.0000 1.2247 0.8897 C A 2 0.0000 1.4142 0.9214 B 2 0.5000 0.0000 0.5000
The customer of grade B who defaulted counts in its
total. No cell holds more customers than its neighbour towards the diagonal, so no p-value is small.