autolog#

pymc_marketing.mlflow.autolog(log_sampler_info=True, log_metadata_info=True, log_model_info=True, sample_error_file='sample-error.txt', summary_var_names=None, arviz_summary_kwargs=None, log_mmm=True, log_clv=True, disable=False, silent=False)[source]#

Autologging support for PyMC models and PyMC-Marketing models.

Includes logging of sampler diagnostics, model information, data used in the model, and InferenceData objects upon sampling the models.

For more information about MLflow, see https://mlflow.org/docs/latest/python_api/mlflow.html

Parameters:
log_sampler_infobool, optional

Whether to log sampler diagnostics. Default is True.

log_metadata_infobool, optional

Whether to log the metadata of inputs used in the model. Default is True.

log_model_infobool, optional

Whether to log model information. Default is True.

sample_error_filestr, optional

The name of the file to log the error if an error occurs during sampling. If None, the error will not be logged. Default is “sample-error.txt”.

summary_var_nameslist[str], optional

The names of the variables to include in the ArviZ summary. Default is all the variables in the InferenceData object.

arviz_summary_kwargsdict, optional

Additional keyword arguments to pass to az.summary.

log_mmmbool, optional

Whether to log PyMC-Marketing MMM models. Default is True.

log_clvbool, optional

Whether to log PyMC-Marketing CLV models. Default is True.

disablebool, optional

Whether to disable autologging. Default is False.

silentbool, optional

Whether to suppress all warnings. Default is False.

Examples

Autologging for a PyMC model:

import mlflow

import pymc as pm

import pymc_marketing.mlflow

pymc_marketing.mlflow.autolog()

# Usual PyMC model code
with pm.Model() as model:
    mu = pm.Normal("mu", mu=0, sigma=1)
    obs = pm.Normal("obs", mu=mu, sigma=1, observed=[1, 2, 3])

# Incorporate into MLflow workflow
mlflow.set_experiment("PyMC Experiment")

with mlflow.start_run():
    idata = pm.sample(model=model)

Autologging for a PyMC-Marketing MMM:

import pandas as pd

import mlflow

from pymc_marketing.mmm import (
    GeometricAdstock,
    LogisticSaturation,
    MMM,
)
import pymc_marketing.mlflow

pymc_marketing.mlflow.autolog(log_mmm=True)

# Usual PyMC-Marketing model code

data_url = "https://raw.githubusercontent.com/pymc-labs/pymc-marketing/main/data/mmm_example.csv"
data = pd.read_csv(data_url, parse_dates=["date_week"])

X = data.drop("y",axis=1)
y = data["y"]

mmm = MMM(
    adstock=GeometricAdstock(l_max=8),
    saturation=LogisticSaturation(),
    date_column="date_week",
    channel_columns=["x1", "x2"],
    control_columns=[
        "event_1",
        "event_2",
        "t",
    ],
    yearly_seasonality=2,
)

# Incorporate into MLflow workflow

mlflow.set_experiment("MMM Experiment")

with mlflow.start_run():
    idata = mmm.fit(X, y)
    posterior_preds = mmm.sample_posterior_predictive(X)

    # Additional specific logging
    fig = mmm.plot_components_contributions()
    mlflow.log_figure(fig, "components.png")

Autologging for a PyMC-Marketing CLV model:

import pandas as pd

import mlflow

from pymc_marketing.clv import BetaGeoModel

import pymc_marketing.mlflow

pymc_marketing.mlflow.autolog(log_clv=True)

mlflow.set_experiment("CLV Experiment")

data_url = "https://raw.githubusercontent.com/pymc-labs/pymc-marketing/main/data/clv_quickstart.csv"
data = pd.read_csv(data_url)
data["customer_id"] = data.index

model = BetaGeoModel(data=data)

with mlflow.start_run():
    model.fit()

with mlflow.start_run():
    model.fit(fit_method="map")