def eager_binomial(total_count, probs, value): probs = stack((1 - probs, probs)) value = stack((total_count - value, value)) backend_dist = import_module( BACKEND_TO_DISTRIBUTIONS_BACKEND[get_backend()]) return backend_dist.Multinomial(total_count, probs, value=value) # noqa: F821
def test_beta_bernoulli_conjugate(batch_shape): batch_dims = ('i', 'j', 'k')[:len(batch_shape)] inputs = OrderedDict((k, Bint[v]) for k, v in zip(batch_dims, batch_shape)) full_shape = batch_shape prior = Variable("prior", Real) concentration1 = Tensor(ops.exp(randn(full_shape)), inputs) concentration0 = Tensor(ops.exp(randn(full_shape)), inputs) latent = dist.Beta(concentration1, concentration0, value=prior) conditional = dist.Bernoulli(probs=prior) reduced = (latent + conditional).reduce(ops.logaddexp, set(["prior"])) assert isinstance(reduced, dist.DirichletMultinomial) concentration = stack((concentration0, concentration1), dim=-1) assert_close(reduced.concentration, concentration) assert_close(reduced.total_count, Tensor(numeric_array(1.))) # we need lazy expression for Beta to draw samples from it with interpretation(funsor.terms.lazy): lazy_latent = dist.Beta(concentration1, concentration0, value=prior) obs = Tensor(rand(batch_shape).round(), inputs) _assert_conjugate_density_ok(latent, conditional, obs, lazy_latent=lazy_latent)
def test_tensor_stack(n, shape, dim): tensors = [randn(shape) for _ in range(n)] actual = stack(tuple(Tensor(t) for t in tensors), dim=dim) expected = Tensor(ops.stack(dim, *tensors)) assert_close(actual, expected)
def eager_beta(concentration1, concentration0, value): concentration = stack((concentration0, concentration1)) value = stack((1 - value, value)) backend_dist = import_module( BACKEND_TO_DISTRIBUTIONS_BACKEND[get_backend()]) return backend_dist.Dirichlet(concentration, value=value) # noqa: F821
def eager_binomial(total_count, probs, value): probs = stack((1 - probs, probs)) value = stack((total_count - value, value)) return Multinomial(total_count, probs, value=value)
def eager_beta(concentration1, concentration0, value): concentration = stack((concentration0, concentration1)) value = stack((1 - value, value)) return Dirichlet(concentration, value=value)