Question

I have draw a sample u from random variable u~uniform(0,1) ;

set.seed(123)

num_samples <- 5    #number of samples
num_time_periods <- 5   # number of years

sample_u <- array(0,c(num_samples,num_time_periods))

for(i in 2:num_time_periods){
    sample_u[,i] <- runif(num_samples, min=0, max=1)}  

> sample_u
       [,1]      [,2]      [,3]      [,4]       [,5]
 [1,]    0 0.2875775 0.0455565 0.9568333 0.89982497
 [2,]    0 0.7883051 0.5281055 0.4533342 0.24608773
 [3,]    0 0.4089769 0.8924190 0.6775706 0.04205953
 [4,]    0 0.8830174 0.5514350 0.5726334 0.32792072
 [5,]    0 0.9404673 0.4566147 0.1029247 0.95450365

and then i also try to do the transition matrix since i have;

p_11=0.0468 (probability in state 1 given that it is in state 1 previously)

p_12=0.0.9532 (probability in state 1 given that it is in state 2 previously)

p_21=0.3232 (probability in state 2 given that it is in state 1 previously)

p_22=0.6768 (probability in state 2 given that it is in state 2 previously)

trans.mat <- matrix(c( 0.0468, 0.9532, 0.3232, 0.6768), 2,2, byrow = TRUE)

I have the following condition:-

1) if sample_u<= p_11, then rho_1 =1 (in state 1) , if sample_u >p_11, then rho_1=2 (in state 2)

I have tried to do this ;

ifelse(sample_u <= p_11, rho_1 <- 1 , rho_1 <- 2 )

but I know that my code is wrong because it is not refering to the previous state (Markov chain). my code is just doing 'if sample u is less or equal than p_11, it is in state 1, else it is in state 2'.

The R code should produce something like below :-

> 
       [,1]      [,2]                                                            [,3]                                                                               [,4] 

 [1,]    1        2 ( reason : u > p_11 -bcoz it is in state 1 previously)    2(reason : u < p_22 -bcoz it is in state 2 previously)     (reason : u > p_22 -bcoz it is in state 2 previously) 
 [2,]    1        2 ( reason : u > p_11 -bcoz it is in state 1 previously)    2 (reason : u < p_22 -bcoz it is in state 2 previously)   (reason : u < p_22 -bcoz it is in state 2 previously)
 [3,]    1        2 ( reason : u > p_11 -bcoz it is in state 1 previously)    1 (reason : u > p_22 -bcoz it is in state 2 previously)   (reason : u > p_11 -bcoz it is in state 1 previously)
 [4,]    1        2 ( reason : u > p_11 -bcoz it is in state 1 previously)    2 (reason : u < p_22 -bcoz it is in state 2 previously)   (reason : u < p_22 -bcoz it is in state 2 previously)
 [5,]    1        2 ( reason : u > p_11 -bcoz it is in state 1 previously)    2 (reason : u < p_22 -bcoz it is in state 2 previously)   (reason : u < p_22 -bcoz it is in state 2 previously)

What im trying to achieve is 'Given that it is in state 1 in previous period, and sample u is less or equal than p_11, then it is in state 1'

or

'Given that it is in state 2 in previous period, and sample u is less or equal than p_22, then it is in state 2'

How do I use the 'if or else or and' statement to determine the state?

. I think i need to correct the 'ifelse' command but im not sure how to fix this.

I want to find the state for each period in a matrix form.

Pls help. thanks!

Was it helpful?

Solution

Since you are simulating a Markov chain, the next state depends on the previous state. Therefore each column can be vectorized.

## store p11 and p22 so that state can be used as index directly
p <- c(0.0468,0.6768)  

## create state matrix and init first state to "1"
state <- matrix(0, nrow(sample_u), ncol(sample_u))
state[,1] <- 1

## Iterate through each time step & update state
for(i in 2:num_time_periods){
  state[,i] <- ifelse(sample_u[,i] < p[state[,i-1]], state[,i-1], ifelse(state[,i-1]==1,2,1))    
}

> state
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    2    1    2
[2,]    1    2    2    2    2
[3,]    1    2    1    2    2
[4,]    1    2    2    2    2
[5,]    1    2    2    2    1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top