// [[Rcpp::export]]
double alphapost(colvec& y,mat& X, int k, colvec t, colvec& betadraw, 
                 mat& Rdraw, mat& S, double gammadraw, double alpha, 
                 double delta, double eta){
  
  int n = X.n_rows, l = n/k;
  
  mat invR = inv(Rdraw);
  mat invS = inv(S);
  mat invSig = invS*invR*invS;
  
  double empSS = 0;
  
  for (int j=0; j<n; j+=k){
    
    colvec res = y.rows(j,j+k-1)-X.rows(j,j+k-1)*betadraw;
    empSS += as_scalar(trans(res)*invSig*res);
    
  }
  
  //colvec alphat = -alpha*t, gammat = - gammadraw/t;
  //double maxalphat = max(alphat), maxgammat = max(gammat);
  double zalpha = max(-alpha*t)/delta, 
    zgamma = max(-gammadraw/t)/eta;
  
  double dalpha = R::dnorm(zalpha,0.0,1.0,0);
  double dgamma = R::dnorm(zgamma,0.0,1.0,0);
  
  double logval =  -0.5*l*log(det(S*Rdraw*S))-0.5*empSS-0.5*pow((alpha/eta),2)-log(1-dalpha)-log(1-dgamma);
  
  return logval;
  
}
// [[Rcpp::export]]
double Rpost(colvec& y, mat& X, int k, colvec& betadraw, 
             mat& Sdraw, mat& R){
  
  int n = X.n_rows, l = n/k;
  
  mat invR = inv(R);
  mat invS = inv(Sdraw);
  mat invSig = invS*invR*invS;
  
  double empSS = 0;
  
  for (int j=0; j<n; j+=k){
    
    colvec res = y.rows(j,j+k-1)-X.rows(j,j+k-1)*betadraw;
    empSS += as_scalar(trans(res)*invSig*res);
    
  }
  
  return (-0.5*l*log(det(R))-0.5*empSS);
  
}
// [[Rcpp::export]]
colvec betaUpdate(colvec& y, mat& X, int k, mat& Sdraw, 
                  mat& Rdraw, colvec& B0, mat& sigB){
  
  int n = X.n_rows, p = X.n_cols;
  mat VB = zeros<mat>(p,p);
  colvec muB = zeros<colvec>(p);
  
  mat invR = inv(Rdraw);
  mat invS = inv(Sdraw);
  mat invSig = invS*invR*invS;
  
  for(int j=0; j<n; j+=k){
    VB += trans(X.rows(j,j+k-1))*invSig*X.rows(j,j+k-1);
    muB += trans(X.rows(j,j+k-1))*invSig*y.rows(j,j+k-1);
  }
  
  mat V1 = inv(inv(sigB)+VB);
  colvec mu1 = V1*(inv(sigB)*B0+muB);
  
  colvec betadraw = vectorise(mvrnorm(1, mu1,V1));
  
  return betadraw;
  
}
// [[Rcpp::export]]
double logLik(colvec& y, mat& X, int k, int nblock, List t,
              colvec& betadraw, mat& Sdraw, mat& Rdraw, 
              colvec& B0, mat& sigB, double delta, double eta,
              colvec& gammadraw, colvec& alphadraw){
  
  int n = X.n_rows, p = X.n_cols, l = n/k;
  
  double logy, logbeta, logprior,logfull;
  
  
  int tlen = k/nblock;
  double alphaj, gammaj, maxalphat, maxgammat; 
  
  colvec alphat = zeros<colvec>(tlen),
    gammat = zeros<colvec>(tlen);
  
  colvec loggamma(nblock), logalpha(nblock);
  colvec valpha(nblock), vgamma(nblock), 
  dalpha(nblock), dgamma(nblock);
  
  NumericVector tj(tlen);
  
  mat invR = inv(Rdraw);
  mat invS = inv(Sdraw);
  mat invSig = invS*invR*invS;
  
  double empSS = 0;
  
  for (int j=0; j<n; j+=k){
    
    colvec res = y.rows(j,j+k-1)-X.rows(j,j+k-1)*betadraw;
    empSS += as_scalar(trans(res)*invSig*res);
    
  }
  
  // log likelihood of data y, pi = M_PI
  logy = as_scalar(-0.5*k*l*log(2*M_PI)-0.5*l*log(det(Sdraw*Rdraw*Sdraw))-0.5*empSS);
  
  // log prior for beta
  logbeta = as_scalar(-0.5*p*log(2*M_PI)-0.5*log(det(sigB))-0.5*(trans(betadraw-B0)*inv(sigB)*(betadraw-B0)));
  
  
  //RNGScope scope;
  
  for(int j=0; j<nblock; j++){
    
    tj = t[j];
    
    alphaj = alphadraw(j);
    alphat = -alphaj*tj;
    maxalphat = max(alphat);
    
    gammaj = gammadraw(j);
    gammat = - gammaj/tj;
    maxgammat = max(gammat);
    
    valpha(j) = maxalphat/delta;
    vgamma(j) = maxgammat/eta;
    dalpha(j) = R::dnorm(valpha(j),0.0,1.0,0);
    dgamma(j) = R::dnorm(vgamma(j),0.0,1.0,0);
    
    loggamma(j) = -0.5*log(2*M_PI)-log(delta)-0.5*pow(gammadraw(j)/delta,2)-log(1-dalpha(j));
    logalpha(j) = -0.5*log(2*M_PI)-log(eta)-0.5*pow(alphadraw(j)/eta,2)-log(1-dgamma(j));
    
  }
  
  logprior = logbeta + sum(loggamma) + sum(logalpha);
  
  logfull = logy + logprior;
  
  return logfull;
}