Suppose I observe $n$ iid realizations of two random variables $X$ and $Y$, denoted respectively $x_i$ and $y_i$. Observations can be groupped into two subsamples, with $n_1$ and $n_2$ observations. I want to decompose the sample covariance $\widehat{\sigma_{XY}}$ by the contribution of each group of observations plus possibly a residual term. Here is what I have:
\begin{eqnarray} \widehat{\sigma_{XY}} & = & \frac{1}{n} \sum^n x_i y_i -n \overline{x} \overline{y} \\ & = & \frac{n_1}{n} \frac{1}{n_1} \sum^{n_1} x_i y_i + \frac{n_2}{n} \frac{1}{n_2} \sum^{n_2} x_i y_i \\ &&- n\left(\frac{n_1}{n} \frac{1}{n_1} \sum^{n_1} x_i + \frac{n_2}{n} \frac{1}{n_2} \sum^{n_2} x_i\right) \left(\frac{n_1}{n} \frac{1}{n_1} \sum^{n_1} y_i + \frac{n_2}{n} \frac{1}{n_2} \sum^{n_2} y_i\right) \\ & =& \frac{n_1}{n} \frac{1}{n_1} \left(\sum^{n_1} x_i y_i -n_1 \overline{x}_1 \overline{y}_1\right) + \frac{n_2}{n} \frac{1}{n_2} \left(\sum^{n_2} x_i y_i -n_2 \overline{x}_2 \overline{y}_2\right) - \frac{n_1n_2}{n}\left(\overline{x}_1\overline{y}_2 + \overline{x}_2 \overline{y}_1\right)\\ & = & \frac{n_1}{n} \widehat{\sigma_{XY, 1}} + \frac{n_2}{n} \widehat{\sigma_{XY, 2}} - \frac{n_1n_2}{n}\left(\overline{x}_1\overline{y}_2 + \overline{x}_2 \overline{y}_1\right) \end{eqnarray}
That is, the overall sample covariance is a weighted average of the sample covariances within each group minus a residual term.
However, in a numerical example this decomposition does not hold as I do not obtain the overall sample covariance on the LHS. Especially the residual term seems too large.
I would appreciate if somebody can double-check whether there is a mistake in the above.
Many thanks