Skip to content

Allow covariates in plot_expected_purchases #1430

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

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

Conversation

PabloRoque
Copy link
Contributor

@PabloRoque PabloRoque commented Jan 24, 2025

Description

Includes covariates when calculation the unconditional frequency expectation. Allows the estimation of future purchases for the "average customer" just after first order for models with covariates.

Additionally:

  • Introduces a new dataset apparel_trans.csv and accompanying apparel_static_cov.csv. These datasets are extracted from R's CLVTools package.
  • A new notebook bg_nbd_covariates.ipynb shows an example of the new functionality using the new dataset.

Related Issue

Checklist


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

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 docs Improvements or additions to documentation CLV tests labels Jan 24, 2025
Copy link

codecov bot commented Jan 24, 2025

Codecov Report

Attention: Patch coverage is 75.00000% with 7 lines in your changes missing coverage. Please review.

Project coverage is 91.51%. Comparing base (f88c98d) to head (9abe758).

Files with missing lines Patch % Lines
pymc_marketing/clv/utils.py 75.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1430      +/-   ##
==========================================
- Coverage   91.59%   91.51%   -0.08%     
==========================================
  Files          60       60              
  Lines        6782     6802      +20     
==========================================
+ Hits         6212     6225      +13     
- Misses        570      577       +7     

☔ 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.

@ColtAllen
Copy link
Collaborator

I'll review this in detail later, but it looks like there may be merge conflicts with #1390

@PabloRoque PabloRoque requested a review from ColtAllen February 11, 2025 11:55
@PabloRoque
Copy link
Contributor Author

@ColtAllen Feel free to have a look at this PR whenever you have some spare time. It is ready for review from my side.

@ColtAllen
Copy link
Collaborator

Introduces a new dataset apparel_trans.csv and accompanying apparel_static_cov.csv. These datasets are extracted from R's CLVTools package.

Is there documentation available for these datasets? I wasn't able to find anything in the R library.

A new notebook bg_nbd_covariates.ipynb shows an example of the new functionality using the new dataset.

Is it possible to consolidate this into the existing BG/NBD notebook?

@PabloRoque
Copy link
Contributor Author

PabloRoque commented Feb 19, 2025

Is there documentation available for these datasets? I wasn't able to find anything in the R library.

Link to apparelTrans and apparelStaticCov

Is it possible to consolidate this into the existing BG/NBD notebook?

Do you propose to add this new dataset, or use the synthetic covariates that we are using in the ParetoNBD notebook?

@ColtAllen
Copy link
Collaborator

Link to apparelTrans and apparelStaticCov

Nice, so this dataset is simulated, then.

Do you propose to add this new dataset, or use the synthetic covariates that we are using in the ParetoNBD notebook?

The Pareto/NBD notebook is using real data available here. I think using the same dataset would make for a good comparison in resolving #1496. That said, let's just remove the apparel datasets for now and do the notebook consolidation in a separate PR. No need for any additional changes to the dev notebook here unless you want to test visualizations.

Copy link
Collaborator

@ColtAllen ColtAllen left a comment

Choose a reason for hiding this comment

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

Let's use the covariate data already being saved in a fitted model rather than require the user to add this to the transaction data (see my comment for more details). This should cut down on the additional code/testing requirements and also be more efficient.

One of these days I'd like to refactor _expected_cumulative_transactions to eliminate the FOR loop, but this would require xarray support in the predictive methods.

Comment on lines +906 to +914
if model.purchase_covariate_cols or model.dropout_covariate_cols:
distinct_covariates_cols = list(
set(model.purchase_covariate_cols).intersection(
set(model.dropout_covariate_cols)
)
)
distinct_covariates = transactions[distinct_covariates_cols].drop_duplicates()
else:
distinct_covariates_cols = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's a customer_id dimension for the covariates in the fitted model idata. Rather than requiring covariates be added to be transaction dataframe, we can obtain the covariates from the model directly and merge them with repeated_and_first_transactions.

@juanitorduz
Copy link
Collaborator

@PabloRoque can we bring this one to the finish line?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLV docs Improvements or additions to documentation tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants