Skip to content

Conversation

cetagostini
Copy link
Contributor

@cetagostini cetagostini commented Sep 20, 2025

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:

  • Allow for intercept toggle when we are doing time series (e.g: intercept=Prior(. . .. )).

Related Issue

Checklist


📚 Documentation preview 📚: https://pymc-marketing--1950.org.readthedocs.build/en/1950/

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.
Copy link

codecov bot commented Sep 20, 2025

Codecov Report

❌ Patch coverage is 90.55556% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.41%. Comparing base (b34d559) to head (17c62a9).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
pymc_marketing/mmm/causal.py 90.55% 17 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@juanitorduz juanitorduz modified the milestones: 0.16.0, 0.17.0 Sep 21, 2025
@cetagostini cetagostini marked this pull request as ready for review September 22, 2025 20:45
Comment on lines +503 to +504
with pytest.raises(ValueError, match=r"'coords' is required and cannot be None\."):
builder._validate_coords_required_are_consistent()
Copy link
Contributor

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

Copy link
Contributor Author

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.

@cetagostini
Copy link
Contributor Author

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!

Copy link
Collaborator

@juanitorduz juanitorduz left a 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

@juanitorduz juanitorduz requested a review from Copilot October 4, 2025 08:55
Copy link

@Copilot Copilot AI left a 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.

for node in self.nodes:
parents = self._parents(node)
# Slopes for each parent -> node
parents = self._parents(node)
Copy link

Copilot AI Oct 4, 2025

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.

Suggested change
parents = self._parents(node)

Copilot uses AI. Check for mistakes.

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
Copy link

Copilot AI Oct 4, 2025

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.

Suggested change
model_config : dict, optional
model_config : dict, optional

Copilot uses AI. Check for mistakes.

custom_config = {
prior_name: Prior("Normal", mu=0, sigma=1, dims=("country",))
for prior_name in ("intercept", "slope")
} | {}
Copy link

Copilot AI Oct 4, 2025

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.

Suggested change
} | {}
}

Copilot uses AI. Check for mistakes.

@cetagostini cetagostini enabled auto-merge (squash) October 4, 2025 15:30
@cetagostini cetagostini merged commit 54de250 into main Oct 4, 2025
30 of 32 checks passed
@cetagostini cetagostini deleted the cetagostini/building_model_from_dag branch October 4, 2025 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants