function lhood=Estep_edgefile_lognormal()

%% This script is the E step for our entire model 

%% K: number of routing strategies
%% alphaprior: 1*K vector
%% betapara: K*NumExperts*NumTkts*NumExperts sparse matrix
%% X: NumTkts*NumExperts*NumExperts sparse matrix
%% TransExpertIndex: Index for experts that have transferred some tickets and how many tickets each has transferred

%% phi_t_1: NumTkts*K*NumExperts sparse matrix
%% gamma_t_1: NumExperts*K

globalvars_edgefile;


maxiter_estep=3;
iter_estep=0;

threh_estep=0.001;

convergence_estep = -1;

old_lhood=0;

lhood=0;


while ((iter_estep<=maxiter_estep)&&(convergence_estep>threh_estep || convergence_estep<0))  
    
  
    
      
    if ((convergence_estep <0) && old_lhood~=0)
        fprintf('Warning: E step %d: the old likelihood %f is larger than the new likelihood %f', iter_estep, old_lhood, lhood);
        break; % to check what the problem is. 
        
    else  
       old_lhood=lhood; 
       term2=0;
       
       term3=0;

       term5=0; 
        
       transexperts=unique(ExpTransTktFile(:,1));
       gammaprior_old=gammaprior;      
       phi_old=phi;
 
       
       intExpTransTktFile=uint32(ExpTransTktFile);
       fullvolumtran=full(volumtran);
       psigammarowsum=psi(sum(gammaprior_old,2));
       psigamma=psi(gammaprior_old);
      % [phi, gammaprior, term2, term3, term5]=UpdatePhiGamma(phi_old, gammaprior_old, intExpTransTktFile,fullvolumtran,betapara,psigamma, psigammarowsum,PureRandTrans);
      % [phi, gammaprior, term2, term3, term5]=UpdatePhiGamma_speedbeta_new(phi_old, gammaprior_old, intExpTransTktFile,fullvolumtran,betaparanew,psigamma, psigammarowsum,PureRandTrans,betaindxflag);
      
       %[phi, gammaprior, term2, term3, term5]=UpdatePhiGamma_lognormal_speedbeta_new(phi_old, gammaprior_old, intExpTransTktFile,fullvolumtran,betaparanew,psigamma, psigammarowsum,PureRandTrans,betaindxflag, TRCtran);
       [phi, gammaprior, term2, term3, term5]=UpdatePhiGamma_lognormal_speedbeta_sixall(phi_old, gammaprior_old, intExpTransTktFile,fullvolumtran,betaparanew,psigamma, psigammarowsum,PureRandTrans,betaindxflag, TRCtran, betapara_VE, betapara_PR);

    
       gammaprior(transexperts,:)=gammaprior(transexperts,:)+repmat(alphaprior,numel(transexperts),1);
       
       term1=NumExperts0*(-sum(gammaln(alphaprior))+gammaln(sum(alphaprior)))+(alphaprior-1)*sum(psi(gammaprior_old(transexperts,:)))'-sum(alphaprior-1)*sum(psi(sum(gammaprior_old(transexperts,:),2)));
 
       term4=sum(sum(gammaln(gammaprior_old(transexperts,:))))-sum(gammaln(sum(gammaprior_old(transexperts,:),2)))-sum(sum((gammaprior_old(transexperts,:)-1).*psi(gammaprior_old(transexperts,:))))+sum(sum(gammaprior_old(transexperts,:)-1,2).*psi(sum(gammaprior_old(transexperts,:),2)));
       
       lhood=term1+term2+term3+term4+term5;
       
       gammaprior_old(transexperts,:)=gammaprior(transexperts,:);
       gammaprior=gammaprior_old;
  
       
       
       if (old_lhood~=0)
        convergence_estep=(lhood-old_lhood)/abs(old_lhood);
       % old_lhood=lhood;
       else
        convergence_estep=-1;
       end
            
       fprintf('E step iteration: %d: new likelihood vs old likelihood as %f vs %f\n', iter_estep, lhood, old_lhood);
       %if (iter_estep==0)
           disp('terms are \n');
           [term1 term2 term3 term4 term5]
           disp('max gammaprior\n')
           max(max(gammaprior))
           disp('alphaprior\n');
           alphaprior
      % end
       %lhood=complikelihood;            
             
       iter_estep=iter_estep+1;  

    end
end


