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

Deepcopy of posterior to allow second fit call #790

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
680 changes: 312 additions & 368 deletions docs/source/notebooks/mmm/mmm_lift_test.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pymc_marketing/mmm/delayed_saturated_mmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1819,6 +1819,7 @@ def add_lift_test_measurements(
time_varying_var_name=time_varying_var_name,
model=self.model,
dist=dist,
name=name,
)

def _create_synth_dataset(
Expand Down
5 changes: 5 additions & 0 deletions pymc_marketing/model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@
idata = pm.sample(**sampler_args)

if self.idata:
self.idata = self.idata.copy()

Check warning on line 474 in pymc_marketing/model_builder.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/model_builder.py#L474

Added line #L474 was not covered by tests
self.idata.extend(idata, join="right")
else:
self.idata = idata
Expand All @@ -479,6 +480,10 @@
combined_data = pd.concat([X_df, y_df], axis=1)
if not all(combined_data.columns):
raise ValueError("All columns must have non-empty names")

if "fit_data" in self.idata:
del self.idata.fit_data

Check warning on line 485 in pymc_marketing/model_builder.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/model_builder.py#L484-L485

Added lines #L484 - L485 were not covered by tests

with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
Expand Down
2 changes: 0 additions & 2 deletions tests/mmm/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ def transform(self, X):


def test_validate_and_preprocess(toy_X, toy_y, test_mmm):
test_mmm

test_mmm.validate("X", toy_X)
test_mmm.mock_method1.assert_called_once_with(test_mmm, toy_X)

Expand Down
14 changes: 14 additions & 0 deletions tests/test_model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,17 @@ def test_fit_after_prior_keeps_prior(toy_X, toy_y):
model.fit(X=toy_X, y=toy_y, chains=1, draws=100, tune=100)
assert "prior" in model.idata
assert "prior_predictive" in model.idata


def test_second_fit(toy_X, toy_y):
model = ModelBuilderTest()

model.fit(X=toy_X, y=toy_y, chains=1, draws=100, tune=100)
assert "posterior" in model.idata
id_before = id(model.idata)
assert "fit_data" in model.idata

model.fit(X=toy_X, y=toy_y, chains=1, draws=100, tune=100)
id_after = id(model.idata)

assert id_before != id_after
Loading