Skip to main content

5  Joint distributions and dependence

Observations collected together often tell us more than isolated measurements. In a computing system, duration and memory use may grow together, or a workload category may affect both. A joint distribution describes how two or more random variables vary simultaneously.

5.1 Discrete joint and marginal laws

For discrete \(X,Y\), the joint probability mass function is

\[ p_{X,Y}(x,y)=\Pr(X=x,Y=y), \quad \sum_x\sum_y p_{X,Y}(x,y)=1. \]

Because the events indexed by \(y\) partition \(\{X=x\}\),

\[ p_X(x)=\sum_y p_{X,Y}(x,y),\qquad p_Y(y)=\sum_x p_{X,Y}(x,y). \]

These are the marginal distributions. Marginals discard information about the pairing of values. Many joint laws can share the same two marginal laws.

When \(p_X(x)>0\), the conditional PMF is

\[ p_{Y\mid X}(y\mid x)=\frac{p_{X,Y}(x,y)}{p_X(x)}. \]

Its sum over \(y\) is one. Conversely, \(p_{X,Y}(x,y)=p_X(x)p_{Y\mid X}(y\mid x)\) reconstructs the joint law from a marginal and conditional model.

5.1.1 Worked example: workload and queue state

Let \(X\) be the number of priority tasks (0, 1, or 2) in a short window, and \(Y\) indicate whether the queue is busy (0 or 1). Consider the following joint distribution:

\(X\) \(Y=0\) \(Y=1\) \(p_X(x)\)
0 .28 .07 .35
1 .22 .18 .40
2 .05 .20 .25
\(p_Y(y)\) .55 .45 1

The probability of a busy queue conditional on two priority tasks is \(0.20/0.25=0.8\). Conditional on a busy queue, the probability of two priority tasks is \(0.20/0.45=4/9\). These are different conditional laws. For example, the expected number of priority tasks when busy is

\[ \operatorname{E}[X\mid Y=1] =\frac{0(0.07)+1(0.18)+2(0.20)}{0.45} =\frac{58}{45}\approx1.289. \]

The unconditional mean is \(0.40+2(0.25)=0.90\). The conditional model provides a sharper description of a busy window.

5.2 Continuous joint densities

An absolutely continuous two-dimensional distribution has joint density \(f_{X,Y}(x,y)\ge0\) with \(\int_{\mathbb R^2}f_{X,Y}(x,y)\,dx\,dy=1\). For a measurable region \(R\),

\[ \Pr((X,Y)\in R)=\iint_R f_{X,Y}(x,y)\,dx\,dy. \]

The joint CDF is \(F_{X,Y}(x,y)=\Pr(X\le x,Y\le y)\). When the density is continuous, differentiating this CDF with respect to both coordinates recovers the joint density. The marginal densities are

\[ f_X(x)=\int_{\mathbb R} f_{X,Y}(x,y)\,dy,\qquad f_Y(y)=\int_{\mathbb R} f_{X,Y}(x,y)\,dx. \]

Conditional density, at \(x\) with \(f_X(x)>0\), is given by

\[ f_{Y\mid X}(y\mid x)=\frac{f_{X,Y}(x,y)}{f_X(x)}. \]

Strictly speaking, conditioning on \(X=x\) is not ordinary division by \(\Pr(X=x)\), which is zero. The density ratio describes a conditional law that integrates against the marginal to recover joint probabilities. For regular densities it can also be understood as the limit of conditioning on shorter and shorter intervals around \(x\).

5.2.1 Worked example: a triangular support

Suppose a normalized completion time \(X\) and a normalized intermediate time \(Y\) obey \(0<Y<X<1\) with constant joint density \(c\) on that region. Normalize by integrating with the correct limits:

\[ 1=c\int_0^1\int_0^xdy\,dx =c\int_0^1x\,dx=\frac c2. \]

Thus \(c=2\). The marginals (within \((0,1)\)) are

\[ f_X(x)=\int_0^x2dy=2x,\qquad f_Y(y)=\int_y^12dx=2(1-y). \]

For fixed \(X=x\), the conditional density of \(Y\) is \(1/x\) on \((0,x)\); that is, \(Y\mid X=x\sim U(0,x)\). Therefore \(\operatorname{E}[Y\mid X=x]=x/2\).

