//---------------------------------------------------------------------- uint MlvsDataImputer::unmix(RNG &rng, double u) const { uint K = post_prob_.size(); for (uint k = 0; k < K; ++k) post_prob_[k] = log_mixing_weights_[k] + dnorm(u, mu_[k], sd_[k], true); post_prob_.normalize_logprob(); return rmulti_mt(rng, post_prob_); }
//---------------------------------------------------------------------- void BLCSSS::draw(){ enum SamplingMethod { DATA_AUGMENTATION = 0, RWM = 1, TIM = 2 }; SamplingMethod method = SamplingMethod(rmulti_mt(rng(), sampler_weights_)); switch (method) { case DATA_AUGMENTATION: { MoveTimer timer = move_accounting_.start_time("auxmix"); BinomialLogitSpikeSlabSampler::draw(); move_accounting_.record_acceptance("auxmix"); break; } case RWM: { MoveTimer timer = move_accounting_.start_time("rwm (total time)"); rwm_draw(); break; } case TIM: { MoveTimer timer = move_accounting_.start_time("TIM (total time)"); tim_draw(); break; } default: report_error("Unknown method in BinomialLogitSpikeSlabSampler::draw."); } // switch }
void HmmFilter::bkwd_sampling_mt(const std::vector<Ptr<Data> > &dv, RNG & eng){ uint n = dv.size(); // pi was already set by fwd. // So the following line would break things when n=1. // pi = one * P.back(); uint s = rmulti_mt(eng,pi); models_[s]->add_data(dv.back()); for(uint i=n-1; i!=0; --i){ pi = P[i].col(s); pi.normalize_prob(); uint r = rmulti_mt(eng,pi); models_[r]->add_data(dv[i-1]); markov_->suf()->add_transition(r,s); s=r; } markov_->suf()->add_initial_value(s); }
void HmmFilter::bkwd_sampling_mt(const std::vector<Ptr<Data>> &data, RNG &rng) { uint sample_size = data.size(); std::vector<int> imputed_state(sample_size); // pi was already set by fwd. // So the following line would break things when sample_size=1. // pi = one * P.back(); uint s = rmulti_mt(rng, pi); models_[s]->add_data(data.back()); imputed_state.back() = s; for (int64_t i = sample_size - 1; i >= 0; --i) { pi = P[i].col(s); pi.normalize_prob(); uint r = rmulti_mt(rng, pi); if (i > 0) { imputed_state[i - 1] = r; } models_[r]->add_data(data[i - 1]); markov_->suf()->add_transition(r, s); s = r; } markov_->suf()->add_initial_value(s); imputed_state_map_[data] = imputed_state; }
int rmulti(int lo, int hi){ return rmulti_mt(GlobalRng::rng, lo, hi);}
Ptr<PosteriorSampler> CS::choose_sampler()const{ uint k = rmulti_mt(rng(), probs_); return samplers_[k]; }
void LiuWestParticleFilter::update(RNG &rng, const Data &observation, int observation_time) { //====== Step 1 // Compute the means and variances to be used in the kernel density // estimate. std::vector<Vector> predicted_state_mean; predicted_state_mean.reserve(number_of_particles()); MvnSuf suf(parameter_particles_[0].size()); for (int i = 0; i < number_of_particles(); ++i) { suf.update_raw(parameter_particles_[i]); predicted_state_mean.push_back(hmm_->predicted_state_mean( state_particles_[i], observation_time, parameter_particles_[i])); } Vector parameter_mean = suf.ybar(); Vector kernel_weights(number_of_particles()); double max_log_weight = negative_infinity(); std::vector<Vector> predicted_parameter_mean(number_of_particles()); double particle_weight = sqrt(1 - square(kernel_scale_factor_)); for (int i = 0; i < number_of_particles(); ++i) { predicted_parameter_mean[i] = particle_weight * parameter_particles_[i] + (1 - particle_weight) * parameter_mean; kernel_weights[i] = log_weights_[i] + hmm_->log_observation_density( observation, predicted_state_mean[i], observation_time, predicted_parameter_mean[i]); if (!std::isfinite(kernel_weights[i])) { kernel_weights[i] = negative_infinity(); } max_log_weight = std::max<double>(max_log_weight, kernel_weights[i]); } double total_weight = 0; for (int i = 0; i < number_of_particles(); ++i) { double weight = exp(kernel_weights[i] - max_log_weight); if (!std::isfinite(weight)) { weight = 0; } total_weight += weight; kernel_weights[i] = weight; } kernel_weights /= total_weight; SpdMatrix sample_variance = suf.sample_var(); if (sample_variance.rank() < sample_variance.nrow()) { //////////////// // Refresh the parameter distribution. //////////////// } Cholesky sample_variance_cholesky(sample_variance); if (!sample_variance_cholesky.is_pos_def()) { report_error("Sample variance is not positive definite."); } Matrix variance_cholesky = kernel_scale_factor_ * sample_variance_cholesky.getL(); //===== Step 2: // Propose new values of state and parameters, and update the weights. // // Space is needed for the new proposals, because sampling and updating is // done with replacement. std::vector<Vector> new_state_particles(number_of_particles()); Vector new_log_weights(number_of_particles()); for (int i = 0; i < number_of_particles(); ++i) { int particle = rmulti_mt(rng, kernel_weights); Vector parameter_proposal = rmvn_L_mt(rng, predicted_parameter_mean[particle], variance_cholesky); try { Vector state_proposal = hmm_->simulate_transition( rng, state_particles_[particle], observation_time - 1, parameter_proposal); parameter_particles_[i] = parameter_proposal; new_state_particles[i] = state_proposal; new_log_weights[i] = hmm_->log_observation_density(observation, state_proposal, observation_time, parameter_proposal) - hmm_->log_observation_density(observation, predicted_state_mean[particle], observation_time, predicted_parameter_mean[particle]); if (!std::isfinite(new_log_weights[i])) { new_log_weights[i] = negative_infinity(); } } catch (...) { // If we're here it means the parameter proposal was an illegal value. // Stuff the new_state_particles[i] entry with the right-sized garbage, // and set the weight to zero (i.e. set the log weight to negative // infinity). new_state_particles[i] = state_particles_[i]; new_log_weights[i] = negative_infinity(); } } std::swap(new_state_particles, state_particles_); std::swap(new_log_weights, log_weights_); }