📄 viterbi_tree.m
字号:
% viterbi_tree.m%% 1-D% Finds the most likely (ML) sequece of state given the input data with a% coarse to fine tree.% Usuage : [qp, lk] = viterbi_tree(w, ES, POS, MU, SI, scl)% w - input data% ES, POS, MU, SI - model parameters. ES is COLUMN STOICHASTIC% scl - if ~=0, use scaling to prevent arithmetic underflow, default=0% qp - ML estimate of the hidden state sequence% lk - likelihood of qp (log likelihood if scl ~= 0)% % Written by : Justin Romberg% Created : 1/20/99function [qp, lk] = viterbi_tree(w, ES, POS, MU, SI, scl)% make sure scl is definedif (nargin < 6) scl = 0;end% number of data points, number of state, and number of levels in the treeN = length(w);M = size(MU,1);L = size(MU,2);% delta(jj,ii) is the "best score" along a state path that ends in state jj% at node ii (all paths that "branch off" of this path before node ii are% independent of this path).% psi(jj,kk,ii) is the most likely state at ii to have children jj% and kk.delta = zeros(M,N);psi = zeros(M,M,N);% allocate space for the state sequenceqp = zeros(1,N);% individual data likelihoods to be in each state% intializationfl = gaussian(w(2), MU(:,2), SI(:,2));if (scl == 0) delta(:,2) = POS.*fl;else delta(:,2) = log(POS) + log(fl);endpsi(:,:,1) = zeros(M,M);% recursionfor ll = 2:L inds1 = 2^(ll-1)+1; inds2 = 2^ll; for ii = inds1:inds2 for jj = 1:M f = gaussian(w(ii), MU(jj,ll), SI(jj,ll)); if (scl == 0) delta(jj,ii) = max(delta(:,parent(ii)).*ES(jj,:,ll)')*f; else delta(jj,ii) = max(delta(:,parent(ii))+log(ES(jj,:,ll)')) + ... log(f); end for kk = 1:M if (scl == 0) psi(jj,kk,parent(ii)) = ... argmax(delta(:,parent(ii)).*ES(jj,:,ll)'.*ES(kk,:,ll)'); else psi(jj,kk,parent(ii)) = ... argmax(delta(:,parent(ii)) + log(ES(jj,:,ll)') + ... log(ES(kk,:,ll)')); end end end endend% termination% at level Lfor ii = inds1:inds2 qp(ii) = argmax(delta(:,ii));end% path (state sequence) backtrackingfor ll = (L-1):-1:1 inds1 = 2^(ll-1)+1; inds2 = 2^ll; for ii = inds1:inds2 qp(ii) = psi(qp(leftChild(ii)),qp(rightChild(ii)),ii); endend%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % function that returns the left child of a givend nodefunction lc = leftChild(ii)lc = 2*ii - 1; % function that returns the right child of a given nodefunction rc = rightChild(ii)rc = 2*ii;% function that returns the parent of a given nodefunction p = parent(ii)p = round(ii/2 + .1); % quick argmax function% x - matrix to find argmax of% dim - dimension to look alongfunction am = argmax(x, dim)if (nargin <2) dim = 1;end[dummy, am] = max(x,[],dim);% "robust" normpdf function. It takes the mean and VARIANCE and returns the% distribution value. The variance is thresholded at 1e-5. The return% value is thresholded at 1e-20.function f = gaussian(x, mui, vari)inds1 = find(vari < 1e-5);vari(inds1) = (1e-5)*ones(size(inds1));f = normpdf(x, mui, sqrt(vari));inds2 = find(f < 1e-20);f(inds2) = (1e-20)*ones(size(inds2));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -