As I understand it, the law of total covariance indicates that the covariance of two data sets X and Y should be equal to the average of the covariances of all subsets of X and Y (eg. [X1, Y1] .....[Xn,Yn]) plus the covariance of the averages of X and Y for all subsets.
When implementing this, I can not achieve equality of the two approaches:
$cov(X,Y) \neq \left(\sum_{i=1}^n \frac{m_i}{m}cov(X_i,Y_i)\right) + cov([\overline{X_1}..\overline{X_n}], [\frac{m_1}{m}\overline{Y_1}..\frac{m_n}{m}\overline{Y_n}]) $
where $m$ is total number of samples in $X$ and $Y$, $n$ is the number of subsets of $X$ and $Y$, and $m_i$ is the number of samples in the subsets $X_i$ and $Y_i$.
I assumed that the subset covariances and averages needed to be weighted by the fraction of samples in their corresponding subsets. While this approach brings the two estimates of total covariance close, the two answers are not exactly identical.
Any suggests as to what is wrong with my approach.
-------------- example of approach ----
For $X$ and $Y$ vectors, which can be separated into three subsets:
$X_1 = [x_1 .. x_{m1}] , \quad Y_1 = [y_1 .. y_{m1}] \\ X_2 = [x_1 .. x_{m2}] , \quad Y_2 = [y_1 .. y_{m2}]\\ X_3 = [x_1 .. x_{m3}] , \quad Y_3 = [y_1 .. y_{m3}]\\$
I first calculate the weighting for each subset pair:
$ w1 = \frac{m1}{m1+m2+m3} \\ w2 = \frac{m2}{m1+m2+m3} \\ w3 = \frac{m3}{m1+m2+m3} $
I calculate the covariance for each pair:
$ c1 = cov(X_1,Y_1) \\ c2 = cov(X_2,Y_2) \\ c3 = cov(X_3,Y_3) \\ $
I then create vectors of the subset means, with one of the vectors weighted by the subset size:
$ X_a = [\overline{X_1},\overline{X_2},\overline{X_3}] \\ Y_a = [w1 \:\overline{Y_1},w2 \: \overline{Y_2},w3 \: \overline{Y_3}] $
and then calculate their covariance:
$ c_a = cov(X_a,Y_a) $
My understanding was that the total covariance should equal the sum of the weighted subset covariances and the covariance of the subset means:
$ cov(X,Y) = (w1 \: c1 + w2 \: c2 + w3 \: c3) + ca $