Simulation of random walk
16 views (last 30 days)
Show older comments
I am simulating a random walk using markov chains . In the case below a 3x3 grid (9 nodes) is being used. I want to make 100 moves and store the postiion (node number) into an array. When I run the code below I get a list of the 100 postions reached but only the last node reached is stored. I want to find the number of times certain nodes are landed on within the 100 iterations and so need to store the 'j' values into an array but I am not sure how to do this. Any help would be greatfully recieved.
T = create_transition(3); % for example, a 3x3
k = 1; % start at node 1 (you have free choice)
trans_pdf = T(k,:); % get the PDF for node i
j = next_node(trans_pdf); % j now holds the destination node for this step in the simulation
for iter = 1:100
trans_pdf = T(j,:); % get the PDF for node i
j = next_node(trans_pdf); % j now holds the destination node for this step in the simulation
node_visited(i)=j
end
Node_1_visits = sum(j == 1);%Top left
Node_5_visits = sum(j== 5);% Middle
Node_6_visits = sum(j== 6);%Middle Right
function dest = next_node(trans_pdf)
cdf = cumsum(trans_pdf);
p = rand(); % uniform random
bool_test = p<=cdf;
[~, dest] = max(bool_test); % find the location of the first True
end
0 Comments
Accepted Answer
Jon
on 14 Jan 2022
Edited: Jon
on 14 Jan 2022
It looks like you already "store the j values into the array node_visited.
So it looks like you should be doing your summaries, e.g.
Node_6_visits = sum(node_visited==6)
Also, if you post more code, it is good to use the "code" button on the MATLAB answers toolbar, this will format it as it appears in the MATLAB editor, making it more readable, and also easier for others to copy if they want to try running it.
2 Comments
Jon
on 14 Jan 2022
You assign node_visited using
node_visited(i)=j
But it looks like the index i does not change in your loop (actually I don't even see where it is assigned in the fragment of code you supplied). Instead you should assign to an index that is incremented by the for statement so
node_visited(iter)=j
More Answers (1)
Image Analyst
on 22 Apr 2022
For what it's worth (to others), I'm attaching all my random walk demos.
0 Comments
See Also
Categories
Find more on Markov Chain Models in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!