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


0

Add a comment

The financial market is not only made of stock options. Other financial products enable market actors to target specific aims. For example, an oil buyer like a flight company may want to cover the risk of increase in the price of oil.

I found a golden website. The blog of Esteban Moro. He uses R to work on networks. In particular he has done a really nice code to make some great videos of networks. This post is purely a copy of his code. I just changed a few arguments to change colors and to do my own network.

3

As you have certainly seen now, I like working on artificial neural networks. I have written a few posts about models with neural networks (Models to generate networks, Want to win to Guess Who and Study of spatial segregation).

1

I already talked about networks a few times in this blog. In particular, I had this approach to explain spatial segregation in a city or to solve the Guess Who? problem. However, one of the question is how to generate a good network.

The function apply() is certainly one of the most useful function. I was scared of it during a while and refused to use it. But it makes the code so much faster to write and so efficient that we can't afford not using it.

1

Have you ever played the board game "Guess who?".

If you want to choose randomly your next holidays destination, you are likely to process in a way which is certainly biased. Especially if you choose randomly the latitude and the longitude.

4

My previous post is about a method to simulate a Brownian motion. A friend of mine emailed me yesterday to tell me that this is useless if we do not know how to simulate a normally distributed variable.

The Brownian motion is certainly the most famous stochastic process (a random variable evolving in the time). It has been the first way to model a stock option price (Louis Bachelier's thesis in 1900).

1

The merge of two insurance companies enables to curb the probability of ruin by sharing the risk and the capital of the two companies.

For example, we can consider two insurance companies, A and B.

How to estimate PI when we only have R and the formula for the surface of a circle (Surface = PI * r * r)?

The estimation of this number has been one of the greatest challenge in the history of mathematics. PI is the ratio between a circle's circumference and diameter.

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.

I am currently doing an internship in England. Therefore, I keep alternating between French and English in my different emails and other forms of communication on the Internet. I have been surprised to see that some websites are able to recognize when I use French or when I use English.

The VIX (volatility index) is a financial index which measures the expectation of the volatility of the stock market index S&P 500 (SPX). The higher is the value of the VIX the higher are the expectations of important variations in the S&P 500 during the next month.

The Olympic Games have finished a couple of days ago. Two entire weeks of complete devotion for sport. Unfortunately I hadn’t got any ticket but I didn’t fail to watch many games on TV and internet.

Hello (New World!), 

My name is Edwin, I’m a 22 year-old French student in applied mathematics. In particular, I study probability, statistics and risk theory.

Blog Archive
Translate
Translate
Loading