Transition or migration matrices in RatS

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
Aqu_nb

Transition or migration matrices in RatS

Unread post by Aqu_nb »

Is there anyone who known whether it is possible to estimate a transition or migration matrices in RATS?

Thanks for your help
Atef
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Transition or migration matrices in RatS

Unread post by TomDoan »

Aqu_nb wrote:Is there anyone who known whether it is possible to estimate a transition or migration matrices in RATS?

Thanks for your help
Atef
Could you be more specific? Any type of Markov model has a transition matrix.
Aqu_nb

Re: Transition or migration matrices in RatS

Unread post by Aqu_nb »

TomDoan wrote:
Aqu_nb wrote:Is there anyone who known whether it is possible to estimate a transition or migration matrices in RATS?

Thanks for your help
Atef
Could you be more specific? Any type of Markov model has a transition matrix.
Dear Tom,

Thanks for your replay. To be more specific what I am looking for is a way to Markov chains via empirical transition matrices. Credit rating companies (such Moody’s, Standard and Poor’s and also JP Morgan) use this model to estimate credit rating transition (also called migration) matrices. Is this more clear?

Thanks for your help
Atef
Aqu_nb

Re: Transition or migration matrices in RatS

Unread post by Aqu_nb »

Aqu_nb wrote:
TomDoan wrote:
Aqu_nb wrote:Is there anyone who known whether it is possible to estimate a transition or migration matrices in RATS?

Thanks for your help
Atef
Could you be more specific? Any type of Markov model has a transition matrix.
Dear Tom,

Thanks for your reply. To be more specific what I am looking for is a way to estimate Markov chains via empirical transition matrices. Credit rating companies (such Moody’s, Standard and Poor’s and also JP Morgan) use this model to estimate credit rating transition (also called migration) matrices. Is this more clear?

I can also refer to following articals who estimate this model: Robert B. Israel, Jeffrey S. Rosenthal and Jason Z. Wei (2000). Jarrow and Turnbull (1995). Jarrow, Lando and Turnbull (1997).

I want to estimate a credit rating probability transition (migration) matrix. Transition of credit rating of one bond isuer (say AAA) from one year to the next year (say AA) given some macroeconomic conditions.

Thanks for your help
Atef
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Transition or migration matrices in RatS

Unread post by TomDoan »

The transition matrices that they need are for a continuous time Markov chain. How you estimate those parameters depends quite a bit on the form of the data. Are your data regularly spaced? (For instance, all start or end of year classification).
Aqu_nb

Re: Transition or migration matrices in RatS

Unread post by Aqu_nb »

TomDoan wrote:The transition matrices that they need are for a continuous time Markov chain. How you estimate those parameters depends quite a bit on the form of the data. Are your data regularly spaced? (For instance, all start or end of year classification).
Dear Tom,

To be more clear what I want is to estimate the same matrices as: Anil Bangia, Francis X. Diebold and Til Schuermann (2000), 'Ratings Migration and the Business Cycle, With Application to Credit Portfolio Stress Testing'. This artical can be viewed from following link: http://www.econ.upenn.edu/~fdiebold/pap ... 37/bds.pdf

In other words I want to estimate both unconditional and conditional trasition matrices (as in the above refered artical).

And now to your question. My data is constructed as following. Regularly spaced and classified (yearly) data from 1995-2008. The dataset contain around 140.000 firms classified in 8 different categogies (7 different rating clasifications and one default clasification) just as in the above refered artical. And what I want to estimate is how these classifications changes from year to (next) year given different macroeconomic scenarios.

Is it possible to estimate these kind of matrices in RATS? If so then how can get the code?

I really appreciate your help
Regards
Atef
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Transition or migration matrices in RatS

Unread post by TomDoan »

The transition matrices are relatively easy to compute; it's really a matter of adjusting the timing of the gaps between the classifications. The following assumes that you have three series - an identifier for the firm, an identifier for the year and a classification (including default as a possibility). It's assumed that the data are sorted by firm, then by year. The following will first compute the raw counts for changes from class to class. That's then divided by the number in each class at the beginning of the period:

