maximum distance in a random walk

Hello,

I'm trying to code the maximum distance of a random walk from the start. However, my code is not giving me something that I can interpret. Indeed, I want to see whether the random walk is going far from the starting points or not according to the value of the reinforcement alpha.
My code is not giving me a proper answer to that. I don't know if it's normal to find the results that I find or not?

RandomW_1D = function(N,pas, alpha){
  
  d = rep(1,N) 
  nextDir = rep(NA,pas)
  prob = rep(0.5,2) 
  
  if (N%%2 != 0) {suiv = floor(N/2)} else {suiv = N/2}  ## the starting point is the middle 
  
  nextDir[1] = suiv
  d[suiv] = d[suiv]+alpha
  
  for (i in 2:pas){
    if (suiv+1 >= length(d)){
      d[suiv] = d[suiv] + alpha 
      nextDir[i] = suiv-1 
      suiv = nextDir[i] 
    } else if(suiv-1 == 0){
      d[suiv] = d[suiv] + alpha
      nextDir[i] = suiv+1
      suiv = nextDir[i]
    } else {
      d[suiv] = d[suiv]  + alpha
      prob[1]=d[suiv-1]/(d[suiv-1]+d[suiv+1])
      prob[2] = d[suiv+1]/(d[suiv-1]+d[suiv+1])
      nextDir[i] = sample(c(suiv-1,suiv+1),1,replace = TRUE, prob = prob ) 
      suiv = nextDir[i]
    }
  }
  d[suiv] = d[suiv]  + alpha 
  return(list(path = data.frame(step = seq_along(c(1:pas)), x = c(1:pas), y = nextDir),Grille = d))
}

distance = function(RandomW_1D,depart){
  res = RandomW_1D(N,pas,alpha)
  res_path <- res$path
  min = min(res$path[3])
  max = max(res$path[3])
  dist1 = depart - abs(max)
  dist2 = depart - abs(min)
  if (abs(dist1)<=abs(dist2)) { distance = abs(dist2)} else { distance = abs(dist1)} 
}

dist_moyenne = function(N,pas,alpha_values){
  dist = rep(NA,N)
  moy_dist = rep(NA,length(alpha_values))
  if (N%%2 != 0) {suiv = floor(N/2)} else {suiv = N/2}
  for (j in 1:length(alpha_values)){
    for (i in 1:N){
      res = RandomW_1D(N,pas,alpha_values[j])
      dist[i] = distance(res,suiv)
    }
    moy_dist[j] = mean(dist)
  }
  plot(alpha_values,moy_dist, type = 'l')
}

N = 100
pas = 1000
alpha_values <- seq(from = 0.01, to = 1, by = 0.01)
dist_moyenne(N,pas,alpha_values)

Does someone have an idea?

Thank you!

It does seem overly complicated.
What are your actual requirements ?
what paramaters do you need to take and what actions do you need to perform with them ?

Here is an implementation of 1dimensional random walk, perhaps you can further adapt it to your needs.


randomwalk_1d <- function(number_of_steps,
                          step_sizes,
                          step_size_probability,
                          setseed=123){
  set.seed(setseed)
  ssp <- sum(step_size_probability)
  
  # param checking
  if(ssp != 1)
    stop(paste0("sum(step_size_probability) != 1, rather = ", ssp))
  
  if(length(step_sizes) != length(step_size_probability))
    stop(paste0("length of step size vector (",length(step_sizes), 
                ") does not match length of step size probability vector (", 
                length(step_size_probability), ")"))
  # calculate
  (steps <- sample(step_sizes,
                   size=number_of_steps,
                   replace=TRUE,
                   prob=step_size_probability))
  
  (journey <- cumsum(steps))
  
  #give outputs
  par(mfrow=c(2,1),mar=c(1,4,1,4))
  plot(steps)
  plot(journey)
  list(steps=steps,
       journey=journey,
       max_distance_from_origin = max(journey))
}

