Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

A second Riddle(r), with a puzzle related with the integer set Ð={,12,3,…,N}, in that it summarises as

Given a random walk on Ð, starting at the middle N/2, with both end states being absorbing states, and a uniform random move left or right of the current value to the (integer) middle of the corresponding (left or right) integer interval, what is the average time to one absorbing state as a function of N?

Once the Markov transition matrix M associated with this random walk is defined, the probability of reaching an absorbing state in t steps can be derived from the successive powers of M by looking at the difference between the probabilities to be (already) absorbed at both t-1 and t steps. From which the average can be derived.

avexit <- function(N=100){
#transition matrix M for the walk
#1 and N+2 are trapping states
tranz=matrix(0,N+2,N+2)
tranz[1,1]=tranz[N+2,N+2]=1
for (i in 2:(N+1))
tranz[i,i+max(trunc((N+1-i)/2),1)]=tranz[i,i-max(trunc((i-2)/2),1)]=1/2
#probabilities of absorption
prowin=proloz=as.vector(0)
init=rep(0,N+2)
init[trunc((N+1)/2)]=1 #first position
curt=init
while(1-prowin[length(prowin)]-proloz[length(prowin)]>1e-10){
curt=curt%*%tranz
prowin=c(prowin,curt[1])
proloz=c(proloz,curt[N+2])}
#probability of new arrival in trapping state
probz=diff(prowin+proloz)
return(sum((2:length(proloz))*probz))}


leading to an almost linear connection between N and expected trapping time.