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

total count as model parameter in multinomial distribution #1556

Closed
mahdikooshkbaghi opened this issue Mar 17, 2023 · 0 comments · Fixed by #1557
Closed

total count as model parameter in multinomial distribution #1556

mahdikooshkbaghi opened this issue Mar 17, 2023 · 0 comments · Fixed by #1557
Labels
enhancement New feature or request

Comments

@mahdikooshkbaghi
Copy link

Here is the issue corresponding to the forum question 5066.
Example:
5 Customers are going to buy apples from 4 available category of apples.

  • The total number of purchase is negative binomial (with zero purchase being the most common one).
  • The probability of choosing apples from each category is drawn from Dirichlet distribution for each customer.
    Here is the minimal code:
import jax
import jax.numpy as jnp
import matplotlib.pyplot as plt
import numpyro
import numpyro.distributions as dist
from numpyro.infer import Predictive

seed = 1234
# numpyro seed
rng_key = jax.random.PRNGKey(seed)
rng_trace, rng_prior_pred, rng_posterior_pred = jax.random.split(rng_key, 3)

def gen_data(num_apple_category: int = 4, n_customers: int = 5):
    mu = 1
    alpha = 2
    with numpyro.plate("customers", n_customers):
        # The latent probability for number of buys for each customer is Negative binomial.
        # The lower number of the purchase has more chance.
        num_purchase = numpyro.sample("num_purchase", dist.NegativeBinomial2(mu, alpha))
        desired_probs = numpyro.sample(
            "desired_probs", dist.Dirichlet(0.5 * jnp.ones(num_apple_category))
        )
        numpyro.sample(
            "apples", dist.Multinomial(total_count=num_purchase, probs=desired_probs)
        )


num_samples = 1
# Draw from priors to make the fake data
prior_predictive = Predictive(gen_data, num_samples=num_samples)
prior_predictions = prior_predictive(rng_prior_pred)

Error:

AssertionError: The total count parameter `n` should not be a jax abstract array.

Versions:

jax=='0.4.1'
jaxlib=='0.4.1'
numpyro=='0.10.1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants