%% SSP Exercise: Generate HMMS

echo on; clc;
% Display the data.mat file, containing ready to play with HMMs, called hmm1, hmm2...
load data; whos
hmm1 % Print structure of HMM with 3 states, each a 2D gaussian model (there is an empty intial and final state for the sake of compilation, that's why the trans matrix is 5x5 and not 3x3).
hmm2 % Another HMM with 3 states

clc;

%
%% Plot the HMM against time

% Example of HMM 1 sequence plotted against time
[X,stateSeq] = generate_hmm(hmm1);
f1 = figure('name','Example sequence'); 
plot_sequence(X,stateSeq);

%
%% Plot the HMM in 2D and see the clusters

% Same example in 2d
f2 = figure('name','Random sequence from HMM1, 2D view'); 
plot_sequence_2d(X,stateSeq,hmm1);

%

% Same for another HMM
% Example of HMM 2 sequence plotted against time
[X,stateSeq] = generate_hmm(hmm2);
f1 = figure('name','Example sequence'); 
plot_sequence(X,stateSeq);
% Same example in 2d
f2 = figure('name','Random sequence from HMM2, 2D view'); 
plot_sequence_2d(X,stateSeq);

%
%% Likelihood: are some sequence more likely to have been generated by HMM1 or HMM2 ?

% We use the forward recursion to compute the likelihood of a sequence with an HMM
echo off;
for i=1:6,
    stri = num2str(i);
    eval( [ 'logProb(' , stri , ',1)=forward_recursion(X' , stri , ',hmm1);' ] ); 

end;
logProb

%


echo off;
for i=1:6,
    stri = num2str(i);
    eval( [ 'logProb(' , stri , ',2)=forward_recursion(X' , stri , ',hmm2);' ] ); 

end;
logProb