//-------------------------------------------------------------------------- void MessageDriven::processMessage(const Message* msg) { const string addr = msg->address(); // create a regular expression OSCRegexp r(OSCAddress::addressFirst(addr).c_str()); // and call propose with this regexp and with the dest osc address tail propose(msg, &r, OSCAddress::addressTail(addr)); }
void Sampler::unlock(uint id) { // Unlock this chain locked_[id] = false; // The hotter chain no longer has to wait for this chain, so // it can propose new state propose(id + 1); // Check if this was the coldest chain if (id % chains_.numChains() != 0) { // Lock the chain that is below (colder) than this. locked_[id - 1] = true; } else { // This is the coldest chain and there is no one to swap with propose(id); } }
int MHSampler::word_proposal(LDADoc& doc, Token& token, int old_topic) { int new_topic = propose(token.id); // prpose a new topic from alias table if (new_topic != old_topic) { float proposal_old = word_proposal_distribution(token.id, old_topic); float proposal_new = word_proposal_distribution(token.id, new_topic); float proportion_old = proportional_funtion(doc, token, old_topic); float proportion_new = proportional_funtion(doc, token, new_topic); double transition_prob = (proportion_new * proposal_old) / (proportion_old * proposal_new); double rejection = rand(); int mask = -(rejection < transition_prob); return (new_topic & mask) | (old_topic & ~mask); } return new_topic; }
std::pair<uint, State> Sampler::step(const std::vector<double>& sigmas, const std::vector<double>& betas) { haveFlushed_ = false; // Listen for replies. As soon as a new state comes back, // add it to the corresponding chain, and submit a new proposed state auto result = requester_.retrieve(); uint id = result.first; double energy = 0.0; for (const auto& r : result.second) { energy += r; } numOutstandingJobs_--; // Update the parameters for this id chains_.setSigma(id, sigmas[id]); chains_.setBeta(id, betas[id]); // Handle the new proposal and add a new state to the chain chains_.append(id, propStates_[id], energy); // Check if this chain was locked. If it was locked, it means that // the chain above (hotter) locked it so that it can swap with it if (locked_[id]) { // Try swapping this chain with the one above it chains_.swap(id, id + 1); // Unlock this chain, propgating the lock downwards unlock(id); } else if (chains_.isHottestInStack(id) && chains_.length(id) % swapInterval_ == 0 && chains_.numChains() > 1) { // The hottest chain is ready to swap. Lock the next chain // to prevent it from proposing any more locked_[id - 1] = true; } else { propose(id); } return {id, chains_.lastState(id)}; }
// word proposal for Sentence-LDA int MHSampler::word_proposal(SLDADoc& doc, Sentence& sent, int old_topic) { int new_topic = old_topic; for (const auto& word_id : sent.tokens) { new_topic = propose(word_id); // prpose a new topic from alias table if (new_topic != old_topic) { float proportion_old = proportional_funtion(doc, sent, old_topic); float proportion_new = proportional_funtion(doc, sent, new_topic); float proposal_old = word_proposal_distribution(word_id, old_topic); float proposal_new = word_proposal_distribution(word_id, new_topic); double transition_prob = (proportion_new * proposal_old) / (proportion_old * proposal_new); double rejection = rand(); int mask = -(rejection < transition_prob); new_topic = (new_topic & mask) | (old_topic & ~mask); } } return new_topic; }
/** @short Markov Chain Monte Carlo @param rng the GSL random number generator @param p the vector of parameters being searched over @param x the vector of observations */ void mcmc(const gsl_rng *rng, gsl_vector *p, const gsl_vector *x) { gsl_vector * p_test = gsl_vector_alloc(p->size); size_t i; gsl_histogram * hist = gsl_histogram_alloc(nbins); gsl_histogram_set_ranges_uniform(hist, min, max); for(i=0; i < chain_length; i++) { gsl_vector_memcpy(p_test,p); propose(rng, p_test); double lnLikelihoodRatio = ln_lik_fn(p_test, x) - ln_lik_fn(p, x); if (take_step(rng, lnLikelihoodRatio)) p = p_test; gsl_histogram_increment(hist, p->data[1]); } gsl_vector_free(p_test); gsl_histogram_fprintf(stdout, hist, "%g", "%g"); gsl_histogram_free(hist); }
Sampler::Sampler(comms::Requester& requester, std::vector<std::string> jobTypes, ChainArray& chainArray, const ProposalFunction& propFn, uint swapInterval) : requester_(requester), jobTypes_(std::move(jobTypes)), chains_(chainArray), propFn_(propFn), nstacks_(chains_.numStacks()), nchains_(chains_.numChains()), propStates_(nstacks_*nchains_), swapInterval_(swapInterval), numOutstandingJobs_(0), locked_(nstacks_ * nchains_, false), haveFlushed_(true) { // Start all the chains from hottest to coldest for (uint i = 0; i < chains_.numTotalChains(); i++) { uint c = chains_.numTotalChains() - i - 1; propose(c); } }
Eigen::VectorXd GaussianCovProposal::operator()(uint id, const Eigen::VectorXd &sample, double sigma) { return propose(id, sample, sigma); }
SEXP hitrun(SEXP alpha, SEXP initial, SEXP nbatch, SEXP blen, SEXP nspac, SEXP origin, SEXP basis, SEXP amat, SEXP bvec, SEXP outmat, SEXP debug) { if (! isReal(alpha)) error("argument \"alpha\" must be type double"); if (! isReal(initial)) error("argument \"initial\" must be type double"); if (! isInteger(nbatch)) error("argument \"nbatch\" must be type integer"); if (! isInteger(blen)) error("argument \"blen\" must be type integer"); if (! isInteger(nspac)) error("argument \"nspac\" must be type integer"); if (! isReal(origin)) error("argument \"origin\" must be type double"); if (! isReal(basis)) error("argument \"basis\" must be type double"); if (! isReal(amat)) error("argument \"amat\" must be type double"); if (! isReal(bvec)) error("argument \"bvec\" must be type double"); if (! (isNull(outmat) | isReal(outmat))) error("argument \"outmat\" must be type double or NULL"); if (! isLogical(debug)) error("argument \"debug\" must be logical"); if (! isMatrix(basis)) error("argument \"basis\" must be matrix"); if (! isMatrix(amat)) error("argument \"amat\" must be matrix"); if (! (isNull(outmat) | isMatrix(outmat))) error("argument \"outmat\" must be matrix or NULL"); int dim_oc = LENGTH(alpha); int dim_nc = LENGTH(initial); int ncons = nrows(amat); if (LENGTH(nbatch) != 1) error("argument \"nbatch\" must be scalar"); if (LENGTH(blen) != 1) error("argument \"blen\" must be scalar"); if (LENGTH(nspac) != 1) error("argument \"nspac\" must be scalar"); if (LENGTH(origin) != dim_oc) error("length(origin) != length(alpha)"); if (nrows(basis) != dim_oc) error("nrow(basis) != length(alpha)"); if (ncols(basis) != dim_nc) error("ncol(basis) != length(initial)"); if (ncols(amat) != dim_nc) error("ncol(amat) != length(initial)"); if (LENGTH(bvec) != ncons) error("length(bvec) != nrow(amat)"); if (LENGTH(debug) != 1) error("argument \"debug\" must be scalar"); int dim_out = dim_oc; if (! isNull(outmat)) { dim_out = nrows(outmat); if (ncols(outmat) != dim_oc) error("ncol(outmat) != length(alpha)"); } int int_nbatch = INTEGER(nbatch)[0]; int int_blen = INTEGER(blen)[0]; int int_nspac = INTEGER(nspac)[0]; int int_debug = LOGICAL(debug)[0]; double *dbl_star_alpha = REAL(alpha); double *dbl_star_initial = REAL(initial); double *dbl_star_origin = REAL(origin); double *dbl_star_basis = REAL(basis); double *dbl_star_amat = REAL(amat); double *dbl_star_bvec = REAL(bvec); int has_outmat = isMatrix(outmat); double *dbl_star_outmat = 0; if (has_outmat) dbl_star_outmat = REAL(outmat); if (int_nbatch <= 0) error("argument \"nbatch\" must be positive"); if (int_blen <= 0) error("argument \"blen\" must be positive"); if (int_nspac <= 0) error("argument \"nspac\" must be positive"); check_finite(dbl_star_alpha, dim_oc, "alpha"); check_positive(dbl_star_alpha, dim_oc, "alpha"); check_finite(dbl_star_initial, dim_nc, "initial"); check_finite(dbl_star_origin, dim_oc, "origin"); check_finite(dbl_star_basis, dim_oc * dim_nc, "basis"); check_finite(dbl_star_amat, ncons * dim_nc, "amat"); check_finite(dbl_star_bvec, ncons, "bvec"); if (has_outmat) check_finite(dbl_star_outmat, dim_out * dim_oc, "outmat"); double *state = (double *) R_alloc(dim_nc, sizeof(double)); double *proposal = (double *) R_alloc(dim_nc, sizeof(double)); double *batch_buffer = (double *) R_alloc(dim_out, sizeof(double)); double *out_buffer = (double *) R_alloc(dim_out, sizeof(double)); memcpy(state, dbl_star_initial, dim_nc * sizeof(double)); logh_setup(dbl_star_alpha, dbl_star_origin, dbl_star_basis, dim_oc, dim_nc); double current_log_dens = logh(state); out_setup(dbl_star_origin, dbl_star_basis, dbl_star_outmat, dim_oc, dim_nc, dim_out, has_outmat); SEXP result, resultnames, path, save_initial, save_final; if (! int_debug) { PROTECT(result = allocVector(VECSXP, 3)); PROTECT(resultnames = allocVector(STRSXP, 3)); } else { PROTECT(result = allocVector(VECSXP, 11)); PROTECT(resultnames = allocVector(STRSXP, 11)); } PROTECT(path = allocMatrix(REALSXP, dim_out, int_nbatch)); SET_VECTOR_ELT(result, 0, path); PROTECT(save_initial = duplicate(initial)); SET_VECTOR_ELT(result, 1, save_initial); UNPROTECT(2); SET_STRING_ELT(resultnames, 0, mkChar("batch")); SET_STRING_ELT(resultnames, 1, mkChar("initial")); SET_STRING_ELT(resultnames, 2, mkChar("final")); if (int_debug) { SEXP spath, ppath, zpath, u1path, u2path, s1path, s2path, gpath; int nn = int_nbatch * int_blen * int_nspac; PROTECT(spath = allocMatrix(REALSXP, dim_nc, nn)); SET_VECTOR_ELT(result, 3, spath); PROTECT(ppath = allocMatrix(REALSXP, dim_nc, nn)); SET_VECTOR_ELT(result, 4, ppath); PROTECT(zpath = allocMatrix(REALSXP, dim_nc, nn)); SET_VECTOR_ELT(result, 5, zpath); PROTECT(u1path = allocVector(REALSXP, nn)); SET_VECTOR_ELT(result, 6, u1path); PROTECT(u2path = allocVector(REALSXP, nn)); SET_VECTOR_ELT(result, 7, u2path); PROTECT(s1path = allocVector(REALSXP, nn)); SET_VECTOR_ELT(result, 8, s1path); PROTECT(s2path = allocVector(REALSXP, nn)); SET_VECTOR_ELT(result, 9, s2path); PROTECT(gpath = allocVector(REALSXP, nn)); SET_VECTOR_ELT(result, 10, gpath); UNPROTECT(8); SET_STRING_ELT(resultnames, 3, mkChar("current")); SET_STRING_ELT(resultnames, 4, mkChar("proposal")); SET_STRING_ELT(resultnames, 5, mkChar("z")); SET_STRING_ELT(resultnames, 6, mkChar("u1")); SET_STRING_ELT(resultnames, 7, mkChar("u2")); SET_STRING_ELT(resultnames, 8, mkChar("s1")); SET_STRING_ELT(resultnames, 9, mkChar("s2")); SET_STRING_ELT(resultnames, 10, mkChar("log.green")); } namesgets(result, resultnames); UNPROTECT(1); GetRNGstate(); if (current_log_dens == R_NegInf) error("log unnormalized density -Inf at initial state"); for (int ibatch = 0, k = 0; ibatch < int_nbatch; ibatch++) { for (int i = 0; i < dim_out; i++) batch_buffer[i] = 0.0; for (int jbatch = 0; jbatch < int_blen; jbatch++) { double proposal_log_dens; for (int ispac = 0; ispac < int_nspac; ispac++) { /* Note: should never happen! */ if (current_log_dens == R_NegInf) error("log density -Inf at current state"); double u1 = R_NaReal; double u2 = R_NaReal; double smax = R_NaReal; double smin = R_NaReal; double z[dim_nc]; propose(state, proposal, dbl_star_amat, dbl_star_bvec, dim_nc, ncons, z, &smax, &smin, &u1); proposal_log_dens = logh(proposal); int accept = FALSE; if (proposal_log_dens != R_NegInf) { if (proposal_log_dens >= current_log_dens) { accept = TRUE; } else { double green = exp(proposal_log_dens - current_log_dens); u2 = unif_rand(); accept = u2 < green; } } if (int_debug) { int l = ispac + int_nspac * (jbatch + int_blen * ibatch); int lbase = l * dim_nc; SEXP spath = VECTOR_ELT(result, 3); SEXP ppath = VECTOR_ELT(result, 4); SEXP zpath = VECTOR_ELT(result, 5); SEXP u1path = VECTOR_ELT(result, 6); SEXP u2path = VECTOR_ELT(result, 7); SEXP s1path = VECTOR_ELT(result, 8); SEXP s2path = VECTOR_ELT(result, 9); SEXP gpath = VECTOR_ELT(result, 10); for (int lj = 0; lj < dim_nc; lj++) { REAL(spath)[lbase + lj] = state[lj]; REAL(ppath)[lbase + lj] = proposal[lj]; REAL(zpath)[lbase + lj] = z[lj]; } REAL(u1path)[l] = u1; REAL(u2path)[l] = u2; REAL(s1path)[l] = smin; REAL(s2path)[l] = smax; REAL(gpath)[l] = proposal_log_dens - current_log_dens; } if (accept) { memcpy(state, proposal, dim_nc * sizeof(double)); current_log_dens = proposal_log_dens; } } /* end of inner loop (one iteration) */ outfun(state, out_buffer); for (int j = 0; j < dim_out; j++) batch_buffer[j] += out_buffer[j]; } /* end of middle loop (one batch) */ for (int j = 0; j < dim_out; j++, k++) REAL(path)[k] = batch_buffer[j] / int_blen; } /* end of outer loop */ PutRNGstate(); PROTECT(save_final = allocVector(REALSXP, dim_nc)); memcpy(REAL(save_final), state, dim_nc * sizeof(double)); SET_VECTOR_ELT(result, 2, save_final); UNPROTECT(5); return result; }
Eigen::VectorXd GaussianCovProposal::boundedPropose(uint id, const Eigen::VectorXd& sample, double sigma) { return bouncyBounds(propose(id, sample, sigma), bounds_.min, bounds_.max); }
ruleset_t * run_simulated_annealing(int iters, int init_size, int nsamples, int nrules, rule_t * rules, rule_t * labels, params_t *params) { ruleset_t *rs; double jump_prob; int dummy, i, j, k, iter, iters_per_step, *rs_idarray = NULL, len; double log_post_rs, max_log_posterior = -1e9, prefix_bound = 0.0; log_post_rs = 0.0; iters_per_step = 200; /* Initialize random number generator for some distrubitions. */ init_gsl_rand_gen(); /* Initialize the ruleset. */ if (create_random_ruleset(init_size, nsamples, nrules, rules, &rs) != 0) return (NULL); log_post_rs = compute_log_posterior(rs, rules, nrules, labels, params, 0, -1, &prefix_bound); if (ruleset_backup(rs, &rs_idarray) != 0) goto err; max_log_posterior = log_post_rs; len = rs->n_rules; if (debug > 10) { printf("Initial ruleset: \n"); ruleset_print(rs, rules, (debug > 100)); } /* Pre-compute the cooling schedule. */ double T[100000], tmp[50]; int ntimepoints = 0; tmp[0] = 1; for (i = 1; i < 28; i++) { tmp[i] = tmp[i - 1] + exp(0.25 * (i + 1)); for (j = (int)tmp[i - 1]; j < (int)tmp[i]; j++) T[ntimepoints++] = 1.0 / (i + 1); } if (debug > 0) printf("iters_per_step = %d, #timepoints = %d\n", iters_per_step, ntimepoints); for (k = 0; k < ntimepoints; k++) { double tk = T[k]; for (iter = 0; iter < iters_per_step; iter++) { if ((rs = propose(rs, rules, labels, nrules, &jump_prob, &log_post_rs, max_log_posterior, &dummy, &tk, params, sa_accepts)) == NULL) goto err; if (log_post_rs > max_log_posterior) { if (ruleset_backup(rs, &rs_idarray) != 0) goto err; max_log_posterior = log_post_rs; len = rs->n_rules; } } } /* Regenerate the best rule list. */ ruleset_destroy(rs); printf("\n\n/*----The best rule list is: */\n"); ruleset_init(len, nsamples, rs_idarray, rules, &rs); printf("max_log_posterior = %6f\n\n", max_log_posterior); printf("max_log_posterior = %6f\n\n", compute_log_posterior(rs, rules, nrules, labels, params, 1, -1, &prefix_bound)); free(rs_idarray); ruleset_print(rs, rules, (debug > 100)); return (rs); err: if (rs != NULL) ruleset_destroy(rs); if (rs_idarray != NULL) free(rs_idarray); return (NULL); }
ruleset_t * run_mcmc(int iters, int nsamples, int nrules, rule_t *rules, rule_t *labels, params_t *params, double v_star) { ruleset_t *rs; double jump_prob, log_post_rs; int *rs_idarray, len, nsuccessful_rej; int i, rarray[2], count; double max_log_posterior, prefix_bound; rs = NULL; rs_idarray = NULL; log_post_rs = 0.0; nsuccessful_rej = 0; prefix_bound = -1e10; // This really should be -MAX_DBL n_add = n_delete = n_swap = 0; /* initialize random number generator for some distributions. */ init_gsl_rand_gen(); /* Initialize the ruleset. */ if (debug > 10) printf("Prefix bound = %10f v_star = %f\n", prefix_bound, v_star); /* * Construct rulesets with exactly 2 rules -- one drawn from * the permutation and the default rule. */ rarray[1] = 0; count = 0; while (prefix_bound < v_star) { // TODO Gather some stats on how much we loop in here. if (rs != NULL) { ruleset_destroy(rs); count++; if (count == (nrules - 1)) return (NULL); } rarray[0] = rule_permutation[permute_ndx++].ndx; if (permute_ndx >= nrules) permute_ndx = 1; ruleset_init(2, nsamples, rarray, rules, &rs); log_post_rs = compute_log_posterior(rs, rules, nrules, labels, params, 0, 1, &prefix_bound); if (debug > 10) { printf("Initial random ruleset\n"); ruleset_print(rs, rules, 1); printf("Prefix bound = %f v_star = %f\n", prefix_bound, v_star); } } /* * The initial ruleset is our best ruleset so far, so keep a * list of the rules it contains. */ if (ruleset_backup(rs, &rs_idarray) != 0) goto err; max_log_posterior = log_post_rs; len = rs->n_rules; for (i = 0; i < iters; i++) { if ((rs = propose(rs, rules, labels, nrules, &jump_prob, &log_post_rs, max_log_posterior, &nsuccessful_rej, &jump_prob, params, mcmc_accepts)) == NULL) goto err; if (log_post_rs > max_log_posterior) { if (ruleset_backup(rs, &rs_idarray) != 0) goto err; max_log_posterior = log_post_rs; len = rs->n_rules; } } /* Regenerate the best rule list */ ruleset_destroy(rs); ruleset_init(len, nsamples, rs_idarray, rules, &rs); free(rs_idarray); if (debug) { printf("\n%s%d #add=%d #delete=%d #swap=%d):\n", "The best rule list is (#reject=", nsuccessful_rej, n_add, n_delete, n_swap); printf("max_log_posterior = %6f\n", max_log_posterior); printf("max_log_posterior = %6f\n", compute_log_posterior(rs, rules, nrules, labels, params, 1, -1, &prefix_bound)); ruleset_print(rs, rules, (debug > 100)); } return (rs); err: if (rs != NULL) ruleset_destroy(rs); if (rs_idarray != NULL) free(rs_idarray); return (NULL); }