Skip to content

Commit

Permalink
fix hmmViterbi llh
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Oct 10, 2016
1 parent 19e39db commit a793365
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
5 changes: 3 additions & 2 deletions chapter13/HMM/hmmViterbi.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
function z = hmmViterbi(x, model)
function [z, llh] = hmmViterbi(x, model)
% Viterbi algorithm calculated in log scale to improve numerical stability.
% This is a wrapper function which transform input and call underlying algorithm
% Input:
% x: 1 x n integer vector which is the sequence of observations
% model: model structure
% Output:
% z: 1 x n latent state
% llh: loglikelihood
% Written by Mo Chen ([email protected]).
A = model.A;
E = model.E;
Expand All @@ -15,4 +16,4 @@
d = max(x);
X = sparse(x,1:n,1,d,n);
M = E*X;
z = hmmViterbi_(M, A, s);
[z,llh] = hmmViterbi_(M, A, s);
8 changes: 4 additions & 4 deletions chapter13/HMM/hmmViterbi_.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function [z, p] = hmmViterbi_(M, A, s)
function [z, llh] = hmmViterbi_(M, A, s)
% Implmentation function of Viterbi algorithm.
% Input:
% M: k x n emmision data matrix M=E*X
% A: k x k transition matrix
% s: k x 1 starting probability (prior)
% Output:
% z: 1 x n latent state
% p: 1 x n probability
% llh: loglikelihood
% Written by Mo Chen ([email protected]).
[k,n] = size(M);
Z = zeros(k,n);
Expand All @@ -20,6 +20,6 @@
Z = Z(idx,:);
Z(:,t) = 1:k;
end
[v,idx] = max(v);
[llh,idx] = max(v);
z = Z(idx,:);
p = exp(v);

2 changes: 1 addition & 1 deletion demo/ch13/hmm_demo.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
n = 10000;
[x, model] = hmmRnd(d, k, n);
%%
z = hmmViterbi(x,model);
[z,p] = hmmViterbi(x,model);
%%
[alpha,llh] = hmmFilter(x,model);
%%
Expand Down

0 comments on commit a793365

Please sign in to comment.