Beispiel #1
0
void MBC_MCMC_loop(ERGMM_MCMC_Model *model, ERGMM_MCMC_Priors *prior,
		   ERGMM_MCMC_MCMCState *cur, ERGMM_MCMC_MCMCSettings *setting, ERGMM_MCMC_ROutput *outlists)
{  
  unsigned int pos=0;
  unsigned int iter, total_iters = setting->sample_size*setting->interval;

  //  Rprintf("Started MCMC loop.\n");
  /* Note that indexing here starts with 1.
     It can be thought of as follows:
     At the end of the updates, we have made iter complete MCMC updates. */
  for(iter=1;iter<=total_iters;iter++){

    R_CheckUserInterrupt(); // So that CTRL-C can interrupt the run.

    ERGMM_MCMC_CV_up(model,prior,cur);
    ERGMM_MCMC_logp_Z(model, cur->state);
    
    // If we have a new MLE (actually, the highest likelihood encountered to date), store it.
    if( cur->state->lpZ > outlists->lpZ[0] ) MBC_MCMC_store_iteration(0,model,cur->state,setting,outlists);
    if( cur->state->lpZ + cur->state->lpLV > outlists->lpZ[1] + outlists->lpLV[1] ) MBC_MCMC_store_iteration(1,model,cur->state,setting,outlists);

    /* every interval save the results */
    if((iter % setting->interval) == 0){
      pos = (iter/setting->interval-1)+MBC_OUTLISTS_RESERVE;

      // Store current iteration.
      MBC_MCMC_store_iteration(pos, model, cur->state, setting, outlists);

    }

  } // end main MCMC loop

  return;
  
}
Beispiel #2
0
void ERGMM_MCMC_loop(ERGMM_MCMC_Model *model, ERGMM_MCMC_Priors *prior,
		     ERGMM_MCMC_MCMCState *cur, ERGMM_MCMC_MCMCSettings *setting, ERGMM_MCMC_ROutput *outlists)
{  
  unsigned int n_accept_z=0, n_accept_b=0, pos=0;
  unsigned int iter, total_iters = setting->sample_size*setting->interval;

  /* Note that indexing here starts with 1.
     It can be thought of as follows:
     At the end of the updates, we have made iter complete MCMC updates. */
  for(iter=1;iter<=total_iters;iter++){

    R_CheckUserInterrupt(); // So that CTRL-C can interrupt the run.
    if(model->latent || cur->state->sender || cur->state->receiver)
      n_accept_z += ERGMM_MCMC_Z_RE_up(model, prior, cur, setting);

    if(model->latent){
      // Update cluster parameters (they are separated from data by Z, so full conditional sampling).
      // Note that they are also updated in coef_up_scl_tr_Z_shift_RE.
      if(model->clusters>0)
	ERGMM_MCMC_CV_up(model,prior,cur);
      else
	ERGMM_MCMC_LV_up(model,prior,cur);
    }

    /* Update coef given this new value of Z and conditioned on everything else.
       Also propose to scale Z and shift random effects.
    */
    if( ERGMM_MCMC_coef_up_scl_Z_shift_RE(model,prior,cur,setting) ){
      n_accept_b++;
    }

    if(cur->state->sender || cur->state->receiver){
      ERGMM_MCMC_REV_up(model,prior,cur);
    }

    // If we have a new MLE (actually, the highest likelihood encountered to date), store it.
    if( cur->state->llk > GET_DEFAULT(outlists->llk,0,0) ) ERGMM_MCMC_store_iteration(0,model,cur->state,setting,outlists);

    // If we have a new posterior mode (actually, the highest joint density of all variables but K observed to date), store it.
    if( cur->state->llk + cur->state->lpZ + cur->state->lpLV + 
	cur->state->lpcoef + cur->state->lpRE + cur->state->lpREV + cur->state->lpdispersion >
	GET_DEFAULT(outlists->llk,1,0) + GET_DEFAULT(outlists->lpZ,1,0) + GET_DEFAULT(outlists->lpLV,1,0) + 
	GET_DEFAULT(outlists->lpcoef,1,0) + GET_DEFAULT(outlists->lpRE,1,0) + GET_DEFAULT(outlists->lpREV,1,0) +
	GET_DEFAULT(outlists->lpdispersion,1,0) )
      ERGMM_MCMC_store_iteration(1,model,cur->state,setting,outlists);

    /* every interval save the results */
    if((iter % setting->interval) == 0){
      pos = (iter/setting->interval-1)+ERGMM_OUTLISTS_RESERVE;

      // Store current iteration.
      ERGMM_MCMC_store_iteration(pos, model, cur->state, setting, outlists);

      // Acceptance rates.
      outlists->coef_rate[pos] = (double) ((double)n_accept_b)/((double)setting->interval);
      if(outlists->Z_rate_move){
	outlists->Z_rate_move[pos] = (double) ((double)n_accept_z)/((double)setting->interval*model->verts); 
      }

      n_accept_z=0; 
      n_accept_b=0; 
    }

  } // end main MCMC loop

  return;
  
}