HMC with custom potential_fn: How to specify support?

Hi! I want to pass a custom potential_fn for HMC because I only have access to an unnormalized probability distribution. However, the distribution has limited support on intervals over the reals and even a discrete random variable (I want to use Mixed HMC). How can I specify the support of my custom potential_fn?

Hi @Jannis, you can use numpyro.factor for this. For example,

def model():
    # replace Uniform by ImproperUniform for other kinds of supports
    x = numpyro.sample("x", dist.Uniform(...))
    numpyro.factor("log_prob", your_custom_log_prob(x))
1 Like

Thank you, that looks promising! I have one question though: What effect does numpyro.factor have on sampling the model? I’m doing HMC so it’s only really relevant for initialization.

Also, how does this compare to this alternative approach I just found: Am I using HMC/NUTS/MCMC correctly? - #16 by neerajprad

All solutions just add your custom log probability to the joint density of your model. Using factor is more convenient I think.

1 Like

Somehow the value of numpyro.factor doesn’t seem to be recorded although I provided a name. At least it doesn’t get returned by mcmc.get_samples(). Is this a expected or is this a bug?

If you want to record that log probability term, you can use numpyro.deterministic primitive. By default, we only record latent variables. For example, in the above example, we only record x values, not its log probabilities.

1 Like