function [alphacost,alphagrad]=terms_wrt_alpha(logalphaprior,gammaprior,NumExperts0, K, transexperts)

%% This function is to calculate the cost and grad w.r.t alpha 

%%gamma: NumExperts*K
%%alpha: 1*K vector

%globalvars;

alphacost1=NumExperts0*(sum(gammaln(exp(logalphaprior)))-gammaln(sum(exp(logalphaprior))));

psigammasum=repmat(psi(sum(gammaprior(transexperts,:),2)),1,K);%NumExperts*K
alphacost2=sum(sum((psigammasum-psi(gammaprior(transexperts,:)))*(exp(logalphaprior)-1)));

alphacost=alphacost1+alphacost2;

alphagrad1=NumExperts0*(psi(exp(logalphaprior))-repmat(psi(sum(exp(logalphaprior))),K,1));
alphagrad2=sum(psigammasum-psi(gammaprior(transexperts,:)))';

alphagrad=(alphagrad1+alphagrad2).*exp(logalphaprior);