Uniform joint density on the triangle y less than x between zero and one; side panels show the increasing marginal density of X and decreasing marginal density of Y.

The triangular support and marginal densities.

Draw the support before integrating. In a nonrectangular region, the inner limits generally depend on the outer coordinate. For the triangle, integrating \(x\) first requires \(y<x<1\).

To calculate \(\Pr(Y<X/2)\), integrate over the smaller triangle:

\[ \Pr(Y<X/2)=\int_0^1\int_0^{x/2}2\,dy\,dx =\int_0^1x\,dx=\frac12. \]

The same answer follows from the conditional uniform law, \(\Pr(Y<x/2\mid X=x)=1/2\) for every admissible \(x\).

5.3 Independence of random variables

\(X\) and \(Y\) are independent if all events defined from one variable are independent of all events defined from the other. For discrete variables this is equivalent to \(p_{X,Y}(x,y)=p_X(x)p_Y(y)\) for all support points. For absolutely continuous variables it is equivalent to the factorization \(f_{X,Y}(x,y)=f_X(x)f_Y(y)\) almost everywhere.

The forward direction follows by applying independence to elementary events or joint CDFs. Conversely, summing or integrating the factored law over a rectangle \(A\times B\) gives \(\Pr(X\in A)\Pr(Y\in B)\).

The triangular model cannot be independent: both marginals are positive throughout \((0,1)\), but the joint density vanishes when \(y>x\). Independence would require positive joint density throughout the square.

If \(X,Y\) are independent and \(g(X),h(Y)\) integrable, the product of expectations factorizes:

\[ \operatorname{E}[g(X)h(Y)] =\operatorname{E}[g(X)]\operatorname{E}[h(Y)]. \]

This follows by summing or integrating the product of the two marginal functions. It is more general than the identity for \(XY\).

5.4 Covariance and correlation

Suppose \(X,Y\) have finite second moments. Define

\[ \operatorname{Cov}(X,Y)= \operatorname{E}[(X-\mu_X)(Y-\mu_Y)] =\operatorname{E}[XY]-\mu_X\mu_Y. \]

Covariance is symmetric and bilinear; for example \(\operatorname{Cov}(aX+b,cY+d)=ac\operatorname{Cov}(X,Y)\). For nonzero standard deviations the Pearson correlation is

\[ \rho_{X,Y}=\frac{\operatorname{Cov}(X,Y)}{\sigma_X\sigma_Y}. \]

To prove \(|\rho|\le1\), let \(U=(X-\mu_X)/\sigma_X\) and \(V=(Y-\mu_Y)/\sigma_Y\). Since \(\operatorname{E}U^2=\operatorname{E}V^2=1\), the nonnegative variance of \(U\pm V\) yields \(0\le2\pm2\operatorname{E}[UV]\). Thus \(-1\le\rho\le1\). Perfect correlation (absolute value one) occurs exactly when the centered variables are proportional almost surely.

Expanding the square of a centered sum yields

\[ \operatorname{Var}(aX+bY) =a^2\operatorname{Var}(X)+b^2\operatorname{Var}(Y) +2ab\operatorname{Cov}(X,Y). \]

For \(n\) variables, expansion yields all pairwise covariance terms:

\[ \operatorname{Var}\left(\sum_i X_i\right) =\sum_i\operatorname{Var}(X_i) +2\sum_{i<j}\operatorname{Cov}(X_i,X_j). \]

Independent variables with finite moments have zero covariance, so independence is sufficient for additive variance. It is not necessary: uncorrelated variables also have additive variance.

5.4.1 Worked example: covariance on the triangle

For the triangle above, \(\operatorname{E}X=2/3\), \(\operatorname{E}Y=1/3\), and both marginal variances equal \(1/18\). Calculate the mixed moment:

\[ \begin{aligned} \operatorname{E}[XY] &=\int_0^1\int_0^x 2xy\,dy\,dx\\ &=\int_0^1x^3dx=\frac14. \end{aligned} \]

The covariance is \(1/4-2/9=1/36\) and the correlation is \(1/2\).

5.4.2 Zero correlation does not imply independence

