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!