Skip to content

Keep Deterministic aliases when cloning a model - #8656

Open
anevolbap wants to merge 2 commits into
pymc-devs:mainfrom
anevolbap:fix-deterministic-aliases
Open

anevolbap wants to merge 2 commits into
pymc-devs:mainfrom
anevolbap:fix-deterministic-aliases

Conversation

@anevolbap

@anevolbap anevolbap commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

model_from_fgraph wrapped a Deterministic in a view only when its variable was a basic RV. A Deterministic that only renames another Deterministic, a data variable or a Potential became the same object as that variable, so one of the names was lost in every transform built on model_from_fgraph: clone_model, pm.do, freeze_dims_and_data. With y = pm.Deterministic("y", x + 1) and z = pm.Deterministic("z", y), clone_model(m).deterministics was ['y', 'y'], and sampling freeze_dims_and_data(m) dropped z from the posterior. A Deterministic over a pm.Data variable or a pm.Potential renamed that variable itself.

Before the dummies are replaced, model_from_fgraph now picks one owner for each underlying variable: the RV, data variable or other non-Deterministic model variable, otherwise the first Deterministic in model order. Every other Deterministic on that variable is registered as a view of it, so each keeps its name and the renamed variable stays the base of the chain. An alias of an RV is a view, as before. A model in which no two model variables share an underlying variable is unchanged.

This came up while reviewing pymc-labs/pymc-marketing#3396, where an alias of a cumulative-sum Deterministic was missing from a fitted MMM posterior; details in #8608.

Related Issue

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

model_from_fgraph wrapped a Deterministic in a view only when its variable was a basic RV. A Deterministic that only renames another Deterministic or a data variable became the same object as that variable, so clone_model, pm.do and freeze_dims_and_data lost one of the names. Pick one owner per underlying variable before replacing the dummies, the non-Deterministic model variable or else the first Deterministic in model order, and register every other Deterministic on it as a view.

Closes pymc-devs#8608
@read-the-docs-community

read-the-docs-community Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 pymc | 🛠️ Build #36050376 | 📁 Comparing 6a3eba4 against latest (648435d)

  🔍 Preview build  

2 files changed
± glossary.html
± _modules/pymc/model/fgraph.html

A Deterministic that only renames a Potential also shared its variable, so cloning renamed the Potential. The owner rule already covers it; test it, type the owners map and say in the comment that the stable sort keeps the model order.
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.24%. Comparing base (648435d) to head (6a3eba4).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #8656   +/-   ##
=======================================
  Coverage   92.24%   92.24%           
=======================================
  Files         130      130           
  Lines       21751    21754    +3     
=======================================
+ Hits        20064    20067    +3     
  Misses       1687     1687           
Files with missing lines Coverage Δ
pymc/model/fgraph.py 97.90% <100.00%> (+0.03%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@anevolbap
anevolbap marked this pull request as ready for review November 20, 2026 18:40

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Aliases of Deterministics are lost through freeze_dims_and_data

1 participant