// -------------------------------------------------- double RngBoost::betaSample(double alpha, double beta) const { static boost::uniform_01<boost::mt19937> zeroone(m_rng); boost::math::beta_distribution<double> beta_dist(alpha, beta); return quantile(beta_dist, zeroone()); }
RealType skewed_beta<RealType>::operator()(real_type parent_val, RNG &rng) const { real_type beta = find_beta(parent_val); real_type u = std::uniform_real_distribution<real_type>()(rng); boost::math::beta_distribution<real_type> beta_dist(m_alpha, beta); real_type x = boost::math::quantile(beta_dist, u); return x; }
int MHSampler::construct_alias_table() { size_t vocab_size = _model->vocab_size(); _topic_indexes = std::vector<TopicIndex>(vocab_size); _alias_tables = std::vector<VoseAlias>(vocab_size); _prob_sum = std::vector<double>(vocab_size); // 构建每个词的alias table (不包含先验部分) std::vector<double> dist; for (size_t i = 0; i < vocab_size; ++i) { dist.clear(); double prob_sum = 0; for (auto& iter : _model->word_topic(i)) { int topic_id = iter.first; // topic index int word_topic_count = iter.second; // topic count size_t topic_sum = _model->topic_sum(topic_id); // topic sum _topic_indexes[i].push_back(topic_id); double q = word_topic_count / (topic_sum + _model->beta_sum()); dist.push_back(q); prob_sum += q; } _prob_sum[i] = prob_sum; if (dist.size() > 0) { _alias_tables[i].initialize(dist); } } // 构建先验参数beta的alias table _beta_prior_sum = 0; std::vector<double> beta_dist(_model->num_topics(), 0); for (int i = 0; i < _model->num_topics(); ++i) { beta_dist[i] = _model->beta() / (_model->topic_sum(i) + _model->beta_sum()); _beta_prior_sum += beta_dist[i]; } _beta_alias.initialize(beta_dist); return 0; }
// -------------------------------------------------- double BasicPdfsBoost::betaPdfActualValue(double x, double alpha, double beta) const { boost::math::beta_distribution<> beta_dist(alpha, beta); return boost::math::pdf(beta_dist, x); }