%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Script to find best initial condition for gmmem
%
% DATE: 31 Dec 2003
% Author: Marc'Aurelio Ranzato
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

cl = 1 : 9;
ncl = length(cl);
THRES = 0.5;
scale = 3;
for cc = 1 : ncl - 1
    
    N_GAUSS{cc} = 2;
    tipo{cc} = 'spherical';
    
end
N_GAUSS{ncl} = 4;
tipo{ncl} = 'full';

comp = 14;

load('performance_8PollFA2/index_ervsparam8PollFA.mat')

nr_esp = 50;
ERo = 1;
stateo = [];
st = [];
ERp = zeros(1,nr_esp);

for nn = 1 : nr_esp

    fprintf(1,'@@@@@@@@@@@@@@@@@@@ Experiment nr.%u @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ \n',nn)
    ris = training_ljmNL3 (cl, indxtr, N_GAUSS, comp, scale, tipo);
    [CM, ER, ERp(nn)] = test_ljmNL3 (cl, cl, indxts, ris, scale, THRES);
    st{nn} = ris.state;
    
    % Compute ERp
    ERp(nn) = 1;
    for iii = 1 : size(CM,1)
        NN(iii) = sum(CM(iii,:));
        ERp(nn) = ERp(nn) - (CM(iii,iii+1)/NN(iii))/size(CM,1);
    end
    ERp(nn) = ERp(nn) - CM(ncl,1)/(sum(CM(ncl,:))*ncl); % unknown class is OK for FA class
    
    if ERo > ERp(nn)     % found better initial state
        ERo = ERp(nn);
        stateo = ris.state;
    end
    
end

save('performance_8PollFA2/BestInitCond8PollFA','ERp','ERo','stateo','st')
