Skip to content

Commit ca599be

Browse files
committed
update lds_demo. TODO: init with ldsPCA in ldsEM
1 parent 91cefe8 commit ca599be

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

chapter13/LDS/ldsEm.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@
3636
model.G = iwishrnd(eye(k),k);
3737
model.C = randn(d,k);
3838
model.S = iwishrnd(eye(d),d);
39-
39+
% [A,C,Z] = ldsPca(X,k,3*k);
40+
% model.mu0 = Z(:,1);
41+
% model.P0 = ;
42+
% model.A = A;
43+
% model.C = C;
44+
% model.G = ;
45+
% model.S = ;
4046

4147
function model = maximization(X ,nu, U, Ezz, Ezy)
4248
n = size(X,2);

demo/ch13/lds_demo.m

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
close all;
2-
%% generate data
2+
%% Parameter
33
clear;
44
d = 2;
5-
k = 4;
5+
k = 2;
66
n = 50;
77

8-
A = [1 0 1 0;
9-
0 1 0 1;
10-
0 0 1 0;
11-
0 0 0 1];
12-
G = 0.001*eye(k);
8+
A = [1,1;
9+
0 1];
10+
G = eye(k)*1e-3;
1311

14-
C = [1 0 0 0;
15-
0 1 0 0];
16-
S = eye(d);
12+
C = [1 0;
13+
0 1];
14+
S = eye(d)*1e-1;
1715

18-
mu0 = [8; 10; 1; 0];
16+
mu0 = [0; 0];
1917
P0 = eye(k);
2018

2119
model.A = A;
@@ -25,43 +23,58 @@
2523
model.mu0 = mu0;
2624
model.P0 = P0;
2725

28-
[z,x] = ldsRnd(model, n);
26+
%% Generate data
27+
[z,x] = ldsRnd(model,n);
2928
figure;
3029
hold on
3130
plot(x(1,:), x(2,:), 'ro');
3231
plot(z(1,:), z(2,:), 'b*-');
3332
legend('observed', 'latent')
33+
title('Generated Data')
3434
axis equal
3535
hold off
36-
37-
%% filter
38-
[mu, V, llh] = kalmanFilter(model, x);
36+
%% Kalman filter
37+
[mu, V, llh] = kalmanFilter(model,x);
3938
figure
4039
hold on
4140
plot(x(1,:), x(2,:), 'ro');
4241
plot(mu(1,:), mu(2,:), 'b*-');
4342
legend('observed', 'filtered')
43+
title('Kalman filter')
4444
axis equal
4545
hold off
46-
47-
%% smoother
48-
[nu, U, llh] = kalmanSmoother(model, x);
46+
%% Kalman smoother
47+
[nu, U, llh] = kalmanSmoother(model,x);
4948
figure
5049
hold on
5150
plot(x(1,:), x(2,:), 'ro');
5251
plot(nu(1,:), nu(2,:), 'b*-');
5352
legend('observed', 'smoothed')
53+
title('Kalman smoother')
5454
axis equal
5555
hold off
56-
57-
%% EM
58-
[model, llh] = ldsEm(x,model);
59-
nu = kalmanSmoother(model, x);
56+
%% LDS Subspace
57+
[A,C,z] = ldsPca(x,k,3*k);
58+
y = C*z;
59+
t = size(z,2);
60+
figure;
61+
hold on
62+
plot(x(1,1:t), x(2,1:t), 'ro');
63+
plot(y(1,1:t), y(2,1:t), 'b*-');
64+
legend('observed', 'projected')
65+
title('LDS subspace learning')
66+
axis equal
67+
hold off
68+
%% LDS EM
69+
[model, llh] = ldsEm(x,k);
70+
nu = kalmanSmoother(model,x);
71+
y = model.C*nu;
6072
figure
6173
hold on
6274
plot(x(1,:), x(2,:), 'ro');
63-
plot(nu(1,:), nu(2,:), 'b*-');
64-
legend('observed', 'smoothed with fitted model')
75+
plot(y(1,:), y(2,:), 'b*-');
76+
legend('observed', 'learned')
77+
title('LDS EM learning')
6578
axis equal
6679
hold off
6780
figure;

0 commit comments

Comments
 (0)