diff options
| author | ziejd2 | 2017-09-28 15:04:40 -0500 |
|---|---|---|
| committer | ziejd2 | 2017-09-28 15:04:40 -0500 |
| commit | 8070dc963753142bb86c4ed698d91fd623ed28e7 (patch) | |
| tree | d0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/HMM/viterbi_path.m | |
| parent | 7cc31810d53176e805532b2789955f4eedbce6bb (diff) | |
| download | BNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz | |
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning. I am calling this BNW_1.02. It can be accessed at: compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/bnt-master/HMM/viterbi_path.m')
| -rw-r--r-- | sourcecodes/bnt-master/HMM/viterbi_path.m | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/HMM/viterbi_path.m b/sourcecodes/bnt-master/HMM/viterbi_path.m new file mode 100644 index 00000000..88010277 --- /dev/null +++ b/sourcecodes/bnt-master/HMM/viterbi_path.m @@ -0,0 +1,62 @@ +function path = viterbi_path(prior, transmat, obslik) +% VITERBI Find the most-probable (Viterbi) path through the HMM state trellis. +% path = viterbi(prior, transmat, obslik) +% +% Inputs: +% prior(i) = Pr(Q(1) = i) +% transmat(i,j) = Pr(Q(t+1)=j | Q(t)=i) +% obslik(i,t) = Pr(y(t) | Q(t)=i) +% +% Outputs: +% path(t) = q(t), where q1 ... qT is the argmax of the above expression. + + +% delta(j,t) = prob. of the best sequence of length t-1 and then going to state j, and O(1:t) +% psi(j,t) = the best predecessor state, given that we ended up in state j at t + +scaled = 1; + +T = size(obslik, 2); +prior = prior(:); +Q = length(prior); + +delta = zeros(Q,T); +psi = zeros(Q,T); +path = zeros(1,T); +scale = ones(1,T); + + +t=1; +delta(:,t) = prior .* obslik(:,t); +if scaled + [delta(:,t), n] = normalise(delta(:,t)); + scale(t) = 1/n; +end +psi(:,t) = 0; % arbitrary value, since there is no predecessor to t=1 +for t=2:T + for j=1:Q + [delta(j,t), psi(j,t)] = max(delta(:,t-1) .* transmat(:,j)); + delta(j,t) = delta(j,t) * obslik(j,t); + end + if scaled + [delta(:,t), n] = normalise(delta(:,t)); + scale(t) = 1/n; + end +end +[p, path(T)] = max(delta(:,T)); +for t=T-1:-1:1 + path(t) = psi(path(t+1),t+1); +end + +% If scaled==0, p = prob_path(best_path) +% If scaled==1, p = Pr(replace sum with max and proceed as in the scaled forwards algo) +% Both are different from p(data) as computed using the sum-product (forwards) algorithm + +if 0 +if scaled + loglik = -sum(log(scale)); + %loglik = prob_path(prior, transmat, obslik, path); +else + loglik = log(p); +end +end |
