# Computing the covaraince of two random variables given by a linear combination of samples drawn i.i.d from same distribution [duplicate]

Suppose we want to compute the covariance between $$z_1$$ and $$z_2$$ given as follows:

$$z_1 = a_1 \cdot x_1 + a_2\cdot x_2 \\ z_2 = a_3 \cdot x_1 + a_4 \cdot x_2\\ % x_1,x_2 \sim p(x)$$

If we compute the covariance between $$z_1$$ and $$z_2$$ we have:

$$\mathbb{E}[z_1\cdot z_2] - \mathbb{E}[z_1]\cdot \mathbb{E}[z_2] = \\ \mathbb{E}[a_1a_3 x_1x_1 + a_1a_4 x_1x_2 + a_2a_3 x_1x_2 + a_2a_4 x_2x_2] - a_1a_3\mathbb{E}[x_1]^2 -a_1a_4\mathbb{E}[x_1]\mathbb{E}[x_2] -a_3a_2\mathbb{E}[x_1]\mathbb{E}[x_2] -a_2a_4\mathbb{E}[x_2]^2 =\\ a_1a_3\text{cov}[x] -a_2a_4 \text{cov}[x]$$

In what I am studying, the last equality holds (or at least that is what I have understood), from the fact that $$x_1$$ and $$x_2$$ are independent hence their covariances is zero.

• That's not going to be true. Try the following in R: set.seed(2021); N <- 10; x <- rnorm(N); y <- rnorm(N); cov(x,y). I get a covariance of $\sim 0.8$, yet your conditions are satisfied. Even upping the sample size to $100,000$, I get a nonzero covariance.
– Dave
Commented May 17, 2021 at 14:17
• My question comes from the fact that independent samples have covariance zero. Perhaps I a missing something. Editing my question to make this clearer Commented May 17, 2021 at 14:23
• Welcome to CV, jdeJuan. I wonder if you are losing the distinction between population and sample? @Dave is most definitely giving an example with a sample. Commented May 17, 2021 at 14:36
• It looks like you goofed somewhere in your calculation, because covariance acts on two variables, not just the one $X$. (And what is $X$?)
– Dave
Commented May 17, 2021 at 14:38
• From $x$ to what? Covariance involves two variables.
– Dave
Commented May 17, 2021 at 15:23