migrate

Using migrate

This package is intended to serve as a set of tools to help convert credit risk data at two time points into traditional credit state migration (aka, “transition”) matrices. At a higher level, migrate is intended to help an analyst understand how risk moved in their credit portfolio over a time interval.

Background

One of the more difficult aspects of making a credit state migration matrix in R (or Python, for that matter) is the fact that the output doesn’t satisfy the structure of a traditional data frame object. Rather, the output needs to be a matrix, which is a data structure that R does support. In the past, there has been difficulty converting a matrix to something more visual-friendly. More recently, however, tools like the kableExtra and gt packages allow us to present visually appealing output that extends the structure of a data frame. Using the matrix-style output of migrate’s functions with a visual formatting package such as the two mentioned above will hopefully help analysts streamline the presentation of their credit portfolio’s state migration matrices to an audience.

Getting Started

If you haven’t done so already, first install migrate with the instructions in the README section.

First, load the package & the mock dataset (as a data frame) using library()

library(migrate)
data("mock_credit")

We can get a look at the data using dplyr::glimpse()

library(dplyr)
glimpse(mock_credit)
#> Rows: 1,000
#> Columns: 4
#> $ customer_id       <chr> "Customer_1001", "Customer_1002", "Customer_1003", "Customer_1004", "Customer_1005", "Customer_1006", "Customer_1007", "Customer_1008", "Customer_1009", "Customer_1010", "Customer_1011", "Customer_1012", "Customer_1013", "Customer_1014", "Customer_1015", "Customer_1016", "Customer_1017", "Customer_1018", "Customer_1019", "Customer_1020", "Customer_1021", "Customer_1022", "Customer_1023", "Customer_1024", "Customer_1025", "Customer_1026", "Customer_1027", "Customer_1028", "Customer_1029", "Customer_1030", "Customer_1031", "Customer_1032", "Customer_1033", "Customer_1034", "Customer_1035", "Customer_1036", "Customer_1037", "Customer_1038", "Customer_1039", "Customer_1040", "Customer_1041", "Customer_1042", "Customer_1043", "Customer_1044", "Customer_1045", "Customer_1046", "Customer_1047", "Customer_1048", "Customer_1049", "Customer_1050", "Customer_1051", "Customer_1052", "Customer_1053", "Customer_1054", "Customer_1055", "Customer_1056", "Customer_1057",...
#> $ date              <date> 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06-30, 2020-06...
#> $ risk_rating       <ord> A, AAA, BBB, BB, AA, BB, CCC, BB, AAA, B, A, A, BBB, A, A, A, AA, CCC, B, A, AA, BBB, A, AA, B, AA, BBB, B, BBB, A, AA, BB, B, BB, BBB, A, CCC, BBB, B, A, BB, A, A, BBB, A, B, BB, BB, BB, B, BBB, CCC, B, BB, A, AA, AA, BBB, AA, A, CCC, BB, AA, B, BB, A, B, A, AAA, B, B, BBB, A, BBB, A, A, AA, CCC, A, AAA, A, A, B, AA, BB, AA, BBB, BBB, BBB, AA, B, BBB, BBB, CCC, BB, A, BB, AAA, AA, BB, BB, A, BBB, BB, AA, AAA, B, AA, BBB, B, BBB, BB, AA, B, BBB, AA, AA, AA, A, A, A, B, B, A, BBB, BBB, B, BBB, AA, B, BB, B, AAA, B, BB, A, A, BB, BBB, BBB, AA, BBB, A, AA, BB, A, A, AA, B, B, BBB, BB, A, BB, B, BB, AA, B, A, BBB, AAA, AA, BB, BB, AA, BB, BB, BB, BBB, B, BBB, AA, AAA, AA, BB, A, AA, AAA, B, A, A, B, A, CCC, BB, BBB, A, BBB, AA, B, AA, BB, AA, AA, A, B, BBB, CCC, BBB, BBB, AAA, B, BBB, AA, BBB, B, BB, A, BB, CCC, BB, CCC, BBB, B, AAA, A, AAA, B, AA, AAA, A, AA, A, BBB, A, AA, B, B, AA, A, A, BBB, A, AA, A, AA, BBB, B, A, AA, A, AAA, BB, BB, BB, A, A, B, BB, A...
#> $ principal_balance <dbl> 915000, 979000, 1400000, 627000, 1403000, 1096000, 396000, 444000, 660000, 348000, 905000, 1054000, 909000, 895000, 2008000, 1281000, 1165000, 1390000, 1557000, 211000, 612000, 844000, 1211000, 693000, 1989000, 1134000, 1569000, 1076000, 514000, 952000, 2511000, 700000, 1200000, 771000, 2090000, 1513000, 155000, 2398000, 1530000, 752000, 22000, 1253000, 878000, 803000, 808000, 351000, 1005000, 1083000, 480000, 1733000, 778000, 1854000, 96000, 2023000, 892000, 1623000, 404000, 1652000, 1601000, 885000, 1387000, 1087000, 316000, 1488000, 992000, 478000, 1066000, 1432000, 375000, 565000, 282000, 1212000, 1616000, 1613000, 357000, 1577000, 1971000, 203000, 1315000, 20000, 284000, 2248000, 2048000, 2105000, 1024000, 875000, 606000, 1176000, 332000, 269000, 1124000, 102000, 913000, 787000, 2990000, 1617000, 259000, 760000, 1756000, 1283000, 2720000, 2386000, 501000, 303000, 315000, 1977000, 807000, 1646000, 1093000, 1346000, 3739000, 74000, 383000, 1001000, 98...

