Defining an arbitrary (unnormalized) density function and sample from it?

Suppose I have a custom probability density function defined up to some constant. For example, it can come from a Gibbs distribution with some manually defined potential.

def unnormalized_pdf(x):
    return torch.exp(-x**2)

Can I use HMC to sample from this distribution? The whole point is not to define the distribution via sampling and conditioning, but define it analytically.

The HMC/NUTS samplers take in potential_fn arg, and you should be able to use that directly. e.g.

import torch
from pyro.infer import MCMC, NUTS

def unnormalized_pdf(x):
    return torch.exp(-x['u']**2)

nuts = NUTS(potential_fn=lambda x: -torch.log(unnormalized_pdf(x)))
mcmc = MCMC(nuts, num_samples=100, initial_params={'u': torch.tensor(0.)})
1 Like

Oh that’s neat. Thanks very much!