Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a truncated normal prior: converting pymc2 to pymc3

Tags:

mcmc

pymc3

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: ?

like image 896
Kemp Avatar asked Sep 18 '15 03:09

Kemp


2 Answers

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)
like image 153
Meysam Hashemi Avatar answered Mar 06 '23 02:03

Meysam Hashemi


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: KDE and trace of the bounded normal distribution

like image 32
Kiudee Avatar answered Mar 06 '23 01:03

Kiudee