Causal Machine Studying for Buyer Retention: a Sensible Information with Python | by Arthur Cruiziat | Aug, 2024

Picture by Claudio Schwarz on Unsplash

An accessible information to leveraging causal machine studying for optimizing shopper retention methods

This text is the second in a sequence on uplift modeling and causal machine studying. The concept is to dive deep into these methodologies each from a enterprise and a technical perspective.

Earlier than leaping into this one, I extremely advocate studying the earlier episode which explains what uplift modeling is and the way it can assist your organization usually.

Hyperlink will be discovered beneath.

Image this: you’ve been a shopper of a financial institution for a pair years. Nevertheless, for a month or two, you’ve been contemplating leaving as a result of their software has develop into too difficult. All of the sudden, an worker of the financial institution calls you. He asks about your expertise and finally ends up shortly explaining to you find out how to use the app. Within the meantime, your daughter, who’s a shopper of the identical financial institution additionally thinks about leaving them due to their buying and selling charges; she thinks they’re too costly. Whereas about to unsubscribe, out of the blue, she receives a voucher permitting her to commerce without spending a dime for a month! How is that even attainable?

In my earlier article, I launched the mysterious approach behind this degree of personalisation: uplift modeling. When conventional approaches often predict an final result — e.g. the chance of churn of a buyer— , uplift modeling predicts the potential results of an motion taken on a buyer. The probability of a buyer staying if known as or if supplied a voucher, for instance!

This strategy permits us to focus on the fitting clients — as we’ll be eradicating clients who wouldn’t react positively to our strategy — but in addition to extend our likelihood of success by tailoring our strategy to every buyer. Due to uplift modeling, not solely can we focus our sources towards the fitting inhabitants, we additionally maximise their affect!

Sounds attention-grabbing, wouldn’t you agree? Effectively that is your fortunate day as on this article we’ll dive deep into the implementation of this strategy by fixing a concrete instance: enhancing our retention. We’ll undergo each step, from defining our exact use case to evaluating our fashions outcomes. Our purpose as we speak is to give you the fitting information and instruments to have the ability to apply this system inside your personal organisation, tailored to your personal information and use case, after all.

  1. We’ll begin by clearly defining our use case. What’s churn? Who can we goal? What actions will we set as much as try to retain our purchasers with?
  2. Then, we’ll look into getting the fitting information for the job. What information do we have to implement uplift modeling and find out how to get it?
  3. After that, we’ll look into the precise modeling, specializing in understanding the assorted fashions behind uplift modeling.
  4. Then, we’ll apply our newly acquired information to a first case with a single retention motion: an e-mail marketing campaign.
  5. Lastly, we’ll deep dive right into a extra difficult implementation with many remedies, approaching user-level personalisation

Earlier than we are able to apply uplift modeling to enhance buyer retention, we have to clearly outline the context. What constitutes “churn” in our enterprise context? Can we wish to goal particular customers? If sure, why? Which actions can we plan on setting as much as retain them? Do now we have price range constraints? Let’s attempt answering these questions.

Defining Churn

That is our first step. By exactly and quantitatively defining churn, we’ll be capable of outline retention and perceive the place we stand, the way it has advanced and, if wanted, take motion. The churn definition you’ll select will 100% depend upon what you are promoting mannequin and sector. Listed here are some elements to think about:

  • In case you’re in a transaction-based firm, you’ll be able to take a look at transaction frequency, or transaction volumes evolution. You can additionally take a look at the time because the final transaction occured or a drop in account exercise.
  • In case you’re in a subscription primarily based firm, it may be so simple as customers who’ve unsubscribed, or subscribed customers who’ve stopped utilizing the product.

In case you’re working in a transaction primarily based tech firm, churn could possibly be outlined as “buyer who has not performed a transaction in 90 days”, whereas for those who’re working for a cellular app you could desire to outline it as “buyer who has not logged in in 30 days”. Each the time-frame and the character of churn must be outlined beforehand as flagging churned person will likely be our first step.

The complexity of your definition will rely in your firm’s specificities in addition to the variety of metrics you wish to take into account. Nevertheless, the concept is to arrange definitions that present thresholds which can be simple to know and that allow us establish churners.

