# How to calculate the weighted covariance?

I'm trying to calculate the weighted co-variance by hand to better understand what is going on. I have read the Wikipeida article and I understand the concept. However, when plugging in numerical values I encounter the following problem:

For example assume I have three observations as given in matrix A

$A = \left[ \begin{array}{ccc} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ \end{array} \right]$

each row vector of matrix A has a weight value associated with it as $w_i = (0.5, 0.33, 0.17)$

Using the equation in Wikipedia which is:

$$q_{jk} = \frac{\sum_{i = 1}^{N} w_i}{(\sum_{i = 1}^{N} w_i)^2 - \sum_{i = 1}^{N} w_{i}^2} \sum_{i = 1}^{N} w_i (x_{ij} - \bar{x}_j)(x_{ik} - \bar{x}_k)$$

if I'm to calculate $q_{21}$ the derivation would look like the following

$$q_{21} = \left[ \frac{1}{1 - (0.25 + 0.1089 + 0.0289 )} \right]\left(w_i(1-0.5)(0 - 0.3) + w_i(0-0.5)(1 - 0.3) + w_i(0-0.5)(0 - 0.3)\right)$$

what should I plug in to $w_i$ ?

Is the other parts of the substitution correctly done?

• The "$\Sigma$" is called summation notation. It asks you to plug in first the value $i=1$, then $i=2$, etc., into all occurrences of its argument. When you do that for the $w_i$ you will not have any more questions.
– whuber
Sep 27, 2013 at 16:22
• @whuber Which $w_i$? 0.5 or 0.33 and how do you differentiate between the two? Sep 27, 2013 at 16:30
• You differentiate by looking at the subscript $i$. How do you know to put "1" in for $x_{ij}$ in the first term to the right of the square bracket? $q_{21}$ only specifies the values of (the subscripts) $j$(=2) and $k$(=1), not the value of the subscript $i$. Observe that the vector (0.5,0.33,0.17) is not equal to $w_i$, despite that equals sign; it's equal to $w$ and the three elements are indexed by $i$, i.e., $w = (w_1, w_2, w_3) = (0.5, 0.33, 0.17)$. (Notational convention messed you up, I'm afraid!) Sep 27, 2013 at 17:24

## 1 Answer

Let A be a matrix with rows being the observations as you have specified. For each row we have a weight. For row i, we have observation vector $x_i$ and weight $w_i$

Focus on the general expression for $q_{jk}$. It is a product of two terms.

The term $\frac{\sum w_i}{(\sum w_i)^2 - \sum w_i^2}$ is independent of the subscripts $j,k$ of $q_{j,k}$.

Note: You have this computed correctly.

The term $\sum_{i=1}^{N} w_i (x_{ij} - \bar{x}_j)(x_{ik} - \bar{x}_k)$ depends on $(j,k)$. On the other hand, $i$ is a running variable.

Note:

• You have $(j,k) = (2,1)$. Then the second term becomes $\sum_{i=1}^{N} w_i (x_{i2} - \bar{x}_2)(x_{i1} - \bar{x}_1)$.
• Weighted mean is $\bar{x} = \sum_{i=1}^{N} w_i*x_i$. For $x_1 = [1 0 0], x_2 = [0 1 0]$ and $x_3 = [0 0 1]$ and weights $w_1 = 0.5, w_2 = .33,w_3 = .17$ we have $\bar{x} = [.5\; 0\; 0] + [0 \;.33\; 0] + [0 \;0 \;.17] = [.5\; .33\; .17]$. Thus, $\bar{x}_1 = .5$ and $\bar{x}_2 = .33$.
• Also, $x_{12} = 0, x_{22} = 1, x_{32} = 0$ and $x_{11} = 1, x_{21} = 0, x_{31} = 0$.
• Thus, $$\sum_{i=1}^{N} w_i (x_{i2} - \bar{x}_2)(x_{i1} - \bar{x}_1) \\ = w_1(x_{12} - \bar{x}_2)(x_{11} - \bar{x}_1) + w_2(x_{22} - \bar{x}_2)(x_{21} - \bar{x}_1) + w_3(x_{32} - \bar{x}_2)(x_{31} - \bar{x}_1) \\ = .5(0 - .33)(1 - .5) + .33(1-.33)(0-.5) + .17(0-.33)(0-.5)\\ = -.165$$