# Clustering ordered categorical data

Suppose I have a list of, say, 100 countries, as well as their respective historical sovereign credit ratings as such

              2020   2019   ...   2000
Country 1     AAA    A-    ...    BBB
Country 2     CCC    B-    ...    BBB
...................................


I am interested in clustering these based on their historical credit ratings. For instance, I expect two countries that have consistently rated highly over the years (say ratings between A- and AAA) would cluster together, countries with varying degrees of ratings (from low to high) over the years 2000 and 2020 would also cluster together, and countries that have consistently rated poorly also. I have looked at a few suggestions online for clustering categorical data based on multiple variables, but usually they are not for ordered categorical data. For instance, the dissimilarity matrix generated by Kmodes, is predicated on the two categories being identical. However, in ordered categorical data, a rating of BBB+ and BBB are incredibly close to one another and thus must be clustered together.

What would be a good solution to such clustering exercise for the countries given the example above?

You can have categories that contain a logic that could be a numeric value and it seems to be your case.

That's why you should consider those ratings from a mathematical point of view and assign a numerical scale that would be comprehensive to your algorithm.

For instance:

AAA+ => 1

AAA  => 2

AAA- => 3

AA+  => 4

AA   => 5

AA-  => 6


etc.

In this way, countries rated AAA+ in 2022 and AA- in 2021 should be close to countries rated AAA in 2022 and AA in 2021 because [1,6] are similar to [2,5] from a numeric point of view.

However, if you consider those rating as separated categories like this:

AAA+ => col_AAA+= True, col_AAA=False, col_AAA-=False, col_AA+=False,...

AAA => col_AAA+= False, col_AAA=True, col_AAA-=False, col_AA+=False,...


etc.

You would have more data to deal with and the algorithm would not see any ranking between columns, and hence would not make good clustering.

I recommend using numeric values for any feature that can have a scale and use categories just in case of independent ones (for instance, sea_access=Yes/No, or opec_member=Yes/No).

I some case, you can also implement an intermediate solution like this one:

AAA+ => col_A= 1, col_B=0, col_C-=0, ...

AAA => col_A= 2, col_B=0, col_C-=0, ...

...

BBB+ => col_A= 0, col_B=1, col_C-=0, ...

BBB => col_A= 0, col_B=2, col_C=0, ...


etc.

It could be interesting if you want to make a clear difference between rating groups (ex: going from AAA to A+ is not as bad as going from A- to BBB+).

Note: clustering could be difficult if you consider too many years, even with algorithms like UMAP or t-SNE. That's why a good option is to consider a few years for a beginning or simplify with smoothing algorithms.