Skip to content

Commit

Permalink
update lds_demo. TODO: init with ldsPCA in ldsEM
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Nov 29, 2018
1 parent 91cefe8 commit ca599be
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
8 changes: 7 additions & 1 deletion chapter13/LDS/ldsEm.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@
model.G = iwishrnd(eye(k),k);
model.C = randn(d,k);
model.S = iwishrnd(eye(d),d);

% [A,C,Z] = ldsPca(X,k,3*k);
% model.mu0 = Z(:,1);
% model.P0 = ;
% model.A = A;
% model.C = C;
% model.G = ;
% model.S = ;

function model = maximization(X ,nu, U, Ezz, Ezy)
n = size(X,2);
Expand Down
61 changes: 37 additions & 24 deletions demo/ch13/lds_demo.m
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
close all;
%% generate data
%% Parameter
clear;
d = 2;
k = 4;
k = 2;
n = 50;

A = [1 0 1 0;
0 1 0 1;
0 0 1 0;
0 0 0 1];
G = 0.001*eye(k);
A = [1,1;
0 1];
G = eye(k)*1e-3;

C = [1 0 0 0;
0 1 0 0];
S = eye(d);
C = [1 0;
0 1];
S = eye(d)*1e-1;

mu0 = [8; 10; 1; 0];
mu0 = [0; 0];
P0 = eye(k);

model.A = A;
Expand All @@ -25,43 +23,58 @@
model.mu0 = mu0;
model.P0 = P0;

[z,x] = ldsRnd(model, n);
%% Generate data
[z,x] = ldsRnd(model,n);
figure;
hold on
plot(x(1,:), x(2,:), 'ro');
plot(z(1,:), z(2,:), 'b*-');
legend('observed', 'latent')
title('Generated Data')
axis equal
hold off

%% filter
[mu, V, llh] = kalmanFilter(model, x);
%% Kalman filter
[mu, V, llh] = kalmanFilter(model,x);
figure
hold on
plot(x(1,:), x(2,:), 'ro');
plot(mu(1,:), mu(2,:), 'b*-');
legend('observed', 'filtered')
title('Kalman filter')
axis equal
hold off

%% smoother
[nu, U, llh] = kalmanSmoother(model, x);
%% Kalman smoother
[nu, U, llh] = kalmanSmoother(model,x);
figure
hold on
plot(x(1,:), x(2,:), 'ro');
plot(nu(1,:), nu(2,:), 'b*-');
legend('observed', 'smoothed')
title('Kalman smoother')
axis equal
hold off

%% EM
[model, llh] = ldsEm(x,model);
nu = kalmanSmoother(model, x);
%% LDS Subspace
[A,C,z] = ldsPca(x,k,3*k);
y = C*z;
t = size(z,2);
figure;
hold on
plot(x(1,1:t), x(2,1:t), 'ro');
plot(y(1,1:t), y(2,1:t), 'b*-');
legend('observed', 'projected')
title('LDS subspace learning')
axis equal
hold off
%% LDS EM
[model, llh] = ldsEm(x,k);
nu = kalmanSmoother(model,x);
y = model.C*nu;
figure
hold on
plot(x(1,:), x(2,:), 'ro');
plot(nu(1,:), nu(2,:), 'b*-');
legend('observed', 'smoothed with fitted model')
plot(y(1,:), y(2,:), 'b*-');
legend('observed', 'learned')
title('LDS EM learning')
axis equal
hold off
figure;
Expand Down

0 comments on commit ca599be

Please sign in to comment.