Skip to main content
. Author manuscript; available in PMC: 2015 Nov 1.
Published in final edited form as: Artif Intell. 2014 Nov 1;216:55–75. doi: 10.1016/j.artint.2014.05.006

Algorithm 3 HMM backward-filtering forward-sampling algorithm for block-sampling z1:T

1: let π0 and π = (π1|…|πK)T be the initial and transition distributions
2: let ut+K be the likelihoods p(yt | zt = k) for each k
3: let ζt+K be the backward messages p(yt+1:T | zt = k) at t for each k
4: normalize each ut to sum to 1, preventing underflow during computations
5: for t = 1,…,T do
6:   ũtut/ (1Tut)
7: end for
8: calculate backward messages over all time points
9: ζ̃T = 1
10: for t = T − 1,…,1 do
11:  transmit messages backward : τt+1ũt+1ζ̃t+1, ζtπτt+1
12:  normalize : ζ̃tζt/ (1Tζt)
13: end for
14: τ1ũ1ζ̃1
15: sample first time point
16: q1π0τ1, 1q1/(1Tq)
17: z1 ~ 1
18: sample other time points
19: for t = 2,…,T do
20:  qtπzt−1τt, tqt/ (1Tqt)
21:  zt ~ t
22: end for