Contains the main functionality for fitting the Meridian MMM model.
meridian
.
model
.
model
.
Meridian
(
input_data
:
meridian
.
data
.
input_data
.
InputData
,
model_spec
:
(
meridian
.
model
.
spec
.
ModelSpec
|
None
)
=
None
,
inference_data
:
(
az
.
InferenceData
|
None
)
=
None
)
Attributes
arviz.InferenceData
object containing the
resulting data from fitting the model.input_data.revenue_per_kpi
. If input_data.revenue_per_kpi
is None, then this is also None.media_tensors.media_spend
and rf_tensors.rf_spend
.ControlsTransformer
to scale controls tensors
using the model's controls data.CenteringAndScalingTransformer
to scale non-media
treatmenttensors using the model's non-media treatment data.The non-media treatments values are scaled by population (for channels where non_media_population_scaling_id
is True
) and normalized by centering and
scaling with means and standard deviations.
Methods
adstock_hill_media
adstock_hill_media
(
media
:
meridian
.
backend
.
Tensor
,
alpha
:
meridian
.
backend
.
Tensor
,
ec
:
meridian
.
backend
.
Tensor
,
slope
:
meridian
.
backend
.
Tensor
,
decay_functions
:
(
str
|
Sequence
[
str
])
=
constants
.
GEOMETRIC_DECAY
,
n_times_output
:
(
int
|
None
)
=
None
)
->
meridian
.
backend
.
Tensor
Transforms media or using Adstock and Hill functions in the desired order.
media
(n_geos, n_media_times, n_media_channels)
containing non-negative media execution values. Typically this is
impressions, but it can be any metric, such as media_spend
. Clicks are
often used for paid search ads.alpha
ec
slope
decay_functions
n_times_output
media
equals self.n_media_times
, in which case n_times_output
defaults to self.n_times
.
[..., n_geos, n_times, n_media_channels]
representing Adstock and Hill-transformed media. adstock_hill_rf
adstock_hill_rf
(
reach
:
meridian
.
backend
.
Tensor
,
frequency
:
meridian
.
backend
.
Tensor
,
alpha
:
meridian
.
backend
.
Tensor
,
ec
:
meridian
.
backend
.
Tensor
,
slope
:
meridian
.
backend
.
Tensor
,
decay_functions
:
(
str
|
Sequence
[
str
])
=
constants
.
GEOMETRIC_DECAY
,
n_times_output
:
(
int
|
None
)
=
None
)
->
meridian
.
backend
.
Tensor
Transforms reach and frequency (RF) using Hill and Adstock functions.
reach
(n_geos, n_media_times, n_rf_channels)
containing non-negative media for reach.frequency
(n_geos, n_media_times, n_rf_channels)
containing non-negative media for frequency.alpha
ec
slope
decay_functions
n_times_output
reach
equals self.n_media_times
, in which case n_times_output
defaults to self.n_times
.
[..., n_geos, n_times, n_rf_channels]
representing Hill and Adstock-transformed RF. calculate_beta_x
calculate_beta_x
(
is_non_media
:
bool
,
incremental_outcome_x
:
meridian
.
backend
.
Tensor
,
linear_predictor_counterfactual_difference
:
meridian
.
backend
.
Tensor
,
eta_x
:
meridian
.
backend
.
Tensor
,
beta_gx_dev
:
meridian
.
backend
.
Tensor
)
->
meridian
.
backend
.
Tensor
Calculates coefficient mean parameter for any treatment variable type.
The "beta_x" in the function name refers to the coefficient mean parameter of any treatment variable. The "x" can represent "m", "rf", "om", or "orf". This function can also be used to calculate "gamma_n" for any non-media treatments.
is_non_media
True
, then
random effects are assumed to be normal. Otherwise, the distribution is
inferred from self.media_effects_dist
.incremental_outcome_x
linear_predictor_counterfactual_difference
eta_x
xi_n
, which is analogous to
"eta".beta_gx_dev
gamma_gn_dev
, which is analogous to "beta_gx_dev".
compute_non_media_treatments_baseline
compute_non_media_treatments_baseline
(
non_media_baseline_values
:
(
Sequence
[
str
|
float
]
|
None
)
=
None
)
->
meridian
.
backend
.
Tensor
Computes the baseline for each non-media treatment channel.
non_media_baseline_values
(n_non_media_channels,)
. Each element is either a float (which means
that the fixed value will be used as baseline for the given channel) or
one of the strings "min" or "max" (which mean that the global minimum or
maximum value will be used as baseline for the values of the given
non_media treatment channel). If float values are provided, it is
expected that they are scaled by population for the channels where model_spec.non_media_population_scaling_id
is True
. If None
, the model_spec.non_media_baseline_values
is used, which defaults to the
minimum value for each non_media treatment channel.
(n_non_media_channels,)
containing the
baseline values for each non-media treatment channel. create_inference_data_coords
create_inference_data_coords
(
n_chains
:
int
,
n_draws
:
int
)
->
Mapping
[
str
,
np
.
ndarray
|
Sequence
[
str
]]
Creates data coordinates for inference data.
create_inference_data_dims
create_inference_data_dims
()
->
Mapping
[
str
,
Sequence
[
str
]]
expand_selected_time_dims
expand_selected_time_dims
(
start_date
:
meridian
.
data
.
time_coordinates
.
Date
=
None
,
end_date
:
meridian
.
data
.
time_coordinates
.
Date
=
None
)
->
(
list
[
str
]
|
None
)
Validates and returns time dimension values based on the selected times.
If both start_date
and end_date
are None, returns None. If specified,
both start_date
and end_date
are inclusive, and must be present in the
time coordinates of the input data.
start_date
end_date
start_date
and end_date
correspond to the entire time range in the input data.
start_date
or end_date
is not in the input data time
dimensions. linear_predictor_counterfactual_difference_media
linear_predictor_counterfactual_difference_media
(
media_transformed
:
meridian
.
backend
.
Tensor
,
alpha_m
:
meridian
.
backend
.
Tensor
,
ec_m
:
meridian
.
backend
.
Tensor
,
slope_m
:
meridian
.
backend
.
Tensor
)
->
meridian
.
backend
.
Tensor
Calculates linear predictor counterfactual difference for non-RF media.
For non-RF media variables (paid or organic), this function calculates the linear predictor difference between the treatment variable and its counterfactual. "Linear predictor" refers to the output of the hill/adstock function, which is multiplied by the geo-level coefficient.
This function does the calculation efficiently by only calculating calling the hill/adstock function if the prior counterfactual is not all zeros.
media_transformed
alpha_m
ec_m
slope_m
linear_predictor_counterfactual_difference_rf
linear_predictor_counterfactual_difference_rf
(
rf_transformed
:
meridian
.
backend
.
Tensor
,
alpha_rf
:
meridian
.
backend
.
Tensor
,
ec_rf
:
meridian
.
backend
.
Tensor
,
slope_rf
:
meridian
.
backend
.
Tensor
)
->
meridian
.
backend
.
Tensor
Calculates linear predictor counterfactual difference for RF media.
For RF media variables (paid or organic), this function calculates the linear predictor difference between the treatment variable and its counterfactual. "Linear predictor" refers to the output of the hill/adstock function, which is multiplied by the geo-level coefficient.
This function does the calculation efficiently by only calculating calling the hill/adstock function if the prior counterfactual is not all zeros.
rf_transformed
alpha_rf
ec_rf
slope_rf
populate_cached_properties
populate_cached_properties
()
Eagerly activates all cached properties.
This is useful for creating a tf.function
computation graph with this
Meridian object as part of a captured closure. Within the computation graph,
internal state mutations are problematic, and so this method freezes the
object's states before the computation graph is created.
sample_posterior
sample_posterior
(
n_chains
:
(
Sequence
[
int
]
|
int
),
n_adapt
:
int
,
n_burnin
:
int
,
n_keep
:
int
,
current_state
:
(
Mapping
[
str
,
backend
.
Tensor
]
|
None
)
=
None
,
init_step_size
:
(
int
|
None
)
=
None
,
dual_averaging_kwargs
:
(
Mapping
[
str
,
int
]
|
None
)
=
None
,
max_tree_depth
:
int
=
10
,
max_energy_diff
:
float
=
500.0
,
unrolled_leapfrog_steps
:
int
=
1
,
parallel_iterations
:
int
=
10
,
seed
:
(
Sequence
[
int
]
|
int
|
None
)
=
None
,
**
pins
)
Runs Markov Chain Monte Carlo (MCMC) sampling of posterior distributions.
For more information about the arguments, see windowed_adaptive_nuts
.
Drawn samples are merged into this model's Arviz inference_data
property.
n_chains
windowed_adaptive_nuts
will be called once for each element. The n_chains
argument of each windowed_adaptive_nuts
call will be equal
to the respective integer element. Using a list of integers, one can
split the chains of a windowed_adaptive_nuts
call into multiple calls
with fewer chains per call. This can reduce memory usage. This might
require an increased number of adaptation steps for convergence, as the
optimization is occurring across fewer chains per sampling call.n_adapt
n_burnin
n_keep
current_state
model.experimental_pin(**pins).sample(n_chains)
.init_step_size
current_state
. For example, if the initial state is: { 'a':
tf.zeros(n_chains), 'b': tf.zeros([n_chains, n_features]), }
then
any of 1.
, {'a': 1., 'b': 1.}
, or {'a': tf.ones(n_chains), 'b':
tf.ones([n_chains, n_features])}
will work. Defaults to the dimension
of the log density to the ¼ power.dual_averaging_kwargs
tfp.mcmc.DualAveragingStepSizeAdaptation
. By default, a target_accept_prob
of 0.85
is set, acceptance probabilities across
chains are reduced using a harmonic mean, and the class defaults are
used otherwise.max_tree_depth
2**max_tree_depth
, for
example, the number of nodes in a binary tree max_tree_depth
nodes
deep. The default setting of 10
takes up to 1024 leapfrog steps.max_energy_diff
1000
.unrolled_leapfrog_steps
max_tree_depth
. Defaults is 1
.parallel_iterations
tf.while_loop
.seed
int32[2]
Tensor or a Python list or tuple of 2 int
s, which
will be treated as stateless seeds; or a Python int
or None
, which
will be treated as stateful seeds. See tfp.random.sanitize_seed
.**pins
joint_dist.experimental_pin(**pins)
.
MCMCOOMError
n_keep
or pass
a list of integers as n_chains
to sample chains serially. For more
information, see ResourceExhaustedError when running Meridian.sample_posterior
. sample_prior
sample_prior
(
n_draws
:
int
,
seed
:
(
int
|
None
)
=
None
)
Draws samples from the prior distributions.
Drawn samples are merged into this model's Arviz inference_data
property.
n_draws
seed