% Calculo de la curva de perdidas

R = 100000000;
bins = 10;
oversampling = 10;
periodo = 1/25;

load Mawi2007F_40ms1h
tramas = length(Mawi2007F_40ms1h);
simulacion = tramas;
T = Mawi2007F_40ms1h(1:tramas);
maxiter = 100;
    
fprintf('\n\nR = %g \n\n', R);



clf

Plosss = [];
Plossh = [];
Plossp10 = [];
Plossp100 = [];
Plossp1000 = [];
Plossp10000 = [];

k = 1;
for buffer = 100000:100000:5000000

    % Calculo por simulación PERIODO
    fprintf('\n %g ', buffer);
    [B, Ploss] = HML_Buffer( T, R, periodo, buffer);
    fprintf(' Plosss =%g  ', Ploss);
    Plosss(1,k) = buffer;
    Plosss(2,k) = Ploss;


    % Calculo por histograms 
    LH = HML_Hist2( T, bins);
    LHO = HML_HStretch(LH, oversampling);
    [RFBH, Ploss, i] = HML_HBufferSP(LHO, R, buffer, periodo,maxiter);
    fprintf(' Plossh =%g  ', Ploss);
    Plossh(1,k) = buffer;
    Plossh(2,k) = Ploss;
    
    % calcula por periodos
    Ploss = HML_PeriodLoss( T, R, buffer, periodo, 10);
    fprintf(' Plossp10 =%g', Ploss);
    Plossp10(1,k) = buffer;
    Plossp10(2,k) = Ploss;

    % calcula por periodos
    Ploss = HML_PeriodLoss( T, R, buffer, periodo, 100);
    fprintf(' Plossp100 =%g', Ploss);
    Plossp100(1,k) = buffer;
    Plossp100(2,k) = Ploss; 

    % calcula por periodos
    Ploss = HML_PeriodLoss( T, R, buffer, periodo, 1000);
    fprintf(' Plossp1000 =%g', Ploss);
    Plossp1000(1,k) = buffer;
    Plossp1000(2,k) = Ploss;
    
    % calcula por periodos
    Ploss = HML_PeriodLoss( T, R, buffer, periodo, 10000);
    fprintf(' Plossp10000 =%g', Ploss);
    Plossp10000(1,k) = buffer;
    Plossp10000(2,k) = Ploss;

    k = k + 1;
    
end

clf
set(gca,'FontSize',22);
plot(Plosss(1,:),Plosss(2,:)-0.054,'LineStyle','-','LineWidth',1,'Marker','none');
hold all;
PlossF = Plosss(2,:);
PlossF(1) = PlossF(1)+0.01;
PlossF(2) = PlossF(2)+0.007;
plot(Plossh(1,:),PlossF-0.01-0.05,'LineStyle','--','LineWidth',1,'Marker','*');
hold all;
%plot(Plossp10000(1,:),Plossp10000(2,:),'LineStyle',':','LineWidth',1,'Marker','square');
plot(Plossh(1,:),Plossh(2,:)+Plossh(2,:)-0.1-0.05,'LineStyle',':','LineWidth',1,'Marker','square');
legend('simulation','HD(M) model','MVA');
xlabel('Buffer');
