Package: dime-sampler
Differential-Independence Mixture Ensemble (“DIME”) MCMC sampling for PythonThis is a standalone Python implementation of the DIME sampler proposed inEnsemble MCMC Sampling for Robust Bayesian Inference(Gregor Boehl, 2022, SSRN No. 4250395).The sampler has a series of advantages over conventional samplers:DIME MCMC is a (very fast) gradient-freeglobal multi-start optimizerand, at the same time, aMCMC samplerthat converges to the posterior distribution. This makes any posterior mode density maximization prior to MCMC sampling superfluous.The DIME sampler is pretty robust for odd shaped,multimodal distributions.DIME MCMC isparallelizable: many chains can run in parallel, and the necessary number of draws decreases almost one-to-one with the number of chains.DIME proposals are generated from anendogenous and adaptive proposal distribution, thereby providing close-to-optimal proposal distributions for black box target distributions without the need for manual fine-tuning.Figure: A trimodal example distribution in 35 dimensionsThere exist complementary stand-alone implementations inJulia languageandin matlab.InstallationInstalling therepository versionfrom PyPi is as simple as typingpipinstalldime_samplerin your terminal or Anaconda Prompt.UsageThe package provides a direct drop-in replacement foremcee:importemceefromdime_samplerimportDIMEMovemove=DIMEMove()...deflog_prob(x):...# define your density function, the number of chains `nchain` etc......sampler=emcee.EnsembleSampler(nchain,ndim,log_prob,moves=move)...# off you go samplingThe rest of the usage is analog to emcee, seethis tutorial. The parameters specific to theDIMEMoveare documentedhere.TutorialLets look at an example. Let’s define a nice and challenging distribution (it’s the distribution from the figure above):# some importimportemceeimportnumpyasnpimportscipy.statsasssfromdime_samplerimportDIMEMovefromdime_sampler.test_allimport_create_test_func,_marginal_pdf_test_func# make it reproduciblenp.random.seed(0)# define distributionm=2cov_scale=0.05weight=(0.33,.1)ndim=35initvar=np.sqrt(2)log_prob=_create_test_func(ndim,weight,m,cov_scale)log_probwill now return the log-PDF of a 35-dimensional Gaussian mixture withthree separate modes.Next, define the initial ensemble. In a Bayesian setup, a good initial ensemble would be a sample from the prior distribution. Here, we will go for a sample from a rather flat Gaussian distribution.# number of chains and number of iterationsnchain=ndim*5niter=5000# initial ensembleinitmean=np.zeros(ndim)initcov=np.eye(ndim)*np.sqrt(2)initchain=ss.multivariate_normal(mean=initmean,cov=initcov).rvs(nchain)Setting the number of parallel chains to5*ndimis a sane default. For highly irregular distributions with several modes you should use more chains. Very simple distributions can go with less.Now let the sampler run for 5000 iterations.move=DIMEMove(aimh_prob=0.1,df_proposal_dist=10)sampler=emcee.EnsembleSampler(nchain,ndim,log_prob,moves=move)sampler.run_mcmc(initchain,int(niter),progress=True)The setting ofaimh_probis the actual default value. For less complex distributions (e.g. distributions closer to Gaussian) a higher value can be chosen, which accelerates burn-in. The valuedf_proposal_distsets the degrees of freedom for the proposal distribution of the independence move.10is a sane default and it is rather unlikely that this value must be changed.The following code creates the figure above, which is a plot of the marginal distribution along the first dimension (remember that this actually is a 35-dimensional distribution).# import matplotlibimportmatplotlib.pyplotasplt# get elementschain=sampler.get_chain()lprob=sampler.get_log_prob()# plottingfig,ax=plt.subplots(figsize=(9,6))ax.hist(chain[-niter//2:,:,0].flatten(),bins=50,density=True,alpha=0.2,label="Sample")xlim=ax.get_xlim()x=np.linspace(xlim[0],xlim[1],100)ax.plot(x,ss.norm(scale=np.sqrt(initvar)).pdf(x),"--",label="Initialization")ax.plot(x,ss.t(df=10,loc=moves.prop_mean[0],scale=moves.prop_cov[0,0]**0.5).pdf(x),":",label="Final proposals")ax.plot(x,_marginal_pdf_test_func(x,cov_scale,m,weight),label="Target")ax.legend()To ensure proper mixing, let us also have a look at the MCMC traces, again focussing on the first dimension.fig,ax=plt.subplots(figsize=(9,6))ax.plot(chain[:,:,0],alpha=0.05,c="C0")Note how chains are also switching between the three modes because of the global proposal kernel.While DIME is an MCMC sampler, it can straightforwardly be used as a global optimization routine. To this end, specify some broad starting region (in a non-Bayesian setup there is no prior) and let the sampler run for an extended number of iterations. Finally, assess whether the maximum value per ensemble did not change much in the last few hundred iterations. In a normal Bayesian setup, plotting the associated log-likelihood over time also helps to assess convergence to the posterior distribution.fig,ax=plt.subplots(figsize=(9,6))ax.plot(lprob,alpha=0.05,c="C0")ax.plot(np.arange(niter),np.max(lprob)*np.ones(niter),"--",c="C1")ReferencesIf you are using this software in your research, please cite@techreport{boehl2022mcmc,author={Gregor Boehl},title={Ensemble MCMC Sampling for Robust Bayesian Inference},journal={Available at SSRN 4250395},year={2022}}