add_saturation_observations#

pymc_marketing.mmm.lift_test.add_saturation_observations(df_lift_test, variable_mapping, saturation_function, model=None, likelihood=None, name='lift_measurements', get_indices=<function exact_row_indices>, *, dist=None)[source]#

Add saturation observations to the likelihood of the model.

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

Not to be used directly for general use. Use MMM.add_lift_test_measurements() or add_lift_measurements_to_likelihood_from_saturation() instead.

Parameters:
  • df_lift_test (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.

    Measurement columns must contain finite real numeric values, and sigma must be strictly positive. Invalid measurements raise ValueError before calibration variables are added. 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[Concatenate[XTensorVariable, P], XTensorVariable]) – Function that takes spend and returns saturation.

  • model (Model | None) – PyMC model with arbitrary number of coordinates, by default None

  • likelihood (Prior | type[DimDistribution] | None) – Serializable likelihood prior, by default Prior("Normal"). Its sigma parameter is set from the lift-test standard errors. Choose a distribution that matches the estimator’s sampling model. The supported distributions are Normal, StudentT, and Gamma. StudentT’s nu is the only parameter accepted, since the data and the model decide the others; dims and centered=False are refused too, and nu must be positive (a non-positive constant or a prior that can go negative is refused). A transform only renames the observed variable to <name>_raw; the observed density is the same.

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

  • get_indices (Callable[[DataFrame, Model], dict[str, Sequence[int] | NDArray[integer]]]) – Function to get the indices of the DataFrame in the model, by default exact_row_indices which assumes that the columns map exactly to the model coordinates.

Examples

Add lift tests for time-varying saturation to a model:

import pymc as pm
import pymc.dims as pmd
import pandas as pd
from pymc_marketing.mmm.lift_test import add_saturation_observations

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)

# These are required since alpha and lam
# have both channel and date dimensions
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:
    # Usually defined in a larger model.
    # Distributions don't matter here, just the shape
    alpha = pmd.HalfNormal("alpha_in_model", dims=("channel", "date"))
    lam = pmd.HalfNormal("lam_in_model", dims="channel")

    add_saturation_observations(
        df_lift_test,
        variable_mapping={
            "alpha": "alpha_in_model",
            "lam": "lam_in_model",
        },
        saturation_function=saturation_function,
    )

Use the saturation classes to add lift tests to a model. NOTE: This is what happens internally of MMM.

import pymc as pm
import pymc.dims as pmd
import pandas as pd

from pymc_marketing.mmm import LogisticSaturation
from pymc_marketing.mmm.lift_test import add_saturation_observations

saturation = LogisticSaturation()

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],
    }
)

df_lift_test = df_base_lift_test.assign(
    channel="channel_1",
)

coords = {
    "channel": ["channel_1", "channel_2"],
}
with pm.Model(coords=coords) as model:
    # Usually defined in a larger model.
    # Distributions dont matter here, just the shape
    lam = pmd.HalfNormal("saturation_lam", dims="channel")
    beta = pmd.HalfNormal("saturation_beta", dims="channel")

    add_saturation_observations(
        df_lift_test,
        variable_mapping=saturation.variable_mapping,
        saturation_function=saturation.function,
    )

Add lift tests for channel, geo saturation functions.

import pymc as pm
import pymc.dims as pmd
import pandas as pd

from pymc_marketing.mmm import LogisticSaturation
from pymc_marketing.mmm.lift_test import add_saturation_observations

saturation = LogisticSaturation()

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],
    }
)

df_lift_test = df_base_lift_test.assign(
    channel="channel_1",
    geo=["G1", "G2", "G2"],
)

coords = {
    "channel": ["channel_1", "channel_2"],
    "geo": ["G1", "G2", "G3"],
}
with pm.Model(coords=coords) as model:
    # Usually defined in a larger model.
    # Distributions dont matter here, just the shape
    lam = pmd.HalfNormal("saturation_lam", dims=("channel", "geo"))
    beta = pmd.HalfNormal("saturation_beta", dims=("channel", "geo"))

    add_saturation_observations(
        df_lift_test,
        variable_mapping=saturation.variable_mapping,
        saturation_function=saturation.function,
    )