Skip to content

Generic Metropolis-Hastings and parallel tempering versions

Vicentini Filippo requested to merge github/fork/gcarleo/mcmc into v2.1

Created by: gcarleo

This addresses #268 (closed) , introducing a generic MetropolisHastings sampler working with batches. All existing samplers have been rewritten in terms of this one, simplifying the codebase and removing duplicates.

In addition to custom C++ TransitionKernel, it is also possible to write transition kernels in pure python if needed.

For example


# Defining a custom kernel for MetropolisHastings
# Notice that this sampler exchanges two random sites
# thus preserving the total magnetization
# Also notice that it is not recommended to define custom kernels in python
# For speed reasons it is better to define exchange kernels using CustomSampler
def exchange_kernel(v, vnew, logprobcorr):

    vnew[:, :] = v[:, :]
    logprobcorr[:] = 0.0

    rands = np.random.randint(v.shape[1], size=(v.shape[0], 2))

    for i in range(v.shape[0]):
        iss = rands[i, 0]
        jss = rands[i, 1]

        vnew[i, iss], vnew[i, jss] = vnew[i, jss], vnew[i, iss]

sa = nk.sampler.MetropolisHastings(ma, exchange_kernel, batch_size=16, sweep_size=20)

Merge request reports