Hello!
I'm trying to code a random walk with the weight function :W(k)=k^alpha
.
The probability to choose the next position is :
W is the weight function defined earlier.
z0 is the initial weight of the vertex/position (in the code I choose z0 = 1)
Zn(x) is the number of time the vertex x has been visited.
Here you can see what I've tried to do:
Next_step = function(i,j,W){
l = nrow(W)
prob = rep(NA,4)
prob[1] = W[i+1,j]
prob[2] = W[i,j+1]
prob[3] = W[i,j-1]
prob[4] = W[i-1,j]
next_step = sample(1:4, 1,replace = TRUE, prob = prob)
if (next_step == 1) { # down
i = i+1
} else if (next_step == 2) { # right
j = j+1
} else if (next_step == 3) { # left
j= j-1
} else if (next_step == 4) { # up
i = i-1
}
return(c(i,j))
}
RW_2d_start = function(n,pas, alpha){
Grille = matrix(1, nrow = n+2, ncol = n+2)
Grille[c(1,n+2),] = 0
Grille[,c(1,n+2)]=0
W = Grille
axe_x = rep(NA,pas)
axe_y = rep(NA,pas)
d = rep(NA,2)
if (n%%2 != 0) {axe_x[1] = floor(n/2)} else {axe_x[1] = n/2} ## middle
axe_y[1] = axe_x[1]
Grille[axe_x[1],axe_y[1]] = Grille[axe_x[1],axe_y[1]]+1
W[axe_x[1],axe_y[1]]=Grille[axe_x[1],axe_y[1]]^alpha
for (i in 2:pas){
d[1:2]= Next_step(axe_x[i-1],axe_y[i-1],Grille)
axe_x[i]=d[1]
axe_y[i]=d[2]
Grille[d[1],d[2]] = Grille[d[1],d[2]]+1
W[d[1],d[2]]=Grille[d[1],d[2]]^alpha
}
return(list(path = data.frame(step = seq_along(axe_x), x = axe_x, y = axe_y),
Grille = Grille))
}
N = 100
pas = 5000
alpha = 1
res <- RW_2d_start(N,pas,alpha)
res_path <- res$path
plot1 <- ggplot(res_path) +
theme_classic() +
geom_path(aes(x = x, y = y),) +
labs(
title = "alpha",
x = "x position",
y = "y position")
plot1
Does anyone know if this is correct?
Thank you !