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

remove spend scaling step from budget optimizer #1070

Merged
merged 2 commits into from
Sep 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
188 changes: 106 additions & 82 deletions docs/source/notebooks/mmm/mmm_budget_allocation_example.ipynb

Large diffs are not rendered by default.

40 changes: 13 additions & 27 deletions pymc_marketing/mmm/mmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2239,28 +2239,14 @@
The matplotlib figure object and axis containing the plot.

"""
if original_scale:
channel_contributions = (
samples["channel_contributions"]
.mean(dim=["sample"])
.mean(dim=["date"])
.values
* self.get_target_transformer()["scaler"].scale_
)
channel_contributions = (

Check warning on line 2242 in pymc_marketing/mmm/mmm.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/mmm/mmm.py#L2242

Added line #L2242 was not covered by tests
samples["channel_contributions"].mean(dim=["date", "sample"]).to_numpy()
)

allocate_spend = (
np.array(list(self.optimal_allocation_dict.values()))
* self.channel_transformer["scaler"].scale_
)
if original_scale:
channel_contributions *= self.get_target_transformer()["scaler"].scale_

Check warning on line 2247 in pymc_marketing/mmm/mmm.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/mmm/mmm.py#L2246-L2247

Added lines #L2246 - L2247 were not covered by tests
Comment on lines +2246 to +2247
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@carlosagostini, Is this still valid, or should we remove the scaling everywhere?

Copy link
Contributor

Choose a reason for hiding this comment

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

Contributions still need to be scaled, but not the budget allocation, because the optimizer now returns it in original scale. So post processing is not necessary 🙌🏻

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok! Thanks! Si is this one good to go ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed!


else:
channel_contributions = (
samples["channel_contributions"]
.mean(dim=["sample"])
.mean(dim=["date"])
.values
)
allocate_spend = np.array(list(self.optimal_allocation_dict.values()))
allocated_spend = np.array(list(self.optimal_allocation_dict.values()))

Check warning on line 2249 in pymc_marketing/mmm/mmm.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/mmm/mmm.py#L2249

Added line #L2249 was not covered by tests

if ax is None:
fig, ax = plt.subplots(figsize=figsize)
Expand All @@ -2274,11 +2260,11 @@

bars1 = ax.bar(
index,
allocate_spend,
allocated_spend,
bar_width,
color="b",
color="C0",
alpha=opacity,
label="Allocate Spend",
label="Allocated Spend",
)

ax2 = ax.twinx()
Expand All @@ -2287,19 +2273,19 @@
index + bar_width,
channel_contributions,
bar_width,
color="r",
color="C1",
alpha=opacity,
label="Channel Contributions",
)

ax.set_xlabel("Channels")
ax.set_ylabel("Allocate Spend", color="b")
ax.set_ylabel("Allocate Spend", color="C0")

Check warning on line 2282 in pymc_marketing/mmm/mmm.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/mmm/mmm.py#L2282

Added line #L2282 was not covered by tests
ax.tick_params(axis="x", rotation=90)
ax.set_xticks(index + bar_width / 2)
ax.set_xticklabels(self.channel_columns)

ax.set_ylabel("Allocate Spend", color="b", labelpad=10)
ax2.set_ylabel("Channel Contributions", color="r", labelpad=10)
ax.set_ylabel("Allocate Spend", color="C0", labelpad=10)
ax2.set_ylabel("Channel Contributions", color="C1", labelpad=10)

Check warning on line 2288 in pymc_marketing/mmm/mmm.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/mmm/mmm.py#L2287-L2288

Added lines #L2287 - L2288 were not covered by tests

ax.grid(False)
ax2.grid(False)
Expand Down
Loading