Conditioning on a boolean constraint

Hi, this is a newbie question but I can’t figure it out. I draw two samples from two Gaussian priors and want to find their marginal distributions conditioned on some boolean constraint. However I cannot figure out how to condition on anything that is not coming directly from pyro.sample. Below is an attempt at getting this done

import pyro
import pyro.distributions as dist
import matplotlib.pyplot as plt

def experiment():
    # these are the latent variables
    a = pyro.sample('a', dist.Normal(-1., 0.3))
    b = pyro.sample('b', dist.Normal( 1., 0.3))

    # this is the observable
    return a > b

conditioned = pyro.sample('condition', experiment, obs=True) 
posterior = pyro.infer.Importance(conditioned, num_samples=1000)
marginal = pyro.infer.EmpiricalMarginal(posterior.run(), sites=['a', 'b'])

samples = [marginal() for _ in range(1000)]

plt.hist([sample[0] for sample in samples], range=[-3, 3], bins=20)
plt.hist([sample[1] for sample in samples], range=[-3, 3], bins=20)
plt.show()

but it fails with bool is not callable. Why would the algorithm want to call the obs value? I figure I need to somehow give the constraint a name so that I can condition on it. My other attempt was to return pyro.param('condition', a > b) in experiment and then use conditioned = pyro.condition(experiment, data={'condition': True}) but this fails with RuntimeError: only Tensors of floating point dtype can require gradients. Can anyone shed some light on how to get this done?

Hi, you might find the discussion in this old GitHub issue useful.

As suggested there, you should move conditioned inside of experiment and return a > b with return sample("condition", dist.Bernoulli(0.9999), obs=torch.tensor(float(a > b))).