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. In this case it is possible to buy on the financial market what is known as a "Call" or a "Call Option".

A Call Option is a contract between two counterparties (the flight company and a financial actor). The buyer of the Call has the opportunity but not the obligation to buy a certain  quantity of a certain product (called the underlying) at a certain date (the maturity) for a certain price (the strike).

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.

To create the network, I used the  Barabási-Albert algorithm that you can find at the end of the post on the different algorithms for networks. Igraph is the library which has been used.
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).

Unfortunately, I missed so far a nice and pleasant aspect of networks : its graphical approach. Indeed, plots of neural networks are often really nice and really useful to understand the network.

Sometimes such a graph can point out some characteristics of the network.
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. Indeed, I aim to study strategy to split a network, but I need first to work with a realistic neural network. I could have downloaded data of a network, but I'd rather study the different models proposed to generate neural networks.

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. If you are like me, that you refuse to use apply because it is scary, read the following lines, it will help you. You want to know how to use apply() in general, with a home-made function or with several parameters ? Then, go to see the following examples.
1

Have you ever played the board game "Guess who?". For those who have not experienced childhood (because it might be the only reason to ignore this board game), this is a game consisting in trying to guess who the opponent player is thinking of among a list of characters - we will call the one he chooses the "chosen character". These characters have several characteristics such as gender, having brown hair or wearing glasses.

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. A bit like they do in this lovely advertising (For those of you who do not speak French, this is about a couple who have won the national gamble prize and have to decide their next travel. The husband randomly picks Australia and the wife is complaining : "Not again!").
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.

My first remark is: use the rnorm() function if the quality of your simulation is not too important (Later, I'll try to explain you why the R "default random generation" functions are not perfect). However, it may be fun to generate a normal distribution from a simple uniform distribution.

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).

The reason why is easy to understand, a Brownian motion is graphically very similar to the historical price of a stock option.
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. A is a well known insurance company with a big capital and is dealing with a risk with a low variance. We will assume that the global risk of all its customers follow a chi-square distribution with one degree of freedom.
Blog Archive
Translate
Translate
Loading