Let \(X\sim U(-1,1)\) and \(Y=X^2\). Since \(X\) is symmetric, \(\operatorname{E}X=\operatorname{E}X^3=0\). Hence \(\operatorname{Cov}(X,Y)=0\). But \(Y\) is completely determined by \(X\), so independence is impossible. For instance, the events \(|X|<1/2\) and \(Y<1/4\) are identical and each has probability \(1/2\); their intersection has probability \(1/2\), not \(1/4\).

Scatter plot of Y equal to X squared for a symmetric uniform X, showing the U-shaped relation despite zero correlation.

A nonlinear deterministic relationship can have zero Pearson correlation.

Correlation measures linear association, while independence rules out every form of statistical dependence. A scatterplot can reveal curvature that a single correlation coefficient misses.

5.5 Conditional expectation and total variance

For a discrete or continuous conditional model, define \(m(x)=\operatorname{E}[Y\mid X=x]\) by summing or integrating \(y\) against the conditional law. The quantity \(\operatorname{E}[Y\mid X]\) is the random variable \(m(X)\), not just one number.

The law of total expectation (tower property) states

\[ \operatorname{E}Y=\operatorname{E}\{\operatorname{E}[Y\mid X]\}. \]

For densities, multiply the inner integral by \(f_X(x)\) and integrate:

\[ \int\left[\int y f_{Y\mid X}(y\mid x)dy\right]f_X(x)dx =\iint y f_{X,Y}(x,y)\,dy\,dx=\operatorname{E}Y. \]

For finite second moments, the law of total variance is

\[ \operatorname{Var}(Y)= \operatorname{E}[\operatorname{Var}(Y\mid X)] +\operatorname{Var}(\operatorname{E}[Y\mid X]). \]

Proof. Write \(Y-\operatorname{E}Y=(Y-m(X))+(m(X)-\operatorname{E}Y)\). The expectation of the cross product vanishes because \(\operatorname{E}[Y-m(X)\mid X]=0\). The first squared term has expectation \(\operatorname{E}[\operatorname{Var}(Y\mid X)]\), and the second is \(\operatorname{Var}(m(X))\) by the tower property.

For the triangular model, \(\operatorname{Var}(Y\mid X=x)=x^2/12\). Since \(\operatorname{E}X^2=1/2\), the within-condition component is \(1/24\). The between-condition component is \(\operatorname{Var}(X/2)=1/72\), and their sum is \(1/18\). This is a useful template for separating measurement uncertainty from variation in the environments where measurements are collected.

5.6 Python experiment: joint moments from a simulation

The triangle can be sampled by drawing two independent uniforms, setting \(X=\max(U,V)\) and \(Y=\min(U,V)\). The two possible orderings each have original density one and map to the same triangle, yielding density two.

import numpy as np

rng = np.random.default_rng(505)
u = rng.random((150_000,2))
x = u.max(axis=1)
y = u.min(axis=1)
print("Means:", x.mean(), y.mean())
print("Covariance:", np.cov(x,y,ddof=0)[0,1])
print("Correlation:", np.corrcoef(x,y)[0,1])
print("P(Y < X/2):", (y<x/2).mean())
assert abs(np.corrcoef(x,y)[0,1]-.5)<.02

5.7 Practice problems

Work in your browser with Google Colab, or download the notebooks for Jupyter.

Practice notebook

Open in Colab Download .ipynb

Worked solutions

Open in Colab Download .ipynb

5.7.1 Exercise 1: The queue-state table

Use the discrete table in this chapter. Calculate \(\operatorname{E}Y\), \(\operatorname{E}[XY]\), \(\operatorname{Cov}(X,Y)\), and \(\operatorname{Var}(X+2Y)\).

\(\operatorname{E}Y=.45\), and \(\operatorname{E}[XY]=1(.18)+2(.20)=.58\). Covariance is \(.58-.90(.45)=.175\). \(\operatorname{E}X^2=.40+4(.25)=1.4\), so \(\operatorname{Var}(X)=.59\) and \(\operatorname{Var}(Y)=.45(.55)=.2475\). The required variance is \(.59+4(.2475)+4(.175)=2.28\).

5.7.2 Exercise 2: A second triangular model

