ShiftedBetaGeoModelIndividual#

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

Shifted Beta Geometric model

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.

Examples

import pymc as pm
from pymc_marketing.clv import ShiftedBetaGeoModelIndividual

model = 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_config={
        "alpha_prior": {"dist": "HalfNormal", "kwargs": {"sigma": 10}},
        "beta_prior": {"dist": "HalfStudentT", "kwargs": {"nu": 4, "sigma": 10}},
    },
    sampler_config={
        "draws": 1000,
        "tune": 1000,
        "chains": 2,
        "cores": 2,
        "nuts_kwargs": {"target_accept": 0.95},
    },
)

model.fit()
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"))

References

Methods

ShiftedBetaGeoModelIndividual.__init__(data)

Initializes model configuration and sampler configuration for the model

ShiftedBetaGeoModelIndividual.build_model()

Creates an instance of pm.Model based on provided data and model_config, and attaches it to self.

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([fit_method])

Infer model posterior

ShiftedBetaGeoModelIndividual.fit_summary(...)

ShiftedBetaGeoModelIndividual.get_params([deep])

Get all the model parameters needed to instantiate a copy of the model, not including training data.

ShiftedBetaGeoModelIndividual.load(fname)

Creates a ModelBuilder instance from a file, Loads inference data for the model.

ShiftedBetaGeoModelIndividual.predict(X_pred)

Uses model to predict on unseen data and return point prediction of all the samples.

ShiftedBetaGeoModelIndividual.predict_posterior(X_pred)

Generate posterior predictive samples on unseen data.

ShiftedBetaGeoModelIndividual.predict_proba(X_pred)

Alias for predict_posterior, for consistency with scikit-learn probabilistic estimators.

ShiftedBetaGeoModelIndividual.sample_posterior_predictive(X_pred)

Sample from the model's posterior predictive distribution.

ShiftedBetaGeoModelIndividual.sample_prior_predictive(X_pred)

Sample from the model's prior predictive distribution.

ShiftedBetaGeoModelIndividual.save(fname)

Save the model's inference data to a file.

ShiftedBetaGeoModelIndividual.set_idata_attrs([idata])

Set attributes on an InferenceData object.

ShiftedBetaGeoModelIndividual.set_params(...)

Set all the model parameters needed to instantiate the model, not including training data.

ShiftedBetaGeoModelIndividual.thin_fit_result(...)

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

Attributes

X

default_model_config

Returns a class default config dict for model builder if no model_config is provided on class initialization Useful for understanding structure of required model_config to allow its customization by users .

default_sampler_config

Returns a class default sampler dict for model builder if no sampler_config is provided on class initialization Useful for understanding structure of required sampler_config to allow its customization by users .

fit_result

id

Generate a unique hash value for the model.

output_var

Returns the name of the output variable of the model.

version

y