ValueError: The value argument must be within the support

I am following Bayesian Methods for Hackers There is an example of receiving messages. 2 Poisson distributions and 1 switch.

I want to implement it in Pyro but getting an error: ValueError: The value argument must be within the support. When I try to use Normal distribution the system can not learn mean.

def daily_messages_model(data):
  total_days = len(data)
  alpha = 1
  lambda_1 = pyro.sample("lambda_1", dist.Exponential(alpha))
  lambda_2 = pyro.sample("lambda_2", dist.Exponential(alpha))
  when_to_switch = pyro.sample("when_to_switch", dist.Uniform(1, total_days))
  lambda_ = lambda_1
  for d in range(total_days-1):
    pyro.sample("x_{}".format(d), dist.Poisson(lambda_), obs=data[d])
    if d > when_to_switch:
      lambda_ = lambda_2


def daily_messages_guide(data):
    # prior
    alpha_1 = pyro.param("alpha_1", torch.tensor(1.0),
                         constraint=constraints.positive)
    pyro.sample("lambda_1", dist.Exponential(alpha_1))
    
    alpha_2 = pyro.param("alpha_2", torch.tensor(1.0),
                         constraint=constraints.positive)
    pyro.sample("lambda_2", dist.Exponential(alpha_2))

    # !!! issue: ValueError: The value argument must be within the support
    when_to_switch = pyro.param("when_to_switch_mean", torch.ones(len(data)))
    pyro.sample("when_to_switch", dist.Categorical(logits=when_to_switch))

    # !!! issue: ValueError: The value argument must be within the support
    # when_to_switch = pyro.param("when_to_switch_arg", torch.tensor(1.0),
    #                      constraint=constraints.positive)
    # pyro.sample("when_to_switch", dist.Exponential(when_to_switch))

    # !!! No learning
    # when_to_switch = pyro.param("when_to_switch_mean", torch.tensor(25.0),
    #                    constraint=constraints.positive)
    # when_to_switch_var = pyro.param("when_to_switch_var", torch.tensor(1.0),
    #                    constraint=constraints.positive)

    # pyro.sample("when_to_switch", dist.Normal(when_to_switch, when_to_switch_var))

Google colab code.

Hi, please see the Pyro version of Chapter 1 of BMH in the official GitHub repository: Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/Ch1_Introduction_Pyro.ipynb at master · CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers · GitHub

1 Like

@ eb8680_2 They use NUTS and MCMC for optimization. I want to use Guide function and SVI.

Looks to me like you are using a Uniform distribution in the model and a Categorical distribution in the guide for the when_to_switch latent. Uniform is defined over real numbers on an an interval whereas Categorical is only defined over non-negative integers on an interval, so their supports are not the same. Recommend you replace your Uniform prior with a Categorical as well.

2 Likes

Thank you, I did and it works.
when_to_switch = pyro.sample("when_to_switch", dist.Categorical(logits=torch.ones(total_days)))

1 Like

glad you got it working!