# example of useage
randomwalk_1d(number_of_steps = 100,
              step_sizes = c(-1,0,1),
              step_size_probability = c(.3,.3,.4))

Hi your code is interesting however I don't know if it's answering my question.

I've got a reinforced random walk : every time the walk choose a vertex, we add a weight of alpha value to this vertex.

The question is : if alpha has a value close to 0, does the walk go further from the starting point? and vice-versa if alpha has a value close to 1, does it remain close to the starting point?

I don't know if it's clear now but thank you anyway for your answer !

randomwalk_1d <- function(number_of_steps,
                          step_sizes,
                          init_step_size_prob,
                          alpha = 0,
                          setseed=123,
                          plot=TRUE){
  set.seed(setseed)
  ssp <- sum(init_step_size_prob)
  
  # param checking
  if(ssp != 1)
    stop(paste0("sum(init_step_size_prob) != 1, rather = ", ssp))
  
  if(length(step_sizes) != length(init_step_size_prob))
    stop(paste0("length of step size vector (",length(step_sizes), 
                ") does not match length of step size probability vector (", 
                length(init_step_size_prob), ")"))
  # calculate
  
    steps <- numeric(number_of_steps)
    alpha_matrix <- matrix(data = 0,
                           nrow = number_of_steps+1,
                           ncol = length(init_step_size_prob))
    alpha_matrix[1,] <- init_step_size_prob
    for(step in seq_len(number_of_steps))
    {
   steps[step] <- sample(step_sizes,
                   size=1,
                   replace=TRUE,
                   prob=alpha_matrix[step,])

   alpha_matrix[step+1,] <- alpha_matrix[step,]
   step_choice_id <- which(steps[step] == step_sizes)
   alpha_matrix[step+1,step_choice_id] <-     alpha_matrix[step+1,step_choice_id] + alpha
   divisor_to_use <-sum(   alpha_matrix[step+1,])
   alpha_matrix[step+1,] <- alpha_matrix[step+1,] / divisor_to_use
    }
  (journey <- cumsum(steps))
  max_pos = max(journey)
  min_pos = min(journey)
  absolute_min_pos = abs(min_pos)
  if(abs(min_pos)>max_pos){
    max_distance_from_origin <- min_pos
  } else if(abs(min_pos)==max_pos){
    max_distance_from_origin <- sample(c(min_pos,max_pos),size = 1)
  } else {
    max_distance_from_origin <- max_pos
  }
  #give outputs
    if(plot){
  par(mfrow=c(2,1),mar=c(1,4,1,4))
  plot(steps)
  plot(journey)}
  list(       alpha_matrix=alpha_matrix,
              steps=steps,
       journey=journey,
       max_distance_from_origin = max_distance_from_origin)
}

# example of useage
randomwalk_1d(number_of_steps = 100,
              step_sizes = c(-1,1),
              alpha=0,
              init_step_size_prob = c(.5,.5))

#get the max distance from 100 experiments on alpha zero
(res_a0 <- map_dbl(1:100,~randomwalk_1d(setseed = .x,
                                       plot = FALSE,
                                       number_of_steps = 100,
                                       step_sizes = c(-1,1),
                                       alpha=0,
                                       init_step_size_prob = c(.5,.5))$max_distance_from_origin))
#get the max distance from 100 experiments on alpha 1
(res_a1 <- map_dbl(1:100,~randomwalk_1d(setseed = .x,
                                   plot = FALSE,
                                   number_of_steps = 100,
                                   step_sizes = c(-1,1),
                                   alpha=1,
                                   init_step_size_prob = c(.5,.5))$max_distance_from_origin))

table(res_a0)
table(res_a1)
par(mfrow=c(2,1),mar=c(3,4,2,4))
plot(table(res_a1),col="red")
plot(table(res_a0),col="blue")

thank you this is very helpful !

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.