Churn Prediction Window

Now that we all know what churn is, we have to outline precisely what we wish to keep away from. What I imply is, can we wish to forestall clients from churning throughout the subsequent 15 days or 30 days? Primarily based on the reply right here, you’ll need to organise your information in a selected method, and outline completely different retention actions. I might advocate to not be too optimistic right here for two causes:

  • The longer the time horizon the tougher it’s for a mannequin to have good performances.
  • The longer we wait after the therapy, the tougher it is going to be to seize its impact.

So let’s be affordable right here. If our definition of churn encompasses a 30-day timeframe, let’s go together with a 30 days horizon and let’s attempt to restrict churn throughout the subsequent 30 days.

The concept is that our timeframe should give us sufficient time to implement our retention methods and observe their affect on person conduct, whereas sustaining our fashions’ performances.

Choosing Goal Customers [Optional]

One other query we have to reply is: are we focusing on a selected inhabitants with our retention actions? A number of causes may inspire such an thought.

  • We observed a rise in churn in a selected phase.
  • We wish to goal extremely priceless clients to maximise our ROI with these actions.
  • We wish to goal new clients to make sure a sturdy activation.
  • We wish to goal clients which can be prone to churn quickly.

Relying by yourself use case, you could wish to choose solely a subset of your clients.

In our case, we’ll select to focus on purchasers with the next chance of churn, in order that we goal clients that want us most.

Defining retention Actions

Lastly, now we have to pick out the precise retention actions we wish to use on our purchasers. This isn’t a straightforward one, and dealing alongside what you are promoting stakeholders right here might be a good suggestion. In our case, we’ll choose 4 completely different actions:

  1. Customized e-mail
  2. In-app notifications highlighting new options or alternatives
  3. Instantly calling our buyer
  4. Particular presents or reductions — one other uplift mannequin may assist us establish one of the best voucher quantity, ought to we discover that subsequent?

Our uplift mannequin will assist us decide which of those actions (if any) is most definitely to be efficient for every particular person person.

We’re prepared! We outlined churn, picked a prediction window, and chosen the actions we wish to retain our clients with. Now, the enjoyable half begins, let’s collect some information and construct a causal machine studying mannequin!

Constructing an efficient uplift mannequin requires a great dataset combining each current person info with experimental information.

Leveraging current person information

First, let’s take a look at our obtainable information. Tech corporations often have entry to plenty of these! In our case, we want buyer degree information resembling:

  1. Buyer info (like age, geography, gender, acquisition channel and so forth.)
  2. Product specifics (creation or subscription date, subscription tier and so forth.)
  3. Transactions info ( frequency of transactions, common transaction worth, complete spend, kinds of merchandise/providers bought, time since final transaction and so forth.)
  4. Engagement (e.g., login frequency, time spent on platform, function utilization statistics, and so forth.)

We will take a look at this information uncooked, however what brings much more worth is to know the way it evolves over time. It permits us to establish behavioral patterns that may doubtless enhance our fashions’ performances. Fortunate for us, it’s fairly easy to do, we simply have to have a look at our information from a special perspective; listed here are a number of transformations that may assist:

  • Taking shifting averages (7, 30 days…) of our predominant utilization metrics — transactions for example.
  • Trying on the proportion modifications over time.
  • Aggregating our information at completely different time scales resembling day by day, weekly and so forth.
  • And even including seasonality indicators such because the day of week or week of 12 months.

These options convey “dynamic info” that could possibly be priceless in terms of detect future modifications! Understanding extra exactly which options we should always choose is past the scope of this text, nonetheless these approaches are greatest practices in terms of work with temporal information.

Bear in mind, our purpose is to create a complete person profile that evolves over time. This temporal information will function the inspiration of our uplift mannequin, enabling us to foretell not who would possibly churn, however who’s most definitely to reply positively to our retention efforts.

Gathering Experimental Knowledge for Uplift Modeling

