% July 2018, Etemadi and Lu
%Our PES  outperforms GPS-In in terms of RSEs when both the methods are using the same sample sizes.  
%Note that for our PES, the sample size includes both the size of subgraph $g$ and reservoir size, i.e., $|\sigma|$. 
%For GPS-In, we only considered the size of subgraph $g$ as a sample size, and ignored two additional values per each sampled edges in $g$.
clear all;clf('reset');
data_name={'Ego-facebook';'CA-GrQc';'CA-CondMat';'HepPh';'Facebook';'Epinions';'Gowalla';'Digg';'Dblp-Coau';...
    'Amazon';'Actor';'Citeseer';'Web-Google';'Dblp';'Pokec';'Hollywood';'FLicker';'Wiki-Franch';'Orkut';'USpatent';'LiveJournal';'Web-Arabic';'Gsh-2015';'MicrosoftAG'};


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;
    p=0;
    m=0;
    mour=0;
    ObRseTgpsin=0;
    
    
    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;
        
        EstTour=data(start:start+iteration-1,4);
       
        ObVarTour(h)=var(EstTour); %calculate variance of closed wedges
        ObRseTour(h)=sqrt(ObVarTour(h))/T;
        
    end
    
    path=char(strcat('GPS-in_2g/',str1,'-raw-data-plot-gps-in.txt'));
    iteration=dlmread(path,',',[6 0 6 0]);
    %iteration=20;
    data=dlmread(path,',',9);
    point_number2=floor(1.0*size(data,1)/(iteration+1));
    n=zeros(point_number2,1);
    %point_number2=11;
    if i==48
    point_number2=3;
    end
    for h=1:point_number2
        start=(h-1)*(iteration+1)+1;
        m(h)=data(start,1);
        pgpsin(h)=m(h)/M;
        start=start+1;
        EstWgpsin=data(start:start+iteration-1,2);       
        EstTgpsin=data(start:start+iteration-1,3);
        EstCgpsin=data(start:start+iteration-1,4);
        
%         ObMeanC(h)=mean(EstCgpsin);    %calculate average of closed wedges
%         ObVarC(h)=var(EstCgpsin); %calculate variance of closed wedges
%         ObRseC(h)=sqrt(ObVarC(h))/C; %calculate RSE of closed wedges
%         AreC=(ObMeanC(h)-C)/C;
        
        ObMeanTgpsin(h)=mean(EstTgpsin);    %calculate average of closed wedges
        ObVarTgpsin(h)=var(EstTgpsin); %calculate variance of closed wedges
        ObRseTgpsin(h)=sqrt(ObVarTgpsin(h))/T;
        AreTgpsin=(ObMeanTgpsin(h)-T)/T;
        
%         ObMeanW(h)=mean(EstW);    %calculate average of closed wedges
%         ObVarW(h)=var(EstW); %calculate variance of closed wedges
%         ObRseW(h)=sqrt(ObVarW(h))/W;
%         AreW=(ObMeanW(h)-W)/W;
    end
    
    h1=subplot(4,6,i);
    pour;
    line1=plot(2*mour(1:point_number),ObRseTour(1:point_number),'r-o','MarkerSize',5,'LineWidth',1.2);
    hold on
    line2=plot(2*mour(1:point_number2),ObRseTgpsin(1:point_number2),'k-^','MarkerSize',5,'LineWidth',1.2);
    %grid on
    set(h1,'Color',[0.96 0.98 0.99]);
    %hold on
    %line3=plot(p,AproxEstRseT,'b','LineWidth',1.2);
    ylim([min(min(ObRseTour),min(ObRseTgpsin))*0.9 1.05*max(max(ObRseTgpsin),max(ObRseTour))]);  
    
    xlim([mour(1)-mour(1)/6 2*(mour(floor(point_number))+mour(floor(point_number))/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',10,'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==23||i==41||i==49||i==57
        ylabel('RSE','FontSize',9,'Interpreter','Latex');
    end
    if i>=19
        xlabel('Sample size','Interpreter','Latex','FontSize',9);
    end
    
    set(gca,'FontSize',3,'XTickMode','auto');
    set(gca,'XTickMode','manual','YTickMode','manual');
    set(gca,'FontSize',7);
    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 ],{'PES ','GPS-In','Approx Est. '},'FontSize',11, 'LineWidth', 4,'interpreter','latex','Orientation','horizontal');
newPosition = [0.38 0 0.01 0.2];
newUnits = 'normalized';
set(hL,'Position', newPosition,'Units', newUnits);
legend('boxoff');
set(gcf,'color','w');
export_fig Fig4 -painters -eps

