# How to use the function Covariance to calculate covariance correctly?

Note: The following questions are from the 23th question of the 2005 Chinese Graduate Mathematical Entrance Examination (first set):

Suppose $$X_{1}, X_{2}, \cdots, X_{n}(n>2)$$ is a simple random sample from population $$\mathrm{N}(0,1)$$, and $$\bar{X}$$ is the sample mean ($$\bar{X}=\frac{X_1+X_2+\cdots+X_n}{n}$$), $$Y_{i}=X_{i}-\bar{X}$$, $$i=1,2, \cdots, n$$.

Now we need to solve the following problems:

(1) The variance $$D Y_{i}$$ of $$Y_{i}$$,$$i=1,2, \cdots, n$$.

(2) the covariance $$\operatorname{Cov}\left(Y_{1}, Y_{n}\right)$$ of $$Y_{1}$$ and $$Y_{n}$$.

I use n = 10 as a special case to solve this problem:

Y1 = TransformedDistribution[x[1] - Sum[x[i], {i, 1, 10}]/10,
Table[x[i] \[Distributed] NormalDistribution[], {i, 1, 10}]]
Variance[Y1]
Y10 = TransformedDistribution[x[10] - Sum[x[i], {i, 1, 10}]/10,
Table[x[i] \[Distributed] NormalDistribution[], {i, 1, 10}]]
Variance[Y10]
Covariance[Y1, Y10]
Correlation[Y1, Y10]


But the above code can't get the covariance of Y1 and Y10 correctly (the reference answer is $$-\frac{1}{10}$$). How can I use the function Covariance to solve this problem?

You should use notation that separates random variables from their distributions. The following code should get you the desired variances and covariances:

n = 10;
distY1Yn = TransformedDistribution[{x[1] - Sum[x[i], {i, 1, n}]/n, x[n] - Sum[x[i], {i, 1, n}]/n},
Table[x[i] \[Distributed] NormalDistribution[], {i, 1, n}]];
Covariance[distY1Yn]
(* {{9/10, -(1/10)}, {-(1/10), 9/10}} *)
Correlation[distY1Yn]
(* {{1, -(1/9)}, {-(1/9), 1}} *)

distY1 = TransformedDistribution[x[1] - Sum[x[i], {i, 1, n}]/n,
Table[x[i] \[Distributed] NormalDistribution[], {i, 1, n}]];
Variance[distY1]
(* 9/10 *)

distYn = TransformedDistribution[x[n] - Sum[x[i], {i, 1, n}]/n,
Table[x[i] \[Distributed] NormalDistribution[], {i, 1, n}]];
Variance[distYn]
(* 9/10 *)