%July 2018, Etemadi and Lu
%Our PES outperforms NES. The observed and estimated ratios between
%$p_{_{N}}$ and $p_{_{D}}$ when the methods achieve the same RSEs between 0.1 and 0.4. The estimated ratios are obtained using Eq. 11
clear all;clf('reset');
data_name={'AstroPh';'CA-CondMat';'HepPh';'Enron-email';'Brightkite';'Gowalla';'Libimseti';'Digg';...
    'Amazon';'Citeseer';'Web-Google';'Youtube';'Dblp';'As-skitter'; 'FLicker';'Orkut';'Wiki-German';'USpatent';'LiveJournal';... 
    'DBpedia';'Web-Arabic';'Gsh-2015';'MicrosoftAG';'Friendster'};
size_subplot=size(data_name,1);

for i=1: size_subplot
    str1=data_name(i);
    path=char(strcat('our/',str1,'-raw-data-plot-tri.txt'));
    properties=dlmread(path,',',[5 0 5 9]);
    C=properties(1); N=properties(2); M=properties(3); T=properties(4); K=properties(5); CW=properties(6); W=properties(7);KW=properties(9);KWT=properties(10);
    iteration=dlmread(path,',',[6 0 6 0]);
    %iteration=700;
    data=dlmread(path,',',9);
    point_number=1.0*size(data,1)/(iteration+1);
    n=zeros(point_number,1);
    ObRseTour=0;
    EstRseTour=0;
    AproxEstRseT=0;
    EstWour=0;
    p=0;
    m=0;
    mour=0;
    ObRseTgpsin=0;
    observed_triangles_rse_REG=0;
    point_number=9;
%     if i==48
%         point_number=2;
%     end
    for h=1:point_number
        start=(h-1)*(iteration+1)+1;
        pour(h)=data(start,1);
        mour(h)=floor(pour(h)*M);
        start=start+1;
        EstWour=data(start:start+iteration-1,3);       
        EstTour=data(start:start+iteration-1,4);
        EstCour=data(start:start+iteration-1,5);      
        ObMeanTour(h)=mean(EstTour);    %calculate average of closed wedges
        ObVarTour(h)=var(EstTour); %calculate variance of closed wedges
        ObRseTour(h)=sqrt(ObVarTour(h))/T;
        AreTour=(ObMeanTour(h)-T)/T;        
    end
    
    pathREG=char(strcat('OurCIKM1PassStreaming/',str1,'-raw-data-plot-TriCIKM1p.txt'));
    properties=dlmread(pathREG,',',[5 0 5 6]);
    C=properties(1); N=properties(2); M=properties(3); T=properties(4); K=properties(5); CW=properties(6); W=properties(7);   
    iteration=dlmread(pathREG,',',[6 0 6 0]);
    data_REG=dlmread(pathREG,',',9);
    point_number2=1.0*size(data_REG,1)/(iteration+1);  
%      if i==48
%         point_number=2;
%      end
    ApproxRatio=0;
    for h=1:point_number       
        start=(h-1)*(iteration+1)+1;
        ob_cw_in_g=data_REG(start:start+iteration-1,3);      

        observed_triangles_mean_REG(h)=mean(ob_cw_in_g);    
        observed_triangles_variance_REG(h)=var(ob_cw_in_g); 
        observed_triangles_rse_REG(h)=sqrt(observed_triangles_variance_REG(h))/observed_triangles_mean_REG(h); 
        
        p(h)=data_REG(start,1);               
        m(h)=floor(p(h)*M);
        ApproxRatio(h)=M/(p(h)*W);
    end
    
    ratio=m./mour;
    rse=[0.408 0.33 0.265 0.21];% 0.18 0.16];% 0.142 0.13 0.12 0.109 0.1];
