Skip to content

Commit 51f082d

Browse files
authored
Add calibration support to YAML MMM builder (#1979)
* Add calibration support to YAML MMM builder Introduces the ability to specify calibration steps in YAML config files for MMM models, including support for lift test and cost per target calibrations. Adds the _apply_calibration_steps function to apply these steps, updates example configs, and provides comprehensive tests for calibration step parsing and error handling. * Update YAML configs and notebook for clarity and path handling Commented out or clarified the 'nuts_sampler' option in multiple YAML config files to improve user guidance. Updated the calibration data file path in 'multi_dimensional_hierarchical_with_arbitrary_effects_model.yml' to use pathlib and pyprojroot for better portability. Refreshed the example notebook output to reflect recent changes in the codebase and dependencies. * Juanito!
1 parent 6919cbb commit 51f082d

12 files changed

+1084
-958
lines changed

data/config_files/basic_model.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ model:
2525
chains: 8
2626
random_seed: 42
2727
target_accept: 0.90
28-
nuts_sampler: "nutpie"
2928

3029
# ----------------------------------------------------------------------
3130
# (optional) idata from a previous sample

data/config_files/example_with_original_scale_vars.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ model:
2525
chains: 8
2626
random_seed: 42
2727
target_accept: 0.90
28-
nuts_sampler: "nutpie"
28+
# if you want to use a custom sampler (e.g: nutpie) you could add # nuts_sampler: "nutpie"
2929

3030
# ----------------------------------------------------------------------
3131
# (optional) idata from a previous sample

data/config_files/multi_dimensional_example_model.yml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@ model:
7676
distribution: HalfNormal
7777
sigma: 1.5
7878
dims: [date, geo]
79-
79+
# ----------------------------------------------------------------------
80+
# (optional) sampler options you plan to forward to pm.sample():
81+
sampler_config:
82+
tune: 800
83+
draws: 400
84+
chains: 2
85+
random_seed: 42
86+
target_accept: 0.80
8087
# ----------------------------------------------------------------------
8188
# Effects with complex priors
8289
effects:
@@ -98,15 +105,6 @@ effects:
98105
dims: ["changepoint", "geo"]
99106
prefix: "trend"
100107

101-
# ----------------------------------------------------------------------
102-
# (optional) sampler options you plan to forward to pm.sample():
103-
sampler_config:
104-
tune: 800
105-
draws: 400
106-
chains: 2
107-
random_seed: 42
108-
target_accept: 0.80
109-
110108
# ----------------------------------------------------------------------
111109
# (optional) idata from a previous sample
112110
idata_path: "multidimensional_model.nc"

data/config_files/multi_dimensional_hierarchical_model.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ model:
6868
chains: 8
6969
random_seed: 42
7070
target_accept: 0.90
71-
nuts_sampler: "nutpie"
71+
# if you want to use a custom sampler (e.g: nutpie) you could add # nuts_sampler: "nutpie"
7272

7373
# ----------------------------------------------------------------------
7474
# (optional) idata from a previous sample

data/config_files/multi_dimensional_hierarchical_model_nested_config.yml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ model:
6363
sigma: 1
6464
dims: "market"
6565
dims: ["date", "market"]
66-
66+
# ----------------------------------------------------------------------
67+
# (optional) sampler options you plan to forward to pm.sample():
68+
sampler_config:
69+
tune: 1000
70+
draws: 200
71+
chains: 8
72+
random_seed: 42
73+
target_accept: 0.90
74+
# if you want to use a custom sampler (e.g: nutpie) you could add # nuts_sampler: "nutpie"
6775
# ----------------------------------------------------------------------
6876
# Effects with complex priors
6977
effects:
@@ -103,16 +111,6 @@ effects:
103111
b: 0.1
104112
dims: ["market", "weekly_fourier"]
105113

106-
# ----------------------------------------------------------------------
107-
# (optional) sampler options you plan to forward to pm.sample():
108-
sampler_config:
109-
tune: 1000
110-
draws: 200
111-
chains: 8
112-
random_seed: 42
113-
target_accept: 0.90
114-
nuts_sampler: "nutpie"
115-
116114
# ----------------------------------------------------------------------
117115
# (optional) idata from a previous sample
118116
# idata_path: "random.nc"

data/config_files/multi_dimensional_hierarchical_with_arbitrary_effects_model.yml

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ model:
5959
sigma: 1
6060
dims: "market"
6161
dims: ["date", "market"]
62-
62+
# ----------------------------------------------------------------------
63+
# (optional) sampler options you plan to forward to pm.sample():
64+
sampler_config:
65+
tune: 1000
66+
draws: 200
67+
chains: 8
68+
random_seed: 42
69+
target_accept: 0.90
70+
# if you want to use a custom sampler (e.g: nutpie) you could add # nuts_sampler: "nutpie"
6371
# ----------------------------------------------------------------------
6472
# Effects with complex priors
6573
effects:
@@ -100,14 +108,43 @@ effects:
100108
dims: ["weekly_fourier", "market"]
101109

102110
# ----------------------------------------------------------------------
103-
# (optional) sampler options you plan to forward to pm.sample():
104-
sampler_config:
105-
tune: 1000
106-
draws: 200
107-
chains: 8
108-
random_seed: 42
109-
target_accept: 0.90
110-
nuts_sampler: "nutpie"
111+
# Calibration examples (lift tests + CPT constraints)
112+
calibration:
113+
- add_lift_test_measurements:
114+
df_lift_test:
115+
class: pandas.DataFrame
116+
kwargs:
117+
data:
118+
market: ["US", "EU"]
119+
channel: ["channel_1", "channel_2"]
120+
x: [80.0, 50.0]
121+
delta_x: [10.0, 6.0]
122+
delta_y: [6.0, 4.0]
123+
sigma: [1.5, 1.2]
124+
name: "example_lift_tests"
125+
- add_cost_per_target_calibration:
126+
data:
127+
class: pandas.read_csv
128+
kwargs:
129+
filepath_or_buffer:
130+
class: pathlib.Path
131+
kwargs:
132+
args:
133+
- class: pyprojroot.here
134+
- "data"
135+
- "processed"
136+
- "X.csv"
137+
calibration_data:
138+
class: pandas.DataFrame
139+
kwargs:
140+
data:
141+
market: ["US", "EU"]
142+
channel: ["channel_1", "channel_2"]
143+
cost_per_target: [30.0, 42.5]
144+
sigma: [2.0, 3.0]
145+
cpt_variable_name: "cost_per_target"
146+
name_prefix: "example_cpt"
147+
111148

112149
# ----------------------------------------------------------------------
113150
# (optional) idata from a previous sample

data/config_files/multi_dimensional_model.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ model:
2626
chains: 8
2727
random_seed: 42
2828
target_accept: 0.90
29-
nuts_sampler: "nutpie"
29+
# if you want to use a custom sampler (e.g: nutpie) you could add # nuts_sampler: "nutpie"
3030

3131
# ----------------------------------------------------------------------
3232
# (optional) idata from a previous sample

data/config_files/multi_dimensional_with_events.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ model:
6767
chains: 4
6868
random_seed: 42
6969
target_accept: 0.92
70-
# nuts_sampler: "nutpie"
71-
# nuts_sampler_kwargs:
72-
# backend: "jax"
7370

7471
# ----------------------------------------------------------------------
7572
# Effects with complex priors

docs/source/gallery/gallery.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ Welcome to the PyMC-Marketing example gallery! This gallery provides visual navi
3636
:link: ../notebooks/mmm/mmm_multidimensional_example.html
3737
:::
3838

39+
:::{grid-item-card} Build MMM from YAML Configuration
40+
:img-top: ../gallery/images/mmm_build_from_yml_example.png
41+
:link: ../notebooks/mmm/mmm_build_from_yml_example.html
42+
:::
43+
3944
:::{grid-item-card} Sensitivity Analysis and Marginal Effects
4045
:img-top: ../gallery/images/mmm_sensitivity_analysis.png
4146
:link: ../notebooks/mmm/mmm_sensitivity_analysis.html

0 commit comments

Comments
 (0)