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

Reuse fixtures in slow MMM tests #515

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions tests/mmm/test_delayed_saturated_mmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
rng: np.random.Generator = np.random.default_rng(seed=seed)


@pytest.fixture(scope="class")
@pytest.fixture(scope="module")
def generate_data():
def _generate_data(date_data: pd.DatetimeIndex) -> pd.DataFrame:
n: int = date_data.size
Expand All @@ -37,7 +37,7 @@ def _generate_data(date_data: pd.DatetimeIndex) -> pd.DataFrame:
return _generate_data


@pytest.fixture(scope="class")
@pytest.fixture(scope="module")
def toy_X(generate_data) -> pd.DataFrame:
date_data: pd.DatetimeIndex = pd.date_range(
start="2019-06-01", end="2021-12-31", freq="W-MON"
Expand Down Expand Up @@ -80,12 +80,12 @@ def model_config_requiring_serialization() -> Dict:
return model_config


@pytest.fixture(scope="class")
@pytest.fixture(scope="module")
def toy_y(toy_X: pd.DataFrame) -> pd.Series:
return pd.Series(data=rng.integers(low=0, high=100, size=toy_X.shape[0]))


@pytest.fixture(scope="class")
@pytest.fixture(scope="module")
def mmm() -> DelayedSaturatedMMM:
return DelayedSaturatedMMM(
date_column="date",
Expand All @@ -95,7 +95,7 @@ def mmm() -> DelayedSaturatedMMM:
)


@pytest.fixture(scope="class")
@pytest.fixture(scope="module")
def mmm_with_fourier_features() -> DelayedSaturatedMMM:
return DelayedSaturatedMMM(
date_column="date",
Expand All @@ -106,15 +106,15 @@ def mmm_with_fourier_features() -> DelayedSaturatedMMM:
)


@pytest.fixture(scope="class")
@pytest.fixture(scope="module")
def mmm_fitted(
mmm: DelayedSaturatedMMM, toy_X: pd.DataFrame, toy_y: pd.Series
) -> DelayedSaturatedMMM:
mmm.fit(X=toy_X, y=toy_y, target_accept=0.8, draws=3, chains=2)
Copy link
Contributor

@ricardoV94 ricardoV94 Feb 6, 2024

Choose a reason for hiding this comment

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

Instead of this nearly useless fit here and in the fixture below, you can take draws from a prior predictive with narrow priors that correspond to a reasonable posterior and store those as if they were the posterior. We do this in almost all CLV tests.

There should be one test that actually calls fit (from real priors) and asserts it is converging to something correct. That could be marked with @pytest.mark.slow so it is skipped by default locally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that sounds good
What converging stats do you have in mind?

Copy link
Contributor

Choose a reason for hiding this comment

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

I dunno. We can simulate data with known parameters and check the posterior converges to those with some relative tolerance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool. I will add that in

Some of the tests still call fit with draws=100
Is that an issue?

Copy link
Contributor

Choose a reason for hiding this comment

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

Depends on whether that is relevant for the test or not

return mmm


@pytest.fixture(scope="class")
@pytest.fixture(scope="module")
def mmm_fitted_with_fourier_features(
mmm_with_fourier_features: DelayedSaturatedMMM,
toy_X: pd.DataFrame,
Expand Down
Loading