Jed Rembold
April 08, 2026
emcee package, which you will probably need
to install through pip
Emcee operates as an abstract data type,
wherein you create a sampling object and then can interact with it and
run samples using defined methodsmcmcensemble library, which seems to be the
closest analog to Python’s emceesampler = emcee.EnsembleSampler(
num_walkers, num_dims,
log_function, args=[extra arguments]
)Generating starting points usually done with some variation of a random gaussian near a starting point:
starts = np.random.multivariate_normal(
mean = [0,1,10],
cov = [[1,0,0],[0,0.5,0], [0,0,5]],
size = num_walkers
)You can then start a sampling run by telling the sampler where all the walkers should begin and how many steps they should take
sampler.run_mcmc(starts, num_iterations)You can get the iteration chains back from the sampler after a
run using .get_chain()
This will usually return a 3D array, indexing over the parameter, walker, and iteration
Can visualize a particular parameter over all walkers using
plt.plot(sampler.get_chain()[:,:,0], 'k', alpha=0.3)After examining, will commonly want to discard the burn in and flatten all the individual walkers:
flat_samples = sampler.get_chain(discard=num_dis,
flat=True)corner package
corner will automatically generate both
individual parameter distributions and all pair-wise 2D histogramsNeed to install and load the
mcmcensemble package and the
bayesplot package for nicer
plotting
Can specify multiple starting points for walkers in a similar way to in Python:
p_means <- c(0, 1, 10)
p_cov <- matrix(c(1, 0, 0, 0, 0.5, 0, 0, 0, 5), 3)
starts <- MASS::mvrnorm(
n=n_walkers, mu=p_means, Sigma=p_cov
)To actually generate the samples, you do:
sampler <- MCMCEnsemble(
f = ln_pdf, # The function you are walking
inits=walker_starts, # Where each walker starts
max.iter = 1000*30, # The total number of steps
n.walkers=n_walkers, # The number of walkers
coda=TRUE # Format output nicely
)You can trim to remove burn-in with:
burned <- window(sampler$samples, start=500)bayesplot library gives you lots of
easy ways to visualize CODA formatted walker chainsmcmc_trace(sampler$samples)mcmc_hist(sampler$samples)mcmc_dens(sampler$samples)summary(sampler$samples)One more library that you may find useful is
ggmcmc
Allows you to convert your
sampler$samples into a nice dataframe
easily:
samples <- ggs(sampler$samples)Then lots of visualizing functions that dataframe can be passed into:
ggs_traceplot(samples)ggs_pairs(samples)ggs_histogram(samples)

