%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Script to compute ER as function of nr. of parameters in MoG for
% different choices of covariance matrix
%
% DATE: Caltech, August 2 2004
% Author Marc'Aurelio Ranzato
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


cl = 1:9;
scale = 3;
THRES = 0.5;
ncl = length(cl);
TUNEFA = 1; 

if exist('performance_8PollFA2/index_ervsparam8PollFA.mat','file') == 2
    load('performance_8PollFA2/index_ervsparam8PollFA.mat')
else
% divide data in training and test sets
%ALDER
    tot = 355;
	ind = randperm(tot) - 1;
	indxts{1} = ind(1:30);
	indxtr{1} = setdiff([0:tot-1],indxts{1});  
%ASH
    tot = 303;
	ind = randperm(tot) - 1;
	indxts{2} = ind(1:30);
	indxtr{2} = setdiff([0:tot-1],indxts{2});  
%CHIN. ELM
    tot = 466;
	ind = randperm(tot) - 1;
	indxts{3} = ind(1:45);
	indxtr{3} = setdiff([0:tot-1],indxts{3});  
%JACARANDA
    tot = 223;
	ind = randperm(tot) - 1;
	indxts{4} = ind(1:20);
	indxtr{4} = setdiff([0:tot-1],indxts{4});  
%MULBERRY
    tot = 74;
	ind = randperm(tot) - 1;
	indxts{5} = ind(1:7);
	indxtr{5} = setdiff([0:tot-1],indxts{5});  
%OAK
    tot = 329;
	ind = randperm(tot) - 1;
	indxts{6} = ind(1:30);
	indxtr{6} = setdiff([0:tot-1],indxts{6});  
%OLIVE
    tot = 220;
	ind = randperm(tot) - 1;
	indxts{7} = ind(1:20);
	indxtr{7} = setdiff([0:tot-1],indxts{7});  
%PINE
    tot = 606;
	ind = randperm(tot) - 1;
	indxts{8} = ind(1:60);
	indxtr{8} = setdiff([0:tot-1],indxts{8});  
%FA
    tot = 26000;
	ind = randperm(tot) - 1;
	indxts{9} = ind(1:2600);
	indxtr{9} = setdiff([0:tot-1],indxts{9});
    save('performance_8PollFA2/index_ervsparam8PollFA.mat','indxts','indxtr')
end



if TUNEFA
    %set pollen classes to default
    experim = [];
    for cnt = 1 : ncl - 1
        ng{cnt} = 2;
        ttype{cnt} = 'spherical';
    end
    cov_t = {'full'};   
    experim{1}.d = 14;
    experim{1}.n{1} = 2 : 10;
    
    
else
    
    % set FA class
    ttype{ncl} = 'full';
    ng{ncl} = 6;
    % set other pollen classes
    cov_t = {'spherical', 'diag', 'full'};
    experim{1}.d = 6;
    experim{1}.n{1} = 2:7;
    experim{1}.n{2} = 2:4;
    experim{1}.n{3} = 1:2;
    experim{2}.d = 8;
    experim{2}.n{1} = 2:5;
    experim{2}.n{2} = 1:3;
    experim{2}.n{3} = 1;
    experim{3}.d = 10;
    experim{3}.n{1} = 2:5;
    experim{3}.n{2} = 1:2;
    experim{3}.n{3} = 1;
    experim{4}.d = 12;
    experim{4}.n{1} = 2:4;
    experim{4}.n{2} = 1:2;
    experim{4}.n{3} = [];
    experim{5}.d = 14;
    experim{5}.n{1} = 2:4;
    experim{5}.n{2} = 1:2;
    experim{5}.n{3} = [];
    experim{6}.d = 16;
    experim{6}.n{1} = 2:4;
    experim{6}.n{2} = 1:2;
    experim{6}.n{3} = [];
    experim{7}.d = 18;
    experim{7}.n{1} = 2:4;
    experim{7}.n{2} = 1:2;
    experim{7}.n{3} = [];
    
end

NEXP = length(experim);
NCOV = length(cov_t);

for ee = 1 : NEXP
    
    for cc = 1 : NCOV
        
        if TUNEFA
            
            for cnt2 = 1 : length(experim{ee}.n{cc})
                
                ttype{ncl} = cov_t{cc};
                comp = experim{ee}.d;
                ng{ncl} = experim{ee}.n{cc}(cnt2);
                fprintf(1,'exp. %d,cov %s, dim. %d, ng %d \n',ee,cov_t{cc},comp, ng{ncl}) 
                ris = training_ljmNL3 (cl, indxtr, ng, comp, scale, ttype);
                [CM, ER, ERp] = test_ljmNL3 (cl, cl, indxts, ris, scale,THRES);
                ERp = 1;
                for iii = 1 : size(CM,1)
                    NN(iii) = sum(CM(iii,:));
                    ERp = ERp - (CM(iii,iii+1)/NN(iii))/size(CM,1);
                end
                ERp = ERp - CM(ncl,1)/(sum(CM(ncl,:))*ncl); % unknown class is OK for FA class
                er{ee}.d = comp;
                er{ee}.n{cc}(cnt2) = ERp;
            end
            
        else
            
            for cnt = 1 : ncl - 1
                ttype{cnt} = cov_t{cc};
            end
            
            for cnt2 = 1 : length(experim{ee}.n{cc})
                
                for cnt3 = 1 : ncl - 1
                    ng{cnt3} = experim{ee}.n{cc}(cnt2);
                end
                comp = experim{ee}.d;
                fprintf(1,'exp. %d,cov %s, dim. %d, ng %d \n',ee,cov_t{cc},comp, ng{1}) 
                ris = training_ljmNL3 (cl, indxtr, ng, comp, scale, ttype);
                [CM, ER, ERp] = test_ljmNL3 (cl, cl, indxts, ris, scale,THRES);
                ERp = 1;
                for iii = 1 : size(CM,1)
                    NN(iii) = sum(CM(iii,:));
                    ERp = ERp - (CM(iii,iii+1)/NN(iii))/size(CM,1);
                end
                ERp = ERp - CM(ncl,1)/(sum(CM(ncl,:))*ncl); % unknown class is OK for FA class
                er{ee}.d = comp;
                er{ee}.n{cc}(cnt2) = ERp;
                %%%keyboard
            end
            
        end
        
    end
    
end

if TUNEFA
    save('performance_8PollFA2/ERvsParamFA8poll','er')
else
    save('performance_8PollFA2/ERvsParam8POLLfa','er')        
end
            
