meridian.model.context.ModelContext

Model context for Meridian.

This class contains all model parameters that do not change between the runs of Meridian.

adstock_decay_spec Returns AdstockDecaySpec object with correctly mapped channels.
baseline_geo_idx Returns the index of the baseline geo.
compiled_control_population_scaling_id The effective population-scaling selection for control variables.

Resolved from the declarative ModelSpec.population_scaled_controls against the input data's control variable coordinates, or taken as-is from the deprecated ModelSpec.control_population_scaling_id.

compiled_holdout_id The effective holdout mask.

Resolved from the declarative ModelSpec.holdout against the input data's time and geo coordinates, or taken as-is from the deprecated ModelSpec.holdout_id.

For a declarative holdout, a resolved draw always wins and is never re-drawn; see spec.RandomHoldoutSpec for why a seed alone cannot reproduce a draw. Only when a RandomHoldoutSpec carries no resolved draw is one made here, once, and memoized for the lifetime of this context.

compiled_non_media_baseline_values ModelSpec.non_media_baseline_values, in positional channel order.
compiled_non_media_population_scaling_id The effective population-scaling selection for non-media channels.

Resolved from the declarative ModelSpec.population_scaled_non_media_channels against the input data's non-media channel coordinates, or taken as-is from the deprecated ModelSpec.non_media_population_scaling_id.

compiled_rf_roi_calibration_period The effective ROI calibration period for reach & frequency channels.

Resolved from the declarative ModelSpec.rf_roi_calibration against the input data's media time and RF channel coordinates, or taken as-is from the deprecated ModelSpec.rf_roi_calibration_period.

compiled_roi_calibration_period The effective ROI calibration period for media channels.

Resolved from the declarative ModelSpec.roi_calibration against the input data's media time and media channel coordinates, or taken as-is from the deprecated ModelSpec.roi_calibration_period.

controls

controls_scaled

controls_transformer Returns a CenteringAndScalingTransformer for controls, if it exists.
holdout_id

input_data

is_national

knot_info

kpi

kpi_scaled

kpi_transformer

media_effects_dist

media_tensors

model_spec

n_controls

n_geos

n_media_channels

n_media_times

n_non_media_channels

n_organic_media_channels

n_organic_rf_channels

n_rf_channels

n_times

non_media_transformer Returns a CenteringAndScalingTransformer for non-media treatments.
non_media_treatments

non_media_treatments_normalized Normalized non-media treatments.

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.

organic_media_tensors

organic_rf_tensors

population

prior_broadcast Returns broadcasted PriorDistribution object.
resolved_random_holdout The draw to record in HoldoutSpec.resolved, or None if there is none.

A RandomHoldoutSpec does not determine a holdout by itself, so the draw the model actually used has to be recorded alongside it; see spec.RandomHoldoutSpec for why a seed alone cannot reproduce one. This inverts compiled_holdout_id back into the declarative form that HoldoutSpec.resolved holds.

There is a draw to record only when the random specification is the one that governed. If the deprecated holdout_id is also set it takes precedence and this returns None.

revenue_per_kpi

rf_tensors

saturation_spec The SaturationSpec object with correctly mapped channels.
total_outcome

total_spend

unique_sigma_for_each_geo

Methods

create_inference_data_coords

View source

Creates data coordinates for inference data.

create_inference_data_dims

View source

Creates data dimensions for inference data.

expand_selected_time_dims

View source

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.

Args
start_date Start date of the selected time period. If None, implies the earliest time dimension value in the input data.
end_date End date of the selected time period. If None, implies the latest time dimension value in the input data.

Returns
A list of time dimension values (as Meridian-formatted strings) in the input data within the selected time period, or do nothing and pass through None if both arguments are Nones, or if start_date and end_date correspond to the entire time range in the input data.

Raises
ValueError if start_date or end_date is not in the input data time dimensions.

get_channel_parameter_tensor

View source

Safely extracts a channel's parameter tensor (e.g., 'alpha', 'beta_g').

Args
dist_tensors An object containing batched distribution tensors (e.g., DistributionTensors).
param_base_name The base name of the parameter (e.g., 'alpha', 'ec', 'beta_g').
channel_name The name of the channel.

Returns
The sliced parameter tensor for the specific channel.

Raises
ValueError If the parameter or channel is not found.

get_channel_parameters

View source

Maps channel names to index, prefix, decay spec, and is_rf.

Args
channel_name Name of the channel.

Returns
A ChannelParameters object containing the channel's metadata.

Raises
ValueError If the channel is not found.

get_media_scaling_factor

View source

Retrieves the population-scaled median used to scale a channel's volume.

For Reach & Frequency (RF) channels, this returns the scaling factor applied to the 'reach' component, as 'frequency' is not transformed.

Args
channel_name The string name of the paid or organic channel.

Returns
A tensor of shape (n_geos,) representing the scaling factor.

Raises
ValueError If the channel is not found, or the transformer is uninitialized.

populate_cached_properties

View source

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.

resolve_non_media_baseline_values

View source

Resolves non-media baseline values into positional channel order.

A mapping only needs to name the channels whose baseline differs from the default; any channel it omits falls back to 'min'.

Args
values A mapping from non-media channel name to baseline value, a sequence already in channel order, or None.

Returns
A list of length n_non_media_channels in channel order, or None if values is None.

Raises
ValueError If a mapping key is not a known non-media channel.