double GenericGaussianVarianceSampler::draw( RNG &rng, double data_df, double data_ss) const { double DF = data_df + 2 * prior_->alpha(); double SS = data_ss + 2 * prior_->beta(); if (sigma_max_ == 0.0) { return 0.0; } else if(sigma_max_ == infinity()){ return 1.0 / rgamma_mt(rng, DF/2, SS/2); } else { return 1.0 / rtrun_gamma_mt(rng, DF/2, SS/2, 1.0/square(sigma_max_)); } }
void MVT::Impute(bool sample, RNG &rng) { std::vector<Ptr<WVD> > &V(mvn->dat()); for (uint i = 0; i < V.size(); ++i) { Ptr<WVD> d = V[i]; const Vector &y(d->value()); double delta = siginv().Mdist(y, mu()); double a = (nu() + y.length()) / 2.0; double b = (nu() + delta) / 2.0; double w = sample ? rgamma_mt(rng, a, b) : a / b; d->set_weight(w); } mvn->refresh_suf(); wgt->refresh_suf(); }
// Draws sigma given phi and observed data. void ArPosteriorSampler::draw_sigma(){ // ss = y - xb y - xb // = yty - 2 bt xty + bt xtx b const Vec &phi(model_->phi()); const Vec &xty(model_->suf()->xty()); const Spd &xtx(model_->suf()->xtx()); double ss = 2 * siginv_prior_->beta(); ss += xtx.Mdist(phi) - 2 * phi.dot(xty) + model_->suf()->yty(); double df = 2 * siginv_prior_->alpha(); df += model_->suf()->n(); double siginv; if (upper_sigma_truncation_point_ == BOOM::infinity()) { siginv = rgamma_mt(rng(), df/2, ss/2); } else { siginv = rtrun_gamma_mt(rng(), df/2, ss/2, 1.0/pow(upper_sigma_truncation_point_, 2)); } model_->set_sigsq(1.0 / siginv); }
void ZeroInflatedPoissonSampler::draw() { double p = model_->zero_probability(); double pbinomial = p; double ppoisson = (1-p) * dpois(0, model_->lambda()); double nc = pbinomial + ppoisson; pbinomial /= nc; int nzero = lround(model_->suf()->number_of_zeros()); double nzero_binomial = rbinom_mt(rng(), nzero, pbinomial); double nzero_poission = nzero - nzero_binomial; int counter = 0; do { if (++counter > 1000) { report_error("rbeta produced the value 0 over 1000 times."); } p = rbeta_mt(rng(), zero_probability_prior_->a() + nzero_binomial, zero_probability_prior_->b() + nzero - nzero_binomial + model_->suf()->number_of_positives()); } while (p <= 0.0 || p >= 1.0); model_->set_zero_probability(p); double a = lambda_prior_->alpha() + model_->suf()->sum_of_positives(); double b = lambda_prior_->beta() + model_->suf()->number_of_positives(); b += nzero_poission; double lambda = -1; // need to declare lambda before the do loop. do { if (++counter > 1000) { report_error("rgamma produced the value 0 over 1000 times."); } lambda = rgamma_mt(rng(), a, b); } while (lambda <= 0.0); model_->set_lambda(lambda); }
Vector rmvt_ivar_mt(RNG & rng, const Vector &mu, const SpdMatrix &ivar, double nu){ double w = rgamma_mt(rng, nu/2,nu/2); return rmvn_ivar_mt(rng, mu, w*ivar); }
Vector rmvt_mt(RNG & rng, const Vector &mu, const SpdMatrix &Sigma, double nu){ double w = rgamma_mt(rng, nu/2, nu/2); return rmvn_mt(rng, mu, Sigma/w); }
double rchisq_mt(BOOM::RNG & rng, double df){ if (!R_FINITE(df) || df <= 0.0) ML_ERR_return_NAN; return rgamma_mt(rng, df / 2.0, 2.0); }