The second a part of our information gathering journey is about gathering information associated to our retention actions. Now, uplift modeling doesn’t require experimental information. When you have historic information due to previous occasions — you could have already got despatched emails to clients or supplied vouchers — you’ll be able to leverage these. Nevertheless, the more moderen and unbiased your information is, the higher your outcomes will likely be. Debiasing observational or non randomized information requires additional steps that we’ll not talk about right here.

So what precisely do we want? Effectively, we have to have an thought of the affect of the actions you propose to take. We have to arrange a randomized experiment the place we take a look at these actions. Quite a lot of extraordinarily good articles already talk about find out how to set these up, and I can’t dive into it right here. I simply wish to add that the higher the setup, and the larger the coaching set, the higher it’s us!

After the experiment, we’ll clearly analyse the outcomes. And whereas these aren’t serving to us immediately in our quest, it should present us with extra understanding of the anticipated affect of our remedies in addition to a great impact baseline we’ll attempt to outperform with our fashions. To not bore you an excessive amount of with definitions and acronyms, however the results of a randomized experiment is known as “Common therapy impact” or ATE. On our aspect, we’re trying to estimate the Conditional Common Therapy Impact (CATE), often known as Particular person Therapy Impact (ITE).

Whereas experimental information is good, uplift modeling can nonetheless present insights with observational information if an experiment isn’t possible. If not randomized, a number of strategies exists to debias our dataset, resembling propensity rating matching. The secret’s to have a wealthy dataset that captures person traits, behaviors, and outcomes in relation to our retention efforts.

Producing artificial information

For the aim of this instance, we’ll be producing artificial information utilizing the causalml bundle from Uber. Uber has communicated so much on uplift modeling and even created a straightforward to make use of and nicely documented Python bundle.

Right here’s how we are able to generate our artificial information for those who’re interested in it.

import pandas as pd
from causalml.dataset import make_uplift_classification

# Dictionary specifying the variety of options that may have a optimistic impact on retention for every therapy
n_uplift_increase_dict = {
"email_campaign": 2,
"in_app_notification": 3,
"call_campaign": 3,
"voucher": 4
}

# Dictionary specifying the variety of options that may have a adverse impact on retention for every therapy
n_uplift_decrease_dict = {
"email_campaign": 1,
"in_app_notification": 1,
"call_campaign": 2,
"voucher": 1
}

# Dictionary specifying the magnitude of optimistic impact on retention for every therapy
delta_uplift_increase_dict = {
"email_campaign": 0.05, # E mail marketing campaign will increase retention by 5 proportion factors
"in_app_notification": 0.03, # In-app notifications have a smaller however nonetheless optimistic impact
"call_campaign": 0.08, # Direct calls have a robust optimistic impact
"voucher": 0.10 # Vouchers have the strongest optimistic impact
}

# Dictionary specifying the magnitude of adverse impact on retention for every therapy
delta_uplift_decrease_dict = {
"email_campaign": 0.02, # E mail marketing campaign would possibly barely lower retention for some clients
"in_app_notification": 0.01, # In-app notifications have minimal adverse impact
"call_campaign": 0.03, # Calls would possibly annoy some clients extra
"voucher": 0.02 # Vouchers would possibly make some clients assume the product is overpriced
}

# Dictionary specifying the variety of combined options (mixture of informative and optimistic uplift) for every therapy
n_uplift_increase_mix_informative_dict = {
"email_campaign": 1,
"in_app_notification": 2,
"call_campaign": 1,
"voucher": 2
}

# Dictionary specifying the variety of combined options (mixture of informative and adverse uplift) for every therapy
n_uplift_decrease_mix_informative_dict = {
"email_campaign": 1,
"in_app_notification": 1,
"call_campaign": 1,
"voucher": 1
}

positive_class_proportion = 0.7 # Baseline retention price