Note that an important feature of the dataset is that there are exactly two (2) unique values in the date column variable

unique(mock_credit$date)
#> [1] "2020-06-30" "2020-09-30"

To summarize the migration within the data, use the migrate() function

migrated_df <- migrate(
  data = mock_credit, 
  date = date, 
  state = risk_rating, 
  id = customer_id, 
  metric = principal_balance
)

head(migrated_df)
#> # A tibble: 6 x 3
#>   risk_rating_start risk_rating_end principal_balance
#>   <fct>             <fct>                       <dbl>
#> 1 AAA               AAA                      0.815   
#> 2 AAA               AA                       0.184   
#> 3 AAA               A                        0.000561
#> 4 AAA               BBB                      0       
#> 5 AAA               BB                       0       
#> 6 AAA               B                        0

To create the state migration matrix, use the build_matrix() function

build_matrix(migrated_df)
#> Using  risk_rating_start  as the 'state_start' column variable
#> 
#> Using  risk_rating_end  as the 'state_end' column variable
#> 
#> Using  principal_balance  as the 'metric' column variable
#> 
#>             AAA         AA            A         BBB         BB          B        CCC
#> AAA 0.814939529 0.18449926 0.0005612145 0.000000000 0.00000000 0.00000000 0.00000000
#> AA  0.070014774 0.63111068 0.1417133794 0.157161170 0.00000000 0.00000000 0.00000000
#> A   0.006651444 0.06533969 0.7059300440 0.173855852 0.04822297 0.00000000 0.00000000
#> BBB 0.000000000 0.00000000 0.1248259489 0.654281908 0.13935108 0.08154107 0.00000000
#> BB  0.000000000 0.00000000 0.0000000000 0.147446202 0.59613689 0.18222712 0.07418978
#> B   0.000000000 0.00000000 0.0000000000 0.005764373 0.09351403 0.66160481 0.23911678
#> CCC 0.000000000 0.00000000 0.0000000000 0.000000000 0.00000000 0.12363465 0.87636535

Or, to do it all in one shot, use the %>%

mock_credit %>% 
  migrate(
    date = date, 
    state = risk_rating, 
    id = customer_id, 
    metric = principal_balance
  ) %>% 
  build_matrix()