**ProbaPerception**, and kindly contributed to R-bloggers)

I was in a party last night and a guy was totally drunk. Not just the guy who had a few drinks and speaks a bit too loud, but the one who is not very likely to remember what he has done during his night, but who is rather very likely to suffer from a huge headache today. The guy was literally randomly walking. One step on the left, another forward, then another backward. I immediately remembered one of my courses where our teacher was talking about random walk process. A random walk process, is a stochastic process which represents something moving randomly on a map. It may be something else than a map, but we will keep focus on a map to make it simpler.

Many questions may be of interest in such a model. The question I was wondering yesterday night is “how long would it take to this guy to go out from the garden if he was really walking randomly?”. Here, we consider the guy to be in a map where you can go toward South-East, North-East, South-West or North-West. Each direction being chosen with the same probability 0.25.

Such a random process is illustrated by the following plots.

A few steps of a long random walk for a garden with 75 units long sides. This process needed 4192 steps to stop. |

The question was “how long would it take to this guy to go out of the garden if he was really walking randomly?”. Obviously, the answer depends on the size of the garden. We will consider a garden which is a square. The length of a side will vary and we will measure how many steps the process needs to reach one of the side of the garden. We then make the size of the garden change to estimate the number of steps according to the size. The following plot illustrates the number of steps needed according to the size of the garden. Without any surprise, the bigger is the garden, the longer the time our poor little guy will need. Besides, the variance of the required time seems to increase with the size of the garden.

Now I would like to estimate with a quantitative approach these observations. I will assume that the number of required steps increases exponentially. I do an exponential regression to estimate it. In other words I assume that numberStep = a*size^b and I want to estimate the couple (a,b). In the simulation I have done, I found a = 0.759 which is not considered as significantly different from 1 (which is the neutral element for multiplication) and b = 1.91. If I use these values, I can estimate the number of steps I would need as shown by the red line in the next plot.

__The code (R):__

#The function to randomly assign the movement

decision = function(){

a = runif(1)

if(a<=0.25){return(c(1,1))}

else if(a<= 0.5){return(c(1,-1))}

else if(a<= 0.75){return(c(-1,1))}

else {return(c(-1,-1))}

}

#The simulation for different size

long = 150

record = 1:long

for (max in 1 : long){

position = matrix(0, nrow = 2, ncol = 1)

k = 0

test = FALSE

while(!test){

k = k+1

dec = decision()

pos1 = position[1, length(position[1,])] + dec[1]

pos2 = position[2, length(position[2,])] + dec[2]

position = cbind(position, c(pos1, pos2))

if(abs(pos1)>max | abs(pos2) > max){

test = TRUE

}

record[max] = k

}

}

plot(record, type = ‘l’, xlab = ‘Size of the garden’, ylab = ‘Number of steps needed’)

# Plot of one random walk process

max = 75

position = matrix(0, nrow = 2, ncol = 1)

k = 0

test = FALSE

while(!test){

k = k+1

dec = decision()

pos1 = position[1, length(position[1,])] + dec[1]

pos2 = position[2, length(position[2,])] + dec[2]

position = cbind(position, c(pos1, pos2))

mypath = file.path(“U:”,”Blog”,”Post10″,”Plot”, paste(“myplot”, k, “.png”, sep = “”))

png(file=mypath)

mytitle = paste(“my title is”, k)

plot(position[1,], position[2,], type = ‘l’, xlim = c(-max,max), ylim = c(-max, max), xlab = ”, ylab = ”)

dev.off()

if(abs(pos1)>max | abs(pos2) > max){

test = TRUE

}

}

# The exponential regression

fit = lm(log(record)~log(1:150))

exp(fit$coef[1])

# a = 0.759

fit$coef[2]

# b = 1.91

**leave a comment**for the author, please follow the link and comment on their blog:

**ProbaPerception**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...