function [] = HMM_EM_demo(N_state,tau,scale_mu,scale_cov,N_count), %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% HMM_EM % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% HMM_EM_demo(N_state,tau,scale_mu,scale_cov,N_count)% %% % %% N_state = number of state [3] % %% tau = lentgh of the sequence [200] % %% scale_mu = scale of the mean [4-5] % %% scale_cov = scale of the cov [1] % %% N_count = number of iteration [100] % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %% Please refer to Max Welling's class notes %% for the symbol notation. %% In figure (1) there is the estimated state evolution %% using the Viterbi algorithm given A, mu and B and the %% sequence of observations (with A, mu and B selected %% randomly). %% In figure(2) there is the sequence of observations (x's) %% The color is the label of the state attached to the observation %% In figure(3) there is the estimated state evolution computed %% using the Viterbi algorithm. In this case A, mu and B %% are estimated using the EM algorithm over the observations. %% The red plots in fig.2 are the real ni and lambda %% The white plots in fig.2 are the estimated ni and lambda %% The squares in fig.2 are the incorrectly estimated states. %% If there are too many mistakes the relabeling procedure fails %% and the squares are no meaningful any longer. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% S. savarese M.Welling - May 2000 %%%% Vision Lab - Caltech %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% X = observation vector %% Y = state vector if (nargin==0) N_state = 3; %% number of states N_count = 100; tau = 100; %% length of the state vector scale_mu = 5; scale_cov = 1; end; colordef black; %clear; %close all; figure(1), clf; figure(2), clf; figure(2), title ('green=state 1; red=state 2; magenta=state 3; yell=state 4; green=state 5; ...') %%%%%%%% initialization color = ['g' 'r' 'm' 'y' 'c' 'b' 'w' ]; %N_state = 3; %% number of states N_obs = 1; %N_count = 100; %tau = 200; %% length of the state vector dim_Y = 2; %% dimension of the state dim_X = 2; %% dimension of observation %scale_mu = 4; %scale_cov = 1; X = zeros(dim_X,tau); %%observation Y = zeros(1,tau); %% state Y_est = zeros(1,tau); %% state estimate Y_mu = zeros(dim_Y,N_state); Y_cov = zeros(dim_Y,dim_Y,N_state); Y_mu_new = zeros(dim_Y,N_state); Y_cov_new = zeros(dim_Y,dim_Y,N_state); Y_axis1 = 1:1:tau; for i=1:N_state, Y_mu(:,i)=randMean(dim_Y,scale_mu); Y_cov(:,:,i)=randCovariance(dim_Y,scale_cov); end; gamma_i = zeros(N_state,tau); omega_ij = zeros(N_state,N_state,tau); A_ij = zeros(N_state,N_state); B_i = zeros(N_state,tau); B_i_new = zeros(N_state,tau); A_ij_new = zeros(N_state,N_state); B_i_new = zeros(N_state,tau); alpha_i = zeros(N_state,tau); kt = zeros(1,tau); beta_i = zeros(N_state,tau); mu_i = zeros(N_state,tau); mu_i_new = zeros(N_state,1); delta_i = zeros(N_state,tau); ni_i_new = zeros(dim_X,N_state); lambda_i_new = zeros(dim_X,dim_X,N_state); %%%%%% generate transition probability matrix A1_ij = zeros(N_state,N_state); A1_ij(:,:) = rand(N_state,N_state); A2=sum(A1_ij,1); A3=ones(N_state,1)*A2(1,:); A_ij=A1_ij./A3; %%%%%%%% generate P(Yo=i) vector P2_Yo = rand(N_state,1); P3_Yo =sum(P2_Yo,1)*ones(N_state,1); P_Yo = P2_Yo./P3_Yo; %%%%%%%%% compute the initial state Yo = new_state(P_Yo,1); %% label of the state Y(1)=Yo; %%%%%% generate the sequence of states for i=2:tau, Y(i) = new_state(A_ij,Y(i-1)); end; %%%%%%% plot the sequence of state figure(1); axis([0 tau+1 0 N_state+1]); grid on; title('square = actual state'); drawnow; %%%%% generate the sequence of observations for i=1:tau, X(:,i)= randvec(1,Y_mu(:,Y(i)),Y_cov(:,:,Y(i))); figure(2); hold on; plot(X(1,i),X(2,i),[color(Y(i)) 'x'],'linewidth',2); figure(1); hold on plot(Y_axis1(i),Y(i),[color(Y(i)) 's'],'linewidth',2); %if (i<10) pause(1); end; drawnow; end; %%%%%%%%% init parameters for the Viterbi algorithm %%%%%%%%% i.e. : mu_i(:,1) B_i delta_i(:,1) for i=1:N_state, for t=1:tau, B_i(i,t) = gauss2(Y_mu(:,i),Y_cov(:,:,i),X(:,t)); end; end; mu_i(:,1) = P_Yo; delta_i(:,1) = B_i(:,1).*mu_i(:,1); delta_i(:,1)=delta_i(:,1)/norm(delta_i(:,1)); %%%%%%%%%%%% store values Y_mu_o = Y_mu; Y_cov_o = Y_cov; A_ij_o = A_ij; mu_i_o = mu_i(:,1); B_i_o = B_i; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% intial condition for the EM algorithm %%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%% Y_mu, Y_cov for i=1:N_state, Y_mu_new(:,i)=randMean(dim_Y,scale_mu); Y_cov_new(:,:,i)=eye(dim_X); end; %%%%%%%%%%%%%%%% B_i for i=1:N_state, for t=1:tau, B_i_new(i,t) = gauss2(Y_mu_new(:,i),Y_cov_new(:,:,i),X(:,t)); end; end; %%%%%%%%%%%%%%% mu_i P2_Yo = rand(N_state,1); P3_Yo =sum(P2_Yo,1)*ones(N_state,1); P_Yo = P2_Yo./P3_Yo; mu_i_new(:,1) = P_Yo; %%%%%%%%%%%%%%%%% A_ij %A1_ij = zeros(N_state,N_state); A1_ij(:,:) = rand(N_state,N_state); A2=sum(A1_ij,1); A3=ones(N_state,1)*A2(1,:); A_ij_new=A1_ij./A3; A_err=A_ij-A_ij_o; A_err=sqrt(A_err.^2); err=sum(sum(A_err,1)); %%%%% A_ij = A_ij_new; mu_i = mu_i_new(:,1); B_i = B_i_new; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% EM algorithm %%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% count = 1; while (count100), fprintf('B_i = %f\n',B_i_new(i,t)); ni_i_new(:,i)=randMean(dim_Y,scale_mu); lambda_i_new(:,:,i)=eye(2); plotGauss_color(Y_mu_new(1,i),Y_mu_new(2,i),Y_cov_new(1,1,i),Y_cov_new(2,2,i),Y_cov_new(2,1,i),'b'); for i=1:N_state, for t=1:tau, B_i_new(i,t) = gauss2(ni_i_new(:,i),lambda_i_new(:,:,i),X(:,t)); end; end; fprintf('alterating loop...\n'); end; %%%%%%%%%%%%%% end try to avoid huge peak end; end; %%%%%%%%%%%%%% end debug service %%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%% updating mu_i(:,1) = mu_i_new; A_ij = A_ij_new; B_i = B_i_new; A_err=A_ij-A_ij_o; A_err=sqrt(A_err.^2); err=sum(sum(A_err,1)); %%%%% plot the sequence of observation figure(2); clf; hold on; plot(X(1,:),X(2,:),'yx','linewidth',2); %if (i<10) pause(1); end; drawnow; if (count==1), %% plot initial mu and cov %%%%%%%% plot Y_mu and Y_cov figure(2); hold on; for i=1:N_state plotGauss_color(Y_mu_new(1,i),Y_mu_new(2,i),Y_cov_new(1,1,i),Y_cov_new(2,2,i),Y_cov_new(2,1,i),'b'); end; %input(''); end; %%%%%%%% plot ni and sigma figure(2); hold on; for i=1:N_state plotGauss_color(ni_i_new(1,i),ni_i_new(2,i),lambda_i_new(1,1,i),lambda_i_new(2,2,i),lambda_i_new(2,1,i),'w'); end; %%%%%%%% plot Y_mu and Y_cov figure(2); hold on; for i=1:N_state, plotGauss_color(Y_mu(1,i),Y_mu(2,i),Y_cov(1,1,i),Y_cov(2,2,i),Y_cov(2,1,i),'r'); end; %%%%% loglikelihood L(count)=log(prod(kt)); fprintf('L=%f\n',L(count)); if (count>1)&(sqrt((L(count)-L(count-1))^2)<0.00001), break; end; if (count>1)&(L(count)