Hello, My name is Maria, MBA student in Sanfransisco, USA. In my credit scoring class, I have hard time making "transition matrix", which explains probability especially in relation to "Markov chain model". It is regarding people's monthly credit payment behavior. Does R have function to caculate it? I am actually a novice in using 'R'. Please help me!!! Maria Gu
kjetil@acelerate.com
2004-Apr-29 21:32 UTC
[R] Probability(Markov chain transition matrix)
On 29 Apr 2004 at 20:27, Maria Gu wrote:> Hello, My name is Maria, MBA student in Sanfransisco, USA. > In my credit scoring class, I have hard time making "transition > matrix", which explains probability especially in relation to "Markov > chain model". It is regarding people's monthly credit payment > behavior. Does R have function to caculate it? I am actually a novice > in using 'R'. Please help me!!! > > Maria Gu >Hola Maria, you need to be more specific in what you want. Do you want to estimate a markov transistion matrix from data? Do you want to simulate from a Markoc chain model? Whatever you want, it is likely possible in R, but we need more information. If you want to simulate from a model, maybe something like:> P <- matrix(c(0.1, 0.4, 0.5,+ 0.9, 0.05, 0.05, + 0.3, 0.6, 0.1),3,3, byrow=TRUE)> simMarkov <- function(P, init, n) {+ p <- dim(P)[1] + chain <- numeric(length=n) + chain[1] <- init + for (i in 2:n) { + chain[i] <- sample(1:p, 1, prob=P[chain[i-1],]) + } + return(chain) + }> simMarkov(P,1,20)[1] 1 2 1 3 2 2 2 1 2 1 2 1 3 1 2 1 3 2 1 1>Kjetil Halvorsen> ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Hi all, thank you for the response of my inquiry. Let me explain more specifically about what I want from R. It is about estimating bad debt related to people's credit card payment behavior. Here we go! Status of a credit account {NC, 0,1,2,.....M} where NC is no-credit status(account has no balance), 0 is where the account has a credit balance but the payment are up to date, 1 is where the account is one payment overdue, and M payment overdue is classified as default. So we come up with Transition matrix stating probabilities of each state FromTo NC 0 1 2 3 NC 0.79 0.21 0 0 0 0 0.09 0.73 0.18 0 0 1 0.09 0.51 0 0.40 0 2 0.09 0.38 0 0 0.55 3 0.06 0.32 0 0 0.62 Thus if one starts with all the accounts having no credit ?0 =(1,0,0,0,0), after one period the distribution of account is ?1=(0.79, 0.21, 0, 0, 0) after subsequent periods, it becomes 佇2=(0.64, 0.32, 0.04, 0, 0), 佇3= (0.540, 0.387, 0.058, 0.015, 0) 佇4=(0.468,0.431, 0.070, 0.023, 0.008) 佇5=(0.417, 0.460, 0.077, 0.028, 0.018) and ++++ 佇10=(0.315, 0.512, 0.091, 0.036, 0.046) This proves a way for estimating the amount of bad debt will appear the future periods. After 10 periods, it estimates that 4.6%(0.046) of the accounts will be bad. I am sure R can solve this, please help me! Maria Gu 510-418-1240 _________________________________________________________________
Bernardo Rangel Tura
2004-May-02 09:35 UTC
[R] Probability(Markov chain transition matrix)
At 17:25 30/04/2004, you wrote:>So we come up with Transition matrix stating probabilities of each state > >FromTo NC 0 1 2 3 >NC 0.79 0.21 0 0 0 >0 0.09 0.73 0.18 0 0 >1 0.09 0.51 0 0.40 0 >2 0.09 0.38 0 0 0.55 >3 0.06 0.32 0 0 0.62 > > >Thus if one starts with all the accounts having no credit ?0 =(1,0,0,0,0), after one period the distribution of account is ?1=(0.79, 0.21, 0, 0, 0) >after subsequent periods, it becomes > >?2=(0.64, 0.32, 0.04, 0, 0), >?3= (0.540, 0.387, 0.058, 0.015, 0) >?4=(0.468,0.431, 0.070, 0.023, 0.008) >?5=(0.417, 0.460, 0.077, 0.028, 0.018) >and ++++ >?10=(0.315, 0.512, 0.091, 0.036, 0.046)Hi I create a rotine for a problem like this, I hope this is useful for you ------------ ############################################# ## MARKOV CHAIN ## ## ## ## ini- initial state ## ## trans- transition matrix ## ## n - number of transitions ## ## f - ini%*%trans ## ## fase - f consolidate ## ## ## ############################################# # initial state ini<-matrix(c(10,0,0,0,0,0,0,0,0),nrow=3,ncol=3) # transition matrix # # [,1] [,2] [,3] # [1,] 0.85 0.1 0.05 # [2,] 0.00 0.7 0.3 # [3,] 0.00 0.0 1.0 # # In R the command is trans<-matrix(c(.85,0,0,.1,.7,0,0.05,0.3,1),ncol=3) markov<-function(ini,trans,n){ l<-ncol(ini) fase<-matrix(0,nrow=l,ncol=l) fases<-array(0,dim=c(nrow(ini),ncol(ini),n)) for (i in 1:n){ f<-ini%*%trans for (w in 1:l){fase[w,w]<-sum(f[,w])} ini<-fase fases[,,i]<-fase # print(fase) # # Fase allow calcule de value for transition: # If sate 1 value 0,95, state 2 value 0,3 and state 3 # value 0 QALY. # I?m calculate de value of any transition if # print(sum(fase%*%c(.95,.3,0))) # # } return(fases) } a<-markov(ini,trans,5) For your case: ini<-matrix(c(1,rep(0,24)),ncol=5) trans<-matrix(c(0.79,0.21,0,0,0,0.09,0.73,0.18,0,0,0.09,0.51,0,0.40,0,0.09,0.38,0,0,0.55,0.06,0.32,0,0,0.62),ncol=5) solv<-markov(ini,trans,10)> solv[,,10][,1] [,2] [,3] [,4] [,5] [1,] 0.3173366 0.0000000 0.0000000 0.0000000 0.0000000 [2,] 0.0000000 0.2876792 0.0000000 0.0000000 0.0000000 [3,] 0.0000000 0.0000000 0.2875772 0.0000000 0.0000000 [4,] 0.0000000 0.0000000 0.0000000 0.2886571 0.0000000 [5,] 0.0000000 0.0000000 0.0000000 0.0000000 0.2810951 Bernardo Rangel Tura, MD, MSc National Institute of Cardiology Laranjeiras Rio de Janeiro Brazil