%Main program for doing the analysis of the results obtained for the 4 basic methods over a set of experiments

% APRENDIZAJE AUTOMÁTICO (G. Pajares y J. M. de la Cruz, Eds.)
% Editorial: RA-MA
% Capítulo 7 y 8: Monte Carlo I y II
% Autores: Eva Besada-Portas, Sergey M. Plis


 draw=1;                   %Draw the results
 run=1;                    %Get new data
 nexptos=50;               % number o experiments 
 %Number of samples to be created in all the experiments
 N=[100,200,500,1000,2000,5000,10000,20000,50000,100000,200000,500000,1000000];
 %N=[100,200,500];
 if run %Generate the files with the results
    LoopRS(nexptos,0,N);
    LoopRS(nexptos,1,N);
    LoopIS(nexptos,0,N);
    LoopIS(nexptos,1,N);
 
    %LoopWeightedResampling(nexptos,0,N);
    LoopWR(nexptos,1,N);
 
    s0=rand(nexptos,length(N))*10+80;
    save StartMCMCData s0
    LoopMCMCIndependentSampler(nexptos,N);
    LoopMCMCMetropolis(nexptos,N);
 end
 
 if draw %Draw the results stored in the files

    %Draw the mean Ratio of Rejected Values
    figure
    load LoopRS0Data %Sampling with uniform
    DrawAnalysis('',1,hmRej./repmat(N,50,1),N);

    load LoopRS1Data %Sampling with gaussian
    hold on;
    DrawAnalysis('Valor medio del numero rechazos/N',2,hmRej./repmat(N,50,1),N);


    %Draw the I_N of RS and IS in the same figure
    figure
    load LoopRS0Data %Sampling with uniform
    subplot(2,2,1);
    DrawAnalysis('RS, q(C) uniforme',0,fmean,N);

    load LoopRS1Data %sampling with gaussian
    subplot(2,2,2);
    DrawAnalysis('RS, q(C) gaussiano',0,fmean,N);


    load LoopIS0Data %sampling with uniform
    subplot(2,2,3);
    DrawAnalysis('IS, q(C) uniforme',0,fmean,N);

    load LoopIS1Data %sampling with gaussian
    subplot(2,2,4);
    DrawAnalysis('IS, q(C) gaussiano',0,fmean,N);

    %Draw the WR results, using 3 different reweighting values
    figure
    load LoopWR1Data
    subplot(3,1,1);
    DrawAnalysis('r(C) pesos',0,fmean{1},N);
    ylim([45,49]);ylabel('');xlabel('');
    subplot(3,1,2);
    DrawAnalysis('r(C) uniforme',0,fmean{2},N);
    ylim([45,49]);xlabel('');
    subplot(3,1,3);
    DrawAnalysis('r(C) gausiano',0,fmean{3},N);
    ylim([45,49]);ylabel('');
    
    %Draw the MCMC results with the IndependenceSampler
    figure
    load LoopMCMCIndependentSamplerData
    subplot(2,1,1);
    indend=min(10,length(N));
    DrawAnalysis('q(C) uniforme',0,fmean{1}(:,1:indend),N(1:indend));
    ylim([45,49]);xlabel('');
    subplot(2,1,2);
    DrawAnalysis('q(C) guassiana',0,fmean{2}(:,1:indend),N(1:indend));
    ylim([45,49]);

    %Draw the MCMC results with te Metropolis sampler
    figure
    load LoopMCMCMetropolisData
    subplot(3,1,1);
    DrawAnalysis('q(C|u^{(i-1)})=N_C(u^{(i-1)},1)',0,fmean{1}(:,1:indend),N(1:indend));
    ylim([45,49]);ylabel('');xlabel('');
    subplot(3,1,2);
    DrawAnalysis('q(C|u^{(i-1)})=N_C(u^{(i-1)},5)',0,fmean{2}(:,1:indend),N(1:indend));
    ylim([45,49]);xlabel('');
    subplot(3,1,3);
    DrawAnalysis('q(C|u^{(i-1)})=N_C(u^{(i-1)},10)',0,fmean{3}(:,1:indend),N(1:indend));
    ylim([45,49]);ylabel('');


 end