Would anybody be able to help me simulate a discrete time markov chain in Matlab?
I have a transition probability matrix with 100 states (100x100) and I'd like to simulate 1000 steps with the initial state as 1.
I'd appreciate any help as I've been trying to do this myself all week with no success and have not been able to source suitable code online.
No products are associated with this question.
chack the following for a 2*2 transition matrix and for 1000 states begining at 1:
transition_probabilities = [0.1 0.9;0.8 0.2]; starting_value = 1; chain_length = 1000;
chain = zeros(1,chain_length); chain(1)=starting_value;
for i=2:chain_length this_step_distribution = transition_probabilities(chain(i-1),:); cumulative_distribution = cumsum(this_step_distribution);
r = rand();
chain(i) = find(cumulative_distribution>r,1); end % provides chain = 1 2 1 2 1 2 1 2 1 1 2 1 2 1 2....
If you also have the emissions matrix, you can use hmmgenerate()
Pseudo-ish-code (from my understanding, (disclosure: not a Markov Model expert by any means))
Use a for-loop to loop n times for length you want. S
transC = [zeros(size(trans,1),1), cumsum(trans,2)]; %cumulative sum of rows, we will use this to decide on the next step.
n = 10; 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),:));
Play games and win prizes!Learn more