Skip to content

Commit

Permalink
changed some variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Jan 27, 2016
1 parent 6aca8a7 commit 62af89e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
13 changes: 8 additions & 5 deletions chapter13/HMM/demo.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
d = 3;
k = 2;
n = 10000;

[x, model] = hmmRnd(d, k, n);
%%
[z, v] = hmmViterbi(x,model);
[alpha,energy] = hmmFilter(x,model);
[gamma, alpha, beta, c] = hmmSmoother(x,model);
[model, energy] = hmmEm(x,k);
plot(energy)
%%
% [alpha,llh] = hmmFilter(x,model);
%%
% [gamma, alpha, beta, c] = hmmSmoother(x,model);
%%
% [model, llh] = hmmEm(x,k);
% plot(llh)
10 changes: 5 additions & 5 deletions chapter13/HMM/hmmEm.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function [model, energy] = hmmEm(x, init)
function [model, llh] = hmmEm(x, init)
% EM algorithm to fit the parameters of HMM model (a.k.a Baum-Welch algorithm)
% x: 1 x n sequence of observations
% init: model or k
Expand All @@ -21,18 +21,18 @@

tol = 1e-4;
maxIter = 100;
energy = -inf(1,maxIter);
llh = -inf(1,maxIter);
for iter = 2:maxIter
% E-step
[gamma,alpha,beta,c] = hmmSmoother_(M,A,s);
energy(iter) = sum(log(c(c>0)));
if energy(iter)-energy(iter-1) < tol*abs(energy(iter-1)); break; end % check likelihood for convergence
llh(iter) = sum(log(c(c>0)));
if llh(iter)-llh(iter-1) < tol*abs(llh(iter-1)); break; end % check likelihood for convergence
% M-step
A = normalize(A.*(alpha(:,1:n-1)*bsxfun(@times,beta(:,2:n).*M(:,2:n),1./c(2:end))'),2); % 13.19
s = gamma(:,1); % 13.18
M = bsxfun(@times,gamma*X',1./sum(gamma,2))*X;
end
energy = energy(2:iter);
llh = llh(2:iter);
model.A = A;
model.E = E;
model.s = s;
Expand Down
11 changes: 6 additions & 5 deletions chapter13/LDS/demo.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
% demo
clear; close all;
d = 3;
k = 3;
n = 10000;
k = 2;
n = 100;

[X,model] = ldsRnd(d,k,n);
% [mu, V, llh] = kalmanFilter(X, model);
% [nu, U, Ezz, Ezy, llh] = kalmanSmoother(X, model);
[mu, V, llh] = kalmanFilter(X, model);
% % [nu, U, Ezz, Ezy, llh] = kalmanSmoother(X, model);
[model, llh] = ldsEm(X, model);
plot(llh);
% plot(llh);

0 comments on commit 62af89e

Please sign in to comment.