# Generate the dataset
df, feature_names = make_uplift_classification(
n_samples=20000, # Elevated pattern dimension for extra strong outcomes
treatment_name=['email_campaign', 'in_app_notification', 'call_campaign', 'voucher'],
y_name='retention',
n_classification_features=20, # Elevated variety of options
n_classification_informative=10,
n_uplift_increase_dict=n_uplift_increase_dict,
n_uplift_decrease_dict=n_uplift_decrease_dict,
delta_uplift_increase_dict=delta_uplift_increase_dict,
delta_uplift_decrease_dict=delta_uplift_decrease_dict,
n_uplift_increase_mix_informative_dict=n_uplift_increase_mix_informative_dict,
n_uplift_decrease_mix_informative_dict=n_uplift_decrease_mix_informative_dict,
positive_class_proportion=positive_class_proportion,
random_seed=42
)

#Encoding remedies variables
encoding_dict = {
'call_campaign': 3,
'email_campaign': 1,
'voucher': 4,
'in_app_notification':2,
'management': 0
}

# Create a brand new column with encoded values
df['treatment_group_numeric'] = df['treatment_group_key'].map(encoding_dict)

Ouf remaining information needs to be organized like this:

dataset description

In a “actual life use case”, this information could be aggregated at time degree, for example this may be for every person a day by day or weekly aggregation of information gathered earlier than we reached out to them.

  • X_1 to X_n could be our person degree options
  • T could be the precise therapy (1 or 0, therapy or management, therapy 1, therapy 2, management relying in your use case)
  • And Y is the precise final result: did the person keep or not?

Knowledge preparation

In our case, with the intention to analyse each our use circumstances, we want additional preparation. Let’s create 2 distinct datasets — coaching and a testing set — for every use case:

  • First use case: a single therapy case, the place we’ll give attention to a single retention technique: sending e-mail to our clients.
  • Second use case: a multi therapy case, the place we’ll evaluate the effectiveness of various remedies and most significantly discover one of the best one for every buyer.
from sklearn.model_selection import train_test_split

def prepare_data(df, feature_names, y_name, test_size=0.3, random_state=42):
"""
Put together information for uplift modeling, together with splitting into practice and take a look at units,
and creating mono-treatment subsets.
"""
# Create binary therapy column
df['treatment_col'] = np.the place(df['treatment_group_key'] == 'management', 0, 1)

# Break up information into practice and take a look at units
df_train, df_test = train_test_split(df, test_size=test_size, random_state=random_state)

# Create mono-treatment subsets
df_train_mono = df_train[df_train['treatment_group_key'].isin(['email_campaign', 'control'])]
df_test_mono = df_test[df_test['treatment_group_key'].isin(['email_campaign', 'control'])]

# Put together options, therapy, and goal variables for full dataset
X_train = df_train[feature_names].values
X_test = df_test[feature_names].values
treatment_train = df_train['treatment_group_key'].values
treatment_test = df_test['treatment_group_key'].values
y_train = df_train[y_name].values
y_test = df_test[y_name].values

# Put together options, therapy, and goal variables for mono-treatment dataset
X_train_mono = df_train_mono[feature_names].values
X_test_mono = df_test_mono[feature_names].values
treatment_train_mono = df_train_mono['treatment_group_key'].values
treatment_test_mono = df_test_mono['treatment_group_key'].values
y_train_mono = df_train_mono[y_name].values
y_test_mono = df_test_mono[y_name].values

return {
'df_train': df_train, 'df_test': df_test,
'df_train_mono': df_train_mono, 'df_test_mono': df_test_mono,
'X_train': X_train, 'X_test': X_test,
'X_train_mono': X_train_mono, 'X_test_mono': X_test_mono,
'treatment_train': treatment_train, 'treatment_test': treatment_test,
'treatment_train_mono': treatment_train_mono, 'treatment_test_mono': treatment_test_mono,
'y_train': y_train, 'y_test': y_test,
'y_train_mono': y_train_mono, 'y_test_mono': y_test_mono
}

# Utilization
information = prepare_data(df, feature_names, y_name)

# Print shapes for verification
print(f"Full take a look at set form: {information['df_test'].form}")
print(f"Mono-treatment take a look at set form: {information['df_test_mono'].form}")

# Entry ready information
df_train, df_test = information['df_train'], information['df_test']
df_train_mono, df_test_mono = information['df_train_mono'], information['df_test_mono']
X_train, y_train = information['X_train'], information['y_train']
X_test, y_test = information['X_test'], information['y_test']
X_train_mono, y_train_mono = information['X_train_mono'], information['y_train_mono']
X_test_mono, y_test_mono = information['X_test_mono'], information['y_test_mono']
treatment_train, treatment_test = information['treatment_train'], information['treatment_test']
treatment_train_mono, treatment_test_mono = information['treatment_train_mono'], information['treatment_test_mono']

Now that our information is prepared, let’s undergo a little bit of principle and examine the completely different approaches obtainable to us!

As we now know, uplift modeling makes use of machine studying algorithms to estimate the heterogeneous therapy impact of an intervention on a inhabitants. This modelling strategy focuses on the Conditional Common Therapy Impact (CATE), which quantifies the anticipated distinction in final result with and with out the intervention for our clients.

Listed here are the primary fashions we are able to use to estimate it:

Direct uplift modeling

  • This strategy is the best one. We merely use a selected algorithm, resembling an uplift choice tree, which loss operate is optimized to resolve this drawback. These fashions are designed to maximise the distinction in outcomes between handled and untreated teams throughout the similar mannequin.
  • We’ll be utilizing an Uplift Random ForestClassifier for instance of this.

Meta-learners

  • Meta-learners use recognized machine studying fashions to estimate the CATE. They’ll mix a number of fashions utilized in other ways, or be skilled on the predictions of different fashions.
  • Whereas many exist, we’ll give attention to two sorts : the S-Learner and the T-Learner

Let’s shortly perceive what these are!

1. S-Learner (Single-Mannequin)

S Learner — supply causalml documentation

The S-Learner is the best meta-learner of all. Why? As a result of it solely consists of utilizing a conventional machine studying mannequin that features the therapy function as enter. Whereas easy to implement, it might wrestle if the significance of the therapy variable is low.

2. T-Learner (Two-Mannequin)

“The T-Learner tries to resolve the issue of discarding the therapy totally by forcing the learner to first break up on it. As an alternative of utilizing a single mannequin, we are going to use one mannequin per therapy variable.

Within the binary case, there are solely two fashions that we have to estimate (therefore the title T)” Supply [3]

Every of those approaches has its execs and cons. How nicely they work will rely in your information and what you’re attempting to attain.

On this article we’ll check out all three: an Uplift Random Forest Classifier, a S-Learner, and a T-Learner, and evaluate their performances in terms of enhancing our firm’s retention.

Mannequin Coaching

Now let’s practice our fashions. We’ll begin with our direct uplift mannequin, the uplift random forest classifier. Then we’ll practice our meta fashions utilizing an XGBoost regressor. Two issues to notice right here:

  • The algorithm selection behind your meta-models will clearly affect the ultimate mannequin performances, thus you could wish to choose it rigorously.
  • Sure, we’re choosing regressors as meta fashions fairly than classifiers, primarily as a result of they supply extra flexibility, outputting a exact impact.

Listed here are the completely different steps you’ll discover within the beneath code:

  • We initialize our end result dataframe
  • Then we practice every mannequin on our coaching set
  • Lastly we predict our therapy results on the take a look at units earlier than saving the outcomes
from causalml.inference.meta import BaseSRegressor, BaseTRegressor
from causalml.inference.tree import UpliftRandomForestClassifier
from xgboost import XGBRegressor

#save leads to a df
df_results_mono = df_test_mono.copy()

# Initialize and practice a randomForest Classifier
rfc = UpliftRandomForestClassifier(control_name='management')
rfc.match(X_train_mono, treatment_train_mono, y_train_mono)

# Initialize and practice S-Learner
learner_s = BaseSRegressor(
learner=XGBRegressor(
n_estimators=100,
max_depth=3,
learning_rate=0.1,
random_state=42
),
control_name='management'
)

learner_s.match(X_train_mono, treatment_train_mono, y_train_mono)

# Initialize and practice T-Learner
learner_t = BaseTRegressor(
learner=XGBRegressor(
n_estimators=100,
max_depth=3,
learning_rate=0.1,
random_state=42
),
control_name='management'
)