Code: Select all

dec rect transit(nclass,nclass)
do i=1,nclass
   do j=1,nclass
      sstats(smpl=(firm==firm{1}.and.year==year{1}+1)) / (class==j.and.class{1}==i)>>transit(i,j)
   end do j
end do i
*
* Do a fail-safed divide by the number of elements per row
*
compute [vector] rowscales=%sumr(transit)
ewise rowscales(i)=%if(rowscales(i)==0,0,1.0/rowscales(i))
compute transit=%diag(rowscales)*transit
To do the conditional calculations, you need to create a series which tags the observations. For instance, something like:

Code: Select all

set recession = year==1981.or.year==1992.or.year==2001
You would do the same as above, but change the sstats to

Code: Select all

 sstats(smpl=(firm==firm{1}.and.year==year{1}+1.and.recession)) / (class==j.and.class{1}==i)>>transit(i,j)
to get the transition probabilities for the recession periods and

Code: Select all

 sstats(smpl=(firm==firm{1}.and.year==year{1}+1.and..not.recession)) / (class==j.and.class{1}==i)>>transit(i,j)
for the expansions.
Aqu_nb

Re: Transition or migration matrices in RatS

Unread post by Aqu_nb »

TomDoan wrote:The transition matrices are relatively easy to compute; it's really a matter of adjusting the timing of the gaps between the classifications. The following assumes that you have three series - an identifier for the firm, an identifier for the year and a classification (including default as a possibility). It's assumed that the data are sorted by firm, then by year. The following will first compute the raw counts for changes from class to class. That's then divided by the number in each class at the beginning of the period:

Code: Select all

dec rect transit(nclass,nclass)
do i=1,nclass
   do j=1,nclass
      sstats(smpl=(firm==firm{1}.and.year==year{1}+1)) / (class==j.and.class{1}==i)>>transit(i,j)
   end do j
end do i
*
* Do a fail-safed divide by the number of elements per row
*
compute [vector] rowscales=%sumr(transit)
ewise rowscales(i)=%if(rowscales(i)==0,0,1.0/rowscales(i))
compute transit=%diag(rowscales)*transit
To do the conditional calculations, you need to create a series which tags the observations. For instance, something like:

Code: Select all

set recession = year==1981.or.year==1992.or.year==2001
You would do the same as above, but change the sstats to

Code: Select all

 sstats(smpl=(firm==firm{1}.and.year==year{1}+1.and.recession)) / (class==j.and.class{1}==i)>>transit(i,j)
to get the transition probabilities for the recession periods and

Code: Select all

 sstats(smpl=(firm==firm{1}.and.year==year{1}+1.and..not.recession)) / (class==j.and.class{1}==i)>>transit(i,j)
for the expansions.
Dear Tom,

I am sorry for not replying earlier. But it is because I was on vacation. Now I am back and thanks a lot for your help with the code. In this regard I have couple of questions.

1) I have sorted data as you have suggested. And it looks like following:

firm year class
1 1995 AA
1 1996 AAA
1 1997 D
2 2001 BB
2 2002 AA
.
.
In toltal a have around 1 million observation. And my dataset is in sasdata form(file). How do I read a sas data in RATs? I can not export sas data to excel due to 1 million observations.

2) In the code you have send where do you refer to a dataset ? Is it called smpl (fx in: sstats(smpl=(firm==firm{1}.and.year==year{1}+1)) / (class==j.and.class{1}==i)>>transit(i,j))?

3) How do I see output? by that I mean the estimated matrices. Is there a code for that and for tests?

Once again Thanks a lot for your help
Regards
Atef
Aqu_nb

Re: Transition or migration matrices in RatS

Unread post by Aqu_nb »

Aqu_nb wrote:
TomDoan wrote:The transition matrices are relatively easy to compute; it's really a matter of adjusting the timing of the gaps between the classifications. The following assumes that you have three series - an identifier for the firm, an identifier for the year and a classification (including default as a possibility). It's assumed that the data are sorted by firm, then by year. The following will first compute the raw counts for changes from class to class. That's then divided by the number in each class at the beginning of the period:

Code: Select all

dec rect transit(nclass,nclass)
do i=1,nclass
   do j=1,nclass
      sstats(smpl=(firm==firm{1}.and.year==year{1}+1)) / (class==j.and.class{1}==i)>>transit(i,j)
   end do j
end do i
*
* Do a fail-safed divide by the number of elements per row
*
compute [vector] rowscales=%sumr(transit)
ewise rowscales(i)=%if(rowscales(i)==0,0,1.0/rowscales(i))
compute transit=%diag(rowscales)*transit
To do the conditional calculations, you need to create a series which tags the observations. For instance, something like:

Code: Select all

set recession = year==1981.or.year==1992.or.year==2001
You would do the same as above, but change the sstats to

Code: Select all

 sstats(smpl=(firm==firm{1}.and.year==year{1}+1.and.recession)) / (class==j.and.class{1}==i)>>transit(i,j)
to get the transition probabilities for the recession periods and

Code: Select all

 sstats(smpl=(firm==firm{1}.and.year==year{1}+1.and..not.recession)) / (class==j.and.class{1}==i)>>transit(i,j)
for the expansions.
Dear Tom,

I am sorry for not replying earlier. But it is because I was on vacation. Now I am back and thanks a lot for your help with the code. In this regard I have couple of questions.

1) I have sorted data as you have suggested. And it looks like following:

firm year class
1 1995 AA
1 1996 AAA
1 1997 D
2 2001 BB
2 2002 AA
.
.
In toltal a have around 1 million observation. And my dataset is in sasdata form(file). How do I read a sas data in RATs? I can not export sas data to excel due to 1 million observations.

2) In the code you have send where do you refer to a dataset ? Is it called smpl (fx in: sstats(smpl=(firm==firm{1}.and.year==year{1}+1)) / (class==j.and.class{1}==i)>>transit(i,j))?

3) How do I see output? by that I mean the estimated matrices. Is there a code for that and for tests?

Once again Thanks a lot for your help
Regards
Atef
Dear Tom,

Now I have tried to open the data (with 65508 observations) both as a txt and excel file. But the program code does not work.

The data is constructed as above and the I use is:

OPEN DATA "C:\Documents and Settings\User\Dokumenter\datatransit.xls"
ALL 65508
DATA(FORMAT=XLS,ORG=COLUMNS) 1 65508 firm year class

dec rect transit(nclass,nclass)
do i=1,nclass
do j=1,nclass
sstats(smpl=(firm==firm{1}.and.year==year{1}+1)) / (class==j.and.class{1}==i)>>transit(i,j)
end do j
end do i
*
* Do a fail-safed divide by the number of elements per row
*
compute [vector] rowscales=%sumr(transit)
ewise rowscales(i)=%if(rowscales(i)==0,0,1.0/rowscales(i))
compute transit=%diag(rowscales)*transit


Using this code the following error accour:



