library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
library(tibble)
library(expm)
## Loading required package: Matrix
##
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
##
## Attaching package: 'expm'
## The following object is masked from 'package:Matrix':
##
## expm
library(ggplot2)
##(a)
P <- matrix(c(0.342,0.005,0.001,0.020,0.632,
0.001,0.252,0.107,0.005,0.635,
0.003,0.043,0.507,0.014,0.433,
0.001,0.002,0.004,0.665,0.328,
0.007,0.007,0.007,0.025,0.954) ,nrow = 5 , byrow = TRUE)
print(P)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.342 0.005 0.001 0.020 0.632
## [2,] 0.001 0.252 0.107 0.005 0.635
## [3,] 0.003 0.043 0.507 0.014 0.433
## [4,] 0.001 0.002 0.004 0.665 0.328
## [5,] 0.007 0.007 0.007 0.025 0.954
eigen(P)
## eigen() decomposition
## $values
## [1] 1.0000000 0.6407485 0.5131153 0.3347737 0.2313625
##
## $vectors
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.4472136 0.08291035 -0.05750010 0.998596592 0.003747464
## [2,] 0.4472136 0.14602171 0.33607265 -0.046095528 0.989892859
## [3,] 0.4472136 0.16892645 0.93974620 0.021976749 -0.141488023
## [4,] 0.4472136 -0.96880597 0.01478795 0.008261958 0.003054581
## [5,] 0.4472136 0.06842789 -0.02018199 -0.011349476 -0.008360250
eigen(t(P))$values
## [1] 1.0000000 0.6407485 0.5131153 0.3347737 0.2313625
eigen(t(P))$vectors
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.01081419 0.01381240 -0.01933154 0.688874153 -0.03569127
## [2,] 0.01058652 0.01124811 0.08397740 -0.004744289 -0.77807075
## [3,] 0.01708962 0.02297136 0.63372323 0.028104694 0.28849531
## [4,] 0.07590987 -0.73041578 0.06741433 0.011986637 -0.03075200
## [5,] 0.99685336 0.68238391 -0.76578343 -0.724221195 0.55601871
Psteady_state <- eigen(t(P))$vectors[,1]
print(Psteady_state) #Steady state
## [1] 0.01081419 0.01058652 0.01708962 0.07590987 0.99685336
Psteady_stateN <- Psteady_state/sum(Psteady_state)
print(Psteady_stateN) #Normalised steady state
## [1] 0.009731526 0.009526648 0.015378691 0.068310122 0.897053014
##############################################
##(b)
#Repeat process for all 5 states then binding
#River to lake
t_values <- c(1,2,5,10,100)
P_index_list <- list()
for(i in 1:length(t_values)){
P_index_list[[i]] <- round((as.matrix(P) %^% t_values[i]), 3)
}
P_index_df1 <- tibble(from_state = 'River', to_state='Lake', prob = NA,
t = seq(from = 1, to = 10))
for(row_i in 1:nrow(P_index_df1)){
mat <- round((as.matrix(P) %^% P_index_df1$t[row_i]),3)
P_index_df1$prob[row_i] <- mat[1,2]
}
ggplot(P_index_df1 , aes(x = t, y = prob )) +
geom_point() +
geom_line()

#Lake to Lake
P_index_df2 <- tibble(from_state = 'Lake', to_state='Lake', prob = NA,
t = seq(from = 1, to = 10))
for(row_i in 1:nrow(P_index_df1)){
mat <- round((as.matrix(P) %^% P_index_df2$t[row_i]),3)
P_index_df2$prob[row_i] <- mat[2,2]
}
ggplot(P_index_df2 , aes(x = t, y = prob )) +
geom_point() +
geom_line()

#Pond to Lake
P_index_df3 <- tibble(from_state = 'Pond', to_state='Lake', prob = NA,
t = seq(from = 1, to = 10))
for(row_i in 1:nrow(P_index_df3)){
mat <- round((as.matrix(P) %^% P_index_df3$t[row_i]),3)
P_index_df3$prob[row_i] <- mat[3,2]
}
ggplot(P_index_df3 , aes(x = t, y = prob )) +
geom_point() +
geom_line()

#Paddy to Lake
P_index_df4 <- tibble(from_state = 'Paddy', to_state='Lake', prob = NA,
t = seq(from = 1, to = 10))
for(row_i in 1:nrow(P_index_df4)){
mat <- round((as.matrix(P) %^% P_index_df4$t[row_i]),3)
P_index_df4$prob[row_i] <- mat[4,2]
}
ggplot(P_index_df4 , aes(x = t, y = prob )) +
geom_point() +
geom_line()

#Non to Lake
P_index_df5 <- tibble(from_state = 'Non', to_state='Lake', prob = NA,
t = seq(from = 1, to = 10))
for(row_i in 1:nrow(P_index_df5)){
mat <- round((as.matrix(P) %^% P_index_df5$t[row_i]),3)
P_index_df5$prob[row_i] <- mat[5,2]
}
ggplot(P_index_df5 , aes(x = t, y = prob )) +
geom_point() +
geom_line()

plot_all <- bind_rows( mutate(P_index_df1,state = "River"),
mutate(P_index_df2,state = "Lake"),
mutate(P_index_df3,state = "Pond"),
mutate(P_index_df4,state = "Paddy"),
mutate(P_index_df5,state = "Non"),)
ggplot(plot_all, aes(x=t,y=prob,color=state)) +
geom_point() +
geom_line()

# As shown by plot, Markov chain converges to the steady state
#provided earlier, for the lake state regardless of starting state.
################################
##(c)
markov <- function(init, mat, n, labels){
if(missing(labels)) labels <- 1:length(init)
sim_list <- numeric(n+1)
state <- 1:length(init)
sim_list[1] <- sample(state, 1, prob=init)
for(i in 2:(n+1)){
sim_list[i] <- sample(state, 1, prob=mat[sim_list[i-1],] )
return(labels[sim_list])
}}
name_state <- c("River","Lake","Pond","Paddy","Non")
colnames(P) <- name_state
rownames(P) <- name_state
M <- 1000; mc_res <- numeric(M)
for (i in 1:M){
mc_sim <- markov(init = c(0,0,0,1,0), mat = P,
50, labels = name_state)
mc_res[i] <- which(mc_sim == "Paddy")[2] - 1
}
mc_res_df <- tibble(time = mc_res)
mc_res_df <- mc_res_df %>% mutate_all(~replace_na(.,0))
mc_res_df <- mc_res_df %>% mutate_all(as.numeric)
medianT <- median(mc_res_df$time)
print(medianT)
## [1] 1
# Median time = 1
##########################
##(d)
meanT <- mean(mc_res_df$time)
print(meanT)
## [1] 0.674
difference <- medianT - meanT
print(difference)
## [1] 0.326