Specifying model by (unnormalized) joint density

Hi everyone,

is it possible in pyro to define the model not by pyro.sample statements but instead directly define it by an (unnormalized) joint density.

Thanks,
Florian

1 Like

Usually I am able to specify a joint density via pyro.sample(..., obs=...) statements, i.e. via a factor graph representation. If the standard distributions are insufficient, you can wrap an arbitrary density in a TorchDistribution object:

class MyCustomDistribution(pyro.distributions.TorchDistribution):
    def __init__(self, ...):
        super(MyCustomDistribution, self)(...)
    def log_prob(self, value):
        return my_custom_log_density(value)

then in a Pyro model you can call

def model(data):
    pyro.sample("density", MyCustomDistribution(...), obs=data)

Note that since you haven’t defined a .sample() method, this custom density can only be used in the model, not in the guide.

3 Likes

Thanks, that’s exactly what I needed. :slight_smile: