add_lift_measurements_to_likelihood#

pymc_marketing.mmm.lift_test.add_lift_measurements_to_likelihood(df_lift_test, variable_mapping, saturation_function, model=None, dist=<class 'pymc.distributions.continuous.Gamma'>, name='lift_measurements')[source]#

Add lift measurements to the likelihood of the model.

General function to add lift measurements to the likelihood of the model.

Parameters:
  • df_lift_test (pd.DataFrame) –

    DataFrame with lift test results with at least the following columns:
    • x: x axis value of the lift test.

    • delta_x: change in x axis value of the lift test.

    • delta_y: change in y axis value of the lift test.

    • sigma: standard deviation of the lift test.

    Any additional columns are assumed to be coordinates in the model.

  • variable_mapping (dict[str, str]) – Dictionary of variable names to dimensions.

  • saturation_function (Callable[[np.ndarray], np.ndarray]) – Function that takes spend and returns saturation.

  • model (Optional[pm.Model], optional) – PyMC model with arbitrary number of coordinates, by default None

  • dist (pm.Distribution, optional) – PyMC distribution to use for the likelihood, by default pm.Gamma

  • name (str, optional) – Name of the likelihood, by default “lift_measurements”

Return type:

None

Examples

Add an arbitrary lift test to a model:

import pymc as pm
import pandas as pd
from pymc_marketing.mmm.lift_test import add_lift_measurements_to_likelihood

df_base_lift_test = pd.DataFrame({
    "x": [1, 2, 3],
    "delta_x": [1, 2, 3],
    "delta_y": [1, 2, 3],
    "sigma": [0.1, 0.2, 0.3],
})

def saturation_function(x, alpha, lam):
    return alpha * x / (x + lam)

df_lift_test = df_base_lift_test.assign(
    channel="channel_1",
    date=["2019-01-01", "2019-01-02", "2019-01-03"],
)

coords = {
    "channel": ["channel_1", "channel_2"],
    "date": ["2019-01-01", "2019-01-02", "2019-01-03", "2019-01-04"],
}
with pm.Model(coords=coords) as model:
    alpha = pm.HalfNormal("alpha_in_model", dims=("channel", "date"))
    lam = pm.HalfNormal("lam_in_model", dims="channel")

    add_lift_measurements_to_likelihood(
        df_lift_test,
        {"alpha": "alpha_in_model", "lam": "lam_in_model"},
        saturation_function,
        model=model,
    )