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))