?? viterbi_path.m
字號:
function [path, loglik] = viterbi_path(prior, transmat, obsmat)% VITERBI Find the most-probable (Viterbi) path through the HMM state trellis.% path = viterbi(prior, transmat, obsmat)%% Inputs:% prior(i) = Pr(Q(1) = i)% transmat(i,j) = Pr(Q(t+1)=j | Q(t)=i)% obsmat(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 tscaled = 1;T = size(obsmat, 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 .* obsmat(:,t);if scaled [delta(:,t), n] = normalise(delta(:,t)); scale(t) = 1/n;endpsi(:,t) = 0; % arbitrary value, since there is no predecessor to t=1for 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) * obsmat(j,t); end if scaled [delta(:,t), n] = normalise(delta(:,t)); scale(t) = 1/n; endend[p, path(T)] = max(delta(:,T));for t=T-1:-1:1 path(t) = psi(path(t+1),t+1);end% loglik = log p.% 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)% loglik computed by the two methods will be different, but the best path will be the same.if scaled loglik = -sum(log(scale)); %loglik = prob_path(prior, transmat, obsmat, path);else loglik = log(p);end
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -