comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function S = sample_mc_endstate(startprob, trans, endprob)
2 % SAMPLE_MC_ENDSTATE Generate a random sequence from a Markov chain until enter the endstate.
3 % seq = sample_mc(startprob, trans, endprob)
4
5 % add an end state
6 Q = size(trans,1);
7 transprob = zeros(Q,Q+1);
8 end_state = Q+1;
9 for i=1:Q
10 for j=1:Q
11 transprob(i,j) = (1-endprob(i)) * trans(i,j);
12 end
13 transprob(i,end_state) = endprob(i);
14 %assert(approxeq(sum(transprob(i,:)), 1))
15 end
16
17 S = [];
18 S(1) = sample_discrete(startprob);
19 t = 1;
20 p = endprob(S(t));
21 stop = (S(1) == end_state);
22 while ~stop
23 S(t+1) = sample_discrete(transprob(S(t),:));
24 stop = (S(t+1) == end_state);
25 t = t + 1;
26 end
27 S = S(1:end-1); % don't include end state