biased random walk ?
Mostra commenti meno recenti
hey,
how can i do random walk ?
im trying to code a 1D biased random walk, with probability of p to right and 1-p to left, when the walker reaches the last element on the right side N - he returns to N-1,
i also need to calculate the time T that takes the walker reach the most left side (0)
then i need to plot the answer of T for different amount of walks the walker did.
heres what i did so far : , if someone can help me that would be amazing, thank you everybody :) ?
%random walk
function [T] = randomwalkHaya(p,N)
T=0;
K = N/2 ;
A = [1,N];
q = 1-p;
probability = [p,q];
for i = 1:400
indx = randi(2);
out = probability(indx);
if out == p
A(1,K+1) = 1 ;
K = K+1 ;
T = T+1 ;
else if out == q
A(1,K-1) = 1 ;
K = K-1 ;
T = T+1 ;
end
end
if A(1,N)==1
A(1,N)=A(1,N)-1;
A(1,N-1)=A(1,N-1)+1;
K = K-1 ;
T = T+1 ;
end
if A(0)==1
break ;
end
end
print(T);
end
6 Commenti
Walter Roberson
il 1 Nov 2020
Your random logic can be simplified to
if rand < p
with no q or probabilities or randi needed.
haia amihay
il 2 Nov 2020
Rik
il 2 Nov 2020
Using code like Walter suggested will allow you to do that.
Walter Roberson
il 2 Nov 2020
if rand < 0.8
go right
else
go left
end
John D'Errico
il 2 Nov 2020
Your code is way too complicated for what you are doing, but that is your choice. Anyway, as Walter suggests,
if rand < p
% Move right
...
else
% move left
...
end
will do what you want, and far more simply.
Next, you have this test:
if A(0)==1
break ;
end
But you need to recognize that MATLAB does not allow a zero index. A(0) will cause an error.
Other things... This next test is generally a bad idea:
if out == p
Never test for exact equality between two floating point numbers. In this case, it will work, but you are treading on thin ice, when there is no need to do so. USE THE TEST I SHOW ABOVE!
Next, what would happen if it took more than 400 steps of the random walk? Depending on p and the start point, you might never get to the edge. The fix is to use a while loop.
What else? Why do you need to store the entire random walk in A? You never do anything with it. You only return the TIME it took to reach the absorbing state on the left end.
How would I plot the time to reach the absorbing state? Honestly, I'd probably completely change your code, running a few million or so parallel random walks at once. When one of them hits the left end, drop it from the set, and record the time needed. So your simulation will get smaller and smaller over time.
And finally, I might do some reading about Markov chains, absorbing states. I might even learn if there is some theory to be read about the time required for a Markov chain to reach an absorbing state.
Adam Danz
il 2 Nov 2020
If you'd like to randomly chose the direction of the next step from 360 degrees rather than just right/left with a bias toward one direction, you could select the next direction from a von mises distribution.
Risposte (0)
Categorie
Scopri di più su Linear Algebra in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!