annotate toolboxes/FullBNT-1.0.7/HMM/mc_sample_endstate.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function S = sample_mc_endstate(startprob, trans, endprob)
wolffd@0 2 % SAMPLE_MC_ENDSTATE Generate a random sequence from a Markov chain until enter the endstate.
wolffd@0 3 % seq = sample_mc(startprob, trans, endprob)
wolffd@0 4
wolffd@0 5 % add an end state
wolffd@0 6 Q = size(trans,1);
wolffd@0 7 transprob = zeros(Q,Q+1);
wolffd@0 8 end_state = Q+1;
wolffd@0 9 for i=1:Q
wolffd@0 10 for j=1:Q
wolffd@0 11 transprob(i,j) = (1-endprob(i)) * trans(i,j);
wolffd@0 12 end
wolffd@0 13 transprob(i,end_state) = endprob(i);
wolffd@0 14 %assert(approxeq(sum(transprob(i,:)), 1))
wolffd@0 15 end
wolffd@0 16
wolffd@0 17 S = [];
wolffd@0 18 S(1) = sample_discrete(startprob);
wolffd@0 19 t = 1;
wolffd@0 20 p = endprob(S(t));
wolffd@0 21 stop = (S(1) == end_state);
wolffd@0 22 while ~stop
wolffd@0 23 S(t+1) = sample_discrete(transprob(S(t),:));
wolffd@0 24 stop = (S(t+1) == end_state);
wolffd@0 25 t = t + 1;
wolffd@0 26 end
wolffd@0 27 S = S(1:end-1); % don't include end state