about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/HMM/mc_sample_endstate.m
diff options
context:
space:
mode:
authorziejd22018-03-14 23:23:33 -0500
committerGitHub2018-03-14 23:23:33 -0500
commit1ff6baa44e22b91eefb48aea6f3befa078c0489b (patch)
treee0fd79d2e32fd2aedda2eadaed0f19af3514c520 /sourcecodes/bnt-master/HMM/mc_sample_endstate.m
parent6882395afdadf4e982b25b5215071a0932730950 (diff)
parentc80226899f5cdd9f11c163817d59445213f5bef0 (diff)
downloadBNW-1ff6baa44e22b91eefb48aea6f3befa078c0489b.tar.gz
Merge pull request #1 from ziejd2/octave_php_separate
Octave php separate
Diffstat (limited to 'sourcecodes/bnt-master/HMM/mc_sample_endstate.m')
-rw-r--r--sourcecodes/bnt-master/HMM/mc_sample_endstate.m27
1 files changed, 27 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/HMM/mc_sample_endstate.m b/sourcecodes/bnt-master/HMM/mc_sample_endstate.m
new file mode 100644
index 00000000..8dc9e0ba
--- /dev/null
+++ b/sourcecodes/bnt-master/HMM/mc_sample_endstate.m
@@ -0,0 +1,27 @@
+function S = sample_mc_endstate(startprob, trans, endprob)
+% SAMPLE_MC_ENDSTATE Generate a random sequence from a Markov chain until enter the endstate.
+% seq = sample_mc(startprob, trans, endprob)
+
+% add an end state
+Q = size(trans,1);
+transprob = zeros(Q,Q+1);
+end_state = Q+1;
+for i=1:Q
+  for j=1:Q
+    transprob(i,j) = (1-endprob(i)) * trans(i,j);
+  end
+  transprob(i,end_state) = endprob(i);
+  %assert(approxeq(sum(transprob(i,:)), 1))
+end                  
+
+S = [];
+S(1) = sample_discrete(startprob);
+t = 1;
+p = endprob(S(t));
+stop = (S(1) == end_state);
+while ~stop
+  S(t+1) = sample_discrete(transprob(S(t),:));
+  stop = (S(t+1) == end_state);
+  t = t + 1;
+end
+S = S(1:end-1); % don't include end state