def step(self, state, t, i): obs = lambda value: full((state._num_particles, ), value) n = state[f"s{t-1}"] + state[f"i{t-1}"] + state[f"r{t-1}"] τ = sample( f"τ{t}", Binomial(state[f"s{t-1}"], 1.0 - exp(-state["λ"] * state[f"i{t-1}"] / n))) Δi = sample(f"Δi{t}", Binomial(τ, state["δ"])) Δr = sample(f"Δr{t}", Binomial(state[f"i{t-1}"], state["γ"])) state[f"s{t}"] = sample(f"s{t}", Delta(state[f"s{t-1}"] - Δi)) state[f"i{t}"] = sample(f"i{t}", Delta(state[f"i{t-1}"] + Δi - Δr), obs=obs(i)) state[f"r{t}"] = sample(f"r{t}", Delta(state[f"r{t-1}"] + Δr))
def log_reg(x_data=None, y_data=None): w = pyro.sample("w", Normal(torch.zeros(d), torch.ones(d))) w0 = pyro.sample("w0", Normal(0., 1.)) with pyro.plate("map", N): x = pyro.sample("x", Normal(torch.zeros(d), 2).to_event(1), obs=x_data) logits = (w0 + x @ torch.FloatTensor(w)).squeeze(-1) y = pyro.sample("pred", Binomial(logits=logits), obs=y_data) return x, y
def fully_pooled(at_bats): """ Number of hits in $K$ at bats for each player has a Binomial distribution with a common probability of success, $\phi$. :param (torch.Tensor) at_bats: Number of at bats for each player. :return: Number of hits predicted by the model. """ phi_prior = Uniform(at_bats.new_tensor(0), at_bats.new_tensor(1)) phi = pyro.sample("phi", phi_prior) return pyro.sample("obs", Binomial(at_bats, phi))
def not_pooled(at_bats): """ Number of hits in $K$ at bats for each player has a Binomial distribution with independent probability of success, $\phi_i$. :param (torch.Tensor) at_bats: Number of at bats for each player. :return: Number of hits predicted by the model. """ num_players = at_bats.shape[0] phi_prior = Uniform(at_bats.new_tensor(0), at_bats.new_tensor(1)).expand_by([num_players]).independent(1) phi = pyro.sample("phi", phi_prior) return pyro.sample("obs", Binomial(at_bats, phi))
def not_pooled(at_bats, hits): r""" Number of hits in $K$ at bats for each player has a Binomial distribution with independent probability of success, $\phi_i$. :param (torch.Tensor) at_bats: Number of at bats for each player. :param (torch.Tensor) hits: Number of hits for the given at bats. :return: Number of hits predicted by the model. """ num_players = at_bats.shape[0] with pyro.plate("num_players", num_players): phi_prior = Uniform(scalar_like(at_bats, 0), scalar_like(at_bats, 1)) phi = pyro.sample("phi", phi_prior) return pyro.sample("obs", Binomial(at_bats, phi), obs=hits)
def partially_pooled(at_bats): """ Number of hits has a Binomial distribution with a logit link function. The logits $\alpha$ for each player is normally distributed with the mean and scale parameters sharing a common prior. :param (torch.Tensor) at_bats: Number of at bats for each player. :return: Number of hits predicted by the model. """ num_players = at_bats.shape[0] loc = pyro.sample("loc", Normal(at_bats.new_tensor(-1), at_bats.new_tensor(1))) scale = pyro.sample("scale", HalfCauchy(at_bats.new_tensor(0), at_bats.new_tensor(1))) alpha = pyro.sample("alpha", Normal(loc, scale).expand_by([num_players]).independent(1)) return pyro.sample("obs", Binomial(at_bats, logits=alpha))
def model(data): # whether new topic or not; prior=0.5; random choice whether old/new with pyro.plate("new_topic_plate", T): new_topic = pyro.sample("new_topic", Binomial(probs=0.5)) # if new topic, if linked to old topic, prior=0.5 with pyro.plate("linked_plate", T): linked = pyro.sample("linked", Binomial(probs=0.5)) # if old topic, which old topic with pyro.plate("old_topic_plate", T): which_old_topic = pyro.sample("which_old_topic", Multinomial(probs=prev_topic_freq)) # beta sampling for topic weights with pyro.plate("beta_plate", T - 1): beta = pyro.sample("beta", Beta(1, alpha)) with pyro.plate("theta_plate", T): # shape [T,C] # Dirichlet distribution (conjugate prior of Mult); symmetric prior theta = pyro.sample("theta", Dirichlet(torch.ones(C) / C)) with pyro.plate("gamma_plate", T_prev): gamma = pyro.sample("gamma", Dirichlet(prev_taus)) with pyro.plate("data", N): z = pyro.sample("z", Categorical(probs=mix_weights(beta))) old = get_old_topics(which_old_topic) a = (new_topic) * (linked) b = (1 - new_topic) c = (new_topic) * (1 - linked) a = a[z].reshape(N, 1) b = b[z].reshape(N, 1) c = c[z].reshape(N, 1) mult_probs = a * gamma[old[z]] + b * prev_theta[old[z]] + c * theta[z] pyro.sample("obs", Multinomial(probs=mult_probs), obs=data)
def model(n_ice, n_obs, floe_size, cover_subp): a_floe = pyro.sample("a_floe", dist.Normal(1., 1.)) b_floe = pyro.sample("b_floe", dist.Normal(0., 1.)) a_cover = pyro.sample("a_cover", dist.Normal(1., 1.)) b_cover = pyro.sample("b_cover", dist.Normal(0., 1.)) a_cover_b = pyro.sample("a_cover_b", dist.Normal(1., 1.)) b_cover_b = pyro.sample("b_cover_b", dist.Normal(0., 1.)) lambda_ice = sigmoid((a_floe * floe_size + b_floe)) alpha_det = sigmoid((a_cover * cover_subp + b_cover)) beta_det = sigmoid((a_cover_b * cover_subp + b_cover_b)) with pyro.plate('subp', size=len(floe_size)): N_ice = pyro.sample('N_ice', Poisson(lambda_ice), obs=n_ice) phi_det = pyro.sample('phi_det', Beta(alpha_det, beta_det)) * (N_ice > 0).float() N_obs = pyro.sample('N_obs', Binomial(N_ice, phi_det), obs=n_obs)
def partially_pooled_with_logit(at_bats, hits): r""" Number of hits has a Binomial distribution with a logit link function. The logits $\alpha$ for each player is normally distributed with the mean and scale parameters sharing a common prior. :param (torch.Tensor) at_bats: Number of at bats for each player. :param (torch.Tensor) hits: Number of hits for the given at bats. :return: Number of hits predicted by the model. """ num_players = at_bats.shape[0] loc = pyro.sample("loc", Normal(scalar_like(at_bats, -1), scalar_like(at_bats, 1))) scale = pyro.sample("scale", HalfCauchy(scale=scalar_like(at_bats, 1))) with pyro.plate("num_players", num_players): alpha = pyro.sample("alpha", Normal(loc, scale)) return pyro.sample("obs", Binomial(at_bats, logits=alpha), obs=hits)
def partially_pooled(at_bats, hits): r""" Number of hits has a Binomial distribution with independent probability of success, $\phi_i$. Each $\phi_i$ follows a Beta distribution with concentration parameters $c_1$ and $c_2$, where $c_1 = m * kappa$, $c_2 = (1 - m) * kappa$, $m ~ Uniform(0, 1)$, and $kappa ~ Pareto(1, 1.5)$. :param (torch.Tensor) at_bats: Number of at bats for each player. :param (torch.Tensor) hits: Number of hits for the given at bats. :return: Number of hits predicted by the model. """ num_players = at_bats.shape[0] m = pyro.sample("m", Uniform(scalar_like(at_bats, 0), scalar_like(at_bats, 1))) kappa = pyro.sample("kappa", Pareto(scalar_like(at_bats, 1), scalar_like(at_bats, 1.5))) with pyro.plate("num_players", num_players): phi_prior = Beta(m * kappa, (1 - m) * kappa) phi = pyro.sample("phi", phi_prior) return pyro.sample("obs", Binomial(at_bats, phi), obs=hits)
def guide(data): # pyro params new_topic_prob = pyro.param("new_topic_prob", lambda: Uniform(0, 1).sample([T]), constraint=constraints.unit_interval) linked_prob = pyro.param("linked_prob", lambda: Uniform(0, 1).sample([T]), constraint=constraints.unit_interval) which_topic_probs = pyro.param("which_topic_probs", lambda: Uniform(0, 1).sample([T_prev]), constraint=constraints.simplex) kappa = pyro.param('kappa', lambda: Uniform(0, 2).sample([T - 1]), constraint=constraints.positive) tau = pyro.param('tau', lambda: MultivariateNormal(0.5 * torch.ones(C), 0.25 * torch.eye(C)).sample([T]), constraint=constraints.unit_interval) # N params for categorical dist; topic weights; symmetric prior phi = pyro.param('phi', lambda: Dirichlet(1 / T * torch.ones(T)).sample([N]), constraint=constraints.simplex) # model params with pyro.plate("new_topic_plate", T): # print(new_topic_prob) new_topic = pyro.sample("new_topic", Binomial(probs=new_topic_prob)) # if new topic, if linked to old topic, prior=0.5 with pyro.plate("linked_plate", T): linked = pyro.sample("linked", Binomial(probs=linked_prob)) # if old topic, which old topic with pyro.plate("old_topic_plate", T): which_old_topic = pyro.sample("which_old_topic", Multinomial(probs=which_topic_probs)) with pyro.plate("beta_plate", T - 1): q_beta = 0 q_beta += pyro.sample("beta", Beta(torch.ones(T - 1), kappa)) # new topic with symmetric prior with pyro.plate("theta_plate", T): theta = pyro.sample("theta", Dirichlet(tau)) # new topic linked to old topic with pyro.plate("gamma_plate", T_prev): gamma = pyro.sample("gamma", Dirichlet(prev_taus)) with pyro.plate("data", N): z = pyro.sample("z", Categorical(phi)) old = get_old_topics(which_old_topic) a = ((new_topic) * (linked)) b = (1 - new_topic) c = ((new_topic) * (1 - linked)) a = a[z].reshape(N, 1) b = b[z].reshape(N, 1) c = c[z].reshape(N, 1) mult_probs = 0 mult_probs += a * gamma[old[z]] + b * prev_theta[old[z]] + c * theta[z]
def partial_pooled(Y): with pyro.plate(Y.shape[0]): θ = Beta(1, 1) return pyro.sample("y_obs", Binomial(total_count=1, probs=θ))