google-meridian 1.0.8__py3-none-any.whl → 1.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
meridian/model/spec.py CHANGED
@@ -15,12 +15,13 @@
15
15
  """Defines model specification parameters for Meridian."""
16
16
 
17
17
  import dataclasses
18
+ from typing import Sequence
19
+ import warnings
18
20
 
19
21
  from meridian import constants
20
22
  from meridian.model import prior_distribution
21
23
  import numpy as np
22
24
 
23
-
24
25
  __all__ = [
25
26
  "ModelSpec",
26
27
  ]
@@ -29,12 +30,22 @@ __all__ = [
29
30
  def _validate_roi_calibration_period(
30
31
  array: np.ndarray | None,
31
32
  array_name: str,
32
- media_channel_dim_name: str,
33
+ channel_dim_name: str,
34
+ prior_type: str,
35
+ prior_type_name: str,
33
36
  ):
34
- if array is not None and (len(array.shape) != 2):
37
+ """Validates the ROI calibration period array."""
38
+ if array is None:
39
+ return
40
+ if prior_type != constants.TREATMENT_PRIOR_TYPE_ROI:
41
+ raise ValueError(
42
+ f"The `{array_name}` should be `None` unless `{prior_type_name}` is"
43
+ f" '{constants.TREATMENT_PRIOR_TYPE_ROI}'."
44
+ )
45
+ if len(array.shape) != 2:
35
46
  raise ValueError(
36
47
  f"The shape of the `{array_name}` array {array.shape} should be"
37
- f" 2-dimensional (`n_media_times` x `{media_channel_dim_name}`)."
48
+ f" 2-dimensional (`n_media_times` x `{channel_dim_name}`)."
38
49
  )
39
50
 
40
51
 
@@ -66,29 +77,85 @@ class ModelSpec:
66
77
  unique_sigma_for_each_geo: A boolean indicating whether to use a unique
67
78
  residual variance for each geo. If `False`, then a single residual
68
79
  variance is used for all geos. Default: `False`.
69
- paid_media_prior_type: A string to specify the prior type for the media
70
- coefficients. Allowed values: `'roi'`, `'mroi'`, `'coefficient'`. Default:
71
- `'roi'`. The `PriorDistribution` contains distributions `roi_m`, `mroi_m`,
72
- and`beta_m`, but only one of these is used depending on the
73
- `paid_media_prior_type`. Likewise, the `PriorDistribution` contains
74
- distributions `roi_rf`, `mroi_rf`, and`beta_rf`, but only one of these is
75
- used depending on the `paid_media_prior_type`. When
76
- `paid_media_prior_type` is `'roi'`, the `PriorDistribution.roi_m` and
77
- `PriorDistribution.roi_rf` parameters are used to specify a prior on the
78
- ROI. When `paid_media_prior_type` is `'mroi'`, the
79
- `PriorDistribution.mroi_m` and `PriorDistribution.mroi_rf` parameters are
80
- used to specify a prior on the mROI. When `paid_media_prior_type` is
81
- `'coefficient'`, the `PriorDistribution.beta_m` and
82
- `PriorDistribution.beta_rf` parameters are used to specify a prior on the
83
- coefficient mean parameters.
80
+ media_prior_type: A string to specify the prior type for the media
81
+ coefficients. Allowed values: `'roi'`, `'mroi'`, `'contribution'`,
82
+ `'coefficient'`. The `PriorDistribution` contains `roi_m`, `mroi_m`,
83
+ `contribution_m`, and `beta_m`, but only one of these is used depending on
84
+ the `media_prior_type`. When `media_prior_type` is `'roi'`, the
85
+ `PriorDistribution.roi_m` parameter is used to specify a prior on the ROI.
86
+ When `media_prior_type` is `'mroi'`, the `PriorDistribution.mroi_m`
87
+ parameter is used to specify a prior on the mROI. When `media_prior_type`
88
+ is `'contribution'`, the `PriorDistribution.contribution_m` parameter is
89
+ used to specify a prior on the contribution. When `media_prior_type` is
90
+ `'coefficient'`, the `PriorDistribution.beta_m` parameter is used to
91
+ specify a prior on the coefficient mean parameters. Default: `'roi'`.
92
+ rf_prior_type: A string to specify the prior type for the RF coefficients.
93
+ Allowed values: `'roi'`, `'mroi'`, `'contribution'`, `'coefficient'`. The
94
+ `PriorDistribution` contains distributions `roi_rf`, `mroi_rf`,
95
+ `contribution_rf`, and`beta_rf`, but only one of these is used depending
96
+ on the `rf_prior_type`. When `rf_prior_type` is `'roi'`, the
97
+ `PriorDistribution.roi_rf` parameter is used to specify a prior on the
98
+ ROI. When `rf_media_prior_type` is `'mroi'`, the
99
+ `PriorDistribution.mroi_rf` parameter is used to specify a prior on the
100
+ mROI. When `rf_prior_type` is `'contribution'`, the
101
+ `PriorDistribution.contribution_rf` parameter is used to specify a prior
102
+ on the contribution. When `rf_prior_type` is `'coefficient'`, the
103
+ `PriorDistribution.beta_rf` parameter is used to specify a prior on the
104
+ coefficient mean parameters. Default: `'roi'`.
105
+ paid_media_prior_type: Deprecated. Use `media_prior_type` and
106
+ `rf_prior_type` instead. A string to specify the prior type for media and
107
+ RF treatments at the same time. Ignored when `media_prior_type` or
108
+ `rf_prior_type` are set. Default: `'roi'`.
84
109
  roi_calibration_period: An optional boolean array of shape `(n_media_times,
85
110
  n_media_channels)` indicating the subset of `time` that the ROI value of
86
- the `roi_m` prior (or mROI value of the `mroi_m` prior) applies to. If
87
- `None`, all times are used. Default: `None`.
111
+ the `roi_m` prior applies to. The ROI numerator is the incremental outcome
112
+ generated during this time period, and the denominator is the spend during
113
+ this time period. (Spend data by time period is required). If `None`, all
114
+ times are used. Only used if `media_prior_type` is `'roi'`.
115
+ Default: `None`.
88
116
  rf_roi_calibration_period: An optional boolean array of shape
89
117
  `(n_media_times, n_rf_channels)` indicating the subset of `time` that the
90
- ROI value of the `roi_rf` prior (or mROI value of the `mroi_rf` prior)
91
- applies to. If `None`, all times are used. Default: `None`.
118
+ ROI value of the `roi_rf` prior applies to. The ROI numerator is the
119
+ incremental outcome generated during this time period, and the denominator
120
+ is the spend during this time period. (Spend data by time period is
121
+ required). If `None`, all times are used. Only used if `rf_prior_type` is
122
+ `'roi'`. Default: `None`.
123
+ organic_media_prior_type: A string to specify the prior type for the organic
124
+ media coefficients. Allowed values: `'contribution'`, `'coefficient'`.
125
+ `PriorDistribution` contains `contribution_om` and `beta_om`, but only one
126
+ of these is used depending on the `organic_media_prior_type`. When
127
+ `organic_media_prior_type` is `'contribution'`, the
128
+ `PriorDistribution.contribution_om` parameter is used to specify a prior
129
+ on the contribution. When `organic_media_prior_type` is `'coefficient'`,
130
+ the `PriorDistribution.beta_om` parameter is used to specify a prior on
131
+ the coefficient mean parameters. Default: `'contribution'`.
132
+ organic_rf_prior_type: A string to specify the prior type for the organic
133
+ reach and frequency coefficients. Allowed values: `'contribution'`,
134
+ `'coefficient'`. The `PriorDistribution` contains distributions
135
+ `contribution_orf`, and `beta_orf`, but only one of these is used
136
+ depending on the `organic_rf_prior_type`. When `organic_rf_prior_type` is
137
+ `'contribution'`, the `PriorDistribution.contribution_orf` parameter is
138
+ used to specify a prior on the contribution. When `organic_rf_prior_type`
139
+ is `'coefficient'`, the `PriorDistribution.beta_orf` parameter is used to
140
+ specify a prior on the coefficient mean parameters. Default:
141
+ `'contribution'`.
142
+ non_media_treatments_prior_type: A string to specify the prior type for the
143
+ non-media treatment coefficients. Allowed values: `'contribution'`,
144
+ `'coefficient'`. `PriorDistribution` contains `contribution_n` and
145
+ `gamma_n`, but only one of these is used depending on the
146
+ `non_media_prior_type`. When `non_media_prior_type` is `'contribution'`,
147
+ the `PriorDistribution.contribution_n` parameter is used to specify a
148
+ prior on the contribution. When `non_media_prior_type` is `'coefficient'`,
149
+ the `PriorDistribution.gamma_n` parameter is used to specify a prior on
150
+ the coefficient mean parameters. Default: `'contribution'`.
151
+ non_media_baseline_values: Optional list with the shape
152
+ `(n_non_media_channels,)`. Each element is either a float (which means
153
+ that the fixed value will be used as baseline for the given channel) or
154
+ one of the strings `"min"` or `"max"` (which mean that the global minimum
155
+ or maximum value will be used as baseline for the scaled values of the
156
+ given non_media treatments channel). If `None`, the minimum value is used
157
+ as baseline for each non-media treatments channel. This attribute is used
158
+ as the default value for the corresponding argument to `Analyzer` methods.
92
159
  knots: An optional integer or list of integers indicating the knots used to
93
160
  estimate time effects. When `knots` is a list of integers, the knot
94
161
  locations are provided by that list. Zero corresponds to a knot at the
@@ -134,9 +201,17 @@ class ModelSpec:
134
201
  hill_before_adstock: bool = False
135
202
  max_lag: int | None = 8
136
203
  unique_sigma_for_each_geo: bool = False
137
- paid_media_prior_type: str = constants.PAID_MEDIA_PRIOR_TYPE_ROI
204
+ media_prior_type: str | None = None
205
+ rf_prior_type: str | None = None
206
+ paid_media_prior_type: str | None = None
138
207
  roi_calibration_period: np.ndarray | None = None
139
208
  rf_roi_calibration_period: np.ndarray | None = None
209
+ organic_media_prior_type: str = constants.TREATMENT_PRIOR_TYPE_CONTRIBUTION
210
+ organic_rf_prior_type: str = constants.TREATMENT_PRIOR_TYPE_CONTRIBUTION
211
+ non_media_treatments_prior_type: str = (
212
+ constants.TREATMENT_PRIOR_TYPE_CONTRIBUTION
213
+ )
214
+ non_media_baseline_values: Sequence[float | str] | None = None
140
215
  knots: int | list[int] | None = None
141
216
  baseline_geo: int | str | None = None
142
217
  holdout_id: np.ndarray | None = None
@@ -148,24 +223,77 @@ class ModelSpec:
148
223
  if self.media_effects_dist not in constants.MEDIA_EFFECTS_DISTRIBUTIONS:
149
224
  raise ValueError(
150
225
  f"The `media_effects_dist` parameter '{self.media_effects_dist}' must"
151
- f" be one of {sorted(list(constants.MEDIA_EFFECTS_DISTRIBUTIONS))}."
226
+ f" be one of {sorted(constants.MEDIA_EFFECTS_DISTRIBUTIONS)}."
152
227
  )
153
- # Validate roi_prior_type.
154
- if self.paid_media_prior_type not in constants.PAID_MEDIA_PRIOR_TYPES:
228
+ # Support paid_media_prior_type for backwards compatibility.
229
+ if self.paid_media_prior_type is not None:
230
+ if self.media_prior_type is not None or self.rf_prior_type is not None:
231
+ raise ValueError(
232
+ "The deprecated `paid_media_prior_type` parameter cannot be used"
233
+ " with `media_prior_type` or `rf_prior_type`. Use"
234
+ " `media_prior_type` and `rf_prior_type` instead."
235
+ )
236
+ else:
237
+ warnings.warn(
238
+ "Using `paid_media_prior_type` parameter will set prior types for"
239
+ " media and RF at the same time. This is deprecated and will be"
240
+ " removed in a future version of Meridian. Use `media_prior_type`"
241
+ " and `rf_prior_type` instead."
242
+ )
243
+ # Validate prior_type.
244
+ if (
245
+ self.effective_media_prior_type
246
+ not in constants.PAID_TREATMENT_PRIOR_TYPES
247
+ ):
155
248
  raise ValueError(
156
- "The `paid_media_prior_type` parameter"
157
- f" '{self.paid_media_prior_type}' must be one of"
158
- f" {sorted(list(constants.PAID_MEDIA_PRIOR_TYPES))}."
249
+ "The `media_prior_type` parameter"
250
+ f" '{self.effective_media_prior_type}' must be one of"
251
+ f" {sorted(constants.PAID_TREATMENT_PRIOR_TYPES)}."
159
252
  )
253
+ if self.effective_rf_prior_type not in constants.PAID_TREATMENT_PRIOR_TYPES:
254
+ raise ValueError(
255
+ "The `rf_prior_type` parameter"
256
+ f" '{self.effective_rf_prior_type}' must be one of"
257
+ f" {sorted(constants.PAID_TREATMENT_PRIOR_TYPES)}."
258
+ )
259
+ if self.organic_media_prior_type not in (
260
+ constants.NON_PAID_TREATMENT_PRIOR_TYPES
261
+ ):
262
+ raise ValueError(
263
+ "The `organic_media_prior_type` parameter"
264
+ f" '{self.organic_media_prior_type}' must be one of"
265
+ f" {sorted(constants.NON_PAID_TREATMENT_PRIOR_TYPES)}."
266
+ )
267
+ if self.organic_rf_prior_type not in (
268
+ constants.NON_PAID_TREATMENT_PRIOR_TYPES
269
+ ):
270
+ raise ValueError(
271
+ "The `organic_rf_prior_type` parameter"
272
+ f" '{self.organic_rf_prior_type}' must be one of"
273
+ f" {sorted(constants.NON_PAID_TREATMENT_PRIOR_TYPES)}."
274
+ )
275
+ if self.non_media_treatments_prior_type not in (
276
+ constants.NON_PAID_TREATMENT_PRIOR_TYPES
277
+ ):
278
+ raise ValueError(
279
+ "The `non_media_treatments_prior_type` parameter"
280
+ f" '{self.non_media_treatments_prior_type}' must be one of"
281
+ f" {sorted(constants.NON_PAID_TREATMENT_PRIOR_TYPES)}."
282
+ )
283
+
160
284
  _validate_roi_calibration_period(
161
- self.roi_calibration_period,
162
- "roi_calibration_period",
163
- "n_media_channels",
285
+ array=self.roi_calibration_period,
286
+ array_name="roi_calibration_period",
287
+ channel_dim_name="n_media_channels",
288
+ prior_type=self.effective_media_prior_type,
289
+ prior_type_name="media_prior_type",
164
290
  )
165
291
  _validate_roi_calibration_period(
166
- self.rf_roi_calibration_period,
167
- "rf_roi_calibration_period",
168
- "n_rf_channels",
292
+ array=self.rf_roi_calibration_period,
293
+ array_name="rf_roi_calibration_period",
294
+ channel_dim_name="n_rf_channels",
295
+ prior_type=self.effective_rf_prior_type,
296
+ prior_type_name="rf_prior_type",
169
297
  )
170
298
 
171
299
  # Validate knots.
@@ -173,3 +301,35 @@ class ModelSpec:
173
301
  raise ValueError("The `knots` parameter cannot be an empty list.")
174
302
  if isinstance(self.knots, int) and self.knots == 0:
175
303
  raise ValueError("The `knots` parameter cannot be zero.")
304
+
305
+ @property
306
+ def effective_media_prior_type(self) -> str:
307
+ """Returns the effective media prior type.
308
+
309
+ The recommended way to set prior types is to use `media_prior_type` and
310
+ `rf_prior_type` directly. If both `media_prior_type` and `rf_prior_type`
311
+ are not set, the deprecated `paid_media_prior_type` is used for both media
312
+ and RF channels. If none of them are set, the default is `roi`.
313
+ """
314
+ if self.media_prior_type is not None:
315
+ return self.media_prior_type
316
+ elif self.paid_media_prior_type is not None:
317
+ return self.paid_media_prior_type
318
+ else:
319
+ return constants.TREATMENT_PRIOR_TYPE_ROI
320
+
321
+ @property
322
+ def effective_rf_prior_type(self) -> str:
323
+ """Returns the effective rf prior type.
324
+
325
+ The recommended way to set prior types is to use `media_prior_type` and
326
+ `rf_prior_type` directly. If both `media_prior_type` and `rf_prior_type`
327
+ are not set, the deprecated `paid_media_prior_type` is used for both media
328
+ and RF channels. If none of them are set, the default is `roi`.
329
+ """
330
+ if self.rf_prior_type is not None:
331
+ return self.rf_prior_type
332
+ elif self.paid_media_prior_type is not None:
333
+ return self.paid_media_prior_type
334
+ else:
335
+ return constants.TREATMENT_PRIOR_TYPE_ROI
@@ -140,9 +140,21 @@ class CenteringAndScalingTransformer(TensorTransformer):
140
140
  self._stdevs = tf.math.reduce_std(tensor, axis=(0, 1))
141
141
 
142
142
  @tf.function(jit_compile=True)
143
- def forward(self, tensor: tf.Tensor) -> tf.Tensor:
144
- """Scales a given tensor using the stored coefficients."""
145
- if self._population_scaling_factors is not None:
143
+ def forward(
144
+ self, tensor: tf.Tensor, apply_population_scaling: bool = True
145
+ ) -> tf.Tensor:
146
+ """Scales a given tensor using the stored coefficients.
147
+
148
+ Args:
149
+ tensor: A tensor of dimension `(n_geos, n_times, n_channels)` to
150
+ transform.
151
+ apply_population_scaling: Whether to apply population scaling before the
152
+ normalization by means and standard deviations.
153
+ """
154
+ if (
155
+ apply_population_scaling
156
+ and self._population_scaling_factors is not None
157
+ ):
146
158
  tensor /= self._population_scaling_factors[:, None, :]
147
159
  return tf.math.divide_no_nan(tensor - self._means, self._stdevs)
148
160
 
@@ -1,41 +0,0 @@
1
- google_meridian-1.0.8.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
2
- meridian/__init__.py,sha256=d85YKzKshDwbViGr-BG7DJhNJh8a-dVF87y83gnTv7I,714
3
- meridian/constants.py,sha256=vhJI7R3kTGIHkLzkyx3i6ZnpcAXdAo4ath1eBS6cQHQ,15197
4
- meridian/analysis/__init__.py,sha256=-FooDZ5OzePpyTVkvRoWQx_xBaRR_hjVLny9H8-kkyQ,836
5
- meridian/analysis/analyzer.py,sha256=HyFJlTUYsv03skU4SiPvqjwevq7TXabwtD9VhoGObsw,200181
6
- meridian/analysis/formatter.py,sha256=F8OYxD2bH13zV10JY63j2ugCOj-DpTXhyJr43n5ukr8,7270
7
- meridian/analysis/optimizer.py,sha256=NwHb5PBhHye4XtPhh0qv0ZMCq6LwErZXFa86BwmtKLs,90115
8
- meridian/analysis/summarizer.py,sha256=jkESRdbH1U3ij-aBdV1JFTYNVJdfALmji5G4jmK4oMs,18403
9
- meridian/analysis/summary_text.py,sha256=n6a-DTZxtS3WvdI_pDEK7lvO3MRUX3h83GzuVnG6sQ4,12438
10
- meridian/analysis/test_utils.py,sha256=xai8oxXu51PDsiQ-ZYTnN_eSLsGu0BUOS8rDTcc6v-E,77719
11
- meridian/analysis/visualizer.py,sha256=_40uBa6QMJSjfwsvswcbGRUN3Urr_Vs16XiwpWETAfc,92624
12
- meridian/analysis/templates/card.html.jinja,sha256=pv4MVbQ25CcvtZY-LH7bFW0OSeHobkeEkAleB1sfQ14,1284
13
- meridian/analysis/templates/chart.html.jinja,sha256=87i0xnXHRBoLLxBpKv2i960TLToWq4r1aVQZqaXIeMQ,1086
14
- meridian/analysis/templates/chips.html.jinja,sha256=Az0tQwF_-b03JDLyOzpeH-8fb-6jgJgbNfnUUSm-q6E,645
15
- meridian/analysis/templates/insights.html.jinja,sha256=6hEWipbOMiMzs9QGZ6dcB_73tNkj0ZtNiC8E89a98zg,606
16
- meridian/analysis/templates/stats.html.jinja,sha256=9hQOG02FX1IHVIvdWS_-LI2bbSaqdyHEtCZkiArwAg0,772
17
- meridian/analysis/templates/style.css,sha256=RODTWc2pXcG9zW3q9SEJpVXgeD-WwQgzLpmFcbXPhLg,5492
18
- meridian/analysis/templates/style.scss,sha256=nSrZOpcIrVyiL4eC9jLUlxIZtAKZ0Rt8pwfk4H1nMrs,5076
19
- meridian/analysis/templates/summary.html.jinja,sha256=LuENVDHYIpNo4pzloYaCR2K9XN1Ow6_9oQOcOwD9nGg,1707
20
- meridian/analysis/templates/table.html.jinja,sha256=mvLMZx92RcD2JAS2w2eZtfYG-6WdfwYVo7pM8TbHp4g,1176
21
- meridian/data/__init__.py,sha256=ixOYHDQExjnPTLLnZ758pRQscZP7c8QJqtc8P4hK-mE,774
22
- meridian/data/arg_builder.py,sha256=f7LEysYmixAagwygZOEiJkRnWggRhTeXj5AXthBpkQ8,3741
23
- meridian/data/input_data.py,sha256=aSOyhu7AYhqsqW9xRB9oClOXsMTTeKnW9_p_n8WzBl4,35238
24
- meridian/data/load.py,sha256=MKCnRoR8ZIfhEAcm5ZUQsjfoEzi1c9SDOgf6BR8Akr0,54642
25
- meridian/data/test_utils.py,sha256=LNtsAalrHuolw7JbdggesJjhc6HXuy_eGPPAf4ix42c,51756
26
- meridian/data/time_coordinates.py,sha256=U4Qv8kXKUYoSH0A5F3REkoScSvrFDHgf_LaxSybdTgo,9881
27
- meridian/model/__init__.py,sha256=bvx8vvXolktsCTDKViU9U1v85pgNWF3haDowTKy11d4,982
28
- meridian/model/adstock_hill.py,sha256=b_YYhqci6ndgi602FFXmx2f12ceC4N0tp338nMMtm54,9283
29
- meridian/model/knots.py,sha256=r7PPaJM96d5pkoOeV9crIOgkM0-rh24mWMvypMiV4aQ,8054
30
- meridian/model/media.py,sha256=Gjr4jm0y_6pFy7aa_oKIuuZ8P7F56e3ZB-3o6msApeA,11876
31
- meridian/model/model.py,sha256=hA6HSaH2cd7Zgm8_JX3Jd79bWQSk8BtdqfEm5C9e3oQ,43323
32
- meridian/model/model_test_data.py,sha256=dqS_vDQUg811UGmyr8ZgWp8VTIra-krA7A2erQlfPlU,12488
33
- meridian/model/posterior_sampler.py,sha256=uUNMdxyoK0LT6hNKiAxEEl-1X0SyBMz-o_Sao5q5Ts8,23228
34
- meridian/model/prior_distribution.py,sha256=6fqx_XIM0DSQICd65XaSRhelsjvZ4ariBfeyOeoKld8,39075
35
- meridian/model/prior_sampler.py,sha256=zGSAQviFO3s2GcVbfG9EfXxo_SNFBFbTQC3e-QBFzio,23079
36
- meridian/model/spec.py,sha256=xaHxfCLWLnWMAkMy2ouDoqGBHI_4tzzX8AaJOsKdu7Q,8878
37
- meridian/model/transformers.py,sha256=te3OJixprWLtv7O00a9GZWE4waTS94NNLVo3tWIl1-k,7420
38
- google_meridian-1.0.8.dist-info/METADATA,sha256=DaSRL6L3xb0AiZBw22nbxDbFqvm2thApTpiEzffGe-o,22055
39
- google_meridian-1.0.8.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
40
- google_meridian-1.0.8.dist-info/top_level.txt,sha256=nwaCebZvvU34EopTKZsjK0OMTFjVnkf4FfnBN_TAc0g,9
41
- google_meridian-1.0.8.dist-info/RECORD,,