Skip to content

Difference in beta support between pymc3 and scipy results in error in sample_prior_predictive() #3898

Closed
@zack-kimble

Description

@zack-kimble

Description of your problem

Please provide a minimal, self-contained, and reproducible example.

import numpy as np
import pymc3 as pm

idx = np.random.randint(0,1000,10000)

with pm.Model() as model:
    mu = pm.Beta('mu', alpha=.01, beta=.01, shape=1000)
    nu = pm.Gamma('nu', mu=30, sigma=5, shape=1000)
    theta = pm.Beta('theta', alpha=mu[idx] * nu[idx],  beta=(1-mu[idx])*nu[idx], shape=10000)
    
    
with model:
    prior_sample = pm.sample_prior_predictive()

Please provide the full traceback.

Traceback (most recent call last):

  File "<ipython-input-39-03487933ffab>", line 14, in <module>
    prior_sample = pm.sample_prior_predictive()

  File "/home/zack/miniconda3/envs/_/lib/python3.8/site-packages/pymc3/sampling.py", line 1495, in sample_prior_predictive
    values = draw_values([model[name] for name in names], size=samples)

  File "/home/zack/miniconda3/envs/_/lib/python3.8/site-packages/pymc3/distributions/distribution.py", line 617, in draw_values
    value = _draw_value(next_,

  File "/home/zack/miniconda3/envs/_/lib/python3.8/site-packages/pymc3/distributions/distribution.py", line 787, in _draw_value
    return param.random(point=point, size=size)

  File "/home/zack/miniconda3/envs/_/lib/python3.8/site-packages/pymc3/model.py", line 56, in __call__
    return getattr(self.obj, self.method_name)(*args, **kwargs)

  File "/home/zack/miniconda3/envs/_/lib/python3.8/site-packages/pymc3/distributions/continuous.py", line 1264, in random
    return generate_samples(stats.beta.rvs, alpha, beta,

  File "/home/zack/miniconda3/envs/_/lib/python3.8/site-packages/pymc3/distributions/distribution.py", line 960, in generate_samples
    samples = generator(size=dist_bcast_shape, *args, **kwargs)

  File "/home/zack/miniconda3/envs/_/lib/python3.8/site-packages/scipy/stats/_distn_infrastructure.py", line 966, in rvs
    raise ValueError("Domain error in arguments.")

ValueError: Domain error in arguments.

Please provide any additional information below.

I am using beta as a hyperprior for other beta distributions and parameterizing based on mean and sample size. As a result, if the random variable from the hyper prior is 0 or 1, alpha or beta will end up 0, which is outside their domain. pymc3.Beta has support on (0,1), so I haven't seen an issue with sampling posteriors. When running sample_prior_predictive(), I get a domain error, which I believe is because scipy.stats.beta().rvs() will happily return 0 or 1.

Maybe this is just because of truncation in float64 (1 is far more common than 0), but I noticed the scipy.stats.beta documentation does state support on [0,1]. Not sure whether or not that makes it a scipy issue instead, but thought it was worth mentioning here.

Versions and main components

  • PyMC3 Version: 3.8
  • Theano Version: 1.0.4
  • Python Version: 3.8.1
  • Operating system: Ubuntu 16.04
  • How did you install PyMC3: (conda/pip) conda-forge

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions