1

The python package lifetimes is using BG/NBD method which is the same as the R package BTYD.

In the R paper, we could estimate the customer lifetime (CLV) package on any newly acquired customer given a specified time range. However, I am not able to find the equivalent function in python.

It seems all the information which I could find is to estimate the CLV given a past frequency and recency as a conditional probability. Has anyone had any experience on this ?

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70
Pythoner
  • 11
  • 1
  • 3

1 Answers1

1

Yes. From the documentation Estimating customer lifetime value using the Gamma-Gamma model.

Step 1: Fit a BG model

Ensure you have data in frequency, monetary_value format, e.g.:

from lifetimes.datasets import load_cdnow_summary_data_with_monetary_value

data = load_cdnow_summary_data_with_monetary_value()
data.head()
             frequency  recency      T  monetary_value
customer_id
1                    2    30.43  38.86           22.35
2                    1     1.71  38.86           11.77
6                    7    29.43  38.86           73.74
7                    1     5.00  38.86           11.77
9                    2    35.71  38.86           25.55

Then fit a BG model:

from lifetimes import BetaGeoFitter

# similar API to scikit-learn and lifelines.
bgf = BetaGeoFitter(penalizer_coef=0.0)
bgf.fit(data['frequency'], data['recency'], data['T'])

Step 2: Fit a Gamma-gamma model

# Filter out customers who did not return 
returning_customers_summary = data[data['frequency']>0]

from lifetimes import GammaGammaFitter

ggf = GammaGammaFitter(penalizer_coef = 0)
ggf.fit(returning_customers_summary['frequency'],
        returning_customers_summary['monetary_value'])

Step 3: Estimate Lifetime Value

Here you're calling the fitted gamma-gamma function with your previously fitted BetaGeoFilter and an array of customer data with {frequency, recency, T, and their spend/event (monetary_value)} (in days) as well as a timeline in months and a monthly discount rate.

print(ggf.customer_lifetime_value(
    bgf, #the model to use to predict the number of future transactions
    summary_with_money_value['frequency'],
    summary_with_money_value['recency'],
    summary_with_money_value['T'],
    summary_with_money_value['monetary_value'],
    time=12, # months
    discount_rate=0.01 # monthly discount rate ~ 12.7% annually
    ).head(10))
"""
customer_id
1      140.096211
2       18.943467
3       38.180574
4       38.180574
5       38.180574
6     1003.868107
7       28.109683
8       38.180574
9      167.418216
10      38.180574
Name: clv, dtype: float64
"""
raphael
  • 2,762
  • 5
  • 26
  • 55