Beta Geometric Negative Binomial Distribution — CLTV Prediction Model
By: Sarit Maitra
Tags: Rfm Analysis, Predictive Analytics, Customer Lifetime Value

We can make some observations here. There are 4339 customers and 12346 made single purchase, so the F and R are 0, and the T is 325 days.

df[‘frequency’].plot(kind=’hist’, bins=50)

print(df[‘frequency’].describe())

print(sum(df[‘frequency’] == 0)/float(len(data)))

As shown, both frequency and recency are distributed quite near 0. Among all customers, >38% of them only made zero repeat purchase while the rest of the sample (62%) is divided into two equal parts: 31% of the customer base makes one repeat purchase while the other 31% of the customer base makes more than one repeat purchase. Similarly, for Recency, most customers have made their last purchase early in their lifetime and then became inactive. Indeed, the last repeat purchase that half our customers will make is within less than a year (252 days to be precise), since their first purchase for 75th quantile.

Let’s do the Model talking now:

We first need to fit the customer probability model to the data so that it picks up on their behaviors and pattern. This is done by looking at each individual’s Frequency, Recency and Age and adjusting its parameters so that it better reflects the intervals in which our customer-base purchases.

The parameters also vary across different customers so it is calculated over two distributions for a more accurate and flexible fit of the data. Mathematically, this is done by taking the expectation of the equation over both distributions.

bgf = BetaGeoFitter(penalizer_coef=0.0)

bgf.fit(df[‘frequency’], df[‘recency’], df[‘T’], )

print (bgf)



# Plot

gbd = beta.rvs(bgf.params_[‘a’], bgf.params_[‘b’], size = 50000)

ggd = gamma.rvs(bgf.params_[‘r’], scale=1./bgf.params_[‘alpha’], size = 50000)

plt.figure(figsize=(14,4))

plt.subplot(121)

plt.title(‘Heterogenity of $p$’)

temp = plt.hist(gbd, 20, alpha=0.75)

plt.subplot(122)

plt.title(‘Heterogenity of $\lambda$’)

temp = plt.hist(ggd, 20, alpha=0.75)

Visualizing F/R Matrix:

Frequency/Recency matrix computes the expected number of transactions a customer is to make in the next time period, given the R(age at last purchase) and F (the number of repeat transactions made).

The matrix has the customer’s recency on the Y axis and the frequency on the X axis and the heatmap component is showing the predicted number of future purchases customers at intersecting points will make in one unit of time. The customers most likely to order are those who’ve placed lots of previous orders and have been seen relatively recently.

plot_frequency_recency_matrix(bgf)

The RF plots maps a customer’s expected purchases by the next year and probability that they’re alive given the frequency / recency. Intuitively, we can see that customers with high frequency and recency are expected to purchase more in the future and have a higher chance of being alive. Customers in the white zone are of interest as well since they are 50/50 on leaving the company but we can still expect them to purchase about 2 to 2.5 times during the next year. These are the customers that may need a little customer servicing to come back and buy more. It is interesting to note that for a fixed recency, customer’s with more frequency are more likely to be considered dead. This is a property of the model that illustrates a clear behavioral story:

A customer making more frequent purchases is more likely to die off if we observe a longer period of inactivity than the customers previous intervals.

We can see that if a customer has bought 120 times and their latest purchase (R) was when they were 120 days back, then they are you best customer (bottom-right). The coldest customers are those that in the top-right corner: they bought a lot quickly, and haven’t seen them in weeks. The tail around (20,50) represents the customers who buy infrequently, but are not seen recently, so they might buy again — we’re unsure if they are dead or just between purchases.

Probability that each customer is alive:

We can visualize is the probability that each customer is alive based on their frequency and recency.

plot_probability_alive_matrix(bgf)

Customers who have purchased recently are almost surely “alive”. Customers who have purchased a lot but not recently, are likely to have dropped out. And the more they bought in the past, the more likely they have dropped out. They are represented in the upper-right.

Ranking customers from best to worst:

Let’s rank the customers from “highest expected purchases in the next period” to lowest. Models expose a method that will predict a customer’s expected purchases in the next period using their history.

t = 31*3

df[‘predicted_purchases’] = bgf.conditional_expected_number_of_purchases_up_to_time(t, df[‘frequency’], df[‘recency’], df[‘T’])

df.sort_values(by=’predicted_purchases’).tail(10)

Listed here is our top 10 customers that the model expects them to purchase in the next 3 months. We can see that the customer who has made 131 purchases, and bought very recently, is probably going to buy again in the next period. The predicted_purchases column displays their expected number of purchases while the other three columns represent their current RFM metrics. The BG/NBD model believes these individuals will be making more purchases within the near future as they are the current best customers.

Assessing model fit (Incorporating Heterogeneity):

After fitting the model, we’re interested in seeing how well it is able to relate to our data. The first is to compare our data versus artificial data simulated with our fitted model’s parameters.

plot_period_transactions(bgf)

The two are almost identical indicating that, the model is a very good fit and predicts the number of periods in the calibration period rather well. The expected number of customers that are going to repeat purchase 0, 1, 2, 3 … 6 times in the future. For each number of repeat purchases (x-axis), we plot both what the model predicted and what the actual numbers were. As we can see, little to no errors in the fit for up to 6 repeat purchases. Let’s do the next fact check.

However, it is always a good idea to compute the overall % error which is(predicted transactions/actual transactions -1) and the % error per transactions done in the calibration period. This will help us to quantify how close to reality the model is.

Model testing:

summary_cal_holdout = calibration_and_holdout_data(data, ‘CustomerID’, ‘InvoiceDate’,

calibration_period_end=’2011–06–08',

observation_period_end=’2011–12–9' )

print(summary_cal_holdout.head())

bgf.fit(summary_cal_holdout[‘frequency_cal’], summary_cal_holdout[‘recency_cal’], summary_cal_holdout[‘T_cal’])

plot_calibration_purchases_vs_holdout_purchases(bgf, summary_cal_holdout)

In this plot, we separate the data into both a in-sample (calibration) and validation (holdout) period. The sample period consists from the beginning to 2011–06–08; the validation period spans the rest of the duration ( 2011–06–09 to 2011–12–09). The plot groups all customers in the calibration period by their number of repeat purchases (x-axis) and then averages over their repeat purchases in the holdout period (y-axis). The plot groups all customers in the calibration period by their number of repeat purchases (x-axis) and then averages over their repeat purchases in the holdout period (y-axis). The orange and blue line presents the model prediction and actual result of the y-axis respectively. The lines are quite close to each other indicating that, the model is not far off at predicting the number of orders each customer will make.

The model is able to accurately predict the customer base’s behavior out of the sample, the model under-estimates at 4 purchases and after 5 purchases.

Customer transactions predictions:

Based on customer history, we can now predict what an individual’s future purchases might look like:

t = 10

individual = df.loc[12347]

bgf.predict(t, individual[‘frequency’], individual[‘recency’], individual[‘T’])

0.15727742663038222

The model predicts that customer’s (id:12347) future transaction is 0.157 in 10 days.

Key takeaways:

Customers are the most important assets of a business and CLTV allows assessing their current and future value in a customer base. The CRM strategy and marketing resource allocation are based on this metric. Business not only need to predict the retention but also analyze the purchase behavior of customers if we consider customer centric business. BG/NBD is a slight variation in the behavioral story associated with the Pareto/NBD but vastly easier to implement. The transition from an exponential distribution to a geometric process (to capture customer dropout) does not require any different psychological theories nor does it have any noteworthy managerial implications.

I can be reached at here .