 Set constraints on probability distributions

Hi,

I was wondering whether it is possible to define constraints on probability distributions in the style of the probabilistic programming framework figaro setConstraint? This function multiplies the weight of the sample by the value of the constraint.

I’ve read about pymc3 potentials but they don’t seem to have the same effect.

I have no idea about the implementation in figaro and not sure about the intended use case, but we have pm.Bound for setting constraints on the domain of the probability distribution. And yes you can also use potential with value evaluated on a CDF function to account for constraints (also for modifying the domain of the distribution)

Thanks for the quick answer!

I’ve checked Bounded but it doesn’t seem to work as I expect. Consider the following example:

with pm.Model() as model:
x = pm.Normal('x', mu=5, sigma=1)
y = pm.Normal('y', mu=7, sigma=1)

BoundedDeterministic = pm.Bound(pm.Deterministic, lower=6.0, upper=7.0)
z = BoundedDeterministic('z', x+y)

I would like to impose a constraint on z (in this case that the values are between 6.0 and 7.0) and see how it affects the variables x and y. I expect that the sampler gives higher probability to the samples that satisfy the constraint, and disregards the samples that don’t.

Unfortunately, the snippet above doesn’t work. I get an error message saying that pm.Deterministic is not a subclass of distribution. Do you know what class I should use for this case?

Cheers.

You cannot set constrain on deterministic variables, you can only set constraints on random variables.
You can do something like this:

import pymc3 as pm
import numpy as np
import theano.tensor as tt

with pm.Model() as model:
x = pm.Normal('x', mu=5, sigma=1, testval=.5)
y = pm.Normal('y', mu=7, sigma=1, testval=6.1)

z = pm.Deterministic('z', x+y)
# constraint
pm.Potential('bound', tt.switch((z>6.)*(z<7.), 0., -np.inf))