google-meridian 1.0.3__py3-none-any.whl → 1.0.5__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.
@@ -0,0 +1,351 @@
1
+ # Copyright 2024 The Meridian Authors.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Shared test data samples."""
16
+
17
+ import collections
18
+ import os
19
+
20
+ from meridian import constants
21
+ from meridian.data import test_utils
22
+ import tensorflow as tf
23
+ import xarray as xr
24
+
25
+
26
+ def _convert_with_swap(array: xr.DataArray, n_burnin: int) -> tf.Tensor:
27
+ """Converts a DataArray to a tf.Tensor with the correct MCMC format.
28
+
29
+ This function converts a DataArray to tf.Tensor, swaps first two dimensions
30
+ and adds the burnin part. This is needed to properly mock the
31
+ _xla_windowed_adaptive_nuts() function output in the sample_posterior
32
+ tests.
33
+
34
+ Args:
35
+ array: The array to be converted.
36
+ n_burnin: The number of extra draws to be padded with as the 'burnin' part.
37
+
38
+ Returns:
39
+ A tensor in the same format as returned by the _xla_windowed_adaptive_nuts()
40
+ function.
41
+ """
42
+ tensor = tf.convert_to_tensor(array)
43
+ perm = [1, 0] + [i for i in range(2, len(tensor.shape))]
44
+ transposed_tensor = tf.transpose(tensor, perm=perm)
45
+
46
+ # Add the "burnin" part to the mocked output of _xla_windowed_adaptive_nuts
47
+ # to make sure sample_posterior returns the correct "keep" part.
48
+ if array.dtype == bool:
49
+ pad_value = False
50
+ else:
51
+ pad_value = 0.0 if array.dtype.kind == "f" else 0
52
+
53
+ burnin = tf.fill([n_burnin] + transposed_tensor.shape[1:], pad_value)
54
+ return tf.concat(
55
+ [burnin, transposed_tensor],
56
+ axis=0,
57
+ )
58
+
59
+
60
+ class WithInputDataSamples:
61
+ """A mixin to inject test data samples to a unit test class."""
62
+
63
+ # TODO: Update the sample data to span over 1 or 2 year(s).
64
+ _TEST_DIR = os.path.join(os.path.dirname(__file__), "test_data")
65
+ _TEST_SAMPLE_PRIOR_MEDIA_AND_RF_PATH = os.path.join(
66
+ _TEST_DIR,
67
+ "sample_prior_media_and_rf.nc",
68
+ )
69
+ _TEST_SAMPLE_PRIOR_MEDIA_ONLY_PATH = os.path.join(
70
+ _TEST_DIR,
71
+ "sample_prior_media_only.nc",
72
+ )
73
+ _TEST_SAMPLE_PRIOR_RF_ONLY_PATH = os.path.join(
74
+ _TEST_DIR,
75
+ "sample_prior_rf_only.nc",
76
+ )
77
+ _TEST_SAMPLE_POSTERIOR_MEDIA_AND_RF_PATH = os.path.join(
78
+ _TEST_DIR,
79
+ "sample_posterior_media_and_rf.nc",
80
+ )
81
+ _TEST_SAMPLE_POSTERIOR_MEDIA_ONLY_PATH = os.path.join(
82
+ _TEST_DIR,
83
+ "sample_posterior_media_only.nc",
84
+ )
85
+ _TEST_SAMPLE_POSTERIOR_RF_ONLY_PATH = os.path.join(
86
+ _TEST_DIR,
87
+ "sample_posterior_rf_only.nc",
88
+ )
89
+ _TEST_SAMPLE_TRACE_PATH = os.path.join(
90
+ _TEST_DIR,
91
+ "sample_trace.nc",
92
+ )
93
+
94
+ # Data dimensions for sample input.
95
+ _N_CHAINS = 2
96
+ _N_ADAPT = 2
97
+ _N_BURNIN = 5
98
+ _N_KEEP = 10
99
+ _N_DRAWS = 10
100
+ _N_GEOS = 5
101
+ _N_GEOS_NATIONAL = 1
102
+ _N_TIMES = 200
103
+ _N_TIMES_SHORT = 49
104
+ _N_MEDIA_TIMES = 203
105
+ _N_MEDIA_TIMES_SHORT = 52
106
+ _N_MEDIA_CHANNELS = 3
107
+ _N_RF_CHANNELS = 2
108
+ _N_CONTROLS = 2
109
+ _ROI_CALIBRATION_PERIOD = tf.cast(
110
+ tf.ones((_N_MEDIA_TIMES_SHORT, _N_MEDIA_CHANNELS)),
111
+ dtype=tf.bool,
112
+ )
113
+ _RF_ROI_CALIBRATION_PERIOD = tf.cast(
114
+ tf.ones((_N_MEDIA_TIMES_SHORT, _N_RF_CHANNELS)),
115
+ dtype=tf.bool,
116
+ )
117
+ _N_ORGANIC_MEDIA_CHANNELS = 4
118
+ _N_ORGANIC_RF_CHANNELS = 1
119
+ _N_NON_MEDIA_CHANNELS = 2
120
+
121
+ def setup(self):
122
+ """Sets up input data samples."""
123
+ self.input_data_non_revenue_no_revenue_per_kpi = (
124
+ test_utils.sample_input_data_non_revenue_no_revenue_per_kpi(
125
+ n_geos=self._N_GEOS,
126
+ n_times=self._N_TIMES,
127
+ n_media_times=self._N_MEDIA_TIMES,
128
+ n_controls=self._N_CONTROLS,
129
+ n_media_channels=self._N_MEDIA_CHANNELS,
130
+ seed=0,
131
+ )
132
+ )
133
+ self.input_data_with_media_only = (
134
+ test_utils.sample_input_data_non_revenue_revenue_per_kpi(
135
+ n_geos=self._N_GEOS,
136
+ n_times=self._N_TIMES,
137
+ n_media_times=self._N_MEDIA_TIMES,
138
+ n_controls=self._N_CONTROLS,
139
+ n_media_channels=self._N_MEDIA_CHANNELS,
140
+ seed=0,
141
+ )
142
+ )
143
+ self.input_data_with_rf_only = (
144
+ test_utils.sample_input_data_non_revenue_revenue_per_kpi(
145
+ n_geos=self._N_GEOS,
146
+ n_times=self._N_TIMES,
147
+ n_media_times=self._N_MEDIA_TIMES,
148
+ n_controls=self._N_CONTROLS,
149
+ n_rf_channels=self._N_RF_CHANNELS,
150
+ seed=0,
151
+ )
152
+ )
153
+ self.input_data_with_media_and_rf = (
154
+ test_utils.sample_input_data_non_revenue_revenue_per_kpi(
155
+ n_geos=self._N_GEOS,
156
+ n_times=self._N_TIMES,
157
+ n_media_times=self._N_MEDIA_TIMES,
158
+ n_controls=self._N_CONTROLS,
159
+ n_media_channels=self._N_MEDIA_CHANNELS,
160
+ n_rf_channels=self._N_RF_CHANNELS,
161
+ seed=0,
162
+ )
163
+ )
164
+ self.short_input_data_with_media_only = (
165
+ test_utils.sample_input_data_non_revenue_revenue_per_kpi(
166
+ n_geos=self._N_GEOS,
167
+ n_times=self._N_TIMES_SHORT,
168
+ n_media_times=self._N_MEDIA_TIMES_SHORT,
169
+ n_controls=self._N_CONTROLS,
170
+ n_media_channels=self._N_MEDIA_CHANNELS,
171
+ seed=0,
172
+ )
173
+ )
174
+ self.short_input_data_with_rf_only = (
175
+ test_utils.sample_input_data_non_revenue_revenue_per_kpi(
176
+ n_geos=self._N_GEOS,
177
+ n_times=self._N_TIMES_SHORT,
178
+ n_media_times=self._N_MEDIA_TIMES_SHORT,
179
+ n_controls=self._N_CONTROLS,
180
+ n_rf_channels=self._N_RF_CHANNELS,
181
+ seed=0,
182
+ )
183
+ )
184
+ self.short_input_data_with_media_and_rf = (
185
+ test_utils.sample_input_data_non_revenue_revenue_per_kpi(
186
+ n_geos=self._N_GEOS,
187
+ n_times=self._N_TIMES_SHORT,
188
+ n_media_times=self._N_MEDIA_TIMES_SHORT,
189
+ n_controls=self._N_CONTROLS,
190
+ n_media_channels=self._N_MEDIA_CHANNELS,
191
+ n_rf_channels=self._N_RF_CHANNELS,
192
+ seed=0,
193
+ )
194
+ )
195
+ self.national_input_data_media_only = (
196
+ test_utils.sample_input_data_non_revenue_revenue_per_kpi(
197
+ n_geos=self._N_GEOS_NATIONAL,
198
+ n_times=self._N_TIMES,
199
+ n_media_times=self._N_MEDIA_TIMES,
200
+ n_controls=self._N_CONTROLS,
201
+ n_media_channels=self._N_MEDIA_CHANNELS,
202
+ seed=0,
203
+ )
204
+ )
205
+ self.national_input_data_media_and_rf = (
206
+ test_utils.sample_input_data_non_revenue_revenue_per_kpi(
207
+ n_geos=self._N_GEOS_NATIONAL,
208
+ n_times=self._N_TIMES,
209
+ n_media_times=self._N_MEDIA_TIMES,
210
+ n_controls=self._N_CONTROLS,
211
+ n_media_channels=self._N_MEDIA_CHANNELS,
212
+ n_rf_channels=self._N_RF_CHANNELS,
213
+ seed=0,
214
+ )
215
+ )
216
+
217
+ test_prior_media_and_rf = xr.open_dataset(
218
+ self._TEST_SAMPLE_PRIOR_MEDIA_AND_RF_PATH
219
+ )
220
+ test_prior_media_only = xr.open_dataset(
221
+ self._TEST_SAMPLE_PRIOR_MEDIA_ONLY_PATH
222
+ )
223
+ test_prior_rf_only = xr.open_dataset(self._TEST_SAMPLE_PRIOR_RF_ONLY_PATH)
224
+ self.test_dist_media_and_rf = collections.OrderedDict({
225
+ param: tf.convert_to_tensor(test_prior_media_and_rf[param])
226
+ for param in constants.COMMON_PARAMETER_NAMES
227
+ + constants.MEDIA_PARAMETER_NAMES
228
+ + constants.RF_PARAMETER_NAMES
229
+ })
230
+ self.test_dist_media_only = collections.OrderedDict({
231
+ param: tf.convert_to_tensor(test_prior_media_only[param])
232
+ for param in constants.COMMON_PARAMETER_NAMES
233
+ + constants.MEDIA_PARAMETER_NAMES
234
+ })
235
+ self.test_dist_rf_only = collections.OrderedDict({
236
+ param: tf.convert_to_tensor(test_prior_rf_only[param])
237
+ for param in constants.COMMON_PARAMETER_NAMES
238
+ + constants.RF_PARAMETER_NAMES
239
+ })
240
+
241
+ test_posterior_media_and_rf = xr.open_dataset(
242
+ self._TEST_SAMPLE_POSTERIOR_MEDIA_AND_RF_PATH
243
+ )
244
+ test_posterior_media_only = xr.open_dataset(
245
+ self._TEST_SAMPLE_POSTERIOR_MEDIA_ONLY_PATH
246
+ )
247
+ test_posterior_rf_only = xr.open_dataset(
248
+ self._TEST_SAMPLE_POSTERIOR_RF_ONLY_PATH
249
+ )
250
+ posterior_params_to_tensors_media_and_rf = {
251
+ param: _convert_with_swap(
252
+ test_posterior_media_and_rf[param], n_burnin=self._N_BURNIN
253
+ )
254
+ for param in constants.COMMON_PARAMETER_NAMES
255
+ + constants.MEDIA_PARAMETER_NAMES
256
+ + constants.RF_PARAMETER_NAMES
257
+ }
258
+ posterior_params_to_tensors_media_only = {
259
+ param: _convert_with_swap(
260
+ test_posterior_media_only[param], n_burnin=self._N_BURNIN
261
+ )
262
+ for param in constants.COMMON_PARAMETER_NAMES
263
+ + constants.MEDIA_PARAMETER_NAMES
264
+ }
265
+ posterior_params_to_tensors_rf_only = {
266
+ param: _convert_with_swap(
267
+ test_posterior_rf_only[param], n_burnin=self._N_BURNIN
268
+ )
269
+ for param in constants.COMMON_PARAMETER_NAMES
270
+ + constants.RF_PARAMETER_NAMES
271
+ }
272
+ self.test_posterior_states_media_and_rf = collections.namedtuple(
273
+ "StructTuple",
274
+ constants.COMMON_PARAMETER_NAMES
275
+ + constants.MEDIA_PARAMETER_NAMES
276
+ + constants.RF_PARAMETER_NAMES,
277
+ )(**posterior_params_to_tensors_media_and_rf)
278
+ self.test_posterior_states_media_only = collections.namedtuple(
279
+ "StructTuple",
280
+ constants.COMMON_PARAMETER_NAMES + constants.MEDIA_PARAMETER_NAMES,
281
+ )(**posterior_params_to_tensors_media_only)
282
+ self.test_posterior_states_rf_only = collections.namedtuple(
283
+ "StructTuple",
284
+ constants.COMMON_PARAMETER_NAMES + constants.RF_PARAMETER_NAMES,
285
+ )(**posterior_params_to_tensors_rf_only)
286
+
287
+ test_trace = xr.open_dataset(self._TEST_SAMPLE_TRACE_PATH)
288
+ self.test_trace = {
289
+ param: _convert_with_swap(test_trace[param], n_burnin=self._N_BURNIN)
290
+ for param in test_trace.data_vars
291
+ }
292
+
293
+ # The following are input data samples with non-paid channels.
294
+
295
+ self.national_input_data_non_media_and_organic = (
296
+ test_utils.sample_input_data_non_revenue_revenue_per_kpi(
297
+ n_geos=self._N_GEOS_NATIONAL,
298
+ n_times=self._N_TIMES,
299
+ n_media_times=self._N_MEDIA_TIMES,
300
+ n_controls=self._N_CONTROLS,
301
+ n_non_media_channels=self._N_NON_MEDIA_CHANNELS,
302
+ n_media_channels=self._N_MEDIA_CHANNELS,
303
+ n_rf_channels=self._N_RF_CHANNELS,
304
+ n_organic_media_channels=self._N_ORGANIC_MEDIA_CHANNELS,
305
+ n_organic_rf_channels=self._N_ORGANIC_RF_CHANNELS,
306
+ seed=0,
307
+ )
308
+ )
309
+
310
+ self.input_data_non_media_and_organic = (
311
+ test_utils.sample_input_data_non_revenue_revenue_per_kpi(
312
+ n_geos=self._N_GEOS,
313
+ n_times=self._N_TIMES,
314
+ n_media_times=self._N_MEDIA_TIMES,
315
+ n_controls=self._N_CONTROLS,
316
+ n_non_media_channels=self._N_NON_MEDIA_CHANNELS,
317
+ n_media_channels=self._N_MEDIA_CHANNELS,
318
+ n_rf_channels=self._N_RF_CHANNELS,
319
+ n_organic_media_channels=self._N_ORGANIC_MEDIA_CHANNELS,
320
+ n_organic_rf_channels=self._N_ORGANIC_RF_CHANNELS,
321
+ seed=0,
322
+ )
323
+ )
324
+ self.short_input_data_non_media_and_organic = (
325
+ test_utils.sample_input_data_non_revenue_revenue_per_kpi(
326
+ n_geos=self._N_GEOS,
327
+ n_times=self._N_TIMES_SHORT,
328
+ n_media_times=self._N_MEDIA_TIMES_SHORT,
329
+ n_controls=self._N_CONTROLS,
330
+ n_non_media_channels=self._N_NON_MEDIA_CHANNELS,
331
+ n_media_channels=self._N_MEDIA_CHANNELS,
332
+ n_rf_channels=self._N_RF_CHANNELS,
333
+ n_organic_media_channels=self._N_ORGANIC_MEDIA_CHANNELS,
334
+ n_organic_rf_channels=self._N_ORGANIC_RF_CHANNELS,
335
+ seed=0,
336
+ )
337
+ )
338
+ self.short_input_data_non_media = (
339
+ test_utils.sample_input_data_non_revenue_revenue_per_kpi(
340
+ n_geos=self._N_GEOS,
341
+ n_times=self._N_TIMES_SHORT,
342
+ n_media_times=self._N_MEDIA_TIMES_SHORT,
343
+ n_controls=self._N_CONTROLS,
344
+ n_non_media_channels=self._N_NON_MEDIA_CHANNELS,
345
+ n_media_channels=self._N_MEDIA_CHANNELS,
346
+ n_rf_channels=self._N_RF_CHANNELS,
347
+ n_organic_media_channels=0,
348
+ n_organic_rf_channels=0,
349
+ seed=0,
350
+ )
351
+ )