1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| clear clc close all
nbStates = 4; load('data1.mat');
nbVar = size(Data,1);
[Priors, Mu, Sigma] = EM_init_kmeans(Data, nbStates); [Priors, Mu, Sigma] = EM(Data, Priors, Mu, Sigma);
expData(1,:) = linspace(min(Data(1,:)), max(Data(1,:)), 100);
x = expData(1,:) ; in = 1; out = [2:nbVar];
nbData = length(x); nbVar = size(Mu,1); nbStates = size(Sigma,3);
for k = 1:nbStates Pxi(k, :) = Priors(k).* mvnpdf(x', Mu(in, k), Sigma(in ,in, k));
end r = Pxi./sum(Pxi);
Sigma_y = zeros(length(out), length(out), nbData); y = zeros(length(out), nbData); for n = 1:nbData for k = 1:nbStates yj_tmp = Mu(out,k) + Sigma(out,in,k)*inv(Sigma(in,in,k)) * (x(:,n)-Mu(in,k)); y(:,n) = y(:,n) + r(k, n).*yj_tmp; end
for k = 1:nbStates yj_tmp = Mu(out,k) + Sigma(out,in,k)*inv(Sigma(in,in,k)) * (x(:,n)-Mu(in,k)); Sigmaj_y_tmp = Sigma(out,out,k) - (Sigma(out,in,k)*inv(Sigma(in,in,k))*Sigma(in,out,k)); Sigma_y(:,:,n) = Sigma_y(:,:,n) + r(k, n).* (Sigmaj_y_tmp+yj_tmp*yj_tmp'); end Sigma_y(:,:,n) = Sigma_y(:,:,n)- y(:,n)*y(:,n)'; end
expData(2:nbVar,:) = y; expSigma = Sigma_y ;
figure('position',[10,10,1000,800],'name','GMM-GMR-demo1');
for n=1:nbVar-1 subplot(3*(nbVar-1),2,(n-1)*2+1); hold on; plot(Data(1,:), Data(n+1,:), 'x', 'markerSize', 4, 'color', [.3 .3 .3]); axis([min(Data(1,:)) max(Data(1,:)) min(Data(n+1,:))-0.01 max(Data(n+1,:))+0.01]); xlabel('t','fontsize',16); ylabel(['x_' num2str(n)],'fontsize',16); end
subplot(3*(nbVar-1),2,[2:2:2*(nbVar-1)]); hold on; plot(Data(2,:), Data(3,:), 'x', 'markerSize', 4, 'color', [.3 .3 .3]); axis([min(Data(2,:))-0.01 max(Data(2,:))+0.01 min(Data(3,:))-0.01 max(Data(3,:))+0.01]); xlabel('x_1','fontsize',16); ylabel('x_2','fontsize',16);
for n=1:nbVar-1 subplot(3*(nbVar-1),2,4+(n-1)*2+1); hold on; plotGMM(Mu([1,n+1],:), Sigma([1,n+1],[1,n+1],:), [0 .8 0], 1); axis([min(Data(1,:)) max(Data(1,:)) min(Data(n+1,:))-0.01 max(Data(n+1,:))+0.01]); xlabel('t','fontsize',16); ylabel(['x_' num2str(n)],'fontsize',16); end
subplot(3*(nbVar-1),2,4+[2:2:2*(nbVar-1)]); hold on; plotGMM(Mu([2,3],:), Sigma([2,3],[2,3],:), [0 .8 0], 1); axis([min(Data(2,:))-0.01 max(Data(2,:))+0.01 min(Data(3,:))-0.01 max(Data(3,:))+0.01]); xlabel('x_1','fontsize',16); ylabel('x_2','fontsize',16);
for n=1:nbVar-1 subplot(3*(nbVar-1),2,8+(n-1)*2+1); hold on; plotGMM(expData([1,n+1],:), expSigma(n,n,:), [0 0 .8], 3); axis([min(Data(1,:)) max(Data(1,:)) min(Data(n+1,:))-0.01 max(Data(n+1,:))+0.01]); xlabel('t','fontsize',16); ylabel(['x_' num2str(n)],'fontsize',16); end
subplot(3*(nbVar-1),2,8+[2:2:2*(nbVar-1)]); hold on; plotGMM(expData([2,3],:), expSigma([1,2],[1,2],:), [0 0 .8], 2); axis([min(Data(2,:))-0.01 max(Data(2,:))+0.01 min(Data(3,:))-0.01 max(Data(3,:))+0.01]); xlabel('x_1','fontsize',16); ylabel('x_2','fontsize',16);
|