Skip to main content
Statistics Math Data
View as Markdown Suggest changes

Conditional Independence Testing, in Pictures

· Reading time: 14 min
Conditional Independence Testing, in Pictures

TL;DR

To test whether XX and YY are linked once ZZ is known, replace both by their residuals given ZZ, εX\varepsilon_X and εY\varepsilon_Y, multiply them and average the products. Divided by its standard error, that average is close to standard normal if XX and YY are independent given ZZ. 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 εY\varepsilon_Y, then temperature by εX\varepsilon_X, 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 ZZ, the temperature at one machine, M2, which we call XX, and the share of balls thrown away, YY. 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:

X⊥ ⁣ ⁣ ⁣⊥Y∣Z  ?X \perp\!\!\!\perp Y \mid Z \;?
?Ball type Zfootball, volleyball or basketballTemperature Xat machine M2Scrap rate Yshare of balls thrown away

The ball type drives both temperature and scrap. Is there more between them?

The answer first

Let f(z)=E[Y∣Z=z]f(z) = \mathbb E[Y \mid Z = z] be the average scrap and g(z)=E[X∣Z=z]g(z) = \mathbb E[X \mid Z = z] the average temperature of ball type zz. The residuals are what is left of scrap and temperature once the ball type is taken out:

εY=Y−f(Z),εX=X−g(Z).\varepsilon_Y = Y - f(Z), \qquad \varepsilon_X = X - g(Z).

In the data, ff and gg are estimated, here by the averages of each type, f^\hat f and g^\hat g. Type averages are the simplest regression on ZZ; any regression that predicts XX and YY from ZZ will do, which matters once ZZ holds many columns (more at the end). For order ii, that gives εY,i=Yi−f^(Zi)\varepsilon_{Y,i} = Y_i - \hat f(Z_i) and εX,i=Xi−g^(Zi)\varepsilon_{X,i} = X_i - \hat g(Z_i). The test multiplies the two residuals, and divides the mean of the products by its standard error:

Ri=εX,i εY,i,T=n Rˉσ^,Rˉ=1n∑i=1nRi,σ^2=1n∑i=1n(Ri−Rˉ)2.R_i = \varepsilon_{X,i}\,\varepsilon_{Y,i}, \qquad T = \frac{\sqrt{n}\,\bar R}{\hat\sigma}, \qquad \bar R = \frac1n\sum_{i=1}^n R_i, \qquad \hat\sigma^2 = \frac1n\sum_{i=1}^n \big(R_i - \bar R\big)^2.

Up to the denominator, TT is the covariance of εX\varepsilon_X and εY\varepsilon_Y. The rest of this post builds it up in pictures, from XX against YY to εX\varepsilon_X against εY\varepsilon_Y, one axis at a time: first YY becomes εY\varepsilon_Y, then XX becomes εX\varepsilon_X.

Ignoring the ball type: XX against YY

Plot temperature against scrap for all 90 orders:

−3−2−10123−3−2−10123Temperature XScrap rate Y

All orders, both variables standardised.

A round cloud, the kind two independent normal variables give. Draw the best straight line through it:

−3−2−10123−3−2−10123Temperature XScrap rate YSlope +0.02

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 T=0.2T = 0.2. Nothing to see.

Colour the same orders by ball type:

−3−2−10123−3−2−10123Temperature XScrap rate YFootballVolleyballBasketball

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 YY by εY\varepsilon_Y

Mark each type’s average scrap, f^(z)\hat f(z), with a dashed line in its colour:

−3−2−10123−3−2−10123Temperature XScrap rate YFootballVolleyballBasketball

The dashed lines mark each type’s average scrap, f^(z)\hat f(z).

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.

−3−2−10123−3−2−10123Temperature XScrap rate YFootballVolleyballBasketball

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 YiY_i becomes εY,i=Yi−f^(Zi)\varepsilon_{Y,i} = Y_i - \hat f(Z_i).

−3−2−10123−3−2−10123Temperature XScrap residual εYFootballVolleyballBasketball

