Stability
The module provides methods to perform stability analytics. It provides a table containing the Population Stability Index (PSI).
Accessor
Initialise the DataFrame with the stability method. Minimal working example:
df.crm.stability(score_ref="SCORE_REFERENCE", score="SCORE")
(
df
.crm.frequency(index="GRADE", column="DATE", cohort="ID")
.table()
.crm.stability(score_ref="SCORE_REF", score="SCORE")
.table()
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
score_ref
|
str
|
Defines the reference population. |
required |
score
|
Union[str, list]
|
Defines the population to be compared to the reference population. |
required |
Returns:
Type | Description |
---|---|
Stability
|
Returns a class called "Stability" providing stability analytics methods. |
Methods
table(sort_by_col=None, sort_by_list=None, sort_asc=True, add_sum=False)
Minimal working example:
df.crm.stability(score_ref="SCORE_REFERENCE", score="SCORE").table()
(
df
.crm.frequency(index="GRADE", column="DATE", cohort="ID")
.table()
.crm.stability(score_ref="SCORE_REF", score="SCORE")
.table()
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sort_by_col
|
str
|
Defines the column to sort. |
None
|
sort_by_list
|
list
|
Defines the list to sort in case of categorical items which have no intrinsic sorting order. |
None
|
sort_asc
|
bool
|
Sorts the previously defined column or list ascending. |
True
|
add_sum
|
bool
|
Adds a sum row to the DataFrame. |
False
|
Returns:
Type | Description |
---|---|
DataFrame
|
Returns the PSI. |
Examples
data
>>> 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]
.table()
>>> (
>>> data
>>> .loc[lambda df: df["DATE"].dt.year.isin([2022, 2023])]
>>> .crm.frequency(index="GRADE", column="DATE", cohort="ID")
>>> .table()
>>> .crm.stability(score_ref="2023-12-31_PCT", score="2022-12-31_PCT")
>>> .table(sort_by_list=crm.cfg.GRADES)
>>> )
GRADE 2022-12-31_ABS 2022-12-31_PCT 2023-12-31_ABS 2023-12-31_PCT PSI_2023-12-31_PCT-2022-12-31_PCT
0 AAA 63 0.076736 69 0.084044 0.000665
1 AA 79 0.096224 95 0.115713 0.003594
2 A 109 0.132765 106 0.129111 0.000102
3 BBB 126 0.153471 106 0.129111 0.004211
4 BB 135 0.164434 132 0.160780 0.000082
5 B 107 0.130329 108 0.131547 0.000011
6 CCC 94 0.114495 99 0.120585 0.000316
7 D 108 0.131547 106 0.129111 0.000046