Isofer/2309 cost per unit part1#2320
Draft
isofer wants to merge 19 commits intoisofer/roas-the-restfrom
Draft
Conversation
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
Contributor
Author
|
@cursoragent review |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## isofer/roas-the-rest #2320 +/- ##
=======================================================
Coverage ? 92.55%
=======================================================
Files ? 79
Lines ? 12703
Branches ? 0
=======================================================
Hits ? 11757
Misses ? 946
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
Author
|
@cursoragent review |
…eting into isofer/2309-cost-per-unit-part1
Contributor
Author
|
@cursoragent review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Goal: Support channels measured in non-monetary units (impressions, clicks) while keeping ROAS and budget optimization in spend terms.
This PR introduces
cost_per_unitsupport for two independent purposes that share only the DataFrame parsing logic (_parse_cost_per_unit_df):1. Historical cost-per-unit — for ROAS calculation
Uses the cost-per-unit from the training period to convert
channel_data(in original units) intochannel_spend(in monetary units). This precomputed spend is stored inidata.constant_dataand used byget_channel_spend()for ROAS calculations inincrementalityandplot_interactive.cost_per_unitcan be provided at init or post-fit viaset_cost_per_unit().channel_spend = channel_data × cost_per_unitis injected intoidata.constant_data.get_channel_spend()returnschannel_spendwhen present, falls back tochannel_data(backward compatible).get_channel_data()always returns raw data in original units (needed by the model evaluator).cost_per_unitproperty on the wrapper derives the rate on-the-fly aschannel_spend / channel_data.2. Future cost-per-unit — for budget optimization
Uses the cost-per-unit for the optimization window to convert monetary budgets into the model's native units before optimization. This is completely independent of the historical cost-per-unit above.
cost_per_unitparameter added tooptimize_budget()andBudgetOptimizer.cost_per_unit[t]→ channel scaling → model.optimal_budgetsremain in monetary units for user convenience.Shared: DataFrame parsing
Both use cases share
MMM._parse_cost_per_unit_df(), which converts a wide-format DataFrame (rows = date × custom_dims, columns = channels) into anxr.DataArray. Missing channels default to 1.0 (already in spend units).Why we store channel_spend instead of cost_per_unit in idata?
channel_spend, not the rate. There is no need to storecost_per_unit.aggregate_idata_time(used in incrementality and for interactive plots) sums every variable with adatedimension. If we stored the rate, we would get aggregatedchannel_dataand aggregatedcost_per_unitafter time aggregation, but there is no way to derive total channel spend from those two.channel_spendis additive, so summing it over time yields the correct total spend.Related Issue
cost_per_unitfor channels with non-monetary data (impressions, clicks, etc.) - part 1 #2309Screenshots / Examples
Changes Made
_parse_cost_per_unit_df()(static, inmultidimensional.py): Shared parser that converts a wide-format DataFrame into anxr.DataArrayaligned to model coordinates. Used by both historical and optimizer paths.mmm_wrapper.py:get_channel_spend()preferschannel_spendoverchannel_data; addget_channel_data()for raw units; addcost_per_unitproperty (derived on-the-fly).schema.py: Addchannel_spendVariableSchema to constant_data (optional).multidimensional.py:cost_per_unitinit param; fit-time injection ofchannel_spend;set_cost_per_unit(); serialize/deserialize_cost_per_unit_input;_parse_cost_per_unit_for_optimizer()on the budget wrapper;cost_per_unitparam onoptimize_budget().budget_optimizer.py: Addcost_per_unitfield and_validate_and_process_cost_per_unit(); convert budgets from monetary to native units in_replace_channel_data_by_optimization_variable(after time distribution, before channel scaling).incrementality.py: Useget_channel_data()instead ofget_channel_spend()forbaseline_arrayin evaluator (baseline must be in original units, not spend).Breaking Changes
get_channel_spend()falls back tochannel_datawhenchannel_spendis absent (backward compatible).