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

Merged
merged 53 commits into from
Jan 21, 2025
Merged

Creating Causal Identification module #1166

merged 53 commits into from
Jan 21, 2025

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 89.28571% with 6 lines in your changes missing coverage. Please review.

Project coverage is 94.52%. Comparing base (485a261) to head (db1d666).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pymc_marketing/mmm/causal.py 85.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1166      +/-   ##
==========================================
- Coverage   94.57%   94.52%   -0.06%     
==========================================
  Files          49       50       +1     
  Lines        5330     5386      +56     
==========================================
+ Hits         5041     5091      +50     
- Misses        289      295       +6     

☔ 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 requested a review from wd60622 November 16, 2024 22:22
@cetagostini cetagostini marked this pull request as ready for review November 16, 2024 22:22
@juanitorduz
Copy link
Collaborator

I think the previous comments on the notebooks have not been addressed yet ;)

Copy link
Contributor Author

Like, good catch!


View entire conversation on ReviewNB

Copy link
Contributor Author

Removed by mean plus noise instead of drop.


View entire conversation on ReviewNB

Copy link
Contributor Author

This will hold a bit by the adstock effects, always with the first points.


View entire conversation on ReviewNB

Copy link
Contributor Author

Yes, I explain about them later on in the text.


View entire conversation on ReviewNB

Copy link
Contributor Author

Got some latex issue when I tried, can you give me a hand? Maybe I did something wrong.


View entire conversation on ReviewNB

Copy link
Contributor Author

Done!


View entire conversation on ReviewNB

Copy link
Contributor Author

Replaced.


View entire conversation on ReviewNB

Copy link
Contributor Author

Done!


View entire conversation on ReviewNB

Copy link
Contributor Author

Must be x1t but maybe I'm running something wrong, can you give me the markdown to replace? Just the style, I can propagate.


View entire conversation on ReviewNB

@juanitorduz
Copy link
Collaborator

@wd60622 it would be great to have another pair of eyes to review this cool PR ;) (if possible)

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.

Some additional code changes :)

docs/source/notebooks/index.md Outdated Show resolved Hide resolved
pymc_marketing/mmm/causal.py Outdated Show resolved Hide resolved
pymc_marketing/mmm/causal.py Outdated Show resolved Hide resolved
pymc_marketing/mmm/causal.py Outdated Show resolved Hide resolved
pymc_marketing/mmm/mmm.py Show resolved Hide resolved
@juanitorduz
Copy link
Collaborator

The notebook looks 🚀 ! I think after the previous small code comments, we can merge this one (plus potential @wd60622 comments)

@cetagostini
Copy link
Contributor Author

Thank you @juanitorduz, I follow your comments! Hope everything is looking a bit better.

@juanitorduz juanitorduz mentioned this pull request Jan 20, 2025
2 tasks
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.

Looks good!

Can we provide a better PR name before the release. "Add Causal DAG support in MMM definition" or something

@juanitorduz juanitorduz merged commit b8f86cd into main Jan 21, 2025
18 of 20 checks passed
@juanitorduz juanitorduz deleted the causal_identification branch January 21, 2025 09:04
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