## SX11. Identifier NCLASS is Not Recognizable. Incorrect Option Field or Parameter Order?
>>>>ect transit(nclass,<<<<
## SX11. Identifier NCLASS is Not Recognizable. Incorrect Option Field or Parameter Order?
>>>>ect transit(nclass,<<<<


Hopefully it is some small error. Thanks

Regards
Atef
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Transition or migration matrices in RatS

Unread post by TomDoan »

You have to set the value of NCLASS yourself. It sounds as if

Code: Select all

compute NCLASS=8
is correct for your data. You should be able to get the entire data set using a text file (CSV, for instance), though it would be a good idea to test everything with a subset as you're currently doing.
Aqu_nb

Re: Transition or migration matrices in RatS

Unread post by Aqu_nb »

TomDoan wrote:You have to set the value of NCLASS yourself. It sounds as if

Code: Select all

compute NCLASS=8
is correct for your data. You should be able to get the entire data set using a text file (CSV, for instance), though it would be a good idea to test everything with a subset as you're currently doing.
Dear Tom,

Now I have tried to run the code by including: compute NCLASS=8. The code now looks like:

OPEN DATA "C:\Documents and Settings\User\Dokumenter\datatransit.xls"
ALL 65517
DATA(FORMAT=XLS,ORG=COLUMNS) 1 65517 firm year class BNP ledighed

compute NCLASS=8

dec rect transit(nclass,nclass)
do i=1,nclass
do j=1,nclass
sstats(smpl=(firm==firm{1}.and.year==year{1}+1)) / (class==j.and.class{1}==i)>>transit(i,j)
end do j
end do i

compute [vector] rowscales=%sumr(transit)
ewise rowscales(i)=%if(rowscales(i)==0,0,1.0/rowscales(i))
compute transit=%diag(rowscales)*transit

When I run this I get the following error:

## SX11. Identifier %SUMR is Not Recognizable. Incorrect Option Field or Parameter Order?
>>>>r] rowscales=%sumr(<<<<

And if I remove r from %SUMR so I have: %SUM. Then I get the following error:

## MAT2. Matrices with Dimensions 1 x 1 and 8 x 8 Involved in * Operation

I can not figure out the problem?

Thanx for your help

Atef
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Transition or migration matrices in RatS

Unread post by TomDoan »

%sumr returns the vector of row sums. It was added with RATS 7.2; you might want to see why you don't have that, since the University has it. At any rate, you can do the same calculation with

compute [vector] rowscales=transit*%fill(nclass,1,1.0)
Aqu_nb

Re: Transition or migration matrices in RatS

Unread post by Aqu_nb »

TomDoan wrote:%sumr returns the vector of row sums. It was added with RATS 7.2; you might want to see why you don't have that, since the University has it. At any rate, you can do the same calculation with

compute [vector] rowscales=transit*%fill(nclass,1,1.0)
Ok. Now Ithink it is runing. But I can not see the output (the estimated matrix)? How do I see the out put?

The code I am runing is:

OPEN DATA "C:\Documents and Settings\User\Dokumenter\datatransit.xls"
ALL 65508
DATA(FORMAT=XLS,ORG=COLUMNS) 1 65508 firm year class

compute nclass=8
dec rect transit(nclass,nclass)
do i=1,nclass
do j=1,nclass
sstats(smpl=(firm==firm{1}.and.year==year{1}+1)) / (class==j.and.class{1}==i)>>transit(i,j)
end do j
end do i
*
* Do a fail-safed divide by the number of elements per row
*
compute [vector] rowscales=transit*%fill(nclass,1,1.0)
ewise rowscales(i)=%if(rowscales(i)==0,0,1.0/rowscales(i))
compute transit=%diag(rowscales)*transit

Thanx
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Transition or migration matrices in RatS

Unread post by TomDoan »

display transit

will do that.
Aqu_nb

Re: Transition or migration matrices in RatS

Unread post by Aqu_nb »

TomDoan wrote:display transit

will do that.
Yes it works. But the problem now is that the rows or colums does not sum 1 (100%). The ouput looks like following:

1 2 3 4 5 6 Default
1 0.39581 0.07705 0.01245 0.00205 0.00085 0.00013 0.01165
2 0.04980 0.32419 0.09470 0.01749 0.00290 0.00045 0.01046
3 0.00463 0.05876 0.32108 0.08518 0.01558 0.00227 0.01250
4 0.00092 0.01041 0.09079 0.28290 0.08201 0.01024 0.02273
5 0.00034 0.00204 0.02927 0.14299 0.22622 0.05427 0.04488
6 0.00002 0.00017 0.00232 0.02726 0.08753 0.29964 0.08306

What do you think the problem can be?

Thanx again:-)
Post Reply