Skip to content

Update MarginalModel for changes in SymbolicRandomVariable #323

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions pymc_experimental/model/marginal_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,15 @@ def is_elemwise_subgraph(rv_to_marginalize, other_input_rvs, output_rvs):
return True


from pytensor.graph.basic import graph_inputs


def collect_shared_vars(outputs, blockers):
return [
inp for inp in graph_inputs(outputs, blockers=blockers) if isinstance(inp, SharedVariable)
]


def replace_finite_discrete_marginal_subgraph(fgraph, rv_to_marginalize, all_rvs):
# TODO: This should eventually be integrated in a more general routine that can
# identify other types of supported marginalization, of which finite discrete
Expand Down Expand Up @@ -621,27 +630,21 @@ def replace_finite_discrete_marginal_subgraph(fgraph, rv_to_marginalize, all_rvs
rvs_to_marginalize = [rv_to_marginalize, *dependent_rvs]

outputs = rvs_to_marginalize
# Clone replace inner RV rng inputs so that we can be sure of the update order
# replace_inputs = {rng: rng.type() for rng in updates_rvs_to_marginalize.keys()}
# Clone replace outter RV inputs, so that their shared RNGs don't make it into
# the inner graph of the marginalized RVs
# FIXME: This shouldn't be needed!
replace_inputs = {}
replace_inputs.update({input_rv: input_rv.type() for input_rv in input_rvs})
cloned_outputs = clone_replace(outputs, replace=replace_inputs)
# We are strict about shared variables in SymbolicRandomVariables
inputs = dependent_rvs_input_rvs + collect_shared_vars(rvs_to_marginalize, blockers=input_rvs)

if isinstance(rv_to_marginalize.owner.op, DiscreteMarkovChain):
marginalize_constructor = DiscreteMarginalMarkovChainRV
else:
marginalize_constructor = FiniteDiscreteMarginalRV

marginalization_op = marginalize_constructor(
inputs=list(replace_inputs.values()),
outputs=cloned_outputs,
inputs=inputs,
outputs=outputs,
ndim_supp=ndim_supp,
)

marginalized_rvs = marginalization_op(*replace_inputs.keys())
marginalized_rvs = marginalization_op(*inputs)
fgraph.replace_all(tuple(zip(rvs_to_marginalize, marginalized_rvs)))
return rvs_to_marginalize, marginalized_rvs

Expand Down
16 changes: 16 additions & 0 deletions pymc_experimental/tests/model/test_marginal_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,3 +758,19 @@ def test_marginalized_hmm_multiple_emissions(batch_emission1, batch_emission2):
test_value_emission2 = np.broadcast_to(-test_value, emission2_shape)
test_point = {"emission_1": test_value_emission1, "emission_2": test_value_emission2}
np.testing.assert_allclose(logp_fn(test_point), expected_logp)


@pytest.importorskip("jax")
def test_mutable_indexing_jax_backend():
from pymc.sampling.jax import get_jaxified_logp

with MarginalModel() as model:
data = pm.MutableData(f"data", np.zeros(10))

cat_effect = pm.Normal("cat_effect", sigma=1, shape=5)
cat_effect_idx = pm.MutableData("cat_effect_idx", np.array([0, 1] * 5))

is_outlier = pm.Bernoulli("is_outlier", 0.4, shape=10)
pm.LogNormal("y", mu=cat_effect[cat_effect_idx], sigma=1 + is_outlier, observed=data)
model.marginalize(["is_outlier"])
get_jaxified_logp(model)