Skip to content
Open
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
16 changes: 14 additions & 2 deletions pymc/model/fgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,18 @@ def first_non_model_var(var):
dummy_var: first_non_model_var(dummy_var.owner.inputs[0])
for dummy_var in model_dummy_vars
}
# A Deterministic that only renames another model variable (an RV, a Potential,
# a data variable or another Deterministic) shares that variable once the dummies
# are replaced. Give each shared variable one owner: the model variable that is
# not a Deterministic, otherwise the first Deterministic in model order (the sort
# is stable, so the outputs keep the model's order). Every other Deterministic on
# it is registered as a view, so each keeps its own name.
owners: dict[Variable, Variable] = {}
for dummy_var in sorted(
(out for out in fgraph.outputs if out in model_dummy_vars_to_vars),
key=lambda out: isinstance(out.owner.op, ModelDeterministic),
):
owners.setdefault(model_dummy_vars_to_vars[dummy_var], dummy_var)
toposort_replace(fgraph, tuple(model_dummy_vars_to_vars.items()))

# Populate new PyMC model mappings
Expand All @@ -367,8 +379,8 @@ def first_non_model_var(var):
model.potentials.append(var)
elif isinstance(op, ModelDeterministic):
[var] = model_var.owner.inputs
# If a Deterministic is a direct view on an RV, view it
if var in model.basic_RVs:
# If a Deterministic only renames another model variable, view it
if owners.get(var, model_var) is not model_var:
var = view_op(var)
model.deterministics.append(var)
elif isinstance(op, ModelNamed):
Expand Down
35 changes: 35 additions & 0 deletions tests/model/test_fgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,41 @@ def test_deterministics(inline_views):
assert m["y__"].owner.inputs[0] is m["y"]


@pytest.mark.parametrize("inline_views", (False, True))
def test_deterministic_aliases(inline_views):
"""A Deterministic that only renames another Deterministic, a data variable or a Potential keeps its name.

Without a view, the alias and the variable it renames would be one object, and one of the
names would be lost. The renamed variable stays the base, and every alias is a view of it.
"""
with pm.Model() as m:
x = pm.Normal("x")
y = pm.Deterministic("y", x + 1)
z = pm.Deterministic("z", y)
pm.Deterministic("w", z)
data = pm.Data("data", np.ones(3))
pm.Deterministic("data_alias", data)
pot = pm.Potential("pot", -(x**2))
pm.Deterministic("pot_alias", pot)

fg, _ = fgraph_from_model(m, inlined_views=inline_views)
m = model_from_fgraph(fg)

assert sorted(var.name for var in m.deterministics) == [
"data_alias",
"pot_alias",
"w",
"y",
"z",
]
assert [var.name for var in m.data_vars] == ["data"]
assert [var.name for var in m.potentials] == ["pot"]
assert m["z"].owner.inputs[0] is m["y"]
assert m["w"].owner.inputs[0] is m["y"]
assert m["data_alias"].owner.inputs[0] is m["data"]
assert m["pot_alias"].owner.inputs[0] is m["pot"]


def test_context_error():
"""Test that model_from_fgraph fails when called inside a Model context.

Expand Down
Loading