Let \(f(x,y)=c\) for \(x\ge0\), \(y\ge0\), \(x+y\le2\), and zero outside. Find \(c\), \(f_X(x)\), \(f_{Y\mid X}(y\mid x)\), and \(\operatorname{E}[Y\mid X=x]\).

The region has area \(\int_0^2(2-x)dx=2\), so \(c=1/2\). For \(0<x<2\), \(f_X(x)=(2-x)/2\) and \(Y\mid X=x\sim U(0,2-x)\), giving conditional density \(1/(2-x)\) on the appropriate interval and conditional mean \((2-x)/2\).

5.7.3 Exercise 3: Two sources of uncertainty

Let \(X\sim\operatorname{Bernoulli}(0.3)\) and \(Y\mid X=x\sim N(2+4x,3^2)\). Find the unconditional mean and variance.

The mean is \(2+4(0.3)=3.2\). The conditional variance is 9 for both values of \(X\), and the conditional-mean variance is \(16(0.3)(0.7)=3.36\). Total variance is \(9+3.36=12.36\).

5.7.4 Exercise 4: Python — compute conditional models with matrices

Use the workload table. In NumPy compute the row and column marginals, both conditional tables, the means, covariance, and correlation. Verify the law of total expectation for \(X\) conditioned on \(Y\). Simulate jointly by sampling flattened cells according to their masses.

Row normalization gives the conditional distribution of \(Y\) given \(X\); column normalization gives that of \(X\) given \(Y\).

import numpy as np

p = np.array([[.28,.07],[.22,.18],[.05,.20]])
x = np.arange(3)
y = np.arange(2)
px, py = p.sum(axis=1), p.sum(axis=0)
yp = p/px[:,None]
xp = p/py[None,:]
ex, ey = x@px, y@py
cov = x@p@y-ex*ey
vx, vy = ((x-ex)**2)@px, ((y-ey)**2)@py
print("Y|X:", yp, "X|Y:", xp, sep="\n")
print("Means, covariance, correlation:", ex, ey, cov, cov/np.sqrt(vx*vy))
assert np.isclose((x@xp)@py,ex)
rng = np.random.default_rng(512)
idx = rng.choice(p.size,size=150_000,p=p.ravel())
a,b = np.unravel_index(idx,p.shape)
assert abs(np.cov(a,b,ddof=0)[0,1]-cov)<.01

5.7.5 Exercise 5: Python — demonstrate dependence with zero covariance

Generate 100,000 values \(X\sim U(-1,1)\), set \(Y=X^2\). Calculate Pearson correlation, draw a scatter plot, and calculate the probabilities of \(A=\{|X|<0.5\}\), \(B=\{Y<0.25\}\) and their intersection. Explain why a correlation-based test alone would not justify independence.

The exact correlation is zero, but the two events are equal.

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(514)
x = rng.uniform(-1,1,100_000)
y = x*x
a, b = np.abs(x)<.5, y<.25
print("Correlation:", np.corrcoef(x,y)[0,1])
print("Joint:", (a&b).mean(), "independent product:", a.mean()*b.mean())
assert np.array_equal(a,b)
assert abs(np.corrcoef(x,y)[0,1])<.02
plt.scatter(x[:3000],y[:3000],s=9,alpha=.35)
plt.xlabel("X")
plt.ylabel("Y = X squared")
plt.show()

The joint is near 0.5 and the product near 0.25. Pearson correlation captures linear association; it cannot detect every nonlinear dependence.

5.7.6 Exercise 6: Python — verify total variance

Simulate the mixture from Exercise 3 and estimate the two components of total variance separately. Compare their sum to the sample variance and to the exact 12.36.

import numpy as np

rng = np.random.default_rng(515)
n = 200_000
x = rng.random(n)<.3
y = rng.normal(2+4*x,3)
weights = np.array([(x==i).mean() for i in (0,1)])
means = np.array([y[x==i].mean() for i in (0,1)])
variances = np.array([y[x==i].var() for i in (0,1)])
within = weights@variances
between = weights@((means-weights@means)**2)
print("Within",within,"Between",between,"Total",y.var())
assert np.isclose(within+between,y.var())
assert abs(y.var()-12.36)<.15

With population-denominator sample variances, the decomposition is an exact algebraic identity for the empirical distribution as well.