In pymc3 how does one configure a truncated normal prior? In pymc2 it's pretty straightforward (below), but in pymc3 it seems there is no longer a truncated normal distribution available.
Pymc2:
TruncatedNormal('gamma_own_%i_' % i, mu=go, tau=v_gamma_inv, value=0, a=-np.inf, b=0)
Pymc3: ?
The following code works in Pymc3 version 3.0
a, b=np.float32(0.0), np.float32(10.0)
K_lo, K_hi = 0.0, 1.0
BoundedNormal = pm.Bound(pm.Normal, lower=K_lo, upper=K_hi)
with pm.Model() as model:
n = BoundedNormal('n', mu=a, tau=b)
tr = pm.sample(2000, pm.NUTS())
traceplot(tr)
In PyMC3 you are able to truncate any distribution using Bound
.
First you have to construct the bounded distribution (here called BoundedNormal
), then create a variable where you input the usual parameters of the underlying distribution:
with pm.Model() as model:
BoundedNormal = pm.Bound(pm.Normal, lower=0, upper=1)
n = BoundedNormal('n', mu=0, tau=10)
tr = pm.sample(2000, pm.NUTS())
The resulting distribution looks like this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With