Migration
The module provides methods to perform migration analytics. In general, it can be used to analyse changes in grades between two dates per cohort. For changes in grades per date, see the "override" method. It provides the number of observations for two dates, the cohort outflows, the cohort inflows, the rating grade upgrades, the rating grade downgrades, the rating grade migration rate (rating grade changes of more than one grade to number of cohort), and the rating grade downgrade rate (rating grade downgrades of more than one grade to rating grade changes of more than one grade). It also provides a "heatmap" migration matrix.
Accessor
Initialise the DataFrame with the migration method. Minimal working example:
df.crm.migration(grade="GRADE", date="DATE", cohort="COHORT")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grade
|
str
|
Defines the grade column. Scores can be transformed to grades via the module "general.py" if necessary. |
required |
date
|
str
|
Defines the date column. The date column should only contain two dates. |
required |
cohort
|
str
|
Defines the cohort identifier. |
required |
Returns:
Type | Description |
---|---|
Migration
|
Returns a class called "Migration" providing migration analytics methods. |
Methods
plot(x_axis_label='Second Reporting Date', y_axis_label='First Reporting Date', grade_range=None, annotation=True, font_size=7, fig_size=(7.5, 7.5), path=None, show=False)
Minimal working example:
df.crm.migration(grade="GRADE", date="DATE", cohort="cohort").plot()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_axis_label
|
str
|
Defines the x-axis label. |
'Second Reporting Date'
|
y_axis_label
|
str
|
Defines the y-axis label. |
'First Reporting Date'
|
grade_range
|
list
|
Extends the grade range to the specified range. Use "cfg.GRADES" for the complete range. |
None
|
annotation
|
bool
|
Adds a "Downgrade" and "Upgrade" box to the plot. |
True
|
font_size
|
int
|
Defines the font size of the plot. |
7
|
fig_size
|
tuple
|
Defines the figure size. |
(7.5, 7.5)
|
path
|
str
|
Defines the saving path including the filename of the figure and should be entered as r"C:\<path>\<filename>.<image_format>". |
None
|
show
|
bool
|
If True, plot is shown. |
False
|
Returns:
Type | Description |
---|---|
BytesIO
|
Returns a "heatmap" migration matrix. |
table()
Minimal working example:
df.crm.migration(grade="GRADE", date="DATE", cohort="COHORT").table()
Returns:
Type | Description |
---|---|
DataFrame
|
Returns a table containing the number of observations two dates, the cohort outflows, the cohort inflows, the rating grade upgrades, the rating grade downgrades, the multi grade migration rate, and the downgrade migration rate. |
Examples
>>> import credit_risk_modelling as crm
>>> data = crm.load_data.load_data()
>>> data
DATE ID GRADE GRADE_PD OVERRIDE OVERRIDE_PD DEFAULT
0 2019-12-31 10 B 0.1000 B 0.1000 0
1 2019-12-31 100 BBB 0.0090 BB 0.0400 0
2 2019-12-31 1000 BBB 0.0090 BBB 0.0090 0
3 2019-12-31 1001 BBB 0.0090 BBB 0.0090 0
4 2019-12-31 1003 BBB 0.0090 BBB 0.0090 0
... ... ... ... ... ... ... ...
4145 2023-12-31 994 AA 0.0010 AA 0.0010 0
4146 2023-12-31 995 AA 0.0010 AA 0.0010 0
4147 2023-12-31 996 A 0.0020 A 0.0020 0
4148 2023-12-31 998 B 0.1000 B 0.1000 0
4149 2023-12-31 999 AAA 0.0002 AAA 0.0002 0
[4150 rows x 7 columns]
>>> (
>>> data
>>> .loc[lambda df: df["DATE"].dt.year.isin([2022, 2023])]
>>> .crm.migration(grade="GRADE", date="DATE", cohort="ID")
>>> .table()
>>> )
OBS_1 OUTFLOWS OBS_1_COHORT OBS_2 INFLOWS OBS_2_COHORT UNCHANGED UP_1 UP_2 UP_3 UP_>3 UP_TOTAL DOWN_1 DOWN_2 DOWN_3 DOWN_>3 DOWN_TOTAL MR DR
0 900 79 821 1000 179 821 575 66 40 10 15 131 64 27 19 5 115 0.141291 0.439655
>>> (
>>> data
>>> .loc[lambda df: df["DATE"].dt.year.isin([2022, 2023])]
>>> .crm.migration(grade="GRADE", date="DATE", cohort="ID")
>>> .plot(show=True)
>>> )