BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
wsdabreu
Obsidian | Level 7

Hi friends, 

 

I'm trying to replicate the matrix from this topic, https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-1-year-migration-matrix/td-p/33..., but it's not working because I don't have this "default" line. Any suggestions? I tried to create a default line but didn't work too. Sorry for the newby question, but i'm newly using data step, and i can't use proc sql, because I challenged myself to do everything in data step.

 

Thanks in advance 🙂

 

 

edit: I tried to comment the line above in the original code, and it works, but for a reason it is jumping some values.

 

C['default','default'] = 1; 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

I see. 

 

In the previous example that you posted, the data did not have any information about the transition state for a loan that is in default. That is because when the loan goes into default, it never transitions out of that state. Because the data did not provide any information, the last row of the transition matrix (as computed from the data) contained all zeros. Thus it was an invalid transition matrix. To make it valid, the program inserted a 1 in the bottom right corner of the matrix.

 

It looks like your matrix is a valid transition matrix, so you do not need to do anything special.

 

I will comment, however, that the transition matrix you posted is deterministic, not stochastic. There is no randomness in the transition from one state to the next because each row contains only one nonzero element.

View solution in original post

6 REPLIES 6
Rick_SAS
SAS Super FREQ

When I click on your link, I do not see the line.

C['default','default'] = 1; 

In fact, the link does not seem to call PROC IML at all.

 

Please post the code that you are having problems with. If it results in an ERROR, please show the relevant portion of the SAS log.

wsdabreu
Obsidian | Level 7

Sorry, I passed the wrong link. It was already fixed.

data Bonds;
length rating next_rating $7;
input bond_id rating $ start_year end_year current_year next_rating $;
datalines;
1 a 2000 2004 2000 a
1 a 2000 2004 2001 a
1 a 2000 2004 2002 a
1 a 2000 2004 2003 a
1 a 2000 2004 2004 b
1 b 2005 2008 2005 b
1 b 2005 2008 2006 b
1 b 2005 2008 2007 b
1 b 2005 2008 2008 c
1 c 2009 2010 2009 c
1 c 2009 2010 2010 default
2 b 2003 2005 2003 b
2 b 2003 2005 2004 b
2 b 2003 2005 2005 a
2 a 2006 2007 2006 a
2 a 2006 2007 2007 default
3 c 2001 2006 2001 c
3 c 2001 2006 2002 c
3 c 2001 2006 2003 c
3 c 2001 2006 2004 c
3 c 2001 2006 2005 c
3 c 2001 2006 2006 default
;

proc iml;
use Bonds;
read all var {bond_id rating next_rating};
close;

R = unique( rating // next_rating );
numR = ncol(R);
C = j(numR, numR, 0);
mattrib C rowname=R colname=R;
/* data does not contain any information about default transitions.
Assume default is end state */
C['default','default'] = 1;

do i = 1 to numR;
do j = 1 to numR;
C[i,j] = sum( rating=R[i] & next_rating=R[j] );
end;
end;

C = C / C[,+]; /* divide each row by sum of row */
print C[format=fract20.];


This is the code, I didn't receive an error, It's jumping values, because I don't have this 'Default' observation and because I comment the line in red.

Rick_SAS
SAS Super FREQ

You didn't receive an error because you DO have a row and column named 'default'. I think if you move the assignment of the (default, default) cell after the DO loops, that should fix the problem:

 

do i = 1 to numR;
do j = 1 to numR;
   C[i,j] = sum( rating=R[i] & next_rating=R[j] );
end;
end;

C['default','default'] = 1;
C = C / C[,+]; /* divide each row by sum of row */
print C[format=fract20.];
wsdabreu
Obsidian | Level 7

Thanks for you support Rick :), Unfortunately I should not have been clear. In my dataset i don't have this "default values". 

 

I have this values in my dataset. CRC is ID, Rating_PDD_Num_2016 and Rating_PDD_Num_2015 are the values for markov transition.

 

Data.PNG

 

this is the result, it's jumping in this case 4 values.

dataset.PNG

 

 my code is this:

 

 

proc iml;

use mes1_2015_2016_2682_TC;
read all var {CRC Rating_PDD_Num_2015_ Rating_PDD_Num_2016_};
close;

R = unique( Rating_PDD_Num_2015_ // Rating_PDD_Num_2016_ );
numR = ncol(R);
Telecomunicacoes = j(numR, numR, 0);
mattrib Telecomunicacoes rowname=R colname=R;
/* data does not contain any information about default transitions.
Assume default is end state */
/*Telecomunicacoes['default','default'] = 1;*/

do i = 1 to numR;
  do j = 1 to numR;
  Telecomunicacoes[i,j] = sum( Rating_PDD_Num_2015_=R[i] & Rating_PDD_Num_2016_=R[j] );
  end;
end;

 

Telecomunicacoes = Telecomunicacoes / Telecomunicacoes[,+]; /* divide each row by sum of row */
print Telecomunicacoes[format=fract20.];

Rick_SAS
SAS Super FREQ

I see. 

 

In the previous example that you posted, the data did not have any information about the transition state for a loan that is in default. That is because when the loan goes into default, it never transitions out of that state. Because the data did not provide any information, the last row of the transition matrix (as computed from the data) contained all zeros. Thus it was an invalid transition matrix. To make it valid, the program inserted a 1 in the bottom right corner of the matrix.

 

It looks like your matrix is a valid transition matrix, so you do not need to do anything special.

 

I will comment, however, that the transition matrix you posted is deterministic, not stochastic. There is no randomness in the transition from one state to the next because each row contains only one nonzero element.

wsdabreu
Obsidian | Level 7

My error was in the interpretation, I did not realize that the output format (fract20.) was dividing the values, so the result gave 1 and not 2/2 for example. Lack of attention.

 

"I will comment, however, that the transition matrix you posted is deterministic, not stochastic. There is no randomness in the transition from one state to the next because each row contains only one nonzero element."

 

Yeah, This is because I'm subsetting the data by sector and we don't have many values. Anyway thank you very much for your help Rick 🙂

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 6 replies
  • 4455 views
  • 5 likes
  • 2 in conversation