GammaGammaModel#

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

Gamma-Gamma model

Estimate the average monetary value of customer transactions.

The model is conditioned on the mean transaction value of each user, and is based on [1], [2].

TODO: Explain assumptions of model

Check GammaGammaModelIndividual for an equivalent model conditioned on individual transaction values.

Parameters:
  • data (pd.DataFrame) –

    DataFrame containing the following columns:
    • customer_id: Customer labels. Must not repeat.

    • mean_transaction_value: Mean transaction value of each customer.

    • frequency: Number of transactions observed for each customer.

  • 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

Gamma-Gamma model condioned on mean transaction value

import pymc as pm
from pymc_marketing.clv import GammaGammaModel

model = GammaGammaModel(
    data=pd.DataFrame({
        "customer_id": [0, 1, 2, 3, ...],
        "mean_transaction_value" :[23.5, 19.3, 11.2, 100.5, ...],
        "frequency": [6, 8, 2, 1, ...],
    }),
    model_config={
        "p_prior": {'dist': 'HalfNormal', kwargs: {}},
        "q_prior": {'dist': 'HalfStudentT', kwargs: {"nu": 4, "sigma": 10}},
        "v_prior": {'dist': 'HalfCauchy', kwargs: {"beta":1}},
    },
    sampler_config={
        "draws": 1000,
        "tune": 1000,
        "chains": 2,
        "cores": 2,
        "nuts_kwargs": {"target_accept": 0.95},
    },
)
model.build_model()
model.fit()
print(model.fit_summary())

# Predict spend of customers for which we know transaction history, conditioned on data.
expected_customer_spend = model.expected_customer_spend(
    customer_id=[0, 1, 2, 3, ...],
    mean_transaction_value=[23.5, 19.3, 11.2, 100.5, ...],
    frequency=[6, 8, 2, 1, ...],
)
print(expected_customer_spend.mean("customer_id"))

# Predict spend of 10 new customers, conditioned on data
new_customer_spend = model.expected_new_customer_spend(n=10)
print(new_customer_spend.mean("new_customer_id"))

References

Methods

GammaGammaModel.__init__(data[, ...])

Initializes model configuration and sampler configuration for the model

GammaGammaModel.build_model()

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

GammaGammaModel.distribution_customer_spend(...)

Posterior distribution of transaction value per customer

GammaGammaModel.distribution_new_customer_spend([...])

Posterior distribution of transaction value for new customers

GammaGammaModel.expected_customer_lifetime_value(...)

Expected customer lifetime value.

GammaGammaModel.expected_customer_spend(...)

Expected transaction value per customer

GammaGammaModel.expected_new_customer_spend()

Expected transaction value for a new customer

GammaGammaModel.fit([fit_method])

Infer model posterior

GammaGammaModel.fit_summary(**kwargs)

GammaGammaModel.get_params([deep])

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

GammaGammaModel.load(fname)

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

GammaGammaModel.predict(X_pred[, extend_idata])

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

GammaGammaModel.predict_posterior(X_pred[, ...])

Generate posterior predictive samples on unseen data.

GammaGammaModel.predict_proba(X_pred[, ...])

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

GammaGammaModel.sample_posterior_predictive(X_pred)

Sample from the model's posterior predictive distribution.

GammaGammaModel.sample_prior_predictive(X_pred)

Sample from the model's prior predictive distribution.

GammaGammaModel.save(fname)

Save the model's inference data to a file.

GammaGammaModel.set_idata_attrs([idata])

Set attributes on an InferenceData object.

GammaGammaModel.set_params(**params)

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

GammaGammaModel.thin_fit_result(keep_every)

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