learner_t.match(X_train_mono, treatment_train_mono, y_train_mono)

# Predict therapy results
df_results_mono[["mono_S_learner"]] = learner_s.predict(X=X_test_mono)
df_results_mono[["mono_T_learner"]] = learner_t.predict(X=X_test_mono)
df_results_mono["random_forest_learner"] = rfc.predict(X_test_mono)

show(df_results_mono[["mono_S_learner", "mono_T_learner", "random_forest_learner"]].imply())

df_mono_results_plot = df_results_mono[["mono_S_learner","mono_T_learner", "random_forest_learner","retention","treatment_col"]].copy()

Observe that we’re nonetheless utilizing causalml right here, and that the API is very simple to make use of, very near a sklearn-like implementation.

Mannequin analysis

How you can consider and evaluate our fashions’ performances? That may be a nice query! As we’re predicting one thing we have no idea — we don’t know the impact of our therapy on our clients as every buyer both acquired the therapy or was within the management group. We can’t use traditional analysis metrics. Hopefully, there are different methods:

The Acquire curve: The acquire curve presents a straightforward solution to visualise our mannequin’s efficiency. The concept behind acquire is easy:

  • We compute the estimated impact of every of our clients, get them organized from the most important impact to the lesser.
  • From right here, we transfer level by level. At every level, we calculate the common therapy impact that means, each the common impact — for management and therapy — and we take the distinction.
  • We try this for each our fashions ordering and a random ordering, simulating random choice, and evaluate each curves!

It helps us perceive which enchancment our mannequin would have introduced versus a random choice.

The AAUC rating: the AAUC rating may be very near the precise acquire curve because it measures the Space beneath the curve of the acquire curve of our mannequin, enabling us to match it with the one of many random mannequin. It summarizes the acquire curve in a straightforward to match quantity.

Within the following code, we calculate these metrics

from causalml.metrics import plot_gain
from causalml.metrics import auuc_score

#AAUC rating
aauc_normalized = auuc_score(df_mono_results_plot, outcome_col='retention', treatment_col='treatment_col', normalize=True, tmle=False)
print(f"AAUC Rating Normalized: {aauc_normalized}")

# Plot Acquire Curve
plot_gain(df_mono_results_plot, outcome_col='retention', treatment_col='treatment_col')
plt.title('Acquire Curve - T-Learner')
plt.present()

Listed here are the outcomes we obtained. Larger scores are higher after all.

  1. T-Learner: ~6.4 (greatest performer)
  2. S-Learner: ~6.3 (very shut second)
  3. Random Forest: ~5.7 (good, however inferior to the others)
  4. Random focusing on: ~0.5 (baseline)

What do these outcomes imply?

  • Effectively, all our fashions are performing manner higher than random focusing on. That is reassuring. They’re about 12 occasions simpler! We’ll perceive what it means by way of affect simply after.
  • We additionally perceive from these AAUC rating that, whereas all fashions are performing fairly nicely, the T-Leaner is one of the best performer

Now let’s check out the acquire curve.

Acquire Curve

How you can learn a acquire curve:

  • X-Axis (Inhabitants): This represents the dimensions of the inhabitants you’re focusing on, ranging from essentially the most responsive people (on the left) to the least responsive (on the fitting).
  • Y-Axis (Acquire): This reveals the cumulative acquire, which is the advance in your final result (e.g., elevated retention).

Acquire curve Interpretation

The acquire curve reveals us the profit — in our preliminary unit therefore “folks retained” — of focusing on the inhabitants utilizing our uplif mannequin or randomly focusing on.

  • In that case evidently if we attain out to the entire inhabitants with our emails, we’d retain roughly 100 extra customers. That is our baseline state of affairs. Observe that each curve ends by this end result which is anticipated contemplating our acquire definition.
  • So find out how to interpret this? Effectively, wanting on the curve we are able to say that utilizing our mannequin, by reaching out to solely 50% of the inhabitants, we are able to save 600 extra customers! Six occasions greater than by reaching out to everybody. How is that attainable? By focusing on solely customers which can be prone to react positively to our outreach, whereas ignoring those that would leverage this e-mail to really churn for example.

It’s time for a small disclaimer: we’re utilizing artificial information right here, our outcomes are extraordinarily unlikely in the true world, however it’s good as an instance.

On this case, our fashions allow us to do extra with much less. This can be a good instance on how we are able to optimize our sources utilizing uplift modeling and focusing on a decrease share of the inhabitants, therefore limiting the operation prices, to acquire a great share of the outcomes. A type of Pareto impact for those who’d like.

However let’s head over to the actually cool stuff : how can we personalize our strategy to each buyer.

Let’s now restart our evaluation, contemplating all our retention methods described above:

  1. E mail marketing campaign
  2. Name marketing campaign
  3. In-app notification
  4. Vouchers

With a purpose to obtain this, we want experimentation outcomes of both a multi-treatment experimentation of all these actions, or to mixture the outcomes of a number of experimentation. the higher the experimental information, the higher predictive output we’ll get. Nevertheless, organising such experiments can take time and sources.

Let’s use our beforehand generated information, retaining in thoughts that getting this information within the first place might be the most important problem of this strategy!

Mannequin Coaching

Let’s begin by coaching our fashions. We’ll preserve the identical mannequin sort as earlier than, a Random Forest, S-Learner, and T-Learner.

Nevertheless, these fashions will now study to distinguish between the consequences of our 4 distinct remedies.

#save leads to a df
df_results_multi = df_test.copy()

# Outline therapy actions
actions = ['call_campaign', 'email_campaign', 'in_app_notification', 'voucher']

# Initialize and practice Uplift Random Forest Classifier
rfc = UpliftRandomForestClassifier(
n_estimators=100,
max_depth=5,
min_samples_leaf=50,
min_samples_treatment=10,
n_reg=10,
control_name='management',
random_state=42
)
rfc.match(X_train , treatment_train, y_train)

# Initialize and practice S-Learner
learner_s = BaseSRegressor(
learner=XGBRegressor(
n_estimators=100,
max_depth=3,
learning_rate=0.1,
random_state=42
),
control_name='management'
)

learner_s.match(X_train , treatment_train, y_train)

# Initialize and practice T-Learner
learner_t = BaseTRegressor(
learner=XGBRegressor(
n_estimators=100,
max_depth=3,
learning_rate=0.1,
random_state=42
),
control_name='management'
)

learner_t.match(X_train , treatment_train, y_train)

Predictions

Now that our fashions are skilled, let’s generate our predictions for every therapy. For every person, we’ll get the uplift of every therapy. This can allow us to decide on the best therapy by person, if any therapy has a optimistic uplift. In any other case, we simply received’t attain out to this individual!

def predict_multi(df, learner, learner_name, X_test):
"""
Predict therapy results for a number of remedies and decide one of the best therapy.
"""

# Predict therapy results
cols = [f'{learner_name}_learner_{action}' for action in actions]
df[cols] = learner.predict(X=X_test)

# Decide one of the best therapy impact
df[f'{learner_name}_learner_effect'] = df[cols].max(axis=1)

# Decide one of the best therapy
df[f"{learner_name}_best_treatment"] = df[cols].idxmax(axis=1)
df.loc[df[f'{learner_name}_learner_effect'] < 0, f"{learner_name}_best_treatment"] = "management"

return df

# Apply predictions for every mannequin
df_results_multi = predict_multi(df_results_multi, rfc, 'rf', X_test)
df_results_multi = predict_multi(df_results_multi, learner_s, 's', X_test)
df_results_multi = predict_multi(df_results_multi, learner_t, 't', X_test)

Right here is the type of information we’ll receive from this, for every mannequin:

We’ll find a way, for every mannequin, to select one of the best therapy for every person!

Mannequin analysis

Now let’s take a look at our strategy analysis. As now we have a number of remedies, it’s barely completely different:

  • For every person we choose one of the best therapy.
  • Then we order our person primarily based on their greatest therapy impact
  • And take a look at what actually occurred : both the person actually stayed or left.

Following this rationale, we simply perceive how we are able to outperform random focusing on by solely focusing on a small share of our complete inhabitants.

From right here, we’re in a position to plot our acquire curve and compute our AAUC. Simple proper? The code beneath does precisely that, nonetheless leveraging causalML.

#AAUC rating
aauc_normalized = auuc_score(df_t_learner_plot_multi, outcome_col='retention', treatment_col='treatment_col', normalize=True, tmle=False)
aauc_non_normalize = auuc_score(df_t_learner_plot_multi, outcome_col='retention', treatment_col='treatment_col', normalize=False, tmle=False)
print(f"AAUC Rating Normalized: {aauc_normalized}")
print(f"AAUC Rating: {aauc_non_normalize}")

# Plot Acquire Curve
plot_gain(df_t_learner_plot_multi, outcome_col='retention', treatment_col='treatment_col')
plt.title('Acquire Curve - T-Learner')
plt.present()

Outcomes interpretation

  1. T-Learner: ~1.45 (greatest performer)
  2. S-Learner: ~1.42 (very shut second)
  3. Random Forest: ~1.20 (good, however inferior to the others)
  4. Random focusing on: ~0.52 (baseline)

What this implies:

  • As soon as once more, all our fashions outperform random focusing on, and as soon as once more the T-Learner is one of the best performer
  • Nevertheless we word that the distinction is decrease than in our first case. Completely different causes may clarify that, one being the precise set-up. We’re contemplating a much bigger inhabitants right here, which we didn’t take into account in our first experiment. It additionally may imply that our fashions don’t carry out as nicely in terms of multi-treatment and we might must iterate and attempt to enhance their efficiency.

However let’s take a look at our acquire curve to know higher our efficiency.

Interpretation of the Multi-Therapy Acquire Curve

  1. As we are able to see, if we had been to focus on 100% of our inhabitants — 30,000 customers — we’d retain a further 850 customers (roughly)
  2. nonetheless, utilizing our fashions, we’re in a position to retain 1,600 customers whereas solely contacting 33% of the overall inhabitants
  3. Lastly, we discover that previous 40% of the inhabitants all curves begin to lower indicating that there isn’t a worth contacting these clients.

We made it. We efficiently constructed a mannequin that permits us to personalize successfully our retention actions to maximise our ROI. Primarily based on this mannequin, our firm determined to place this mannequin to manufacturing and saved thousands and thousands not losing sources reaching out to everybody, but in addition focusing the fitting sort of effort on the fitting buyer!

Placing such a mannequin to manufacturing is one other problem in itself as a result of we have to guarantee its efficiency in the long run, and preserve retraining it when attainable. The framework to do this could be to:

  • Generate inference together with your mannequin on 80% of your goal inhabitants
  • Maintain 10% of your goal inhabitants intact : Management
  • Maintain a further 10% of your inhabitants to maintain experimenting to coach your mannequin for the subsequent time interval (month/quarter/12 months relying in your capabilities)

We would look into this afterward!

In case you made it this far, thanks! I hope this was attention-grabbing and that you just realized find out how to create an uplift mannequin and find out how to consider its efficiency.

If I did a great job, you could now know that uplift fashions are an unbelievable device to know and that it will probably result in nice, direct and measurable affect. You additionally could have understood that uplift fashions allow us to focus on the fitting inhabitants with the fitting therapy, however require a robust and exploitable experimental information to be skilled on. Getting this information updated is commonly the large problem of such tasks. It’s relevant on historic/observational information, one would wish so as to add particular cleansing and treating steps to make sure that the info is unbiased.

So what’s subsequent? Whereas we’re deep-diving on the earth of causal machine studying, I wish to ensure you are heard. So if you wish to look into particular subjects that you just assume you possibly can apply in your personal firm and want to study extra about it, let me know, I’ll do my greatest. Let’s preserve all studying from one another! Till subsequent time, pleased modeling!

Until in any other case famous, all pictures are by the creator

[1] https://en.wikipedia.org/wiki/Uplift_modelling

[2] https://causalml.readthedocs.io/en/newest/index.html

[3] https://matheusfacure.github.io/python-causality-handbook/landing-page.html