Skip to content

Commit

Permalink
update LDS
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Mar 9, 2017
1 parent 0d88ef5 commit ccedd27
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
2 changes: 0 additions & 2 deletions chapter13/LDS/TODO.txt

This file was deleted.

8 changes: 5 additions & 3 deletions chapter13/LDS/kalmanFilter.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function [mu, V, llh] = kalmanFilter(X, model)
% Kalman filter
function [mu, V, llh] = kalmanFilter(model, X)
% Kalman filter (forward algorithm for linear dynamic system)
% NOTE: This is the exact implementation of the Kalman filter algorithm in PRML.
% However, this algorithm is not practical. It is numerical unstable.
% Input:
% X: d x n data matrix
% model: model structure
Expand All @@ -23,7 +25,7 @@
I = eye(k);

PC = P*C';
R = (C*PC+S);
R = C*PC+S;
K = PC/R; % 13.97
mu(:,1) = mu0+K*(X(:,1)-C*mu0); % 13.94
V(:,:,1) = (I-K*C)*P; % 13.95
Expand Down
6 changes: 4 additions & 2 deletions chapter13/LDS/kalmanSmoother.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function [nu, U, Ezz, Ezy, llh] = kalmanSmoother(X, model)
function [nu, U, Ezz, Ezy, llh] = kalmanSmoother(model, X)
% Kalman smoother (forward-backward algorithm for linear dynamic system)
% NOTE: This is the exact implementation of the Kalman smoother algorithm in PRML.
% However, this algorithm is not practical. It is numerical unstable.
% Input:
% X: d x n data matrix
% model: model structure
Expand Down Expand Up @@ -28,7 +30,7 @@

% forward
PC = P0*C';
R = (C*PC+S);
R = C*PC+S;
K = PC/R;
mu(:,1) = mu0+K*(X(:,1)-C*mu0);
V(:,:,1) = (I-K*C)*P0;
Expand Down
21 changes: 18 additions & 3 deletions chapter13/LDS/ldsEm.m
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
function [model, llh] = ldsEm(X, model)
function [model, llh] = ldsEm(X, init)
% EM algorithm for parameter estimation of linear dynamic system.
% NOTE: This is the exact implementation of the EM algorithm in PRML.
% However, this algorithm is not practical. It is numerical unstable and
% there is too much redundant degree of freedom.
% Input:
% X: d x n data matrix
% model: prior model structure
% Output:
% model: trained model structure
% llh: loglikelihood
% Written by Mo Chen ([email protected]).
tol = 1e-4;
d = size(X,1);
if isstruct(init) % init with a model
model = init;
elseif numel(init) == 1 % random init with latent k
k = init;
model.A = randn(k,k);
model.G = iwishrnd(eye(k),k);
model.C = randn(d,k);
model.S = iwishrnd(eye(d),d);
model.mu0 = randn(k,1);
model.P0 = iwishrnd(eye(k),k);
end
tol = 1e-2;
maxIter = 100;
llh = -inf(1,maxIter);
for iter = 2:maxIter
% E-step
[nu, U, Ezz, Ezy, llh(iter)] = kalmanSmoother(X, model);
[nu, U, Ezz, Ezy, llh(iter)] = kalmanSmoother(model,X);
if llh(iter)-llh(iter-1) < tol*abs(llh(iter-1)); break; end % check likelihood for convergence
% M-step
model = maximization(X, nu, U, Ezz, Ezy);
Expand Down
10 changes: 5 additions & 5 deletions demo/ch13/lds_demo.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
n = 100;

[X,Z,model] = ldsRnd(d,k,n);
[mu, V, llh] = kalmanFilter(X, model);

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

[nu, U, Ezz, Ezy, llh] = kalmanSmoother(model, X);
% [model, llh] = ldsEm(X,k);
% plot(llh);
%

0 comments on commit ccedd27

Please sign in to comment.