Skip to content
wannacode

Correlation and cause

In short: Correlation shows that two things move together, not that one causes the other; a third cause or Simpson’s paradox can turn the conclusion around.

Problem

“The banner lifts orders”

Marketing put up a “Free delivery” banner. It was not a test: the banner was shown at someone’s discretion. The log has 10,000 visits: the device, whether the banner was seen (saw_banner), whether there was an order:

Pythondata: the shop tables

With the banner 5.66% of visits order, without it 3.63%. Marketing wants to show it to everyone: the gap is 2 points, and with the banner there are 56% more orders.

But does the banner cause it?

The idea

Who saw the banner?

Before you trust the gap, look at who is in each group:

Pythondata: the shop tables

Of 3000 visits from a desktop, 2400 saw the banner. Of 7000 from a phone, only 1400. And desktops order more often anyway: 7.70% against 2.99%.

The “with banner” group is full of desktops, and the “without banner” group of phones. So you are not comparing banner with no banner, but mostly desktop with phone. The device is a confounder: it affects both who sees the banner and who orders.

The idea

Split by device

Compare the banner within each device. Then the device is the same on both sides:

Pythondata: the shop tables

On desktop 7.50% order with the banner, 8.50% without it. On mobile 2.50% against 3.11%. The banner does worse on both devices, while overall it looked better. This is called Simpson’s paradox: the direction for everyone together is the opposite of the direction in each group.

More precisely, if you are curious

On each device on its own the difference is within the noise: 600 desktop visits without the banner are few.

Pythondata: the shop tables

So the honest conclusion is not “the banner hurts” but “there is no evidence that the banner helps”. The opposite direction overall comes entirely from the mix of devices.

Try it

Compute the paradox

Store in gap the difference in conversion with banner − without banner for each device, in points, and in overall_gap the same difference for all visits together.

Pythondata: the shop tables

The idea

Why the total flips

The overall conversion of a group is an average of the devices, weighted by how many visits from each device are in the group:

Pythondata: the shop tables

With the banner 63% of visits are from a desktop, without it only 10%. So the banner group gets a lot of the desktops’ high conversion: (180 + 35) orders from 3800 visits are 5.66%. The group without the banner is almost all phones: (51 + 174) from 6200 are 3.63%.

The overall gap does not come from the banner. It comes from where the banner was shown: where people order more anyway.

The idea

Correlation: corr

The same mistake lurks in the numbers for the year. Here are the units sold by week: drinks (mineral water and orange juice) and vegetables (tomatoes and cucumbers). How much do they go together?

Pythondata: the shop tables

The correlation is a number from −1 to 1. Close to 1: when one is large, the other is large too, and the points lie around a line going up. Around 0: there is no such link. Close to −1: one grows, the other falls. a.corr(b) computes it, and df.corr() gives all pairs of columns at once.

Here it is 0.46: in the weeks with more vegetables, more drinks are sold too. Does that mean vegetables sell drinks?

The idea

The third thing: the season

Split the weeks into summer (June to August) and the rest:

Pythondata: the shop tables

In summer, on average 121.6 drinks and 68.2 vegetables sell per week, the rest of the time 81.5 and 38.5. Summer lifts both. Within the summer, though, the correlation is −0.13, and in the other weeks −0.15: about zero. The link comes from the season, not from the vegetables.

1. In the weeks when more tomatoes sell, more mineral water sells too. A colleague suggests: “Let’s cut back on tomatoes, to sell more water.” What is he missing?

The idea

Why the A/B test is different

In the previous lesson the new cart was better, and that was a cause, not just a link. The difference from the banner is one thing: who decides who sees what.

  • In the A/B test chance decides. In both groups desktops were 35%. Everything you do not measure is spread evenly as well.
  • With the banner someone decides. They show it mostly on desktop, and the groups differ in the device, and maybe in other things that are not in the table.

Splitting by device fixes only the factor you know about and have recorded. Random assignment fixes the ones you do not know about too. That is why, when you can, you run a test.

The idea

How to say it honestly

Data that were only collected, not from a test, are called observational. From them you can say “they go together”, not “one leads to the other”. Compare:

  • “The banner lifts orders by 2 points.” This is a claim about a cause, which the data do not give.
  • “Visits with the banner order more often, but mostly because they are from a desktop. On each device on its own the banner does not help. To answer this we need an A/B test.”

Before you trust a difference in observational data, ask:

  1. Who ends up in each group, and why?
  2. Is there a third thing (device, season, type of customer) that moves both?
  3. Does the difference flip if you split by it?

Exercise

A fair comparison

Compare the banner fairly: what is the conversion with and without the banner if both groups had the device mix of all visits? Store in desktop_share the share of desktops among all visits, and in with_banner and without_banner the weighted conversions in percent.

Pythondata: the shop tables

The gift card and the pan sell together: weeks with many cards have many pans too. Store in overall their correlation over all weeks, and in outside the correlation without the holiday weeks (the holidays mask: from 1 December to 4 January).

Pythondata: the shop tables

Test

Check yourself

1. What is Simpson’s paradox?
2. Why is the device a confounder for the banner?
3. The correlation between two columns is 0.46. What do you know for sure?
4. Why does the A/B test of the cart not suffer from the banner’s problem?
5. Which sentence about the banner log is honest?

Made by Ivo Gergov as part of Evo Tech's campaign for accessible programming education ·