Model context for Meridian.
meridian.model.context.ModelContext(
input_data: data.InputData, model_spec: spec.ModelSpec
)
This class contains all model parameters that do not change between the runs
of Meridian.
Attributes |
adstock_decay_spec
|
Returns AdstockDecaySpec object with correctly mapped channels.
|
baseline_geo_idx
|
Returns the index of the baseline geo.
|
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.
|
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
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
View source
create_inference_data_dims() -> Mapping[str, Sequence[str]]
Creates data dimensions for inference data.
expand_selected_time_dims
View source
expand_selected_time_dims(
start_date: tc.Date = None, end_date: tc.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.
| 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
get_channel_parameter_tensor(
dist_tensors: Any, *, param_base_name: str, channel_name: str
) -> backend.Tensor
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
get_channel_parameters(
channel_name: str
) -> ChannelParameters
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.
|
View source
get_media_scaling_factor(
channel_name: str
) -> backend.Tensor
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
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.