Closed
Description
Describe the issue:
The function get_tau_sigma
in distributions/continuous.py
does not properly handle lists of Variable
s. When a list is passed, the function attempts to convert it to a numpy
array before checking for positivity (N.B. the positivity check does not occur if a Variable
is passed).
Reproduceable code example:
import pymc as pm
print(pm.__version__)
import pytensor.tensor as pt
import numpy as np
data = np.random.normal(size=100)
with pm.Model() as model:
mu_1 = pm.Normal('mu_1', mu=0.0, sigma=1.0)
mu_2 = pm.Normal('mu_2', mu=0.0, sigma=1.0)
mu = [mu_1, mu_2]
sigma_1 = pm.HalfNormal('sigma_1', sigma=1.0)
sigma_2 = pm.HalfNormal('sigma_2', sigma=1.0)
sigma = [sigma_1, sigma_2]
# the following works:
# sigma = pt.as_tensor_variable([sigma_1, sigma_2])
w = pm.Dirichlet('w', a=np.ones(2))
_ = pm.NormalMixture('data', w=w, mu=mu, sigma=sigma, observed=data)
Error message:
5.9.1+3.gec4407d3
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[6], line 18
15 sigma = [sigma_1, sigma_2]
17 w = pm.Dirichlet('w', a=np.ones(2))
---> 18 _ = pm.NormalMixture('data', w=w, mu=mu, sigma=sigma, observed=data)
File ~/science/python/pymc/pymc/distributions/mixture.py:552, in NormalMixture.__new__(cls, name, w, mu, sigma, tau, comp_shape, **kwargs)
551 def __new__(cls, name, w, mu, sigma=None, tau=None, comp_shape=(), **kwargs):
--> 552 _, sigma = get_tau_sigma(tau=tau, sigma=sigma)
554 return Mixture(name, w, Normal.dist(mu, sigma=sigma, size=comp_shape), **kwargs)
File ~/science/python/pymc/pymc/distributions/continuous.py:242, in get_tau_sigma(tau, sigma)
240 else:
241 sigma_ = np.asarray(sigma)
--> 242 if np.any(sigma_ <= 0):
243 raise ValueError("sigma must be positive")
244 tau = sigma_**-2.0
File ~/miniconda3/envs/pymc/lib/python3.11/site-packages/pytensor/tensor/variable.py:72, in _tensor_py_operators.__bool__(self)
70 return True
71 else:
---> 72 raise TypeError("Variables do not support boolean operations.")
TypeError: Variables do not support boolean operations.
PyMC version information:
5.9.1+3.gec4407d3
Context for the issue:
Since the distributions that use get_tau_sigma
(e.g., NormalMixture
) state that sigma
and tau
should be tensor_like
, and since tensor_like
includes lists and other iterables, this is a bug.