% March 2016, Etemadi and Lu
% Comparison of REG and REg.
% Data format: 
% March 2016, Etemadi and Lu
% Comparison of REG and REg.
% Data format for RE_g.txt: 
% #run,# sampled edges,Closed wedge(CW)    
%Example
% 0,21356,21      
% Data format for REG.txt: 
%#run,# sampled edges,CW,Wedges,CW in g,CW checked in G,closure check
%0,1921,5,40,0,5,40

clear all;clf('reset');
set(1,'defaulttextinterpreter','latex');

data_name={'Ego-facebook';'Enron-email';'Brightkite';'Dblp-Coau';...
    'Amazon';'Web-NotreDame';'Citeseer';'Dogster';'Web-Google';'Youtube';'Dblp';'As-skitter';...
    'FLicker';'LiveJournal';'Orkut';'Orkut2';... 
    'Web-Arabic';'Twitter';'MicrosoftAG';'Friendster'};
t_p=[0.85,0.85,0.85,0.85,0.85, 0.85,0.85,0.85,0.85,0.85,  0.85,0.85,0.85,0.85,0.85,  0.85,0.85,0.85,0.85,0.85];
size_subplot=size(data_name,1);

for i=1: size_subplot
    str1=data_name(i);
    pathREG=char(strcat('Theorem1_intermidiate_data_REG/',str1,'-raw-data-plot-REG.txt'));
    properties=dlmread(pathREG,',',[5 0 5 6]);
    C=properties(1); N=properties(2); M=properties(3); triangles=properties(4); K=properties(5); CW=properties(6); W=properties(7);
    iteration=dlmread(pathREG,',',[6 0 6 0]);
    data_REG=dlmread(pathREG,',',9);
    point_number=1.0*size(data_REG,1)/(iteration+1);
    p=zeros(point_number,1);
    observed_triangles_variance_REG=zeros(point_number,1);
    observed_triangles_mean_REG=zeros(point_number,1);
    observed_triangles_rse_REG=zeros(point_number,1);    
    for h=1:point_number
        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,5);       
        ob_triangles_REG=data_REG(start:start+iteration-1,3)/3;
        observed_triangles_mean_REG(h)=mean(ob_triangles_REG);    %calculate average of closed wedges
        observed_triangles_variance_REG(h)=var(ob_triangles_REG); %calculate variance of closed wedges
        observed_triangles_rse_REG(h)=sqrt(observed_triangles_variance_REG(h))/observed_triangles_mean_REG(h); %calculate RSE of closed wedges 
       % Estimate_triangles_rse_REG(h)=1/sqrt(3*observed_triangles_mean_REG(h));
    end
    Estimate_triangles_rse_REG=zeros(point_number,1);
    for j=1:point_number
        pp=p(j);        
        a1 =((1.0/3.0)*triangles*(pp.^2-pp.^4));
        a2 = (8.0/9.0)*K*(pp.^3-pp.^4);               
       % Estimate_triangles_rse_REG(j) =sqrt(a1+a2)/(pp*pp*triangles);  
        Estimate_triangles_rse_REG(j)=1.0/sqrt(3.0*observed_triangles_mean_REG(j));
    end
   
    h1=subplot(4,5,i);
    plot(p,Estimate_triangles_rse_REG,...;
         p,  observed_triangles_rse_REG,'r-O','LineWidth',0.8,'MarkerSize',4);
    ylim([0 0.4]);
    xlim([0 p(11)+p(11)/20]);
    p = get(h1, 'position');
    p(1)=p(1)-0.05;
    p(2)=p(2)+0.01;
    p(3)=p(3)+0.036;
    p(4)=p(4)+0.035;
    set(h1, 'position', p);
    
    if i==5 || i==10|| i==15||i==20 ||i==25
        t=legend({'Est.','Obs.'},'FontSize',5,'FontName','CMU Serif');
        legend('boxoff');
        p1=get(t,'Position');
        set(t,'Position',[p1(1) p1(2)-0.05 p1(3)-0.04 p1(4)]);
         
    end
    h = findobj(gca,'Type','text');
    set(h,'fontsize',5 ,'Position', [0 0 500 500]);
    t=title(str1,'FontSize',7);
    p1=get(t,'Position');
    set(t,'Position',[p1(1) p1(2)*t_p(i) p1(3)]);
    if i==1 || i==6 || i==11 ||i==16||i==21
       ylabel('RSE','FontSize',8);
    else
      set(gca,'YTickLabel',[]);  
    end
    
    box off;
    set(gca,'FontSize',5);
    NumTicks = 5;
    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.0u'));
    
    if i>=16 
    xlabel('$p_{_{E_G}}$','FontSize',10);
    end
end    
saveas(gcf,'Plot_Theorem2','eps2c');

