Beta Geometric Negative Binomial Distribution — CLTV Prediction Model | by Sarit Maitra | Towards Data Science

https://sarit-maitra.medium.com/membership
Customer life time value (CLTV) is one of the most important metric to modern customer centric business scenario. It is the metric indicating the total revenue a business can reasonably expect from a single customer during the entire relationship. With CLTV business not only quantify the relationship but prioritize the CRM activity and take necessary steps to keep the existing customers happy. It also helps companies to focus on those potential customers who can bring in the more revenue in the future.
80% of the effect comes from 20% of the causes, this is known as 80/20 rule or Pareto principle...Vilfredo Pareto.
If we put Pareto’s principle in business context, we can safely assume that 20% key customers are contributing 80% is business revenue. A number of studies have developed scoring mechanisms (e.g. regression models to predict a customer’s future behavior). The measures of a customer’s past behavior are key predictors of their future behavior in all the empirical analysis. It is common practice to summarize a customer’s past behavior by investigating the Recency, Frequency & Monetary (RFM) characteristics.
Let us use some simple equations as below:
Customer Value = Average Order Value (AOV)* Purchase Frequency
Churn Rate: Churn Rate is the % of customers who have not ordered again.
Customer Lifetime = 1/ churn rate
Churn Rate= 1-Repeat Rate
The above formulas may look quite simple; however, complexity in terms of predictive analytics involved in measuring CLTV where future sales and $ values to be predicted given historical data. This can be done either using regression techniques or probabilistic modeling approach.
We shall target a probabilistic model for predicting CLTV in non-contractual setting on an individual level of business. Using the results of this exercise, managers should be able to:
Distinguish active customers from inactive customers,
Generate transaction forecasts for individual customers,
Predict the purchase volume of the entire customer base.
The stochastic model presented here, featuring Beta Geometric Negative Binomial Distribution (BG/NBD) framework to capture the flow of transactions over time. BG/NBD portrays the story being about how/when customers become inactive.
The BG/NBD require only two pieces of information about each customer’s past purchasing history: “recency” (when the last transaction occurred) and “frequency” (how many transactions was made in a specified time period). The notation used to represent this information is [X = x, t(x), T], where x is the number of transactions observed in the time period (0, T) and t(x) (0 < t(x) ≤T) is the time of the last transaction. Using these two key summary statistics, SMC(2) derive expressions for a number of managerially relevant quantities, such as:
E[X(t)], the expected number of transactions in a time period of length t, which is central to computing the expected transaction volume for the whole customer base over time.
P[X(t) = x], the probability of observing x transactions in a time period of length t.
E[Y (t)| X = x, t(x), T], the expected number of transactions in the period (T,T + t] for an individual with observed behavior (X = x, tx, T).
Therefore, customers will purchase at a randomly distributed interval within a time range. After each purchase they have a certain probability of dying or becoming inactive. Each customer is different and have varying purchase intervals and probability of going inactive.
Let’s explore the data.
Frequency (F) is the number of repeat purchases the customer has made.
T represents the age of the customer which is equal to the duration between a customer’s first purchase and the end of the period under study.
Recency (R) is the age of the customer when they made their most recent purchases. This is equal to the duration between a customer’s first purchase and their latest purchase.
Monetary Value
After doing the necessary cleaning and creating a new data frame containing CustomerID, InvoiceDate (remove the time) and adding a new column (‘sales’) :
data[‘InvoiceDate’] = pd.to_datetime(data[‘InvoiceDate’]).dt.datedata = data[pd.notnull(data[‘CustomerID’])]data = data[(data[‘Quantity’]>0)]data[‘Sales’] = data[‘Quantity’] * data[‘UnitPrice’]cols_of_interest = [‘CustomerID’, ‘InvoiceDate’, ‘Sales’]data = data[cols_of_interest]print(data.head())print(data[‘CustomerID’].nunique())
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.
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)# Plotgbd = 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)
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.
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.
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*3df[‘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.
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.
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.
Based on customer history, we can now predict what an individual’s future purchases might look like:
t = 10individual = 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.
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 .