%     if i==48
%          rse=[0.408 0.33];
%     end
    h1=subplot(4,6,i);
    line1=plot(observed_triangles_rse_REG,ratio,'k-^','MarkerSize',5,'LineWidth',1.3);
    hold on
    line2=plot(observed_triangles_rse_REG,ApproxRatio,'b-','LineWidth',1.3);
    grid on
    set(h1,'Color',[0.96 0.98 0.99]);
    
    ymin1=ratio(1);
    if ApproxRatio(1)>ymin1
        ymin1=ApproxRatio(1);
    end
    
    ymax1=ratio(point_number);
    if ApproxRatio(point_number)<ymax1 
        ymax1=ApproxRatio(point_number);
    end
    y1=floor(ymax1);
    yn=ceil(ymin1);
    step=(y1+yn)/5;
    
    ylim([ymax1*0.95 ymin1*1.01]);
    xlim([0.9*observed_triangles_rse_REG(point_number) 1.05*observed_triangles_rse_REG(1)]);
    set(gca,'XTick',[0.1 : 0.1 : 0.4]);
    %set(gca,'YTick',[y1*0.95 : step : yn*1.01]);
    %xlim([m(1)-m(1)/6 m(floor(point_number))+m(floor(point_number))/60]);
    box off;
    
     p = get(h1, 'position');
    if mod (i,6)==1
        p(1)=p(1)-0.05;
     elseif mod (i,6)==2
        p(1)=p(1)-0.08;
     elseif mod (i,6)==3
        p(1)=p(1)-0.11;
     elseif mod (i,6)==4
        p(1)=p(1)-0.14;
     elseif mod (i,6)==5
        p(1)=p(1)-0.17;
     elseif mod (i,6)==0
        p(1)=p(1)-0.2; 
     end 
     if i<=6
      p(2)=p(2)+0.02;
      
    elseif i<=12
      p(2)=p(2)+0.024;
      
    elseif i<=18
      p(2)=p(2)+0.032;
    else
         p(2)=p(2)+0.036;
    end
     p(3)=p(3)-0.01;  %+0.036;
     p(4)=p(4)+0.01;
    set(h1, 'position', p);

    %h=refline([0 0]);
    j = findobj(gca,'Type','text');
    set(j,'Position', [0 0 500 500]);
    %name=strcat('(',num2str(i),')',{' '},str1); 
    t=title(str1,'FontSize',12,'FontWeight','bold','Interpreter','Latex');
    p1=get(t,'Position');
    set(t,'Position',[p1(1) p1(2)  p1(3)]);
    
    if i==1||i==7 || i==13 || i==19 ||i==33||i==41||i==49||i==57
        ylabel('$p_{_{N}}/p_{_{D}}$','FontSize',12,'Interpreter','Latex');
    end
    %set(get(gca,'YLabel'),'Rotation',0);
    %y=get(gca,'YLabel');
    
    
    if i>=19
        xlabel('$RSE$','Interpreter','Latex','FontSize',9);
    else
      set(gca,'XTickLabel',[]); 
    end
    
    
    set(gca,'FontSize',3,'XTickMode','auto');
    set(gca,'XTickMode','manual','YTickMode','manual');
    set(gca,'FontSize',9);
    NumTicks = 4;
    L = get(gca,'XLim');
    %set(gca,'XTick',linspace(L(1),L(2),NumTicks));
    n=get(gca,'Xtick');
    b=size(n,2);
    z=n(1,1:b-1);
    format shortG;
    %set(gca,'Xtick',[z'],'Xticklabel',num2str(get(gca,'XTick')','%1.0e'));
end
hL = legend([line1 line2],{'Obs. ','Est. ','Approx Est. '},'FontSize',11, 'LineWidth', 4,'interpreter','latex','Orientation','horizontal');
newPosition = [0.42 0 0.005 0.15];
newUnits = 'normalized';
set(hL,'Position', newPosition,'Units', newUnits);
legend('boxoff');
set(gcf,'color','w');
export_fig Fig7 -painters -eps