XX against εY\varepsilon_Y.

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:

−3−2−10123−3−2−10123Temperature XScrap residual εYSlope +0.42FootballVolleyballBasketball

XX against εY\varepsilon_Y, with the best line through all orders.

It rises, with slope +0.42. Three reasons why εY\varepsilon_Y is the right thing to put on the y-axis:

  • It is what is new. f^(Zi)\hat f(Z_i) is the scrap we expect from the ball type alone, known before anyone looks at the temperature. εY,i\varepsilon_{Y,i} is the surprise: how much more or less order ii scraps than its type usually does. “Does temperature tell us anything about scrap beyond the ball type?” becomes “Does temperature predict εY\varepsilon_Y?”
  • Without a link, the cloud is flat. If X⊥ ⁣ ⁣ ⁣⊥Y∣ZX \perp\!\!\!\perp Y \mid Z, the temperature adds nothing to what the ball type says about scrap: E[Y∣X,Z]=E[Y∣Z]=f(Z)\mathbb E[Y \mid X, Z] = \mathbb E[Y \mid Z] = f(Z). So εY\varepsilon_Y averages to zero at every temperature, E[εY∣X,Z]=0\mathbb E[\varepsilon_Y \mid X, Z] = 0, and the best line is flat up to noise, whatever the ball types do. In XX against YY 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 XX with εY\varepsilon_Y is the average of the covariances within each type: Cov⁡(X,εY)=E[Cov⁡(X,Y∣Z)]\operatorname{Cov}(X, \varepsilon_Y) = \mathbb E\big[\operatorname{Cov}(X, Y \mid Z)\big]. The pull between types is gone.

Step 2: replace XX by εX\varepsilon_X

The colours now share one height, but still sit side by side along the temperature axis. Mark each type’s average temperature, g^(z)\hat g(z):

−3−2−10123−3−2−10123Temperature XScrap residual εYFootballVolleyballBasketball

The dashed lines mark each type’s average temperature, g^(z)\hat g(z).

Move each type’s line to zero, and its orders with it:

−3−2−10123−3−2−10123Temperature XScrap residual εYFootballVolleyballBasketball

Each arrow moves a type’s line, and all its orders, to zero: footballs left, basketballs right.

Every order’s temperature XiX_i becomes εX,i=Xi−g^(Zi)\varepsilon_{X,i} = X_i - \hat g(Z_i):

−3−2−10123−3−2−10123Temperature residual εXScrap residual εYFootballVolleyballBasketball

εX\varepsilon_X against εY\varepsilon_Y.

The three colours merge into one cloud, centred on zero, that runs from bottom left to top right. The best line through it:

−3−2−10123−3−2−10123Temperature residual εXScrap residual εYSlope +0.73FootballVolleyballBasketball

εX\varepsilon_X against εY\varepsilon_Y, 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 εY\varepsilon_Y. That spread flattens the line, from +0.73 to +0.42, and adds noise to the products.
  • Errors in f^\hat f stop mattering. f^\hat f is estimated, not known. Whatever error it carries is a function of the ball type, and εX\varepsilon_X is uncorrelated with every function of the ball type. So an error in f^\hat f alone doesn’t bias Rˉ\bar R, nor does one in g^\hat g alone; only the two errors multiplied do.
  • It is the familiar regression. The slope of εY\varepsilon_Y on εX\varepsilon_X is the coefficient of temperature in a linear regression of scrap on temperature and ball type (Frisch–Waugh–Lovell).

Last, the products Ri=εX,i εY,iR_i = \varepsilon_{X,i}\,\varepsilon_{Y,i} that TT averages. Their sign depends only on the quadrant an order lies in:

++−−−3−2−10123−3−2−10123Temperature residual εXScrap residual εYSlope +0.73FootballVolleyballBasketball

The products RiR_i 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 Rˉ\bar R 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: εY\varepsilon_Y averages to zero at every temperature and ball type, and εX\varepsilon_X depends on nothing else, so

