Skip to content
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

Creating Causal Identification module #1166

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open

Conversation

cetagostini
Copy link
Contributor

@cetagostini cetagostini commented Nov 4, 2024

Description

Short description: Integration of CausalGraphModel in BaseMMM Class

This update integrates a CausalGraphModel into the BaseMMM class, allowing for automated causal identification based on backdoor criteria, assuming a given Directed Acyclic Graph (DAG).

Summary of Changes

  1. Added Causal Graph Option:

    • The BaseMMM class now accepts an optional dag parameter, which can be provided either as a string (DOT format) or a networkx.DiGraph.
    • If dag is provided, a CausalGraphModel is instantiated to analyze causal relationships and determine necessary adjustment sets.
  2. Automatic Minimal Adjustment Set Handling:

    • The BaseMMM initialization now includes logic to calculate the minimal adjustment set required to estimate the causal effect of the treatment variables (assume to be media channels) on the outcome.
    • control_columns are automatically updated to include variables from the minimal adjustment set only.
    • If the variable yearly_seasonality is not in the minimal adjustment set, the yearly_seasonality parameter is set to None, effectively disabling it in the model.
  3. Warnings for Missing Adjustment Sets:

    • If a minimal adjustment set cannot be identified, a warning is issued, and not modifications are made during the initialization.

Code Example

Here's how to initialize BaseMMM with a DAG for causal inference:

dag_str = """
digraph {
    x1 -> y;
    x2 -> y;
    yearly_seasonality -> y;
    event_1 -> y;
    event_2 -> y;
}
"""

mmm = MMM(
    model_config=my_model_config,
    sampler_config=my_sampler_config,
    date_column="date_week",
    adstock=GeometricAdstock(l_max=8),
    saturation=LogisticSaturation(),
    channel_columns=["x1", "x2"],
    control_columns=["event_1", "event_2"],
    yearly_seasonality=2,  # Disabled if 'yearly_seasonality' is not in minimal adjustment set
    dag=dag_str,
    outcome_column="y",
)

Related Issue

  • Closes #
  • Related to #

Checklist

Modules affected

  • MMM
  • CLV

Type of change

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

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

@github-actions github-actions bot added the MMM label Nov 4, 2024
@cetagostini cetagostini requested review from wd60622 and juanitorduz and removed request for wd60622 November 4, 2024 23:35
@wd60622
Copy link
Contributor

wd60622 commented Nov 4, 2024

What is z in the 2nd body example? Would that be in the model?

@wd60622 wd60622 added causal inference enhancement New feature or request labels Nov 4, 2024
@cetagostini
Copy link
Contributor Author

What is z in the 2nd body example? Would that be in the model?

Old example, I did the correction!

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@github-actions github-actions bot added the docs Improvements or additions to documentation label Nov 13, 2024
Copy link

codecov bot commented Nov 13, 2024

Codecov Report

Attention: Patch coverage is 75.86207% with 14 lines in your changes missing coverage. Please review.

Project coverage is 95.37%. Comparing base (1464354) to head (b31a86f).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pymc_marketing/mmm/mmm.py 50.00% 8 Missing ⚠️
pymc_marketing/mmm/causal.py 84.61% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1166      +/-   ##
==========================================
- Coverage   95.64%   95.37%   -0.28%     
==========================================
  Files          39       40       +1     
  Lines        4089     4147      +58     
==========================================
+ Hits         3911     3955      +44     
- Misses        178      192      +14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cetagostini cetagostini marked this pull request as draft November 15, 2024 16:44
@github-actions github-actions bot added the tests label Nov 16, 2024
@cetagostini cetagostini marked this pull request as ready for review November 16, 2024 22:22
Copy link

review-notebook-app bot commented Nov 17, 2024

View / edit / reply to this conversation on ReviewNB

juanitorduz commented on 2024-11-17T18:02:02Z
----------------------------------------------------------------

Did anything chane in the MMM example notebook?


Copy link

review-notebook-app bot commented Nov 17, 2024

View / edit / reply to this conversation on ReviewNB

juanitorduz commented on 2024-11-17T18:20:27Z
----------------------------------------------------------------

Add subtitle like: business problem


Copy link

review-notebook-app bot commented Nov 17, 2024

View / edit / reply to this conversation on ReviewNB

juanitorduz commented on 2024-11-17T18:20:28Z
----------------------------------------------------------------

Shall we remove the first data points which are generated by the natural fact that we can not adstock much for the initial point ?


Copy link

review-notebook-app bot commented Nov 17, 2024

View / edit / reply to this conversation on ReviewNB

juanitorduz commented on 2024-11-17T18:20:28Z
----------------------------------------------------------------

Again, lets remove the first point because this initial jump is just artificial and looks odd.


Copy link

review-notebook-app bot commented Nov 17, 2024

View / edit / reply to this conversation on ReviewNB

juanitorduz commented on 2024-11-17T18:20:29Z
----------------------------------------------------------------

Any idea on the divergences?


Copy link

review-notebook-app bot commented Nov 17, 2024

View / edit / reply to this conversation on ReviewNB

juanitorduz commented on 2024-11-17T18:20:30Z
----------------------------------------------------------------

Can we use $x_1$ instead of $x1$?


Copy link

review-notebook-app bot commented Nov 17, 2024

View / edit / reply to this conversation on ReviewNB

juanitorduz commented on 2024-11-17T18:20:31Z
----------------------------------------------------------------

Maybe we should display the HDI instead of the heat plots as we need to fix that the legend here is miningless (I will fix this soon)


Copy link

review-notebook-app bot commented Nov 17, 2024

View / edit / reply to this conversation on ReviewNB

juanitorduz commented on 2024-11-17T18:20:31Z
----------------------------------------------------------------

Do we need to display these warnings?


@juanitorduz
Copy link
Collaborator

@carlosagostini I really liked the notebook 🚀 !

I think if we improve the level of abstraction (see comments above) and update the notebooks, we can merge this one soon!

@drbenvincent It would be great if you could review the notebook to provide feedback if you have time 🙏 :)

tests/mmm/test_causal.py Outdated Show resolved Hide resolved
Copy link
Contributor

@wd60622 wd60622 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments and questions

@cetagostini
Copy link
Contributor Author

Hey guys, the PR is good. But I reckon, I need a hand with the tests.

@wd60622 @juanitorduz Happy if we can have a quick meeting to address them?

@cetagostini
Copy link
Contributor Author

Ready for final review @juanitorduz

@juanitorduz
Copy link
Collaborator

@cetagostini is the change in the MMM example relevant for this PR? I can't find a difference between the original and the change :)

@cetagostini
Copy link
Contributor Author

cetagostini commented Nov 29, 2024

@cetagostini is the change in the MMM example relevant for this PR? I can't find a difference between the original and the change :)

I need it to run to get a new model.nc with the latest changes, as well, I'm use to run it to be sure everything works. But indeed nothing change! @juanitorduz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
causal inference docs Improvements or additions to documentation enhancement New feature or request MMM tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants