- #1
bradyj7
- 122
- 0
Hi,
I'm trying to simulate a discrete time time markov chain in matlab. Unfortunately I am neither a markov chain expert or a good MATLAB coder.
My problem is simple, I have a transition probability matrix with 100 states (100x100) and I want to simulate a 1000 steps beginning from state 1.
This is the code I have. I'd be grateful if somebody had time to look at the comments in the code to see if the code is correct for simulating a markov chain.
Kind Regards
I'm trying to simulate a discrete time time markov chain in matlab. Unfortunately I am neither a markov chain expert or a good MATLAB coder.
My problem is simple, I have a transition probability matrix with 100 states (100x100) and I want to simulate a 1000 steps beginning from state 1.
This is the code I have. I'd be grateful if somebody had time to look at the comments in the code to see if the code is correct for simulating a markov chain.
Kind Regards
Code:
Use a for-loop to loop n times for length you want.
transC = cumsum(trans,2); %cumulative sum of rows, we will use this to decide on the next step.
n = 1000;
states = zeros(1,n); %storage of states
states(1) = 1; %start at state 1 (or whatever)
for ii = 2:n
%Generate a random number and figure out where it falls in the cumulative sum of that state's trasition matrix row
[~,states(ii)] = histc(rand,transC(states(ii-1),:));
end