Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Supplying test values in pymc 3

I am exploring the use of bounded distributions in pymc. I am trying to bound a Gamma prior distribution between two values. The model specification seems to fail due to the absence of test values. How may I pass a testval argument such that I am able to specify these sorts of models?

For completeness I have included the error, as well as a minimal example below. Thank you!

AttributeError: <pymc.quickclass.Gamma object at 0x110a62890> has no default value to use, checked for: ['median', 'mean', 'mode'] pass testval argument or provide one of these.

import pymc as pm
import numpy as np

ndims = 2
nobs = 20

zdata = np.random.normal(loc=0, scale=0.75, size=(ndims, nobs))

BoundedGamma = pm.Bound(pm.Gamma, 0.5, 2)

with pm.Model() as model:
    xbound = BoundedGamma('xbound', alpha=1, beta=2)
    z = pm.Normal('z', mu=0, tau=xbound, shape=(ndims, 1), observed=zdata)

edit: for reference purposes, here is a simple working model utilizing a bounded gamma prior distribution:

import pymc as pm
import numpy as np

ndims = 2
nobs = 20

zdata = np.random.normal(loc=0, scale=0.75, size=(ndims, nobs))

BoundedGamma = pm.Bound(pm.Gamma, 0.5, 2)

with pm.Model() as model:
    xbound = BoundedGamma('xbound', alpha=1, beta=2, testval=2)
    z = pm.Normal('z', mu=0, tau=xbound, shape=(ndims, 1), observed=zdata)

with model:
    start = pm.find_MAP()

with model:
    step = pm.NUTS()

with model: 
    trace = pm.sample(3000, step, start)

pm.traceplot(trace);
like image 876
jangevaa Avatar asked Mar 22 '26 01:03

jangevaa


1 Answers

Use that line:

xbound = BoundedGamma('xbound', alpha=1, beta=2, testval=1)
like image 103
nouiz Avatar answered Mar 23 '26 16:03

nouiz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!