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

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function S = sample_mc_endstate(startprob, trans, endprob)
Daniel@0 2 % SAMPLE_MC_ENDSTATE Generate a random sequence from a Markov chain until enter the endstate.
Daniel@0 3 % seq = sample_mc(startprob, trans, endprob)
Daniel@0 4
Daniel@0 5 % add an end state
Daniel@0 6 Q = size(trans,1);
Daniel@0 7 transprob = zeros(Q,Q+1);
Daniel@0 8 end_state = Q+1;
Daniel@0 9 for i=1:Q
Daniel@0 10 for j=1:Q
Daniel@0 11 transprob(i,j) = (1-endprob(i)) * trans(i,j);
Daniel@0 12 end
Daniel@0 13 transprob(i,end_state) = endprob(i);
Daniel@0 14 %assert(approxeq(sum(transprob(i,:)), 1))
Daniel@0 15 end
Daniel@0 16
Daniel@0 17 S = [];
Daniel@0 18 S(1) = sample_discrete(startprob);
Daniel@0 19 t = 1;
Daniel@0 20 p = endprob(S(t));
Daniel@0 21 stop = (S(1) == end_state);
Daniel@0 22 while ~stop
Daniel@0 23 S(t+1) = sample_discrete(transprob(S(t),:));
Daniel@0 24 stop = (S(t+1) == end_state);
Daniel@0 25 t = t + 1;
Daniel@0 26 end
Daniel@0 27 S = S(1:end-1); % don't include end state