#
# Frog in a lily pond (Levin Peres Wilmber, Example 1.1)
# Lasse Leskelä 2012-10-29
#
# Compute the probabilities
#
#   mu[1,t] that a frog occupies the east pad on the t-th day
#
# and
#
#   mu[2,t] that a frog occupies the west pad on the t-th day
#
# for a frog which jumps from the east pad to the west pad with probabability p,
# and from the west pad to the east pad with probability q.
#

# Transition matrix
p <- .2
q <- .1
P <- matrix(0,2,2)
P[1,] <- c(1-p,p)
P[2,] <- c(q,1-q)


# Initial distribution.
# We assume that the frog initially sits on the east pad.
mu0 <- c(1,0)


# Compute the location distribution at time t for t in [0,T].
T <- 20
mu <- matrix(0,T+1,2)
mu[1,] <- mu0
for (t in 1:T) {
  mu[t+1,] <- mu[t,] %*% P
}

# Plot the results with respect to time.
plot(  0:T, mu[,1], type = 'o', col='blue', pch = 1, ylim = c(0,1), ann = FALSE)
points(0:T, mu[,2], type = 'o', col='red',  pch = 8)
title(xlab = 'Time', ylab = 'Pr(east) in blue,   Pr(west) in red', main = 'Frog in a lily pond')
