Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Generation of random draws from Weibull returns constant values #7220

Closed
tomicapretto opened this issue Mar 26, 2024 · 2 comments · Fixed by #7288
Closed

BUG: Generation of random draws from Weibull returns constant values #7220

tomicapretto opened this issue Mar 26, 2024 · 2 comments · Fixed by #7288
Labels

Comments

@tomicapretto
Copy link
Contributor

tomicapretto commented Mar 26, 2024

Describe the issue:

The generation of random draws from a Weibull distribution is returning the same values when the input is constant along some axis. This does not happen with other distributions such as the Gamma. See the example below.

Reproduceable code example:

import numpy as np
import scipy.special as sp
import pymc as pm

rng = np.random.default_rng(123)

# Simulate mean from an only-intercept model. 2 chains, 100 draws, 5 observations.
# So 'mu' is the same for all the observations (because it's intercept-only)
mu_draws = np.abs(150 + np.dstack([rng.normal(size=(2, 100, 1))] * 5))

# Simulate some alpha values
alpha_draws = np.abs(rng.normal(size=(2, 100, 1)))

# With 'mu' and 'alpha' get 'beta', which is what pm.Weibull needs
beta_draws = mu_draws / sp.gamma(1 + 1 / alpha_draws)

# See the draws, for a given chain and draw, they look all the same!
weibull_draws = pm.draw(pm.Weibull.dist(alpha=alpha_draws, beta=beta_draws))
weibull_draws

print((weibull_draws == weibull_draws[:, :, 0][..., None]).all())
# True --> they're in fact all the same

You can see this is not the case for the gamma distribution

gamma_draws = pm.draw(pm.Gamma.dist(alpha=alpha_draws, beta=beta_draws))
(gamma_draws == gamma_draws[:, :, 0][..., None]).all() # False

Error message:

No response

PyMC version information:

5.11.0

Context for the issue:

Someone reported this in Bambi after they saw a warning with az.plot_ppc()
bambinos/bambi#788

@tomicapretto
Copy link
Contributor Author

I think this is where the problem occurs:

@classmethod
def rng_fn(cls, rng, alpha, beta, size) -> np.ndarray:
return np.asarray(beta * rng.weibull(alpha, size=size))

See below

import numpy as np

rng = np.random.default_rng(1234)

alpha = np.abs(rng.normal(size=(100, 20, 1)))
beta = np.abs(np.dstack([rng.normal(size=(100, 20, 1))] * 5))

alpha_draws = rng.weibull(alpha, size=None)
alpha_draws.shape # (100, 20, 1)

np.asarray(beta * alpha_draws).shape # (100, 20, 5)

But it's reusing the same that's sampled from rng.weibull along the last axis, and since beta is constant along that axis, the sampled values are repeated.

@ricardoV94
Copy link
Member

ricardoV94 commented Mar 26, 2024

Probably needs a

if size is None:
  size = np.broadcast_shapes(alpha.shape, beta.shape)

(Untested code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants