-
Notifications
You must be signed in to change notification settings - Fork 325
BuildModelFromDAG class for causal components #1950
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
Conversation
Introduces the BuildModelFromDAG class to build PyMC probabilistic models directly from a causal DAG and tabular data, with validation for dims, coords, and priors. Adds comprehensive tests for parsing, validation, error handling, and model construction, ensuring robust integration and usage.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1950 +/- ##
==========================================
+ Coverage 93.40% 93.41% +0.01%
==========================================
Files 67 67
Lines 8341 8521 +180
==========================================
+ Hits 7791 7960 +169
- Misses 550 561 +11 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…com/pymc-labs/pymc-marketing into cetagostini/building_model_from_dag
with pytest.raises(ValueError, match=r"'coords' is required and cannot be None\."): | ||
builder._validate_coords_required_are_consistent() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to test pydantic behavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This checks _validate_coords_required_are_consistent
function. Not following the comment here, but codecov was asking for it.
Hey planning to dedicate to notebooks this month. @juanitorduz @williambdean any code review here? If we merge during the next few weeks, I can start to develop the notebook example! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cetagostini I left some minor comments on more specific hints that would be great before merging 🙏 .
I think we should create an issue about the parting part, ideally nx can help us with that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces the BuildModelFromDAG
class for building PyMC models directly from causal DAGs and tabular data. The implementation provides a comprehensive solution for converting DAG representations (DOT format or edge lists) into PyMC probabilistic models with proper validation and error handling.
Key changes:
- Addition of
BuildModelFromDAG
class with DAG parsing, validation, and PyMC model construction capabilities - Comprehensive validation for dimensions, coordinates, and priors consistency
- Support for both DOT format and simple edge-list DAG specifications
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
pymc_marketing/mmm/causal.py | Implements the main BuildModelFromDAG class with DAG parsing, validation logic, and PyMC model building functionality |
tests/mmm/test_causal.py | Adds comprehensive test suite covering initialization, validation, error handling, and model construction scenarios |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
pymc_marketing/mmm/causal.py
Outdated
for node in self.nodes: | ||
parents = self._parents(node) | ||
# Slopes for each parent -> node | ||
parents = self._parents(node) |
Copilot
AI
Oct 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate assignment of parents
variable. Line 450 already assigns parents = self._parents(node)
, making line 452 redundant.
parents = self._parents(node) |
Copilot uses AI. Check for mistakes.
pymc_marketing/mmm/causal.py
Outdated
coords : dict | ||
Mapping from dim names to coordinate values. All coord keys must exist as | ||
columns in ``df`` and will be used to pivot the data to match dims. | ||
model_config : dict, optional |
Copilot
AI
Oct 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing indentation for the model_config
parameter description. It should be aligned with the other parameters in the docstring.
model_config : dict, optional | |
model_config : dict, optional |
Copilot uses AI. Check for mistakes.
tests/mmm/test_causal.py
Outdated
custom_config = { | ||
prior_name: Prior("Normal", mu=0, sigma=1, dims=("country",)) | ||
for prior_name in ("intercept", "slope") | ||
} | {} |
Copilot
AI
Oct 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty dictionary merge } | {}
is unnecessary and adds no value. The dictionary comprehension alone is sufficient.
} | {} | |
} |
Copilot uses AI. Check for mistakes.
Description
Introduces the
BuildModelFromDAG
class to build PyMC models directly from a causal DAG and tabular data, with validation for dims, coords, and priors. Adds comprehensive tests for parsing, validation, error handling, and model construction, ensuring robust integration and usage.Note
In the future, I'm planning to extend the class to get a forward pass and then be able to apply saturation and lagging for certain arrows in the dag.
Example notebook here, available for PyMC People.
To-do's:
intercept=Prior(. . .. )
).Related Issue
Checklist
pre-commit.ci autofix
to auto-fix.📚 Documentation preview 📚: https://pymc-marketing--1950.org.readthedocs.build/en/1950/