Conditional Independence Testing, in Pictures
TL;DR
To test whether and are linked once is known, replace both by their residuals given , and , multiply them and average the products. Divided by its standard error, that average is close to standard normal if and are independent given . The idea is that of the partial correlation, whose test goes back to Fisher (1924). In simulated factory data, temperature and scrap look unrelated: the best line through all orders is flat. Replace scrap by , then temperature by , and the same orders show a clear trend.
The question
A factory makes footballs, volleyballs and basketballs. Some balls come off the line below standard and are thrown away: the scrap. For each order we know its ball type , the temperature at one machine, M2, which we call , and the share of balls thrown away, . The factory and its 90 orders, 30 per ball type, are simulated.
The ball type affects both: each type runs at its own temperature and has its own scrap rate. That alone links temperature and scrap, but it is no news, and no one can change the ball type of an order. The question is whether temperature tells us anything about scrap beyond the ball type:
The ball type drives both temperature and scrap. Is there more between them?
The answer first
Let be the average scrap and the average temperature of ball type . The residuals are what is left of scrap and temperature once the ball type is taken out:
In the data, and are estimated, here by the averages of each type, and . Type averages are the simplest regression on ; any regression that predicts and from will do, which matters once holds many columns (more at the end). For order , that gives and . The test multiplies the two residuals, and divides the mean of the products by its standard error:
Up to the denominator, is the covariance of and . The rest of this post builds it up in pictures, from against to against , one axis at a time: first becomes , then becomes .
Ignoring the ball type: against
Plot temperature against scrap for all 90 orders:
All orders, both variables standardised.
A round cloud, the kind two independent normal variables give. Draw the best straight line through it:
The best straight line through all orders.
It is flat, with slope +0.02. The same statistic with the overall averages in place of the type averages is . Nothing to see.
Colour the same orders by ball type:
The same orders, coloured by ball type.
The types sit in different places. Footballs run hot and scrap little, basketballs run cool and scrap more, volleyballs lie in between. Across types, hotter means less scrap. Within each colour, the orders rise from left to right: hotter means more scrap. The two cancel, and the flat line of the first picture is what is left.
Step 1: replace by
Mark each type’s average scrap, , with a dashed line in its colour:
The dashed lines mark each type’s average scrap, .
Footballs scrap least, basketballs most. That difference is what the ball type alone explains, so take it out: move each type’s line to zero, and its orders with it.
Each arrow moves a type’s line, and all its orders, to zero: footballs up, basketballs down.
Every order moves by its type’s average: its scrap becomes .
against .
The colours now share one height. Within each colour, the orders still rise from left to right; nothing within a type has moved. Draw the best line through all orders again:
against , with the best line through all orders.
It rises, with slope +0.42. Three reasons why is the right thing to put on the y-axis:
- It is what is new. is the scrap we expect from the ball type alone, known before anyone looks at the temperature. is the surprise: how much more or less order scraps than its type usually does. “Does temperature tell us anything about scrap beyond the ball type?” becomes “Does temperature predict ?”
- Without a link, the cloud is flat. If , the temperature adds nothing to what the ball type says about scrap: . So averages to zero at every temperature, , and the best line is flat up to noise, whatever the ball types do. In against there is no such guarantee: the differences between types alone can tilt the line either way, or, as here, hide a trend. Its slope says nothing either way.
- Only the relation within each type is left. The covariance of with is the average of the covariances within each type: . The pull between types is gone.
Step 2: replace by
The colours now share one height, but still sit side by side along the temperature axis. Mark each type’s average temperature, :
The dashed lines mark each type’s average temperature, .
Move each type’s line to zero, and its orders with it:
Each arrow moves a type’s line, and all its orders, to zero: footballs left, basketballs right.
Every order’s temperature becomes :
against .
The three colours merge into one cloud, centred on zero, that runs from bottom left to top right. The best line through it:
against , with the best line.
It rises steeply, with slope +0.73: an order that runs hotter than usual for its ball type scraps more than usual for its ball type. That is the finding, and the first picture hid it.
Step 1 alone already gives a flat line without a link. Step 2 is still worth taking:
- It removes noise. How far the types sit apart in temperature has nothing to do with . That spread flattens the line, from +0.73 to +0.42, and adds noise to the products.
- Errors in stop mattering. is estimated, not known. Whatever error it carries is a function of the ball type, and is uncorrelated with every function of the ball type. So an error in alone doesn’t bias , nor does one in alone; only the two errors multiplied do.
- It is the familiar regression. The slope of on is the coefficient of temperature in a linear regression of scrap on temperature and ball type (Frisch–Waugh–Lovell).
Last, the products that averages. Their sign depends only on the quadrant an order lies in:
The products are positive in the green quadrants, negative in the red ones.
An order in the top right is hotter and scrappier than its type’s average, one in the bottom left cooler and cleaner: both give a positive product. Orders in the other two quadrants give negative ones. Without a link, the quadrants would balance and would be near zero. Here most orders sit in the green ones.
From an average to a decision
Without a link, the products average to zero. That follows from step 1: averages to zero at every temperature and ball type, and depends on nothing else, so
The mean then scatters around zero, by an amount that shrinks as orders accumulate. The central limit theorem says by how much: is close to normal, with the standard deviation of a single product, . Dividing by its estimate is the quotient in , and it puts every test on the same scale:
That gives the decision rule. A standard normal lands beyond only 5% of the time, so we reject independence at the 5% level if . The p-value is , with the standard normal distribution function.
The distribution of under independence. The shaded tails beyond ±1.96 hold 5%.
Ignoring the ball type, the statistic is :
ignoring the ball type.
It lies in the middle of the bell: no reason to doubt independence. Given the ball type, the statistic is :
given the ball type.
It lies far out in the tail, where a standard normal lands less than once in ten million draws.
When the ball type is not enough
Three ball types make and easy: an average per type. In practice holds more, such as the material, the season and the machine settings, some of it continuous, often dozens of columns. There is no average per combination of all of them, so the averages become regressions: any model that predicts or from , such as a random forest or gradient boosting. The regressions are fitted on one part of the data and give residuals on another, so that a model that memorises its training orders doesn’t leave residuals that are too small. Everything after the residuals stays as above: their products, and the decision rule.
With any regression in place of the averages, the test is known as the generalised covariance measure. Shah and Peters work out when stays close to standard normal: as long as the errors of and , multiplied, shrink faster than . That is the second reason for step 2, in general: each regression only has to be fairly good, because their errors multiply. With three ball types and 30 orders each, the type averages are good enough.
Some assumption is needed
The same paper shows that a condition of this kind can’t be avoided. A test keeps its level if, without a link, it rejects at most 5% of the time; it has power if, with a link, it rejects more often than that. Once is continuous, a test that keeps its level for every distribution without a link has no power against any distribution with one.
The reason is that can hide anything. Say is the time an order starts, to the second, and temperature and scrap both follow a pattern that flips every few seconds. Given , they are independent: knowing the second, the pattern is known. But 90 orders never land on the same second twice, so no regression can learn the pattern from them. The residuals keep it, they move together, and is large, although there is no link given . A test that is never fooled by such a pattern, however fine, must also ignore every real link, because the data can’t tell the two apart. So every test rests on an assumption about ; here, that the regressions can learn and .
All steps at once
From against to against : colour by ball type, move each type’s scrap to zero, then its temperature. The best line is refitted as the orders move.
Caveats
measures one thing: the covariance of and , averaged over all orders. A link whose products cancel or thin out in that average goes unseen. That comes from the covariance, not from conditioning on : any test built on a covariance or a straight line shares it, the plain correlation test of the first picture included. The first three caveats are links of that kind, each drawn with a flat best line.
U-shapes
If orders that run much hotter or much cooler than usual both scrap more, the products in the four quadrants cancel. sees only the straight-line part of a link.
Links that depend on
Another set of orders, as residuals, with a flat best line:
Simulated residuals with a flat best line. finds nothing.
The points form an X, not a round cloud. Colour them by ball type:
The same residuals by ball type.
Hotter means more scrap for footballs and less for basketballs, and the products cancel across types. If temperature matters for footballs alone, the other types add products without signal, and the link thins out. and take the ball type out of each variable on its own; nothing in lets the link between them change with the ball type.
Links that depend on another signal
This post keeps to one signal and one kind of scrap. A real line records many signals, scrap has several reasons, and a test like this one takes one signal at a time. The same X can hide behind a flat line:
Simulated residuals with a flat best line.
This time the colours that split it are not the ball type but another signal, whether the line runs fast:
The same residuals, by line speed.
If temperature raises scrap when the line runs fast and lowers it when it runs slow, the products cancel, as for the ball types. If it raises scrap only when the line runs fast, slow orders thin the link out. A test of temperature alone sees neither, and neither does a test of speed alone.
A link, not a cause
A large says that temperature and scrap are linked beyond the ball type, not how. Something unobserved, , such as a raw-material batch that heats the machine and weakens the balls, links them as well:
An unobserved drives both temperature and scrap.
It gives the same pictures and the same as a direct effect of temperature on scrap. The test can’t tell the two apart; only knowledge of the process, or an experiment, can.
Significant is not relevant
grows with , so with enough orders even a tiny link gives a large . A link that raises scrap by 0.1 percentage points can be real and significant, and still too small to act on. How much it matters is a question of size, not of : the slope of on , in scrap per degree, says how much scrap a degree costs, and belongs next to .
Independent orders
The step to a standard normal assumes that the products of different orders are independent. Orders that follow each other in time, sharing a shift or a batch, are not, and then understates the noise, so comes out too large. A standard error computed over whole shifts or batches rather than single orders accounts for that. Scrap Hypotheses, Evidence at a Glance runs the test on many signals at once, next to feature importance.