LinearTrendEffect#
- class pymc_marketing.mmm.additive_effect.LinearTrendEffect(*, trend, prefix, date_dim_name='date', linear_trend_first_date=None, linear_trend_last_date=None)[source]#
Wrapper for LinearTrend to use with MMM’s MuEffect protocol.
This class adapts the LinearTrend component to be used as an additive effect in the MMM model.
- Parameters:
trend (
LinearTrend) – The LinearTrend instance to wrap.prefix (
str) – The prefix to use for variables in the model.date_dim_name (
str) – The name of the date dimension in the model.
Examples
Out of sample predictions:
Note
No new changepoints are used for the out of sample predictions. The trend effect is linearly extrapolated from the last changepoint.
import pandas as pd import numpy as np import matplotlib.pyplot as plt import pymc as pm import pymc.dims as pmd from pymc_marketing.mmm.linear_trend import LinearTrend from pymc_marketing.mmm.additive_effect import LinearTrendEffect seed = sum(map(ord, "LinearTrend out of sample")) rng = np.random.default_rng(seed) class MockMMM: pass dates = pd.date_range("2025-01-01", periods=52, freq="W") coords = {"date": dates} model = pm.Model(coords=coords) mock_mmm = MockMMM() mock_mmm.dims = () mock_mmm.model = model effect = LinearTrendEffect( trend=LinearTrend(n_changepoints=8), prefix="trend", ) with mock_mmm.model: effect.create_data(mock_mmm) pmd.Deterministic( "effect", effect.create_effect(mock_mmm), dims="date", ) idata = pm.sample_prior_predictive(random_seed=rng) idata["posterior"] = idata.prior n_new = 10 + 1 new_dates = pd.date_range( dates.max(), periods=n_new, freq="W", ) with mock_mmm.model: mock_mmm.model.set_dim("date", n_new, new_dates) effect.set_data(mock_mmm, mock_mmm.model, None) pm.sample_posterior_predictive( idata, var_names=["effect"], random_seed=rng, extend_inferencedata=True, ) draw = rng.choice(range(idata.posterior.sizes["draw"])) sel = dict(chain=0, draw=draw) before = idata.posterior["effect"].sel(sel).to_series() after = idata.posterior_predictive["effect"].sel(sel).to_series() ax = before.plot(color="C0") after.plot(color="C0", linestyle="dashed", ax=ax) plt.show()
(
Source code,png,hires.png,pdf)
Methods
LinearTrendEffect.__init__(**data)Create a new model by parsing and validating input data from keyword arguments.
LinearTrendEffect.check_lift_tests(mmm, ...)Warn if lift tests added to the MMM overlap data this effect uses.
Warn if this effect misreads the placeholder spend of budget scenarios.
LinearTrendEffect.construct([_fields_set])LinearTrendEffect.copy(*[, include, ...])Returns a copy of the model.
Create the required data in the model.
Create the trend effect in the model.
LinearTrendEffect.dict(*[, include, ...])Reconstruct from a dict, using registry for nested LinearTrend.
Return supplementary data groups to store in DataTree.
Opt this effect in to spend-counterfactual incrementality analysis.
LinearTrendEffect.json(*[, include, ...])Compute the class name for parametrizations of generic classes.
LinearTrendEffect.parse_file(path, *[, ...])LinearTrendEffect.parse_raw(b, *[, ...])LinearTrendEffect.schema([by_alias, ...])LinearTrendEffect.schema_json(*[, by_alias, ...])LinearTrendEffect.set_data(mmm, model, X)Set the data for new predictions.
LinearTrendEffect.to_dict([_orig])Serialize to a dict via Pydantic model_dump.
LinearTrendEffect.update_forward_refs(**localns)LinearTrendEffect.validate(value)Attributes
Name of the posterior deterministic holding this effect's contribution.
Names of deterministics to take from the posterior rather than recompute.
Configuration for the model, should be a dictionary conforming to [
ConfigDict][pydantic.config.ConfigDict].Whether the effect can be evaluated on dates other than the fitted ones.