ShiftedBetaGeoModelIndividual#

class pymc_marketing.clv.models.shifted_beta_geo.ShiftedBetaGeoModelIndividual(data=None, *, model_config=None, sampler_config=None)[source]#

Shifted Beta Geometric model for individual customers.

Model for customer behavior in a discrete contractual setting. It assumes that:

  • At the end of each period, a customer has a probability theta of renewing the contract and 1 - theta of cancelling

  • The probability theta does not change over time for a given customer

  • The probability theta varies across customers according to a Beta prior distribution with hyperparameters alpha and beta.

Based on [1].

Parameters:
data: pd.DataFrame

DataFrame containing the following columns:

  • customer_id: Customer labels. There should be one unique label for each customer.

  • t_churn: Time at which the customer cancelled the contract (starting at 0). It should equal T for users that have not cancelled by the end of the observation period.

  • T: Maximum observed time period (starting at 0).

model_config: dict, optional

Dictionary of model prior parameters. If not provided, the model will use default priors specified in the default_model_config class attribute.

sampler_config: dict, optional

Dictionary of sampler parameters. Defaults to None.

References

[1]

Fader, P. S., & Hardie, B. G. (2007). How to project customer retention. Journal of Interactive Marketing, 21(1), 76-90. https://journals.sagepub.com/doi/pdf/10.1002/dir.20074

Examples

import pandas as pd
import pymc as pm

from pymc_extras.prior import Prior
from pymc_marketing.clv import ShiftedBetaGeoModelIndividual

data = pd.DataFrame({
        customer_id=[0, 1, 2, 3, ...],
        t_churn=[1, 2, 8, 4, 8 ...],
        T=[8 for x in range(len(customer_id))],
    })

model = ShiftedBetaGeoModelIndividual(
    model_config={
        "alpha": Prior("HalfNormal", sigma=10),
        "beta": Prior("HalfStudentT", nu=4, sigma=10),
    },
    sampler_config={
        "draws": 1000,
        "tune": 1000,
        "chains": 2,
        "cores": 2,
        "nuts_kwargs": {"target_accept": 0.95},
    },
)

model.fit(data=data)
print(model.fit_summary())

# Predict how many periods in the future are existing customers
likely to cancel (ignoring that some may already have cancelled)
expected_churn_time = model.distribution_customer_churn_time(
    customer_id=[0, 1, 2, 3, ...],
)
print(expected_churn_time.mean("customer_id"))

# Predict churn time for 10 new customers, conditioned on data
new_customers_churn_time = model.distribution_new_customer_churn_time(n=10)
print(new_customers_churn_time.mean("new_customer_id"))

Methods

ShiftedBetaGeoModelIndividual.__init__([...])

Initialize model configuration and sampler configuration for the model.

ShiftedBetaGeoModelIndividual.attrs_to_init_kwargs(attrs)

Convert the model configuration and sampler configuration from the attributes to keyword arguments.

ShiftedBetaGeoModelIndividual.build_from_idata(idata)

Build the model from the InferenceData object.

ShiftedBetaGeoModelIndividual.build_model([data])

Build the model.

ShiftedBetaGeoModelIndividual.create_idata_attrs()

Create attributes for the inference data.

ShiftedBetaGeoModelIndividual.distribution_customer_churn_time(...)

Sample distribution of churn time for existing customers.

ShiftedBetaGeoModelIndividual.distribution_new_customer_churn_time([...])

Sample distribution of churn time for new customers.

ShiftedBetaGeoModelIndividual.distribution_new_customer_theta([...])

Sample distribution of theta parameter for new customers.

ShiftedBetaGeoModelIndividual.fit([data, ...])

Infer model posterior.

ShiftedBetaGeoModelIndividual.fit_summary(...)

Compute the summary of the fit result.

ShiftedBetaGeoModelIndividual.graphviz(**kwargs)

Get the graphviz representation of the model.

ShiftedBetaGeoModelIndividual.idata_to_init_kwargs(idata)

Create the initialization kwargs from an InferenceData object.

ShiftedBetaGeoModelIndividual.load(fname[, ...])

Create a ModelBuilder instance from a file.

ShiftedBetaGeoModelIndividual.load_from_idata(idata)

Create a ModelBuilder instance from an InferenceData object.

ShiftedBetaGeoModelIndividual.save(fname, ...)

Save the model's inference data to a file.

ShiftedBetaGeoModelIndividual.set_idata_attrs([idata])

Set attributes on an InferenceData object.

ShiftedBetaGeoModelIndividual.table(...)

Get the summary table of the model.

ShiftedBetaGeoModelIndividual.thin_fit_result(...)

Return a copy of the model with a thinned fit result.

Attributes

default_model_config

Default model configuration.

default_sampler_config

Default sampler configuration.

fit_result

Get the posterior fit_result.

id

Generate a unique hash value for the model.

posterior

Access the 'posterior' attribute of the InferenceData object.

posterior_predictive

Access the 'posterior_predictive' attribute of the InferenceData object.

predictions

Access the 'predictions' attribute of the InferenceData object.

prior

Access the 'prior' attribute of the InferenceData object.

prior_predictive

Access the 'prior_predictive' attribute of the InferenceData object.

version

idata

sampler_config

model_config