GammaGammaModel#

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

Gamma-Gamma Model for expected future monetary value.

The Gamma-Gamma model assumes expected future spend follows a Gamma distribution, and the scale of this distribution is also Gamma-distributed.

This model is conditioned on the mean value of repeat transactions for each customer, and is based on [1], [2]. Data must be summarized by frequency and monetary_value for each customer, using clv.rfm_summary() or equivalent.

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

Parameters:
dataDataFrame

DataFrame containing the following columns:

  • customer_id: Unique customer identifier

  • monetary_value: Mean transaction value of repeat purchases for each customer

  • frequency: Number of repeat transactions observed for each customer

model_configdict, optional

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

sampler_configdict, optional

Dictionary of sampler parameters. Defaults to None.

References

[1]

Fader, P. S., & Hardie, B. G. (2013). “The Gamma-Gamma model of monetary value”. https://www.brucehardie.com/notes/025/gamma_gamma.pdf

[2]

Peter S. Fader, Bruce G. S. Hardie, and Ka Lok Lee (2005), “RFM and CLV: Using iso-value curves for customer base analysis”, Journal of Marketing Research, 42 (November), 415-430. https://journals.sagepub.com/doi/pdf/10.1509/jmkr.2005.42.4.415

Examples

import pymc as pm
from pymc_marketing.clv import GammaGammaModel

model = GammaGammaModel(
    data=pandas.DataFrame({
        "customer_id": [0, 1, 2, 3, ...],
        "monetary_value" :[23.5, 19.3, 11.2, 100.5, ...],
        "frequency": [6, 8, 2, 1, ...],
    }),
    model_config={
        "p": {'dist': 'HalfNormal', kwargs: {}},
        "q": {'dist': 'HalfStudentT', kwargs: {"nu": 4, "sigma": 10}},
        "v": {'dist': 'HalfCauchy', kwargs: {"beta":1}},
    },
    sampler_config={
        "draws": 1000,
        "tune": 1000,
        "chains": 2,
        "cores": 2,
        "nuts_kwargs": {"target_accept": 0.95},
    },
)

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(
    data=pandas.DataFrame(
        {
        "customer_id": [0, 1, 2, 3, ...],
        "monetary_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"))

Methods

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

Initialize model configuration and sampler configuration for the model.

GammaGammaModel.attrs_to_init_kwargs(attrs)

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

GammaGammaModel.build_from_idata(idata)

Build model from the InferenceData object.

GammaGammaModel.build_model()

Build the model.

GammaGammaModel.create_fit_data(X, y)

Create the fit_data group based on the input data.

GammaGammaModel.create_idata_attrs()

Create attributes for the inference data.

GammaGammaModel.distribution_customer_spend(data)

Posterior distribution of mean spend values for each customer.

GammaGammaModel.distribution_new_customer_spend([...])

Posterior distribution of mean spend values for new customers.

GammaGammaModel.expected_customer_lifetime_value(...)

Compute the average lifetime value for a group of one or more customers.

GammaGammaModel.expected_customer_spend(data)

Compute the expected future mean spend value per customer.

GammaGammaModel.expected_new_customer_spend()

Compute the expected mean spend value for a new customer.

GammaGammaModel.fit([method, fit_method])

Infer model posterior.

GammaGammaModel.fit_summary(**kwargs)

Compute the summary of the fit result.

GammaGammaModel.graphviz(**kwargs)

Get the graphviz representation of the model.

GammaGammaModel.load(fname)

Create a ModelBuilder instance from a file.

GammaGammaModel.load_from_idata(idata)

Create a ModelBuilder instance from an InferenceData object.

GammaGammaModel.post_sample_model_transformation()

Perform transformation on the model after sampling.

GammaGammaModel.predict([X, extend_idata])

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

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

Generate posterior predictive samples on unseen data.

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

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

GammaGammaModel.sample_posterior_predictive([...])

Sample from the model's posterior predictive distribution.

GammaGammaModel.sample_prior_predictive([X, ...])

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.thin_fit_result(keep_every)

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

Attributes

X

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.

output_var

Output variable of the model.

posterior

posterior_predictive

predictions

prior

prior_predictive

version

y