Skip to content

Commit ec507be

Browse files
committed
Fix ROAS calculation and dimension validation bugs
- Fix bug #78169684: ROAS denominator now correctly uses actual channel spend instead of allocation * total_dates, which properly accounts for carryover periods with zero spend - Fix bug #5e0e10a6: Remove unnecessary 'sample' dimension requirement from contribution_over_time to match allocation_roas behavior and support both (chain, draw) and sample formats
1 parent e2a44b0 commit ec507be

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

pymc_marketing/mmm/plotting/budget.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,16 @@ def allocation_roas(
107107
**pc_kwargs,
108108
)
109109

110-
n_periods = len(samples.date)
111-
roas_da = samples["channel_contribution_original_scale"].sum("date") / (
112-
samples["allocation"] * n_periods
110+
# Compute total spend per channel from actual channel data in the dataset
111+
# This correctly handles carryover periods (which have zero spend)
112+
channels = list(samples["allocation"].channel.values)
113+
channel_spend = xr.concat([samples[ch] for ch in channels], dim="channel")
114+
channel_spend = channel_spend.assign_coords(channel=channels)
115+
total_spend_per_channel = channel_spend.sum("date")
116+
117+
roas_da = (
118+
samples["channel_contribution_original_scale"].sum("date")
119+
/ total_spend_per_channel
113120
)
114121
roas_da.name = "roas"
115122

@@ -175,7 +182,7 @@ def contribution_over_time(
175182
-------
176183
tuple[Figure, NDArray[Axes]] or PlotCollection
177184
"""
178-
for dim in ("channel", "date", "sample"):
185+
for dim in ("channel", "date"):
179186
if dim not in samples.dims:
180187
raise ValueError(
181188
f"Expected '{dim}' dimension in samples, but none found."

0 commit comments

Comments
 (0)