Confounding and Colliders
A confounder feeds both treatment and outcome, so you must adjust for it. A collider is fed by both, so adjusting for it creates bias.
Why Does This Exist?
Your model aces the training split, then flops the moment you act on it. That gap is often not overfitting. It's the wrong adjustment: you left a shared cause out, or you let a shared consequence in.
Predictive models love extra columns. Causal questions punish them. Add a shared cause and you leave bias in. Add a shared consequence and you manufacture bias from clean data. On this page you'll see both directions with hand counted tables: a 16.7 point coffee gap that's all smoking, and an 8 person talent table where filtering on fame invents a 50 point beauty gap.
This page teaches the two junctions and the opposite fix for each. Causal graphs is the full map with the backdoor rule. Correlation vs causation explains why the raw contrast misleads in the first place. From here, propensity score methods show which list belongs in the adjustment, and randomized experiments show what to do when the needed cause was never measured.
Basics first: a causal graph draws one arrow for each direct effect, like smoking toward heart trouble. Treatment means the exposure you could change, outcome means the result you measure, third variable means any other cause that comes first, and means a downstream consequence both can cause. If you can read those four letters, the rest is bookkeeping.
Think of It Like This
Rainy Saturday and yearbook picks
You notice puddles and umbrella sightings move together on Saturdays. Rain drives both: it fills the puddles and it sends people out with umbrellas. Umbrellas do not cause puddles. Compare rainy days to rainy days and dry days to dry days and the link collapses. Rain is the confounder , pavement wetness is , umbrella sales are .
Now picture yearbook picks. Staff pick a student who has great grades or a great jump shot. Among picked kids, grades and jump shots look traded off. Meet a picked kid with average grades and you would bet they can shoot, since something got them picked. Neither causes the other. Filtering on the pick, the collider , manufactured the link. The fix is to stop filtering, not to adjust harder.
Where it stops: rain does not choose friends, hide income, or misreport habits. Real causes do all three. The valve logic still holds, but one missing arrow on your graph still ruins the estimate even when the comparison logic is perfect.
How It Actually Works
A causal graph is a directed acyclic graph, called a DAG. Each node is a variable and each arrow says changing changes through that direct link. Acyclic means you can never follow arrows forward back to the start. means the share of outcome among those observed with treatment , while means the share if you forced everyone to . You want the second, but the data hand you the first.
The fork that fakes an effect
A fork stays open by default. Correlation travels against the arrows: and move together through their shared cause even when does nothing to . That open route is a backdoor path.
You close it by conditioning on , meaning compare treated and control within each level of , then average with population weights:
Here means one stratum such as smokers, means its population share, and the sum means the weighted average across strata. Leave out and the crude contrast mixes causation with background imbalance.
The collider that invents one
A collider stays blocked by default. Two independent causes look unlinked until you filter on their shared consequence. Conditioning on opens it: learning makes likelier among the selected, since something had to cause selection.
So you leave alone. Don't add it to a regression, don't match on it, and don't filter the sample on it. Conditioning on a descendant of opens the path too, which is why post treatment surveys and opt in samples are dangerous.
Worked example: 12 coffee drinkers, gap 16.7 to zero
Take 12 people as a teaching table, not a trial. is daily coffee, is a heart flag, is smoking. Smokers drink more coffee and carry higher baseline risk, so drives both.
- Non smokers, , 6 people. : 4 people, in 0, so . : 2 people, in 0, so . Stratum gap is .
- Smokers, , 6 people. : 2 people, in 1, so . : 4 people, in 2, so . Stratum gap is .
Pool everything and smoking does the talking:
Crude gap is , a 16.7 point gap with a true effect of zero. The bias is the full 16.7 points.
Fix it with population weights and :
Adjusted gap is . The diagram draws this pair as the crude bars collapsing after smoking is fixed. Adjustment works here only because smoking was measured and the graph has no other backdoor path.
Worked example: 8 faces, independence turned negative
Take 8 people, 2 in each combo, as a teaching table. is high talent, is high beauty, means famous when or . Talent and beauty start independent.
Population, all 8: and . Gap is .
Famous, , 6 people after dropping the 2 with neither trait: but . Gap is , a 50 point negative link that never existed. Knowing a famous person lacks talent tells you they likely bring beauty, since something earned the fame. The diagram draws 50 against 50 turning into 50 against 100 once fame filters the sample.
Code
The point is the check: rebuild both teaching tables from counts and confirm the hand gaps.
crude_control: float = 1 / 6crude_treated: float = 2 / 6gap_crude: float = crude_treated - crude_control
strat_gap: float = 0.5 - 0.5
pop_talent: float = 2 / 4pop_no_talent: float = 2 / 4gap_pop: float = pop_talent - pop_no_talent
famous_talent: float = 2 / 4famous_no_talent: float = 2 / 2gap_famous: float = famous_talent - famous_no_talent
print(f"{crude_control:.3f} {crude_treated:.3f} {gap_crude:.3f}") # -> 0.167 0.333 0.167print(f"{strat_gap:.3f}") # -> 0.000print(f"{gap_pop:.2f} {gap_famous:.2f}") # -> 0.00 -0.50The printout matches the hand computation: crude 0.167 against 0.333 for the 16.7 point bias with a zero stratified gap, then 0.00 population gap against a minus 0.50 famous gap.
Watch Out For
The healthy user who faked a heart benefit
Symptom: an observational file says a therapy protects the heart, yet a randomized trial disagrees. The prescribed group was healthier, wealthier, and better cared for, so care and habits played the role of . Fix: draw socioeconomic status and baseline care before modeling, adjust for those shared causes, and never trust the unadjusted contrast. When the key cause was never measured, admit it and run A/B testing instead of adding more columns.
The hospital file that linked two unrelated diseases
Symptom: among admitted patients, two independent conditions look negatively linked. Admission favors having at least one, so it plays the role of . Learning one condition is absent raises the odds of the other, since something drove admission. Fix: don't condition on admission, don't train only on the selected sample, and model the selection itself when you must work with it.
The Quick Version
- A fork stays open and fakes an effect, so you must condition on the shared cause .
- In the 12 person coffee table the crude heart rates are 1 of 6 against 2 of 6, a 16.7 point gap, while both smoking strata have gaps of zero.
- A collider stays blocked until touched, so you must leave the shared consequence alone.
- In the 8 person talent table the beauty rates are 50 percent in both talent groups, but among the 6 famous the rates are 50 percent against 100 percent.
- Stratifying on a shared cause removes confounding, while filtering on a shared consequence manufactures selection bias.
- When the needed shared cause was never measured, no adjustment rescues the estimate and randomization is the fix.