MMMPlotSuite.allocated_contribution_by_channel_over_time#

MMMPlotSuite.allocated_contribution_by_channel_over_time(samples, hdi_prob=0.94, dims=None, split_by=None, original_scale=True, scale_factor=None, figsize=None, subplot_kwargs=None, **kwargs)[source]#

Plot the allocated contribution by channel with uncertainty intervals.

This function visualizes the mean allocated contributions by channel along with HDI (Highest Density Interval) uncertainty bands. Supports dimension filtering via dims and creating separate subplots via split_by.

Parameters:
samplesxr.Dataset or az.InferenceData

The dataset containing the samples of channel contributions. Can be an xr.Dataset with ‘sample’ dimension, or az.InferenceData (e.g., from sample_response_distribution) with ‘chain’ and ‘draw’ dims.

hdi_probfloat, default 0.94

The probability mass for the HDI interval.

dimsdict[str, str | int | list], optional

Dimension filters to apply. Example: {“geo”: “US”} to filter to a single geo, or {“geo”: [“US”, “UK”]} to include multiple values.

split_bystr or list of str, optional

Dimension(s) to create separate subplots for. Each unique combination of values in these dimensions will get its own subplot. If None, auto-detects extra dimensions beyond ‘channel’, ‘date’, ‘sample’.

original_scalebool, default True

If True, prefer ‘channel_contribution_original_scale’ variable if available.

scale_factorfloat, optional

Scale factor to apply to the contributions.

figsizetuple[float, float], optional

The size of the figure. Default is (10, 6) for single panel, scaled automatically for multiple panels.

subplot_kwargsdict, optional

Additional keyword arguments for subplot layout. Can include: - ‘nrows’: Number of rows in subplot grid - ‘ncols’: Number of columns in subplot grid Only one of ‘nrows’ or ‘ncols’ can be specified when using split_by.

**kwargs

Additional keyword arguments passed to plt.subplots().

Returns:
figmatplotlib.figure.Figure

The Figure object containing the plot.

axesmatplotlib.axes.Axes or numpy.ndarray of matplotlib.axes.Axes

The Axes object with the plot for single panel, or array of Axes for multiple subplots.

Examples

Basic usage with optimization samples:

>>> allocation, _ = mmm.optimize_budget(budget=100_000, num_periods=52)
>>> samples = mmm.sample_response_distribution(
...     allocation_strategy=allocation,
...     time_granularity="weekly",
...     num_periods=52,
...     noise_level=0.1,
... )
>>> fig, ax = mmm.plot.allocated_contribution_by_channel_over_time(samples)

Filter to a specific dimension value:

>>> fig, ax = mmm.plot.allocated_contribution_by_channel_over_time(
...     samples, dims={"geo": "US"}
... )

Create subplots split by dimension:

>>> fig, axes = mmm.plot.allocated_contribution_by_channel_over_time(
...     samples, split_by="geo"
... )