I am using the simulate function (inside the Econometrics Toolbox of Matlab) to generate random walk for a discrete time markov chain (dtmc).
However, I need to simulate a random walk with the length of 18million steps and I realize the the bottleneck is mainly caused by the existing implementation which samples one value at a time.
I read that it would be faster if I can vectorize the randsample(V, 1, true, P) call but I couldn't quite figure out the exact translation of the loop implementation to a singular randomsample call. P is my transition probability matrix with non-negative values and the sum of each row is 1. All runs start with the same initial state which is the 1st state in the X0 with the probability of 1 (and the rest of the values in the X0 are 0).
Below is the code for the standard (loop) implementation of the sampling:
%x number of steps are created (x number of walk)
X = zeros(1+numSteps,numSims);
for j = 1:numSims
simState = find(X0~=0,1);
X(1,j) = simState;
X0(simState) = X0(simState)-1;
for i = 2:(1+numSteps)
u = rand;
simState = find(u < cumsum(P(simState,:)),1);
X(i,j) = simState;
end
end
I tried randsample(X, 1, true, P(simState,:));
but I get the
W must have length 18000000.
error. In addition, I am not exactly sure how I can actually make sure the first element of the generated random walk is fixed and it is the first state and I just need to generate the random walk from 2nd step and on.
Aucun commentaire:
Enregistrer un commentaire