E[εX εY]=E[εX  E[εY∣X,Z]]=0.\mathbb E[\varepsilon_X\,\varepsilon_Y] = \mathbb E\big[\varepsilon_X\;\mathbb E[\varepsilon_Y \mid X, Z]\big] = 0.

The mean Rˉ\bar R then scatters around zero, by an amount that shrinks as orders accumulate. The central limit theorem says by how much: n Rˉ\sqrt n\,\bar R is close to normal, with the standard deviation of a single product, σ\sigma. Dividing by its estimate σ^\hat\sigma is the quotient in TT, and it puts every test on the same scale:

T≈N(0,1)if X⊥ ⁣ ⁣ ⁣⊥Y∣Z.T \approx \mathcal N(0, 1) \quad \text{if } X \perp\!\!\!\perp Y \mid Z.

That gives the decision rule. A standard normal lands beyond ±1.96\pm 1.96 only 5% of the time, so we reject independence at the 5% level if ∣T∣>1.96|T| > 1.96. The p-value is p=2 (1−Φ(∣T∣))p = 2\,\big(1 - \Phi(|T|)\big), with Φ\Phi the standard normal distribution function.

−4−3−2−1012345672.5%2.5%Test statistic T

The distribution of TT under independence. The shaded tails beyond ±1.96 hold 5%.

Ignoring the ball type, the statistic is T=0.2T = 0.2:

−4−3−2−1012345672.5%2.5%Ignoring ZT = 0.2Test statistic T

TT ignoring the ball type.

It lies in the middle of the bell: no reason to doubt independence. Given the ball type, the statistic is T=5.5T = 5.5:

−4−3−2−1012345672.5%2.5%Ignoring ZT = 0.2Given ZT = 5.5Test statistic T

TT 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 f^\hat f and g^\hat g easy: an average per type. In practice ZZ 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 XX or YY from ZZ, 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, TT 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 TT stays close to standard normal: as long as the errors of f^\hat f and g^\hat g, multiplied, shrink faster than 1/n1/\sqrt n. 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 ZZ 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 ZZ can hide anything. Say ZZ is the time an order starts, to the second, and temperature and scrap both follow a pattern that flips every few seconds. Given ZZ, 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 TT is large, although there is no link given ZZ. 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 ZZ; here, that the regressions can learn ff and gg.

All steps at once

From XX against YY to εX\varepsilon_X against εY\varepsilon_Y: colour by ball type, move each type’s scrap to zero, then its temperature. The best line is refitted as the orders move.

Caveats

TT measures one thing: the covariance of εX\varepsilon_X and εY\varepsilon_Y, 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 ZZ: 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

εXεY

If orders that run much hotter or much cooler than usual both scrap more, the products in the four quadrants cancel. TT sees only the straight-line part of a link.

Another set of orders, as residuals, with a flat best line:

εXεY

Simulated residuals with a flat best line. TT finds nothing.

The points form an X, not a round cloud. Colour them by ball type:

εXεYFootballBasketball

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. f^\hat f and g^\hat g take the ball type out of each variable on its own; nothing in TT lets the link between them change with the ball type.

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:

εXεY

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:

εXεYFastSlow

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 large TT says that temperature and scrap are linked beyond the ball type, not how. Something unobserved, UU, such as a raw-material batch that heats the machine and weakens the balls, links them as well:

?Ball type Zfootball, volleyball or basketballTemperature Xat machine M2Scrap rate Yshare of balls thrown awayUnobserved Ue.g. raw-material batch

An unobserved UU drives both temperature and scrap.

It gives the same pictures and the same TT 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

TT grows with n\sqrt n, so with enough orders even a tiny link gives a large TT. 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 TT: the slope of εY\varepsilon_Y on εX\varepsilon_X, in scrap per degree, says how much scrap a degree costs, and belongs next to TT.

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 σ^\hat\sigma then understates the noise, so TT 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.

AI Chat

Messages you send are processed by the Google Gemini API to generate responses. Do not share sensitive personal data. See the privacy policy for details.