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

Add WeeklyFourier #1443

Merged
merged 10 commits into from
Jan 29, 2025
Merged

Add WeeklyFourier #1443

merged 10 commits into from
Jan 29, 2025

Conversation

PabloRoque
Copy link
Contributor

@PabloRoque PabloRoque commented Jan 28, 2025

Description

  • Introduces WeeklyFourier
  • Fixes sample_curve. Now returns same sizes when called with use_dates: bool = True | False [see test_sample_curve_same_size]
  • Parametrizes some tests so they are now run on the 3 different Fourier classes.

Related Issue

Checklist


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

Copy link

codecov bot commented Jan 28, 2025

Codecov Report

Attention: Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.

Project coverage is 94.61%. Comparing base (dd2ff1f) to head (02ca97b).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
pymc_marketing/mmm/fourier.py 95.23% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1443      +/-   ##
==========================================
- Coverage   94.61%   94.61%   -0.01%     
==========================================
  Files          50       50              
  Lines        5423     5441      +18     
==========================================
+ Hits         5131     5148      +17     
- Misses        292      293       +1     

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

@wd60622 wd60622 added the model components Related to the various model components label Jan 28, 2025
@@ -833,6 +833,63 @@ def _get_default_start_date(self) -> datetime.datetime:
return datetime.datetime(year=now.year, month=now.month, day=1)


class WeeklyFourier(FourierBase):
"""Monthly fourier seasonality.
Copy link
Contributor

Choose a reason for hiding this comment

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

Weekly *

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad, missed this one

"""Monthly fourier seasonality.

.. plot::
:context: close-figs
Copy link
Contributor

Choose a reason for hiding this comment

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

Think a space is needed here. It is not rendering in the docs for some reason

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wrong import + missing from __init__.py. Should be fixed now (tested locally)

days_in_period: float = DAYS_IN_WEEK

def _get_default_start_date(self) -> datetime.datetime:
"""Get the default start date for monthly seasonality.
Copy link
Contributor

Choose a reason for hiding this comment

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

Docstring change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@PabloRoque PabloRoque changed the title Add WeeklyFourier and related tests Add WeeklyFourier Jan 28, 2025
@PabloRoque PabloRoque self-assigned this Jan 28, 2025
@PabloRoque PabloRoque marked this pull request as ready for review January 28, 2025 16:17
@PabloRoque PabloRoque requested a review from wd60622 January 28, 2025 16:32
@PabloRoque
Copy link
Contributor Author

@wd60622 Anything else on this PR?

@wd60622
Copy link
Contributor

wd60622 commented Jan 29, 2025

@wd60622 Anything else on this PR?

Is the doc rendering correctly?

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. Just a request to deduplicate tests

tests/mmm/test_fourier.py Show resolved Hide resolved
Comment on lines 122 to 138
@pytest.mark.parametrize(
argnames="seasonality",
argvalues=[YearlyFourier, MonthlyFourier, WeeklyFourier],
ids=[
"yearly",
"monthly",
"weekly",
],
)
def test_sample_curve_same_size(seasonality) -> None:
n_order = 2
periodicity = seasonality(n_order=n_order)
prior = periodicity.sample_prior(samples=10)
curve_without_dates = periodicity.sample_curve(prior, use_dates=False)
curve_with_dates = periodicity.sample_curve(prior, use_dates=False)

assert curve_without_dates.sizes == curve_with_dates.sizes
Copy link
Contributor

Choose a reason for hiding this comment

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

Not needed the tests as above are same

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 is to test that the changes I made to apply make sense. This test was not passing before.

periodicity = seasonality(n_order=n_order)
prior = periodicity.sample_prior(samples=10)
curve_without_dates = periodicity.sample_curve(prior, use_dates=False)
curve_with_dates = periodicity.sample_curve(prior, use_dates=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this false as well? Or is the variable name wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

doh!

The sample_curve(..., use_dates) param took both values in my mind. Fixed now

The introduction of np.ceil in sample_curve still holds

@PabloRoque PabloRoque requested a review from wd60622 January 29, 2025 18:20
@PabloRoque
Copy link
Contributor Author

@wd60622 Anything else on this PR?

Is the doc rendering correctly?

Yes. See docs build

Comment on lines +158 to +165
def test_sample_curve_same_size(seasonality) -> None:
n_order = 2
periodicity = seasonality(n_order=n_order)
prior = periodicity.sample_prior(samples=10)
curve_without_dates = periodicity.sample_curve(prior, use_dates=False)
curve_with_dates = periodicity.sample_curve(prior, use_dates=True)

assert curve_without_dates.shape == curve_with_dates.shape
Copy link
Contributor

Choose a reason for hiding this comment

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

Still dont understand how this is not duplicate but thats fine

@wd60622 wd60622 merged commit 759bfd2 into pymc-labs:main Jan 29, 2025
20 checks passed
@PabloRoque PabloRoque deleted the fourier-seasonalities branch January 29, 2025 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MMM model components Related to the various model components tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants