Lesson 7.4: Neural networks, causal inference and uplift
Get a working picture of how neural networks learn, then move from prediction to cause with difference-in-differences and uplift modelling for targeting.
By the end of this lesson you will be able to describe what a neural network computes and how it learns, work a single neuron and a small network by hand, say when deep learning is worth using on business data and when boosted trees are the better choice, and move from predicting outcomes to estimating the effect of an action. You will compute a forward pass, watch gradient descent converge and diverge as the learning rate changes, estimate a policy's effect with difference-in-differences, and use uplift to find that contacting everyone earns ₹1 lakh while contacting only the customers who respond to contact earns ₹3 lakh. You should have read Lessons 4.2, 4.4 and 2.4, since a neuron is a logistic regression, learning is the optimisation behind it, and causal questions begin where correlation stops. The mathematics is weighted sums, one curve and averages.
This lesson joins two topics that look unrelated and share a theme: both go beyond predicting what will happen. Neural networks are the technology behind modern image, speech and language systems, and a data scientist should understand how they work even if tabular problems rarely need them. Causal inference and uplift address what many business decisions actually require: not "who will buy?" but "who will buy because of what we do?"
Neural networks
A neuron
A neuron is a small calculation. It takes several inputs, multiplies each by a weight, adds them up with an extra constant called the bias, and passes the total through a function called an activation. If that sounds like logistic regression, it is: logistic regression is a single neuron with a sigmoid activation. The weighted sum is called z.
Take inputs x1 = 2 and x2 = 1, weights 0.5 and −1, and a bias of 0.3. Then z = 2 × 0.5 + 1 × (−1) + 0.3 = 0.3. A sigmoid activation turns this into 1 ÷ (1 + e^(−0.3)) = 0.574. The other very common activation, ReLU, for rectified linear unit, returns z if it is positive and 0 otherwise, so here it returns 0.3. ReLU is simple, fast and works well in deep networks. The activation is what lets a network do more than draw a straight line: without it, stacking layers would still produce a straight-line model.
Layers and the forward pass
A network arranges neurons in layers. Inputs feed a hidden layer of neurons, whose outputs feed the next layer, and so on until an output layer gives the prediction. Computing the output from the inputs is the forward pass.
Take a tiny network with two inputs, two hidden neurons with ReLU, and one output neuron with a sigmoid, on the input (1, 2). The first hidden neuron has weights (0.5, 0.25) and bias 0, so z = 1 × 0.5 + 2 × 0.25 = 1.0, and ReLU gives 1.0. The second has weights (−1, 0.5) and bias 0.5, so z = −1 + 1 + 0.5 = 0.5, and ReLU gives 0.5. The output neuron has weights (1.2, −0.8) and bias −0.2, so z = 1.2 × 1.0 − 0.8 × 0.5 − 0.2 = 0.6, and the sigmoid gives 1 ÷ (1 + e^(−0.6)) = 0.646. The network predicts a 64.6 per cent probability.
The number of weights grows quickly. A network with 10 inputs, a hidden layer of 32 neurons, a second of 16 and one output has (10 × 32 + 32) + (32 × 16 + 16) + (16 + 1) = 352 + 528 + 17 = 897 parameters, against 11 for a logistic regression on the same 10 inputs. A network with 100 inputs and layers of 256 and 128 has 58,881. Modern language models have billions. This flexibility lets a network learn very complex patterns, and also makes it prone to overfitting, the problem of Lesson 4.4.
Learning: loss and gradient descent
Training a network means choosing the weights. Start with random weights, compute the predictions, measure the error with a loss function, such as the log loss of Lesson 4.2, and adjust the weights to reduce it. The adjustment uses gradient descent: compute the slope of the loss with respect to each weight, which tells you which direction increases the loss, and move each weight a small step the other way. The size of the step is the learning rate. The slopes of all the weights are found efficiently by backpropagation, which applies the chain rule layer by layer from the output back to the inputs. You never do this by hand, but you should know that learning is repeated small downhill steps, called epochs when taken over the full dataset.
The idea is easiest to see with a single weight and a loss of (w − 3)², minimised at w = 3. The slope is 2 × (w − 3), and each step changes w to w − learning rate × slope. The distance from 3 is multiplied by (1 − 2 × learning rate) at each step. Starting from w = 0 with a learning rate of 0.1, that factor is 0.8, and after 10 steps w is 3 − 3 × 0.8^10 = 2.68. With a rate of 0.5 the factor is 0 and it lands on 3 at once. With 0.9 the factor is −0.8: w overshoots, to 5.4 after one step, and then spirals in. With 1.1 the factor is −1.2: each step overshoots by more, and the process diverges.
Predict first: you double the learning rate of a training run that is converging slowly. Will it converge faster, slower, or can it fail altogether? Decide before reading on.
All three are possible. Doubling a small rate usually speeds things up, but past a threshold the steps overshoot the minimum, and the loss can bounce or explode. Real loss surfaces are far more complicated than a parabola, so practitioners tune the rate carefully, often using schedules that start large and shrink.
Controlling overfitting, and the main architectures
Networks with many parameters need restraint. Common tools are early stopping, halting when validation loss starts to rise, weight penalties like the ridge penalty of Lesson 4.4, dropout, which randomly switches off neurons during training so the network cannot rely on any one, and more data. Different architectures suit different data. A plain multilayer perceptron like the one above handles tables. Convolutional networks exploit the grid structure of images. Recurrent networks and, now more commonly, transformers handle sequences such as text, with transformers underlying today's large language models.
Neural networks or boosted trees?
On images, audio and text, deep learning is the standard approach, because it learns useful features from raw data, where hand-built features are hopeless. On tabular business data, such as the loan and customer tables in this course, the picture is different. Boosted trees are usually as accurate or more, train faster on modest data, need little scaling and tuning, and are easier to explain. A neural network on a table earns its place mainly when the data is very large, when inputs include text or images alongside the numbers, or when you need a single model to learn from several kinds of data at once.
· Boosted trees · Neural networksBest on · Tabular data · Images, audio, text, very large data
Needs scaling · No · Yes
Data needed · Moderate · Often large
Tuning effort · Moderate · High
Training speed on tables · Fast · Slower, may need special hardware
Explainability · Moderate, with SHAP · Low, with SHAP and other tools
Typical first choice for a lender or retailer's table · Yes · Rarely
## Causal inference: from prediction to effect
A predictive model answers "what will happen?" A decision needs "what will happen if I do this?" Lesson 2.4 explained that correlation is not causation and that randomised experiments are the cleanest route to cause. This section gives the language and two practical tools for when you have an experiment, and for when you do not.
Potential outcomes
The framework is simple. For each person there are two potential outcomes: what would happen if they were treated, and what would happen if they were not. The causal effect for that person is the difference. We can never observe both for the same person, so the effect cannot be measured for an individual, which is the fundamental problem of causal inference. What we can estimate is the average treatment effect, the average of the individual effects across a group. A randomised experiment does this because random assignment makes the treated and control groups alike in everything but the treatment, so the difference in their average outcomes is an unbiased estimate of the effect.
When you cannot randomise, you work with observational data, and the danger is confounding: the people who got the treatment differ from those who did not in ways that also affect the outcome. Several methods try to correct for it, each resting on assumptions that are untestable and must be argued for.
Method · Idea · Key assumptionRegression adjustment · Control for measured confounders in a model · All important confounders are measured
Matching and propensity weighting · Compare treated cases with similar untreated ones · All confounders are measured, and similar cases exist
Difference-in-differences · Compare the change over time in a treated group with the change in a control group · Without the treatment, both groups would have changed alike
Regression discontinuity · Compare cases just either side of a threshold · Cases near the threshold are otherwise alike
Instrumental variables · Use a factor that shifts treatment but affects the outcome only through it · Such a factor exists and is valid
### Difference-in-differences
A bank introduces a faster complaint process at one set of branches, the treated branches, and not at others, the control branches. Complaints per 1,000 customers at the treated branches fell from 20 before to 14 after, a drop of 6. At the control branches they fell from 18 to 16, a drop of 2, for reasons unrelated to the new process, such as a general improvement in service.
Predict first: is the effect of the new process a fall of 6, of 2, or of 4 complaints per 1,000? Decide before reading on.
The natural estimate is 4. The treated branches fell by 6, but 2 of that fall would have happened anyway, as the control branches show. The difference-in-differences estimate subtracts the control group's change from the treated group's change: (14 − 20) − (16 − 18) = −6 − (−2) = −4. The simple before-and-after comparison of −6 overstates the effect by 50 per cent, and the comparison of treated and control after the change alone, 14 against 16, ignores that the treated branches started higher.
The method rests on the parallel trends assumption. If the treated branches were already improving faster than the controls before the change, the estimate wrongly credits the new process. The check is to look at several periods before the change and confirm that the two groups moved together. In regression form, the effect is the coefficient on the interaction of "treated group" and "after".
Uplift modelling: who responds because of the action?
A predictive model for marketing asks who is likely to buy. But many of the likely buyers would buy anyway, and spending on them is wasted. Uplift modelling estimates, for each person, the change in their probability of the outcome caused by the action: the probability if contacted minus the probability if not. It needs experimental data, in which some customers were contacted and a random control group was not. It sorts customers into four types.
Type · Responds if contacted? · Responds if not contacted? · Uplift · ActionPersuadables · Yes · No · Positive · Contact
Sure things · Yes · Yes · Zero · Do not spend
Lost causes · No · No · Zero · Do not spend
Sleeping dogs · No · Yes · Negative · Do not contact
The last group is the surprise: some customers are put off by being contacted, and leave or complain. A model that ranks by probability of buying will happily target them.
A worked example
A campaign contacted 10,000 customers and held out another 10,000 as a control. Overall, 6.0 per cent of the contacted customers converted against 4.5 per cent of the control, an average uplift of 1.5 points. That looks fine. But split by age. Among 5,000 younger customers, 8 per cent of those contacted converted against 4 per cent of the control, an uplift of 4 points. Among 5,000 older customers, 4 per cent of those contacted converted against 5 per cent of the control, an uplift of −1 point. The older group was slightly put off.
Each conversion is worth ₹2,000 of margin and each contact costs ₹20. Contacting all 10,000 customers adds 1.5 per cent × 10,000 = 150 conversions worth ₹3,00,000, at a cost of ₹2,00,000, a profit of ₹1,00,000. Contacting only the 5,000 younger customers adds 4 per cent × 5,000 = 200 conversions worth ₹4,00,000, at a cost of ₹1,00,000, a profit of ₹3,00,000. Contacting only the older customers would lose 1 per cent × 5,000 = 50 conversions, a loss of ₹1,00,000 in margin on top of ₹1,00,000 in cost, a loss of ₹2,00,000. Targeting by uplift triples the profit of the blanket campaign.
Building an uplift model
The simplest way to estimate individual uplift, the two-model approach, fits one model for the contacted group and another for the control group, and subtracts the predicted probabilities for each customer. A second approach adds the treatment flag and its interactions as features in a single model. Specialised methods model the uplift directly. Evaluating an uplift model is harder than evaluating a prediction, because we never see both outcomes for one person. The standard tools rank customers by predicted uplift and compare the treated-minus-control difference in each band, drawing an uplift curve, the analogue of the gain curve in Lesson 6.2. The data must come from a randomised experiment, so that the groups are comparable.
python
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.neuralnetwork import MLPClassifier
from sklearn.pipeline import makepipeline
from sklearn.preprocessing import StandardScaler
a small neural network on a table: scaling is essential, early stopping guards against overfitting
mlp = makepipeline(StandardScaler(),
MLPClassifier(hiddenlayersizes=(32, 16), earlystopping=True,
validationfraction=0.15, randomstate=0, maxiter=500))
mlp.fit(Xtrain, ytrain)
difference-in-differences as a regression: the coefficient on treated:after is the effect
did = smf.ols("complaints ~ treated + after + treated:after", data=branchmonths).fit()
print(did.params["treated:after"], did.confint().loc["treated:after"].tolist())
two-model uplift: one model per arm, then subtract the predicted probabilities
treated = df[df["contacted"] == 1]
control = df[df["contacted"] == 0]
mt = HistGradientBoostingClassifier().fit(treated[features], treated["converted"])
mc = HistGradientBoostingClassifier().fit(control[features], control["converted"])
df["uplift"] = mt.predictproba(df[features])[:, 1] - mc.predictproba(df[features])[:, 1]
contact only those whose expected extra margin beats the cost
margin, cost = 2000, 20
target = df[df["uplift"] margin > cost]
Common mistakes
Using a deep network on a small table. Boosted trees are usually better and cheaper.
Forgetting to scale inputs for a network. Training becomes slow or unstable.
A learning rate that is too high. The loss bounces or explodes. Lower it.
No early stopping on a large network. It will overfit.
Reading an observational association as an effect. Confounding can reverse it.
Difference-in-differences without checking trends. Parallel trends must hold before the change.
Targeting by purchase probability. It finds sure things and sleeping dogs. Use uplift.
Building uplift models without a randomised control group. The data cannot separate cause from selection.
Judging an uplift model by accuracy. Use uplift curves on held-out experimental data.
Key takeaways
A neuron is a weighted sum plus a bias passed through an activation. A network stacks layers of neurons, and logistic regression is the single-neuron case.
Training minimises a loss by gradient descent, small steps against the slope, with the step size set by the learning rate. In a simple example, too small is slow, a good rate converges, and a rate above 1 diverges.
On images, audio and text, deep learning leads. On tabular data, boosted trees are usually as good or better, with less tuning and more explainability.
The causal effect needs a comparison with what would have happened otherwise. Randomised experiments provide it, and observational methods rely on assumptions that must be argued for.
Difference-in-differences estimates an effect as the treated group's change minus the control group's change, here −6 − (−2) = −4, and relies on parallel trends.
Uplift modelling targets the customers whose behaviour the action changes. In the example, contacting only the younger customers earned ₹3 lakh against ₹1 lakh for contacting everyone, because older customers had negative uplift.
Check yourself
1. A neuron has inputs 3 and −1, weights 0.4 and 0.6, and a bias of 0.2. What are z and the ReLU output?
z = 1.2 − 0.6 + 0.2 = 0.8, and ReLU gives 0.8.
2. A network has 20 inputs, a hidden layer of 10 neurons and one output. How many parameters does it have?
(20 × 10 + 10) + (10 × 1 + 1) = 210 + 11 = 221.
3. In gradient descent on (w − 3)², with learning rate 0.25, by what factor does the distance from 3 shrink each step?
1 − 2 × 0.25 = 0.5.
4. Treated outcomes fall from 50 to 40 and control outcomes from 48 to 46. What is the difference-in-differences estimate?
(40 − 50) − (46 − 48) = −10 + 2 = −8.
5. A segment has a conversion rate of 3 per cent if contacted and 4 per cent if not. Should you contact it?
No. The uplift is −1 point, so contact lowers conversion and also costs money.
Try this
Use a dataset with a yes-or-no outcome, and, if you can, an experiment with a control group. Set aside forty-five minutes.
1. Work the one-neuron and the two-layer forward pass by hand with numbers of your own, and check the one-neuron result with the simulator.
2. Fit a small neural network and a boosted tree on the same data, with scaling for the network and early stopping for both. Compare their held-out scores and their training times.
3. Use the gradient descent simulator to find a learning rate that converges in about ten steps and one that diverges, and write what you see.
4. Find a before-and-after change in your work, such as a process change at some sites and not others. Compute the simple change, the control change and the difference-in-differences, and say how you would check parallel trends.
5. If you have an experiment, compute the average uplift and the uplift in at least three segments. Identify any segment with negative uplift.
6. Use the uplift simulator with your margin and cost to find the break-even uplift, and decide which segments clear it.
7. Write a short note: whether a neural network was worth the effort on your table, the causal estimate and its main assumption, and which group you would and would not target.
A good result is a note with one causal estimate, the assumption it needs, and a targeting rule based on uplift and cost. That completes Module 7. You can explain models, handle rare events, work with text, understand neural networks and move from prediction to cause. The last module takes everything into the real world: putting models into production, keeping them healthy, a full scorecard case study and presenting your work.
Tags: Data Science Neural networks Causal inference Uplift modelling Difference in differences