Multivariate statistics: covariance and dependence Dr. Konrad Völkel
Heinrich Heine University Düsseldorf
Joint distribution and Bayes ¶ If X , Y : Ω → R X,Y \colon \Omega \to \mathbb{R} X , Y : Ω → R are two random variables with the same domain, we can form the joint distribution :
( X , Y ) : = ( X × Y ) ∘ Δ : Ω → R 2 , a ↦ ( X ( a ) , Y ( a ) ) . (X, Y) := (X \times Y) \circ \Delta \colon \Omega \to \mathbb{R}^2,\ a \mapsto (X(a),Y(a)). ( X , Y ) := ( X × Y ) ∘ Δ : Ω → R 2 , a ↦ ( X ( a ) , Y ( a )) . Here Δ : Ω → Ω × Ω \Delta \colon \Omega \to \Omega \times \Omega Δ : Ω → Ω × Ω is the diagonal map a ↦ ( a , a ) a \mapsto (a,a) a ↦ ( a , a ) .
For a random variable X : Ω → R n X \colon \Omega \to \mathbb{R}^n X : Ω → R n we can write X = ( X 1 , … , X n ) X = (X_1,\dots,X_n) X = ( X 1 , … , X n ) with X i : Ω → R X_i \colon \Omega \to \mathbb{R} X i : Ω → R given by π i ∘ X \pi_i \circ X π i ∘ X , where π i : R n → R \pi_i \colon \mathbb{R}^n \to \mathbb{R} π i : R n → R is the i i i -th coordinate projection.
The distributions of the X i X_i X i are called the marginal distributions , or margins , of X X X .
More generally, ( X 1 , X 3 , X 9 ) (X_1,X_3,X_9) ( X 1 , X 3 , X 9 ) is also called a marginal distribution of X X X , as is any composition of X X X with a linear projection π : R n → R m \pi \colon \mathbb{R}^n \to \mathbb{R}^m π : R n → R m – not necessarily onto a coordinate (hyper)plane of the standard basis of R n \mathbb{R}^n R n .
The distribution of X i X_i X i is P ( X i ∈ A ) = P ( X ∈ π i − 1 ( A ) ) P(X_i \in A) = P(X \in \pi_i^{-1}(A)) P ( X i ∈ A ) = P ( X ∈ π i − 1 ( A )) .
For a random variable ( X , Y ) : Ω → R 2 (X,Y) \colon \Omega \to \mathbb{R}^2 ( X , Y ) : Ω → R 2 we can compute the distribution of Y Y Y in the discrete case as
P ( Y ∈ A ) = ∑ x ∈ X ( Ω ) P ( ( X , Y ) ∈ { x } × A ) P(Y \in A) = \sum_{x \in X(\Omega)} P((X,Y) \in \{x\} \times A) P ( Y ∈ A ) = ∑ x ∈ X ( Ω ) P (( X , Y ) ∈ { x } × A ) (with the corresponding integral in the continuous case), having decomposed π 2 − 1 ( A ) = X ( Ω ) × A \pi_2^{-1}(A) = X(\Omega)\times A π 2 − 1 ( A ) = X ( Ω ) × A into disjoint subsets.
For a random variable ( X , Y ) : Ω → R 2 (X,Y) \colon \Omega \to \mathbb{R}^2 ( X , Y ) : Ω → R 2 ,
P ( ( X , Y ) = ( x , y ) ) = P ( X = x , Y = y ) = P ( Y = y ∣ X = x ) P ( X = x ) P((X,Y) = (x,y)) = P(X = x, Y=y) = P(Y = y | X = x)P(X = x) P (( X , Y ) = ( x , y )) = P ( X = x , Y = y ) = P ( Y = y ∣ X = x ) P ( X = x ) by the definition of conditional probability.
In the pmf notation we also write
p ( x , y ) = p ( y ∣ x ) p ( x ) p(x,y) = p(y|x)p(x) p ( x , y ) = p ( y ∣ x ) p ( x ) where this is to be read as a definition:
p ( y ∣ x ) = p ( x , y ) p ( x ) p(y|x) = \frac{p(x,y)}{p(x)} p ( y ∣ x ) = p ( x ) p ( x , y ) More precisely, one writes the distributions with their random variables as subscripts when several are in play:
p X , Y ( x , y ) = p Y ∣ X ( y ∣ x ) p X ( x ) , p_{X,Y}(x,y) = p_{Y|X}(y \mid x)\, p_X(x), p X , Y ( x , y ) = p Y ∣ X ( y ∣ x ) p X ( x ) , where, for each fixed x x x with p X ( x ) > 0 p_X(x) > 0 p X ( x ) > 0 , the function p Y ∣ X ( ⋅ ∣ x ) p_{Y|X}(\cdot \mid x) p Y ∣ X ( ⋅ ∣ x ) is the pmf of the conditional distribution of Y Y Y (likewise in the continuous case, with densities instead of pmfs). This formula is called the product rule ; the sum rule is the preceding remark – summing over the variable we are not interested in.
Let X , Y : Ω → R X, Y \colon \Omega \to \mathbb{R} X , Y : Ω → R be discrete random variables. Then the following are equivalent:
X X X and Y Y Y are independent (in the sense of the chapter Expected value and variance , section Independence );
the joint pmf factorises: p X , Y ( x , y ) = p X ( x ) p Y ( y ) p_{X,Y}(x,y) = p_X(x)\, p_Y(y) p X , Y ( x , y ) = p X ( x ) p Y ( y ) for all x , y x,y x , y ;
conditioning changes nothing: p Y ∣ X ( y ∣ x ) = p Y ( y ) p_{Y|X}(y \mid x) = p_Y(y) p Y ∣ X ( y ∣ x ) = p Y ( y ) for all x x x with p X ( x ) > 0 p_X(x) > 0 p X ( x ) > 0 and all y y y .
The independence of X X X and Y Y Y means P ( X = x , Y = y ) = P ( X = x ) P ( Y = y ) P(X = x, Y = y) = P(X = x)\,P(Y = y) P ( X = x , Y = y ) = P ( X = x ) P ( Y = y ) for all x , y x,y x , y , and that is literally statement 2. For the equivalence of 2 and 3 we substitute the definition p Y ∣ X ( y ∣ x ) = p X , Y ( x , y ) p X ( x ) p_{Y|X}(y \mid x) = \frac{p_{X,Y}(x,y)}{p_X(x)} p Y ∣ X ( y ∣ x ) = p X ( x ) p X , Y ( x , y ) : if 2 holds, the factor p X ( x ) > 0 p_X(x) > 0 p X ( x ) > 0 cancels, leaving p Y ∣ X ( y ∣ x ) = p Y ( y ) p_{Y|X}(y\mid x) = p_Y(y) p Y ∣ X ( y ∣ x ) = p Y ( y ) . If conversely 3 holds, then p X , Y ( x , y ) = p Y ∣ X ( y ∣ x ) p X ( x ) = p Y ( y ) p X ( x ) p_{X,Y}(x,y) = p_{Y|X}(y\mid x)\,p_X(x) = p_Y(y)\,p_X(x) p X , Y ( x , y ) = p Y ∣ X ( y ∣ x ) p X ( x ) = p Y ( y ) p X ( x ) for all x x x with p X ( x ) > 0 p_X(x) > 0 p X ( x ) > 0 ; for x x x with p X ( x ) = 0 p_X(x) = 0 p X ( x ) = 0 both sides are 0.
Statement 3 is the generalisation of P ( A ∣ B ) = P ( A ) P(A \mid B) = P(A) P ( A ∣ B ) = P ( A ) from the chapter Expected value and variance : knowing X X X does not shift the distribution of Y Y Y . In the continuous case the same equivalence holds with densities, but only “almost everywhere” – densities, after all, are determined only up to null sets (see Continuous distributions ).
Bayes’ theorem.
Given two events A , B ⊆ Ω A,B \subseteq \Omega A , B ⊆ Ω ,
P ( A ∣ B ) = P ( B ∣ A ) P ( A ) P ( B ) . P(A|B) = \dfrac{P(B|A)P(A)}{P(B)}. P ( A ∣ B ) = P ( B ) P ( B ∣ A ) P ( A ) . Why the base rate matters. A test detects a rare disease reliably: 99 % 99\,\% 99 % of those who have it are detected, and only 1 % 1\,\% 1 % of the healthy get a false positive. Even so, after a positive test the probability of actually having the disease is small. The reason is the base rate P ( A ) P(A) P ( A ) : if only one person in a thousand is ill, then for every ill person correctly detected there are about ten healthy ones falsely flagged – the small error rate of 1 % 1\,\% 1 % is acting on a group a thousand times larger. In the language of the confusion matrix from the chapter Titanic EDA : the test is a classifier, the 99 % 99\,\% 99 % is its recall (its sensitivity ), the 1 % 1\,\% 1 % its false positive rate, and what we are after is its precision – the proportion of ill people among those who test positive. With A A A = “ill” and B B B = “test positive”, Bayes’ theorem performs exactly this weighting:
P ( A ∣ B ) = 0.99 ⋅ 0.001 0.99 ⋅ 0.001 + 0.01 ⋅ 0.999 = 0.00099 0.00099 + 0.00999 ≈ 0.09. P(A|B) = \dfrac{0.99 \cdot 0.001}{0.99 \cdot 0.001 + 0.01 \cdot 0.999} = \dfrac{0.00099}{0.00099 + 0.00999} \approx 0.09. P ( A ∣ B ) = 0.99 ⋅ 0.001 + 0.01 ⋅ 0.999 0.99 ⋅ 0.001 = 0.00099 + 0.00999 0.00099 ≈ 0.09. P ( A ∣ B ) P(A|B) P ( A ∣ B ) is emphatically not P ( B ∣ A ) P(B|A) P ( B ∣ A ) : it is P ( B ∣ A ) P(B|A) P ( B ∣ A ) times the ratio P ( A ) / P ( B ) P(A)/P(B) P ( A ) / P ( B ) , and P ( B ) P(B) P ( B ) is dominated by the large group of the healthy. Confusing the two conditional probabilities is the base rate fallacy – common enough to have a name of its own. For the Titanic rule “women survive”, recall 0.68 and precision 0.74 were close together, because survivors and women are about equally frequent; here a base rate of one in a thousand stands between 0.99 and 0.09. (Where the denominator comes from is settled by the law of total probability below.)
To work out the denominator P ( B ) P(B) P ( B ) , the following theorem often helps:
The sets B ∩ A i B \cap A_i B ∩ A i are pairwise disjoint and their union is B B B , so P ( B ) = ∑ i P ( B ∩ A i ) P(B) = \sum_i P(B \cap A_i) P ( B ) = ∑ i P ( B ∩ A i ) (finite additivity), and by the definition of conditional probability P ( B ∩ A i ) = P ( B ∣ A i ) P ( A i ) P(B \cap A_i) = P(B|A_i)\,P(A_i) P ( B ∩ A i ) = P ( B ∣ A i ) P ( A i ) .
In Bayes’ theorem you can therefore replace the denominator by P ( B ) = P ( B ∣ A ) P ( A ) + P ( B ∣ A c ) P ( A c ) P(B) = P(B|A)P(A) + P(B|A^c)P(A^c) P ( B ) = P ( B ∣ A ) P ( A ) + P ( B ∣ A c ) P ( A c ) when A A A and its complement are all you have to go on.
Bayes’ theorem, version 2.
Given two random variables X , Y : Ω → R n X,Y \colon \Omega \to \mathbb{R}^n X , Y : Ω → R n ,
P ( X = x ∣ Y = y ) = P ( Y = y ∣ X = x ) P ( X = x ) P ( Y = y ) . P(X=x | Y=y) = \dfrac{ P(Y=y|X=x)P(X=x) }{P(Y=y)}. P ( X = x ∣ Y = y ) = P ( Y = y ) P ( Y = y ∣ X = x ) P ( X = x ) . We often apply this as follows: Y Y Y is “known” and X X X is the provisional model.
We then call Y = y Y=y Y = y the data D D D and X = x X=x X = x the hypothesis H H H .
For data and hypothesis this gives
Bayes’ theorem, version 3.
P ( H ∣ D ) = P ( D ∣ H ) P ( H ) P ( D ) P(H|D) = \dfrac{ P(D|H) P(H) }{ P(D) } P ( H ∣ D ) = P ( D ) P ( D ∣ H ) P ( H ) Here P ( H ) P(H) P ( H ) is called the prior (the a priori probability of the hypothesis),
P ( D ) P(D) P ( D ) the evidence , P ( D ∣ H ) P(D|H) P ( D ∣ H ) the likelihood and P ( H ∣ D ) P(H|D) P ( H ∣ D ) the posterior (the a posteriori probability of the hypothesis). A way to remember it: the likelihood answers the question “how probable are the data, supposing the hypothesis is true?”.
Unlike P ( H ) P(H) P ( H ) , the likelihood P ( D ∣ H ) P(D|H) P ( D ∣ H ) can at least in principle be computed.
How to work out a likelihood. So far H H H and D D D have been events with finitely many outcomes. In practice the hypothesis is usually a whole distribution. Suppose we have n n n measurements D = ( x 1 , … , x n ) D = (x_1,\dots,x_n) D = ( x 1 , … , x n ) and work with normal distributions: the hypothesis H H H says that the data come from an N ( μ , σ 2 ) \mathcal{N}(\mu,\sigma^2) N ( μ , σ 2 ) with μ = 0 \mu = 0 μ = 0 and σ = 2 \sigma = 2 σ = 2 . The likelihood P ( D ∣ H ) P(D \mid H) P ( D ∣ H ) is then the probability (or probability density) of seeing exactly these n n n values if the hypothesis is right – and since the measurements are taken to be independent, the joint density factorises into a product by the proposition above:
P ( D ∣ H ) = ∏ i = 1 n p μ , σ ( x i ) = ∏ i = 1 n 1 2 π σ exp ( − ( x i − μ ) 2 2 σ 2 ) with μ = 0 , σ = 2. P(D \mid H) = \prod_{i=1}^n p_{\mu,\sigma}(x_i) = \prod_{i=1}^n \dfrac{1}{\sqrt{2\pi}\,\sigma} \exp\left( -\dfrac{(x_i - \mu)^2}{2\sigma^2} \right) \quad \text{with } \mu = 0,\ \sigma = 2. P ( D ∣ H ) = i = 1 ∏ n p μ , σ ( x i ) = i = 1 ∏ n 2 π σ 1 exp ( − 2 σ 2 ( x i − μ ) 2 ) with μ = 0 , σ = 2. Three roles have to be kept cleanly apart here: the data-generating distribution , which exists out in the world and which we do not know; the observed data D D D , which we have; and the model distribution , which we declare to be our hypothesis. The likelihood compares the second with the third. If we let μ \mu μ and σ \sigma σ vary instead of holding them fixed, the number becomes a function L ( μ , σ ) : = ∏ i = 1 n p μ , σ ( x i ) L(\mu,\sigma) := \prod_{i=1}^n p_{\mu,\sigma}(x_i) L ( μ , σ ) := ∏ i = 1 n p μ , σ ( x i ) , and searching for the parameters that maximise it is the maximum likelihood principle – see Statistical models and estimators , where taking logarithms turns the product into a sum.
Bayesian updating. Today’s posterior is tomorrow’s prior. In the disease-test example the probability of being ill after a positive test was about 0.09. If the test is repeated, independently of the first run, then 0.09 is now the prior P ( H ) P(H) P ( H ) , the likelihood of a positive result is still 0.99 for the ill and 0.01 for the healthy, and the evidence is again worked out by total probability:
P ( H ∣ D ) = 0.99 ⋅ 0.09 0.99 ⋅ 0.09 + 0.01 ⋅ 0.91 ≈ 0.91. P(H|D) = \dfrac{0.99 \cdot 0.09}{0.99 \cdot 0.09 + 0.01 \cdot 0.91} \approx 0.91. P ( H ∣ D ) = 0.99 ⋅ 0.09 + 0.01 ⋅ 0.91 0.99 ⋅ 0.09 ≈ 0.91. Two positive tests in a row turn one in a thousand first into nine per cent and then into ninety-one. Every new observation pushes the posterior further along, and the outcome depends on the initial prior: start at P ( H ) = 0.001 P(H) = 0.001 P ( H ) = 0.001 and you need two tests; start at 0.5 (because there are symptoms already, say) and a single test puts you at 0.99. This process is called Bayesian updating ; the chapter Statistical models turns the likelihood into a general estimation procedure.
Covariance ¶ First a warning familiar from the chapter Descriptive statistics : covariance and correlation measure a relationship across all the data points. If the data split into groups, the relationship within each group can have the opposite sign – see Simpson’s paradox . So the numbers in this chapter tell you something only once you know that the sample comes from a single population.
Given two random variables X , Y : Ω → R X,Y \colon \Omega \to \mathbb{R} X , Y : Ω → R , the covariance
is defined as
Cov ( X , Y ) : = E ( ( X − E X ) ( Y − E Y ) ) . \operatorname{Cov}(X,Y) := \mathbb{E}\left( \left( X - \mathbb{E}X \right) \left( Y - \mathbb{E}Y \right) \right). Cov ( X , Y ) := E ( ( X − E X ) ( Y − E Y ) ) . In particular, V ( X ) = Cov ( X , X ) \mathbb{V}(X) = \operatorname{Cov}(X,X) V ( X ) = Cov ( X , X ) .
The covariance suffers from the same complaint as the variance: it depends on the scale. The cure is to normalise by the standard deviations:
The (Pearson) correlation coefficient is
ρ ( X , Y ) : = Cov ( X , Y ) σ ( X ) σ ( Y ) . \rho(X,Y) := \dfrac{\operatorname{Cov}(X,Y)}{\sigma(X)\sigma(Y)}. ρ ( X , Y ) := σ ( X ) σ ( Y ) Cov ( X , Y ) . Clearly ρ ( X , Y ) = 0 \rho(X,Y)=0 ρ ( X , Y ) = 0 exactly when Cov ( X , Y ) = 0 \operatorname{Cov}(X,Y)=0 Cov ( X , Y ) = 0 .
One says that X X X and Y Y Y are positively correlated if ρ ( X , Y ) > 0 \rho(X,Y) > 0 ρ ( X , Y ) > 0 ,
negatively correlated (or anticorrelated) if ρ ( X , Y ) < 0 \rho(X,Y) < 0 ρ ( X , Y ) < 0 ,
and uncorrelated if ρ ( X , Y ) = 0 \rho(X,Y)=0 ρ ( X , Y ) = 0 .
For data. If x , y ∈ R n x, y \in \mathbb{R}^n x , y ∈ R n are given as two columns of a table, you replace the expected value in the definitions by the mean and obtain the empirical covariance and the empirical correlation coefficient
Cov ( x , y ) : = 1 n ∑ i = 1 n ( x i − x ˉ ) ( y i − y ˉ ) = 1 n x ~ ⋅ y ~ , r : = Cov ( x , y ) σ ( x ) σ ( y ) = x ~ ⋅ y ~ ∥ x ~ ∥ ∥ y ~ ∥ , \operatorname{Cov}(x,y) := \frac{1}{n} \sum_{i=1}^n (x_i - \bar x)(y_i - \bar y) = \frac{1}{n}\, \tilde x \cdot \tilde y, \qquad r := \frac{\operatorname{Cov}(x,y)}{\sigma(x)\,\sigma(y)} = \frac{\tilde x \cdot \tilde y}{\lVert \tilde x \rVert \, \lVert \tilde y \rVert}, Cov ( x , y ) := n 1 i = 1 ∑ n ( x i − x ˉ ) ( y i − y ˉ ) = n 1 x ~ ⋅ y ~ , r := σ ( x ) σ ( y ) Cov ( x , y ) = ∥ x ~ ∥ ∥ y ~ ∥ x ~ ⋅ y ~ , with the centred vectors x ~ , y ~ \tilde x, \tilde y x ~ , y ~ from the chapter Expected value and variance . The correlation coefficient of data is thus, quite literally, the cosine of the angle between the centred columns. It is the number np.corrcoef(x, y)[0, 1] prints in the chapter Titanic EDA ; np.cov(x, y, ddof=0) gives the covariance matrix in the n n n -version (without ddof=0, Numpy divides by n − 1 n-1 n − 1 here, unlike np.var). For the Iris exercise from the chapter Descriptive statistics you compute it once across all the rows and once per species:
x = datasets.load_iris().data; y = datasets.load_iris().target
print(np.corrcoef(x[:, 0], x[:, 1])[0, 1]) # -0.118
for s in range(3):
print(s, np.corrcoef(x[y == s, 0], x[y == s, 1])[0, 1]) # 0.743, 0.526, 0.457The covariance is symmetric: Cov ( X , Y ) = Cov ( Y , X ) \operatorname{Cov}(X,Y) = \operatorname{Cov}(Y,X) Cov ( X , Y ) = Cov ( Y , X )
The covariance is bilinear: Cov ( a X + b Y , Z ) = a Cov ( X , Z ) + b Cov ( Y , Z ) \operatorname{Cov}(aX+bY, Z) = a\operatorname{Cov}(X,Z) + b\operatorname{Cov}(Y,Z) Cov ( a X + bY , Z ) = a Cov ( X , Z ) + b Cov ( Y , Z ) for a , b ∈ R a,b \in \mathbb{R} a , b ∈ R .
The covariance is positive (semi)definite: Cov ( X , X ) = V ( X ) ≥ 0 \operatorname{Cov}(X,X) = \mathbb{V}(X) \geq 0 Cov ( X , X ) = V ( X ) ≥ 0 , and Cov ( X , X ) = 0 \operatorname{Cov}(X,X) = 0 Cov ( X , X ) = 0 holds exactly when X X X is almost surely constant.
Symmetry and semidefiniteness can be read straight off the definition (Cov ( X , X ) = E ( ( X − E X ) 2 ) ≥ 0 \operatorname{Cov}(X,X) = \mathbb{E}((X-\mathbb{E}X)^2) \geq 0 Cov ( X , X ) = E (( X − E X ) 2 ) ≥ 0 ). For bilinearity, a X + b Y − E ( a X + b Y ) = a ( X − E X ) + b ( Y − E Y ) aX + bY - \mathbb{E}(aX+bY) = a(X - \mathbb{E}X) + b(Y - \mathbb{E}Y) a X + bY − E ( a X + bY ) = a ( X − E X ) + b ( Y − E Y ) by linearity of the expected value; multiplying out with ( Z − E Z ) (Z - \mathbb{E}Z) ( Z − E Z ) and using linearity once more gives the claim. For the case of equality: E ( ( X − E X ) 2 ) = 0 \mathbb{E}((X-\mathbb{E}X)^2) = 0 E (( X − E X ) 2 ) = 0 says that the non-negative random variable ( X − E X ) 2 (X-\mathbb{E}X)^2 ( X − E X ) 2 has expected value 0, and hence is almost surely 0.
Symmetry and bilinearity give us the rules of calculation that come up constantly in practice:
Rules of calculation for the covariance. For random variables X , Y X, Y X , Y and a constant β ∈ R \beta \in \mathbb{R} β ∈ R ,
V ( X + Y ) = V ( X ) + V ( Y ) + 2 Cov ( X , Y ) \mathbb{V}(X+Y) = \mathbb{V}(X) + \mathbb{V}(Y) + 2\operatorname{Cov}(X,Y) V ( X + Y ) = V ( X ) + V ( Y ) + 2 Cov ( X , Y ) ,
Cov ( X , β ) = 0 \operatorname{Cov}(X, \beta) = 0 Cov ( X , β ) = 0 ,
Cov ( X + β , Y ) = Cov ( X , Y ) \operatorname{Cov}(X+\beta, Y) = \operatorname{Cov}(X,Y) Cov ( X + β , Y ) = Cov ( X , Y ) ,
Cov ( X , Y ) = E ( X Y ) − E ( X ) E ( Y ) \operatorname{Cov}(X,Y) = \mathbb{E}(XY) - \mathbb{E}(X)\,\mathbb{E}(Y) Cov ( X , Y ) = E ( X Y ) − E ( X ) E ( Y ) .
(1) V ( X + Y ) = Cov ( X + Y , X + Y ) = Cov ( X , X ) + Cov ( X , Y ) + Cov ( Y , X ) + Cov ( Y , Y ) \mathbb{V}(X+Y) = \operatorname{Cov}(X+Y, X+Y) = \operatorname{Cov}(X,X) + \operatorname{Cov}(X,Y) + \operatorname{Cov}(Y,X) + \operatorname{Cov}(Y,Y) V ( X + Y ) = Cov ( X + Y , X + Y ) = Cov ( X , X ) + Cov ( X , Y ) + Cov ( Y , X ) + Cov ( Y , Y ) by bilinearity, and the two middle terms are equal by symmetry.
(2) The constant random variable β \beta β has expected value β \beta β , so β − E β = 0 \beta - \mathbb{E}\beta = 0 β − E β = 0 and Cov ( X , β ) = E ( ( X − E X ) ⋅ 0 ) = 0 \operatorname{Cov}(X,\beta) = \mathbb{E}((X - \mathbb{E}X) \cdot 0) = 0 Cov ( X , β ) = E (( X − E X ) ⋅ 0 ) = 0 .
(3) Bilinearity and (2): Cov ( X + β , Y ) = Cov ( X , Y ) + Cov ( β , Y ) = Cov ( X , Y ) \operatorname{Cov}(X+\beta, Y) = \operatorname{Cov}(X,Y) + \operatorname{Cov}(\beta, Y) = \operatorname{Cov}(X,Y) Cov ( X + β , Y ) = Cov ( X , Y ) + Cov ( β , Y ) = Cov ( X , Y ) .
(4) Multiply out the definition and use linearity of the expected value:
E ( X Y − X E Y − Y E X + E X E Y ) = E ( X Y ) − 2 E X E Y + E X E Y = E ( X Y ) − E X E Y . \mathbb{E}\big(XY - X\,\mathbb{E}Y - Y\,\mathbb{E}X + \mathbb{E}X\,\mathbb{E}Y\big) = \mathbb{E}(XY) - 2\,\mathbb{E}X\,\mathbb{E}Y + \mathbb{E}X\,\mathbb{E}Y = \mathbb{E}(XY) - \mathbb{E}X\,\mathbb{E}Y. E ( X Y − X E Y − Y E X + E X E Y ) = E ( X Y ) − 2 E X E Y + E X E Y = E ( X Y ) − E X E Y . Rule (4) is the counterpart of the formula V ( X ) = E ( X 2 ) − ( E X ) 2 \mathbb{V}(X) = \mathbb{E}(X^2) - (\mathbb{E}X)^2 V ( X ) = E ( X 2 ) − ( E X ) 2 from the chapter Expected value and variance ; for X = Y X = Y X = Y the two coincide. From (1) it also follows that V ( X 1 + ⋯ + X n ) = ∑ i V ( X i ) \mathbb{V}(X_1 + \dots + X_n) = \sum_i \mathbb{V}(X_i) V ( X 1 + ⋯ + X n ) = ∑ i V ( X i ) whenever the X i X_i X i are pairwise uncorrelated.
Independent random variables are uncorrelated. If X X X and Y Y Y are independent (in the sense of the chapter Expected value and variance , section Independence) and have finite variance, then E ( X Y ) = E ( X ) E ( Y ) \mathbb{E}(XY) = \mathbb{E}(X)\,\mathbb{E}(Y) E ( X Y ) = E ( X ) E ( Y ) and hence Cov ( X , Y ) = 0 \operatorname{Cov}(X,Y) = 0 Cov ( X , Y ) = 0 .
In the discrete case, independence makes the joint pmf a product, p ( x , y ) = p X ( x ) p Y ( y ) p(x,y) = p_X(x)\,p_Y(y) p ( x , y ) = p X ( x ) p Y ( y ) , and the double sum splits:
E ( X Y ) = ∑ x ∑ y x y p X ( x ) p Y ( y ) = ( ∑ x x p X ( x ) ) ( ∑ y y p Y ( y ) ) = E ( X ) E ( Y ) . \mathbb{E}(XY) = \sum_{x}\sum_{y} x\,y\,p_X(x)\,p_Y(y) = \Big(\sum_x x\,p_X(x)\Big)\Big(\sum_y y\,p_Y(y)\Big) = \mathbb{E}(X)\,\mathbb{E}(Y). E ( X Y ) = x ∑ y ∑ x y p X ( x ) p Y ( y ) = ( x ∑ x p X ( x ) ) ( y ∑ y p Y ( y ) ) = E ( X ) E ( Y ) . In the continuous case the sums become integrals over the joint density p ( x , y ) = p X ( x ) p Y ( y ) p(x,y) = p_X(x)\,p_Y(y) p ( x , y ) = p X ( x ) p Y ( y ) (joint densities are introduced in the next chapter), and the calculation is the same. Rule (4) then gives Cov ( X , Y ) = E ( X Y ) − E X E Y = 0 \operatorname{Cov}(X,Y) = \mathbb{E}(XY) - \mathbb{E}X\,\mathbb{E}Y = 0 Cov ( X , Y ) = E ( X Y ) − E X E Y = 0 .
The converse fails: uncorrelated random variables can be dependent. Take X X X uniformly distributed on [ − 1 , 1 ] [-1,1] [ − 1 , 1 ] and Y = X 2 Y = X^2 Y = X 2 : then E ( X Y ) = E ( X 3 ) = 0 = E X E Y \mathbb{E}(XY) = \mathbb{E}(X^3) = 0 = \mathbb{E}X\,\mathbb{E}Y E ( X Y ) = E ( X 3 ) = 0 = E X E Y , so Cov ( X , Y ) = 0 \operatorname{Cov}(X,Y) = 0 Cov ( X , Y ) = 0 , and yet Y Y Y is a function of X X X . The covariance measures linear relationships only. For jointly normally distributed ( X , Y ) (X,Y) ( X , Y ) , on the other hand, the converse does hold, as we shall see in the next chapter.
The covariance therefore behaves like an inner product – but only “modulo constants”, since constant random variables have covariance 0 with everything, themselves included. This can be made rigorous by reading the covariance as an inner product on an L 2 \mathcal{L}^2 L 2 space of random variables in which two random variables differing only by a constant are identified. The correlation coefficient then plays the role of the cosine of the angle between two random variables. For data it literally is that (see the remark For data above); the “cosine similarity” so widespread in machine learning is the same cosine, just without the centring.
You can get some practice at “seeing” the correlation coefficient in a scatter plot of two variables,
for instance with the game “Guess the Correlation” by Omar Wagih .
Figure 1: A sample from a bivariate normal distribution, with the two marginal histograms.
The picture shows why the correlation cannot be read off the individual variables. Each of the two marginal histograms is an unremarkable bell curve; they would look exactly the same if the correlation had the opposite sign, or were 0, because the marginal distributions do not depend on it. The relationship shows only in the joint scatter plot, in the tilt of the cloud of points. This is the observation made above about the joint distribution: it cannot be reconstructed from the individual ones.
What covariance and correlation mean geometrically we will see in the next chapter, on the multivariate normal distribution: there the covariance matrix describes the shape and position of the ellipses that are the contour lines of the density.