Error using CustomDist with observed values and multiple shapes #6916
-
Hi there and thanks for all your work, We are trying to build a poor mans version of a generalized/poisson binomial distribution using the data_dims = (10, 16, 16 )
random_image = lambda : np.random.choice([0, 1], size=data_dims)
observed_data = ((random_image() - random_image()) ** 2).sum(axis=(1, 2))
def custom_dist(
p: TensorVariable,
size: TensorVariable,
):
return pytensor.tensor.sum(pm.Bernoulli.dist(p=p), axis=[1, 2])
with pm.Model() as model:
px = pm.Beta("px", alpha=1, beta=1, shape=data_dims)
py = pm.Beta("py", alpha=1, beta=1, shape=data_dims)
p = pm.Deterministic("p", px*(1-py) + (1-px)*py)
r = pm.CustomDist("c", p, dist=custom_dist, observed=observed_data)
pm.sample() I'm sure there are better ways to model this but in any case we are trying how the basics of pymc work for now. When sampling this model we get an error:
I'm confused as to why it needs to compute the logp of an observed quantity? Or is something else going on here. We're on version 5.8.1. Any help is appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
PyMC needs to now the logp of an observed quantity as well (it's the likelihood)! In some cases |
Beta Was this translation helpful? Give feedback.
-
Hey there, We are trying to use pymc to sample (and later fit) some "complicated" distribution. In our case we are looking at the sum of independently distributed Bernouli RVs. We know (from wikipedia) that this corresponds to a "Poisson binomial distribution". We were hoping that pymc would help us sample/fit that distribution/likelihood without needing to implement the details of the PDF (this is probably were we are wrong) Here is another (more minimal) example:
Thanks for your patience and your help. |
Beta Was this translation helpful? Give feedback.
To be clear, PyMC relies on random methods (either given by the dist or random kwargs) for prior and posterior predictive sampling. For posterior sampling, which uses MCMC, PyMC needs to know the log density (or log pmf) of the variables (given by the logp kwarg, or sometimes inferred from the dist kwarg).