void HDP::doc_state_update(DocState* doc_state, int i, int update) { int d = doc_state->doc_id_; int w = doc_state->words_[i].word_; int c = doc_state->words_[i].count_; int k = doc_state->words_[i].topic_assignment_; //assert(k >= 0); // we must have it assigned before or assigned to a new one. if (update > 0) { if (hdp_state_->topic_lambda_[k][w] == 0) unique_topic_by_word_[w].insert(k); if (word_counts_by_topic_doc_[k][d] == 0) unique_topic_by_doc_[d].insert(k); } update *= c; // Update HDP state smoothing_prob_sum_ -= smoothing_prob_[k]; hdp_state_->word_counts_by_topic_[k] += update; hdp_state_->topic_lambda_[k][w] += update; doc_prob_sum_[d] -= doc_prob_[k][d]; word_counts_by_topic_doc_[k][d] += update; if (update < 0 ) { if (hdp_state_->topic_lambda_[k][w] == 0) unique_topic_by_word_[w].erase(k); if (word_counts_by_topic_doc_[k][d] == 0) unique_topic_by_doc_[d].erase(k); } if (update > 0 && k == hdp_state_->num_topics_) { // a new topic is generated. RNGScope scope; hdp_state_->num_topics_ ++; double new_stick = Rf_rbeta(1.0, hdp_state_->gamma_) * hdp_state_->pi_left_; hdp_state_->pi_left_ = hdp_state_->pi_left_ - new_stick; hdp_state_->pi_[k] = new_stick; if ((int)hdp_state_->word_counts_by_topic_.size() < hdp_state_->num_topics_ + 1) { int new_size = 2 * hdp_state_->num_topics_ + 1; vct_ptr_resize(&hdp_state_->topic_lambda_, new_size, hdp_state_->size_vocab_); hdp_state_->word_counts_by_topic_.resize(new_size, 0); hdp_state_->beta_u_.resize(new_size, 0); //hdp_state_->beta_v_.resize(new_size, 0.0); hdp_state_->pi_.resize(new_size, 0.0); vct_ptr_resize(&word_counts_by_topic_doc_, new_size, num_docs_); vct_ptr_resize(&table_counts_by_topic_doc_, new_size, num_docs_); smoothing_prob_.resize(new_size, 0.0); vct_ptr_resize(&doc_prob_, new_size, num_docs_); } } double etaW = hdp_state_->size_vocab_ * hdp_state_->eta_; smoothing_prob_[k] = hdp_state_->alpha_ * hdp_state_->pi_[k] / (hdp_state_->word_counts_by_topic_[k] + etaW); smoothing_prob_sum_ += smoothing_prob_[k]; doc_prob_[k][d] = word_counts_by_topic_doc_[k][d] / (hdp_state_->word_counts_by_topic_[k] + etaW); doc_prob_sum_[d] += doc_prob_[k][d]; }
void HDP::sample_first_level_concentration(double gamma_a, double gamma_b) { /// (p 585 in escobar and west) double shape = gamma_a; double scale = gamma_b; int n = 0; RNGScope scope; for (int k = 0; k < hdp_state_->num_topics_; ++k) { n += hdp_state_->beta_u_[k]; } double eta = Rf_rbeta(hdp_state_->gamma_ + 1, n); double pi = shape + hdp_state_->num_topics_ - 1; double rate = 1.0 / scale - log(eta); pi = pi / (pi + rate * n); unsigned int cc = Rf_rbinom(1,pi); if (cc == 1) hdp_state_->gamma_ = Rf_rgamma(shape + hdp_state_->num_topics_, 1.0 / rate); else hdp_state_->gamma_ = Rf_rgamma(shape + hdp_state_->num_topics_ - 1, 1.0 / rate); }
void HDP::sample_second_level_concentration(double alpha_a, double alpha_b) { double shape = alpha_a; double scale = alpha_b; RNGScope scope; int n = 0; for (int k = 0; k < hdp_state_->num_topics_; ++k) { n += hdp_state_->beta_u_[k]; } double rate, sum_log_w, sum_s; for (int step = 0; step < 20; ++step) { sum_log_w = 0.0; sum_s = 0.0; for (int d = 0; d < num_docs_; ++d) { sum_log_w += log(Rf_rbeta(hdp_state_->alpha_ + 1, doc_states_[d]->doc_length_)); sum_s += (double)Rf_rbinom(1,doc_states_[d]->doc_length_ / (doc_states_[d]->doc_length_ + hdp_state_->alpha_)); } rate = 1.0 / scale - sum_log_w; hdp_state_->alpha_ = Rf_rgamma(shape + n - sum_s, 1.0 / rate); } }
Type rbeta(Type shape1, Type shape2) { return Rf_rbeta(asDouble(shape1), asDouble(shape2)); }
extern "C" void glm_gibbs(double * rZ, double * rxo, double * rlam, int * rmodelprior, double * rpriorprob, double * rbeta1, double * rbeta2, int * rburnin, int * rniter, int * rscalemixture, double * ralpha, int * rno, int * rna, int * rp, double * B_mcmc, double * prob_mcmc, int * gamma_mcmc, double * phi_mcmc, double * lam_mcmc, double * B_rb, double * prob_rb, double * intercept_mcmc, double * xo_scale) { GetRNGstate(); //MCMC Variables// int burnin=*rburnin; int niter=*rniter; //Dimensions// int p=*rp; int no=*rno; int na=*rna; //Phi Variables// double phi=1.0; //Yo Variables// std::vector<double> Z(rZ, rZ+no); std::vector<double> xo(rxo, rxo+no*p); standardize_xo(xo,xo_scale,no,p); std::vector<double> xoyo(p); double yobar=0; std::vector<double> xoxo(p*p); dgemm_( &transT, &transN, &p, &p, &no, &unity, &*xo.begin(), &no, &*xo.begin(), &no, &inputscale0, &*xoxo.begin(), &p ); //Construct Xa// std::vector<double> xa(p*(p+1)/2); //Triangular Packed Storage std::vector<double> d(p); chol_xa(xa,xoxo,d,p); //Reserve Memory for Submatrices// std::vector<double> xog; xog.reserve(no*p); std::vector<double> xogyo; xogyo.reserve(p); std::vector<double> xogxog_Lamg; xogxog_Lamg.reserve(p*p); std::vector<double> xag; xag.reserve(na*p); //Ya Variables// std::vector<double> xaya(p); //Beta Variables// double intercept=0; std::vector<double> Bols(p); std::vector<double> B(p,0.0); std::vector<double> Bg; Bg.reserve(p); //Lambda Variables// int scalemixture=*rscalemixture; double alpha=*ralpha; std::vector<double> lam(rlam,rlam+p); std::vector<double> lamg; lamg.reserve(p); //vector instead of diagonal pxp matrix //Gamma Variables// std::vector<int> gamma(p,1); int p_gamma=std::accumulate(gamma.begin(),gamma.end(),0); bool gamma_diff=true; int modelprior=*rmodelprior; //Probability Variables// std::vector<double> prob(p); std::vector<double> odds(p); std::vector<double> priorprob(rpriorprob,rpriorprob+p); //Theta Variables// double theta=0.5; double beta1=*rbeta1; double beta2=*rbeta2; //Store Initial Values// std::copy(B.begin(),B.end(),B_mcmc); std::copy(prob.begin(),prob.end(),prob_mcmc); std::copy(gamma.begin(),gamma.end(),gamma_mcmc); std::copy(lam.begin(),lam.end(),lam_mcmc); //Run Gibbs Sampler// for (int t = 1; t < niter; ++t) { //Form Submatrices// if(p_gamma) submatrices_uncollapsed(gamma_diff,B,xog,xag,lamg,Bg,gamma,lam,xo,xa,p_gamma,p,no,na); //Draw xoyo// draw_xoyo(Z,xoyo,yobar,xo,xog,Bg,phi,no,p,p_gamma,intercept); //Draw xaya// draw_uncollapsed_xaya(xaya,xa,xag,Bg,phi,na,p,p_gamma); //Compute Probabilities// if(modelprior==1) { bernoulli_probabilities(prob,odds,Bols,d,xoyo,xaya,priorprob,lam,phi); }else if(modelprior==2) { betabinomial_probabilities(prob,odds,Bols,d,xoyo,xaya,theta,lam,phi); }else { uniform_probabilities(prob,odds,Bols,d,xoyo,xaya,lam,phi); } //Draw Gamma// draw_gamma(gamma,p_gamma,prob); //Draw Theta// if(modelprior==2) theta=Rf_rbeta(beta1+p_gamma,p-p_gamma+beta2); //Draw Beta// draw_beta(gamma,B,Bols,d,lam,phi); //Draw Intercept// intercept=yobar+sqrt(1/(no*phi))*Rf_rnorm(0,1); //Draw Lambda// if(scalemixture) draw_lambda_t(lam,gamma,alpha,B,phi); //Store Draws// intercept_mcmc[t]=intercept; std::copy(gamma.begin(),gamma.end(),(gamma_mcmc+p*t)); std::copy(prob.begin(),prob.end(),(prob_mcmc+p*t)); std::copy(B.begin(),B.end(),(B_mcmc+p*t)); std::copy(lam.begin(),lam.end(),(lam_mcmc+p*t)); //Rao Blackwell// if(t>=burnin) rao_blackwell(B_rb,prob_rb,B,prob,burnin,niter); //Has Gamma Changed?// gamma_diff=gamma_change(gamma_mcmc,t,p); } PutRNGstate(); }