99 Fishing
99 Fishing

Reputation: 5

Creating graphs that show the distribution in space of a large number of 2D Random Walks at three different time points

So essentially I have this code here that I can use to generate a 2D Random Walk discretely along N number of steps with M number of walkers. I can plot them all on the same graph here.

clc;
clearvars;
N = 500; % Length of the x-axis, also known as the length of the random walks.
M = 3; % The amount of random walks.
x_t(1) = 0;
y_t(1) = 0;
for m=1:M
    for n = 1:N % Looping all values of N into x_t(n).
        A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
        x_t(n+1) = x_t(n) + A;
        A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
        y_t(n+1) = y_t(n) + A;
    end
    plot(x_t, y_t);
    hold on
end
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
axis square;

Now, I want to be able to Create graphs that show the distribution in space of the positions of a large number (e.g. n = 1000) random walkers, at three different time points (e.g. t = 100, 200 and 300 or any three time points really).

I'm not sure how to go about this, I need to turn this into a function and iterate it through itself three different times and store the coordinates? I have a rough idea but iffy on actually implementing. I'd assume the safest and least messy way would be to use subplot() to create all three plots together in the same figure.

Appreciate any assistance!

Upvotes: 0

Views: 368

Answers (3)

obchardon
obchardon

Reputation: 10792

You can use cumsum to linearize the process. Basically you only want to cumsum a random matrix composed of [-1 and 1].

clc;
close all;
M = 50; % The amount of random walks.
steps = [10,200,1000]; % here we analyse the step 10,200 and 1000
cc = hsv(length(steps)); % manage the color of the plot
%generation of each random walk
x = sign(randn(max(steps),M));
y = sign(randn(max(steps),M));
xs = cumsum(x);
xval = xs(steps,:);
ys = cumsum(y);
yval = ys(steps,:);

hold on
for n=1:length(steps)
    plot(xval(n,:),yval(n,:),'o','markersize',1,'color',cc(n,:),'MarkerFaceColor',cc(n,:));
end

legend('10','200','1000')
axis square
grid on;

Results:

enter image description here

EDIT:

Thanks to @LuisMendo that answered my question here, you can use a binomial distribution to get the same result:

steps = [10,200,10000];
cc = hsv(length(steps)); % manage the color of the plot
M = 50;
DV = [-1 1];
p = .5; % probability of DV(2)
% Using the @LuisMendo binomial solution:
for ii = 1:length(steps)
    SDUDx(ii,:) = (DV(2)-DV(1))*binornd(steps(ii), p, M, 1)+DV(1)*steps(ii);
    SDUDy(ii,:) = (DV(2)-DV(1))*binornd(steps(ii), p, M, 1)+DV(1)*steps(ii);
end
hold on
for n=1:length(steps)
    plot(SDUDx(n,:),SDUDy(n,:),'o','markersize',1,'color',cc(n,:),'MarkerFaceColor',cc(n,:));
end
legend('10','200','1000')
axis square
grid on;

What is the advantage ? Even if you have a big number of steps, let's say 1000000, matlab can handle it. Because in the first solution you have a bruteforce solution, and in the second case a statistical solution.

Upvotes: 1

Adam Klingenberger
Adam Klingenberger

Reputation: 61

I will demonstrate the 1-dimensional case for clarity; you only need to implement this for each dimension you add.

Model N steps for M walkers using an NxM matrix.

>> N = 5;
>> M = 4;
>> steps = sign(randn(N,M));

steps =

     1     1     1     1
    -1     1    -1     1
     1    -1    -1    -1
     1     1    -1     1
     1    -1    -1    -1

For plotting, it is useful to make a second NxM matrix s containing the updated positions after each step, where s(N,M) gives the position of walker M after N steps.

Use cumsum to vectorize instead of looping.

>> s = cumsum(steps)

s =

     1     1     1     1
     0     2     0     2
     1     1    -1     1
     2     2    -2     2
     3     1    -3     1

To prevent plot redraw after each new line, use hold on.

>> figure; hold on
>> plot(1:N, s(1:N, 1:M), 'marker', '.', 'markersize', 20, 'linewidth', 3)
>> xlabel('Number of steps'); ylabel('Position')

The output plot looks like this: picture

This method scales very well to 2- and 3-dimensional random walks.

Upvotes: 0

Floris
Floris

Reputation: 518

If you want to show the distribution of a large number, say 1000, of these points, I would say the most suitable way of plotting is as a 'point cloud' using scatter. Then you create an array of N points for both the x and the y coordinate, and let it compute the coordinate in a loop for i = 1:Nt, where Nt will be 100, 200, or 300 as you describe. Something along the lines of the following:

N = 500;
x_t = zeros(N,1);
y_t = zeros(N,1);
Nt = 100;
for tidx = 1:Nt
    x_t = x_t + sign(randn(N,1));
    y_t = y_t + sign(randn(N,1));
end
scatter(x_t,y_t,'k*');

This will give you N x and y coordinates generated in the same way as in the sample you provided.

One thing to keep in mind is that sign(0)=0, so I suppose there is a chance (admittedly a small one) of not altering the coordinate. I am not sure if you intended this behaviour to be possible (a walker standing still)?

Upvotes: 0

Related Questions