View source on GitHub
|
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. |
compiled_control_population_scaling_id
|
The effective population-scaling selection for control variables.
Resolved from the declarative |
compiled_holdout_id
|
The effective holdout mask.
Resolved from the declarative For a declarative holdout, a |
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
|
compiled_rf_roi_calibration_period
|
The effective ROI calibration period for reach & frequency channels.
Resolved from the declarative |
compiled_roi_calibration_period
|
The effective ROI calibration period for media channels.
Resolved from the declarative |
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
|
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 There is a draw to record only when the random specification is the one
that governed. If the deprecated |
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
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]]
Creates data dimensions for inference data.
expand_selected_time_dims
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
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
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. |
get_media_scaling_factor
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
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.
resolve_non_media_baseline_values
resolve_non_media_baseline_values(
values: (Mapping[str, float | str] | Sequence[float | str] | None)
) -> (list[float | str] | None)
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. |
View source on GitHub