MMMPlotSuite.contributions_over_time#

MMMPlotSuite.contributions_over_time(var, data=None, hdi_prob=0.85, dims=None, backend=None)[source]#

Plot time-series contributions for specified variables.

Visualizes how variables contribute over time, showing the median line and HDI bands. Useful for understanding channel contributions, intercepts, or other time-varying effects in your model.

Parameters:
varlist of str

Variable names to plot from the posterior group. Must have a “date” dimension. Examples: [“channel_contribution”], [“intercept”], [“channel_contribution”, “intercept”].

dataxr.Dataset, optional

Dataset containing posterior data with variables in var. If None, uses self.idata.posterior. This parameter allows: - Testing with mock data without modifying self.idata - Plotting external results not stored in self.idata - Comparing different posterior samples side-by-side - Avoiding unintended side effects on self.idata

hdi_probfloat, default 0.85

Probability mass for HDI interval (between 0 and 1).

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

Dimension filters to apply. Keys are dimension names, values are either: - Single value: {“country”: “US”, “user_type”: “new”} - List of values: {“country”: [“US”, “UK”]}

If provided, only the selected slice(s) will be plotted.

backendstr, optional

Plotting backend to use. Options: “matplotlib”, “plotly”, “bokeh”. If None, uses global config via mmm_plot_config[“plot.backend”]. Default is “matplotlib”.

Returns:
PlotCollection

arviz_plots PlotCollection object containing the plot.

Use .show() to display or .save("filename") to save. Unlike the legacy suite which returned (Figure, Axes), this provides a unified interface across all backends.

Raises:
ValueError

If hdi_prob is not between 0 and 1.

ValueError

If no posterior data found in self.idata and no data argument provided.

ValueError

If any variable in var not found in data.

See also

LegacyMMMPlotSuite.contributions_over_time

Legacy matplotlib-only implementation

Notes

Breaking changes from legacy implementation:

  • Returns PlotCollection instead of (Figure, Axes)

  • Variable names must be passed in a list (was already list in legacy)

Examples

Basic usage - plot channel contributions:

mmm.fit(X, y)
pc = mmm.plot.contributions_over_time(var=["channel_contribution"])
pc.show()

Plot multiple variables together:

pc = mmm.plot.contributions_over_time(
    var=["channel_contribution", "intercept"]
)
pc.show()

Filter by dimension:

pc = mmm.plot.contributions_over_time(
    var=["channel_contribution"], dims={"geo": "US"}
)
pc.show()

Filter with multiple dimension values:

pc = mmm.plot.contributions_over_time(
    var=["channel_contribution"], dims={"geo": ["US", "UK"]}
)
pc.show()

Use different backend:

pc = mmm.plot.contributions_over_time(
    var=["channel_contribution"], backend="plotly"
)
pc.show()

Provide explicit data (option 1 - via data parameter):

custom_posterior = xr.Dataset(...)
pc = mmm.plot.contributions_over_time(
    var=["my_contribution"], data=custom_posterior
)
pc.show()

Provide explicit data (option 2 - direct instantiation):

from pymc_marketing.mmm.plot import MMMPlotSuite

mps = MMMPlotSuite(custom_idata)
pc = mps.contributions_over_time(var=["my_contribution"])
pc.show()