Package: mcmc-samplers
MCMC SamplersA Python package for performing MCMC sampling with PyTorch.InstallationTo install this package, run the following command from the terminal:pipinstallmcmc-samplersFrameworkEach sampling algorithm is run using the__call__method inherited from theSamplerbase class. The differences in implementation between the samplers are primarily determined by how the following two attributes are defined:proposalsacceptance_kernelsCommon proposals, acceptance kernels, and pairings of these two objects are described next.ProposalsOne of the most important design choices in creating an MCMC sampler is how to propose samples. Proposals should be easy to sample from, efficient to evaluate, and resemble the target distribution as closely as possible. This package implements the following proposals:GaussianRandomWalk: A Gaussian random walk.AdaptiveCovariance: A Gaussian random walk where the covariance adapts to match the empirical covariance of the past states of the Markov chain.ScaledCovariance: A Gaussian random walk where the covariance is a scaled version of another (possibly time-varying) covariance.HamiltonianDynamics: A gradient-based proposal that uses Hamiltonian dynamics to propose samples from high-density regions.Acceptance kernelsThe acceptance kernel in an MCMC scheme is used to guarantee that the Markov chain converges to the target distribution. The most common acceptance kernel comes from the Metropolis-Hastings algorithm, and most other acceptance kernels are based on the one used in that algorithm. Indeed, all of the samplers in this package use an acceptance kernel derived from the Metropolis-Hastings one.SamplersAny MCMC sampler can be seen simply as a proposal paired with an acceptance kernel. This package implements the following common pairings:MetropolisHastings: Pairs the Metropolis-Hastings acceptance kernel with a Gaussian random walk proposal.DelayedRejectionAdaptiveMetropolis: Pairs the delayed rejection algorithm using two stages with the Gaussian random walk proposal using an adaptive covariance.HamiltonianMonteCarlo: Pairs the Metropolis-Hastings acceptance kernel with a Hamiltonian dynamics proposal.Example: Banana distributionA walkthrough of part of thebananaexample is given here to demonstrate how the package can be used.First import the necessary packages.importtorchfromtorch.distributions.multivariate_normalimportMultivariateNormalfrommcmc_samplersimport*Then define a function that evaluates the log probability of the (unnormalized) target distribution. In this example, a class is used.classBanana:def__init__(self):mean=torch.zeros(2)cov=torch.tensor([[1.,0.9],[0.9,1.]])self.mvn=MultivariateNormal(mean,cov)deflog_prob(self,x:torch.Tensor)->torch.Tensor:x=torch.atleast_2d(x)y=torch.cat((x[:,0:1],x[:,1:2]+(x[:,0:1]+1)**2),dim=1)returnself.mvn.log_prob(y)Inside the main function, instantiate theBananaclass and specify sampling parameters. This example uses the DRAM algorithm, so the initial sample and initial covariance for the adaptive Gaussian random walk must be specified.target=Banana()init_sample=torch.tensor([0.,-1.])init_cov=torch.tensor([[1.,0.9],[0.9,1.]])Lastly, create theSamplerobject and run for the desired number of iterations.dram=DelayedRejectionAdaptiveMetropolis(target=target.log_prob,x0=init_sample,cov=init_cov)num_samples=int(1e4)samples,log_probs=dram(num_samples)To visualize the results, the samples can be used to create aSamplerVisualizerobject. This object can then be called to plot the sample chains and 1D and 2D histograms.importmatplotlib.pyplotaspltlabels=['$x_1$','$x_2$']visualizer=SampleVisualizer(samples)visualizer.chains(labels=labels)visualizer.triangular_hist(bins=50,labels=labels)plt.show()Author informationAuthor: Nicholas GaliotoEmail:ngalioto@umich.eduLicense: GPL3Copyright 2024, Nicholas Galioto