Пример #1
0
  void LSB::draw_gamma(){
    // draws vector of 0's and 1's indicating which coefficients are
    // nonzero

    if(max_nflips_ == 0) return;

    Selector inc = mod_->inc();
    uint nv = inc.nvars_possible();
    double logp = log_model_prob(inc);
    if(!std::isfinite(logp)){
      ostringstream err;
      err << "LogitSamplerBma did not start with a legal configuration."
	  << endl
	  << "Selector vector:  " << inc << endl
	  << "beta:            " << mod_->included_coefficients() <<endl;
      throw_exception<std::runtime_error>(err.str());
    }

    std::vector<uint> flips = seq<uint>(0, nv-1);
    std::random_shuffle(flips.begin(), flips.end());
    uint hi = std::min<uint>(nv, max_nflips_);
    for(uint i=0; i<hi; ++i){
      uint I = flips[i];
      inc.flip(I);
      double logp_new = log_model_prob(inc);
      if( keep_flip(logp, logp_new)) logp = logp_new;
      else inc.flip(I);  // reject the flip, so flip back
    }
    mod_->coef().set_inc(inc);
  }
 double BLSSS::mcmc_one_flip(Selector &mod, uint which_var, double logp_old) {
   mod.flip(which_var);
   double logp_new = log_model_prob(mod);
   double u = runif_mt(rng(), 0,1);
   if(log(u) > logp_new - logp_old) {
     mod.flip(which_var);  // reject draw
     return logp_old;
   }
   return logp_new;
 }