memoryless markov chain in matlab

To simulate and analyze a memoryless Markov chain in MATLAB, you can follow these steps:

  1. Define the transition probability matrix P, where P(i,j) is the probability of transitioning from state i to state j.

  2. Define an initial state distribution vector pi, where pi(i) is the probability of starting in state i.

  3. Use the mcadisp function from the MATLAB Communications Toolbox to create a comm.MCBase object that represents the Markov chain.

main.m
mc = mcadisp(P, pi);
21 chars
2 lines
  1. Simulate a sequence of states by calling the simulate method of the comm.MCBase object.
main.m
numSamples = 1000;
x = mc.simulate(numSamples);
48 chars
3 lines

This generates a row vector x of length numSamples, where each element is an integer corresponding to a state in the Markov chain.

  1. Compute the state probabilities by counting the number of occurrences of each state in the simulated sequence.
main.m
numStates = size(P, 1); % number of states
stateProbs = histcounts(x, 1:numStates+1) / numSamples;
99 chars
3 lines
  1. Compute the transition probabilities by counting the number of transitions between each pair of states in the simulated sequence.
main.m
transCounts = zeros(numStates);
for i = 1:length(x)-1
    transCounts(x(i), x(i+1)) = transCounts(x(i), x(i+1)) + 1;
end
transProbs = transCounts ./ sum(transCounts, 2);
170 chars
6 lines

Note that a memoryless Markov chain has the property that the transition probabilities do not depend on the past states, so you can simulate and analyze it without considering the history beyond the current state.

gistlibby LogSnag