# The probability to choose a direction depending on the previous choices?

Hello!

I want to simulate the probability for a particle to choose the right direction in a bifurcation depending on the number of time it has chosen that direction.

In other words, when a particle is moving on Z, and if the particle is in position k, it's next position will depend on the number of time the particle have chosen the position k-1 and k+1. so P(X=k+1|X=k,Y(k+1)=n) will increase or decrease according to n (with Y(k+1) the number of time the particle have chosen k+1).

``````## I wanted to begin with a simple simulation on Z

Z = c(-2,1,0,1,2)
k = c()
k=0
for (i in 1:length(Z)){
k[i]=sample(c(Z[i-1],Z[i+1]),1)
}
``````

it's seems simple but I don't know how to integrate the fact that the choice depends on the previous choices in my code.

Does anyone have an idea?

Thank you!

Hi,

It seems you're trying to build some sort of Markov chain, but could you explain in some more detail (maybe graphically) what the numbers in this vector Z represent and provide a few examples with numeric values. It's not clear what the k+1 and k-1 mean as well...

PJ

Hello,

Z represents integers : I choose randomly integer numbers from -2 to 2 (to make it simple).

For example : if a particle is in the position 0, its next position will be 1 ou -1 depending on its previous choices. If it has already chosen the position 1, the probability to choose the position 1 is higher than the probability to choose the position -1.

To give you an another illustration for the k, k+1 and k-1: there is a trajectory and in the position k , a particle have to choose its next position k+1 or k-1. This depends on if the particle have already been in k-1 or k+1. The more a position is chosen, the more the particle will choose that position again.

Here another example with ants:
there is a simple bifurcation where some ants have to choose between the left and right direction. When an ant choose a direction, it leaves some pheromones on it. So, the next ants will highly choose the branch with pheromones on it. The more a branch have pheromones, the more the ants will choose that branch of the bifurcation.

I hope it's clear and you understand better.

Thank you!

Hi,

Here is one algorithm I think might do what you are looking for, though there are a lot of ways to define direction and rules. In my case, there is a grid and you can go up, down, left, and right, but cannot go outside the grid or return from the same position you just came from.

The example below shows the final grid with how often each position was visited, and a dataframe that shows the trace of the path. Note that it's easy to start going in circles as revisiting the previous path is reinforced as you were asking.

``````library(dplyr)

set.seed(2) #Only needed for reproducibility

#Create the grid
nRow = 10
nCol = 10
myGrid = matrix(1, nrow = nRow, ncol = nCol)

#Starting position
x = 1 # x-axis is horizontal (row), with 0 at top
y = 1 # y-axis is vertical (column), with 0 at top

nSteps = 25

#Keep track of previous direction and path
prevDir = NULL
path = data.frame()

#Run algorithm for n steps
for(i in 1:nSteps){

myGrid[y,x] = myGrid[y,x] + 1

#Get the values up, down, left and right (0 if outside the grid)
prob = c(max(0,myGrid[y-1,x]), max(0,myGrid[y,(x+1) %% (nCol+1)]),
max(0,myGrid[(y+1) %% (nRow+1),x]), max(0,myGrid[y,x-1]))

#Prevent going back from where we came before (comment out if allowed)
prob[prevDir] = 0

#Calculate the probability of moving to any of the directions
# based on the times it has been visited before
prob = prob / sum(prob)

#Pick a direction (and save the previous one)
nextDir = sample(1:4, 1, prob = prob)
prevDir = (nextDir+1) %% 4 + 1 #e.g. When we go up (1), do not go down (3) next step

#Keep track of the path (comment out if not needed)
path = bind_rows(path, list(x = x, y = y,
dir = c("up", "right", "down", "left")[nextDir]))

#Get the next x-coordinate
x = case_when(
nextDir == 2 ~ x + 1,
nextDir == 4 ~ x - 1,
TRUE ~ x
)

#Get the next y-coordinate
y = case_when(
nextDir == 1 ~ y - 1,
nextDir == 3 ~ y + 1,
TRUE ~ y
)

}

myGrid - 1 #Remove the default 1 to see how often a cell was visited
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    2    1    0    0    0    0    0    0    0     0
#>  [2,]    1    2    0    1    1    0    0    0    0     0
#>  [3,]    0    2    3    2    1    0    0    0    0     0
#>  [4,]    0    0    2    1    0    0    0    0    0     0
#>  [5,]    0    0    1    1    1    0    0    0    0     0
#>  [6,]    0    0    1    1    1    0    0    0    0     0
#>  [7,]    0    0    0    0    0    0    0    0    0     0
#>  [8,]    0    0    0    0    0    0    0    0    0     0
#>  [9,]    0    0    0    0    0    0    0    0    0     0
#> [10,]    0    0    0    0    0    0    0    0    0     0
paste(path\$dir, collapse = " - ") #The full path (starting at 1,1 (top left))
#>  "right - down - down - right - right - right - up - left -
# down - left - down - right - down - right - down - left - left -
# up - up - up - left - up - left - up - right"
``````

Created on 2022-04-02 by the reprex package (v2.0.1)

Hope this helps,
PJ

Thank you ! this helps a lot !

This topic was automatically closed 7 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.