% July 2018, Etemadi and Lu
%The observed RSEs of $\widehat{\Delta}_{NES}$ support our estimated RSEs based on Eq. 1

clear all;clf('reset');

data_name={'Ego-facebook';'Wiki-Vote';'AstroPh';'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);
    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);    
    for h=1:point_number2       
        start=(h-1)*(iteration+1)+1;
        p(h)=data_REG(start,1);
        start=start+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); 
        pp=p(h);
        EstVar=T*(1-pp^2)/pp^2+(16/15)*K*(1-(pp))/pp;
        Estimate_RSE_pro(h)=sqrt(EstVar)/T; 
        ApproxEstimate_RSE_pro(h)=1/sqrt(observed_triangles_mean_REG(h)); 
    
    end
    
    h1=subplot(4,6,i);
   
    s=1;
    line1=plot(p(s:point_number2),observed_triangles_rse_REG(s:point_number2),'r-o','MarkerSize',5,'LineWidth',1.2);
    hold on
%     line2=plot(p(s:point_number2),Estimate_RSE_pro(s:point_number2),'k','LineWidth',0.6);
%     hold on
    line3=plot(p(s:point_number2),ApproxEstimate_RSE_pro(s:point_number2),'b','LineWidth',1.2);
    %grid on
    set(h1,'Color',[0.96 0.98 0.99]);
    
%     if i==28 ||i==30 
      ylim([0.03 0.48]);
%     else
%         ylim([ApproxEstimate_RSE_pro(point_number2)/1.1 0.48]);
%     end
     xlim([p(1)-p(1)/6 p(floor(point_number2))+p(floor(point_number2))/40]);
    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.042;
      
    elseif i<=12
      p(2)=p(2)+0.047;
      
    elseif i<=18
      p(2)=p(2)+0.058;
    else
         p(2)=p(2)+0.067;
    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)*0.85  p1(3)]);
    
    if i==1||i==7 || i==13 || i==19 ||i==33||i==41||i==49||i==57
        ylabel('RSE','FontSize',9,'Interpreter','Latex');
    else
        set(gca,'YTickLabel',[]);
    end
    if i>=19
        xlabel('$p$','Interpreter','Latex','FontSize',12);
    end
    
    
    set(gca,'FontSize',3,'XTickMode','auto');
    set(gca,'XTickMode','manual','YTickMode','manual');
    set(gca,'FontSize',8);
    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 line3],{'Obs. ','Est. ','Approx Est. '},'FontSize',11, 'LineWidth', 4,'interpreter','latex','Orientation','horizontal');
newPosition = [0.42 0 0.005 0.22];
newUnits = 'normalized';
set(hL,'Position', newPosition,'Units', newUnits);
legend('boxoff');
set(gcf,'color','w');
%saveas(gcf,'RseOurCIKM1PassStreaming','eps2c');
export_fig Fig6 -painters -eps


