Weighted mean


 * See weight function for the continuous case.

The weighted mean, or weighted average, of a non-empty list of data


 * $$[x_1, x_2, \dots, x_n]\,,$$

with corresponding non-negative weights


 * $$[w_1, w_2, \dots, w_n]\,,$$

at least one of which is positive, is the quantity calculated by


 * $$\bar{x} = \frac{ \sum_{i=1}^n w_i x_i}{\sum_{i=1}^n w_i},

$$

which means:



\bar{x} = \frac{w_1 x_1 + w_2 x_2 + \cdots + w_n x_n}{w_1 + w_2 + \cdots + w_n}. $$

So data elements with a high weight contribute more to the weighted mean than do elements with a low weight.

If all the weights are equal, then the weighted mean is the same as the arithmetic mean. While weighted means generally behave in a similar fashion to arithmetic means, they do have a few counter-intuitive properties, as captured for instance in Simpson's paradox.

Weighted versions of other means can also be calculated. Examples of such weighted means include the weighted geometric mean and the weighted harmonic mean.

The notion of weighted mean plays a role in descriptive statistics and also occurs in a more general form in several other areas of mathematics.

In the special case, often encountered in practice, where the weights are normalized (i.e. are nonnegative and sum up to 1), the denominator of the fraction simplifies to 1.

Length-weighted mean
For weighting a response variable based upon its dependency on x, a distance variable.



\bar{y} = \frac{y_2 x_2 - y_1 x_1}{x_2 - x_1} $$

Example
Let's say we had two school classes, one with 20 students, and one with 30 students. The grades in each class on a particular test were:


 * Morning class = 62, 67, 71, 74, 76, 77, 78, 79, 79, 80, 80, 81, 81, 82, 83, 84, 86, 89, 93, 98


 * Afternoon class = 81, 82, 83, 84, 85, 86, 87, 87, 88, 88, 89, 89, 89, 90, 90, 90, 90, 91, 91, 91, 92, 92, 93, 93, 94, 95, 96, 97, 98, 99

The straight average for the morning class is 80% and the straight average of the afternoon class is 90%. If we were to find a straight average of 80% and 90%, we would get 85% for the mean of the two class averages. However, this is not the average of all the students' grades. To find that, you would need to total all the grades and divide by the total number of students:



\bar{x} = \frac{4300\%}{50} = 86\%. $$

Or, you could find the weighted average of the two class means already calculated, using the number of students in each class as the weighting factor:



\bar{x} = \frac{(20)80\% + (30)90\%}{20 + 30} = 86\%. $$

Note that if we no longer had the individual students' grades, but only had the class averages and the number of students in each class, we could still find the mean of all the students grades, in this way, by finding the weighted mean of the two class averages.

Convex combination
Since only the relative weights are relevant, any weighted mean can be expressed using coefficients that sum to one. Such a linear combination is called a convex combination.

Using the previous example, we would get the following:



\frac{20}{20 + 30} = 0.4\, $$



\frac{30}{20 + 30} = 0.6\, $$



\bar{x} = \frac{(0.4)80\% + (0.6)90\%}{0.4 + 0.6} = 86\% $$

This simplifies to:



\bar{x} = (0.4)80\% + (0.6)90\% = 86\% $$

Dealing with variance
For the weighted mean of a list of data for which each element $$x_i\,\!$$ comes from a different probability distribution with known variance $${\sigma_i}^2\,$$, one possible choice for the weights is given by:



w_i = \frac{1}{\sigma_i^2}. $$

The weighted mean in this case is:



\bar{x} = \frac{ \sum_{i=1}^n x_i/{\sigma_i}^2}{\sum_{i=1}^n 1/{\sigma_i}^2}, $$

and the variance of the weighted mean is:



\sigma_{\bar{x}}^2 = \frac{ 1 }{\sum_{i=1}^n 1/{\sigma_i}^2}, $$

which reduces to $$ \sigma_{\bar{x}}^2 = \frac{ {\sigma_0}^2 }{n}$$, when all $$\sigma_i = \sigma_0\,$$.

The significance of this choice is that this weighted mean is the maximum likelihood estimator of the mean of the probability distributions under the assumption that they are independent and normally distributed with the same mean.

Weighted sample variance
Typically when you calculate a mean it is important to know the variance and standard deviation of that mean. When a weighted mean is used, the variance of the weighted sample is different from the variance of the unweighted sample. The biased weighted sample variance is defined similarly to the normal biased sample variance:



\sigma_{normal}^2\ = \frac{ \sum_{i=1}^N{\left(x_i - \mu\right)^2} }{ N } \;\;\; \sigma_{weighted}^2\ = \frac{ \sum_{i=1}^N{{w_i}\left(x_i - \mu\right)^2} }{ \sum_{i=1}^N{w_i} } $$

For small sample of populations, it is customary to use an unbiased estimator for the population variance. In normal unweighted samples, the N in the denominator (corresponding to the sample size) is changed to N-1. While this is simple in unweighted samples, it becomes tedious for weighted samples. Thus, the unbiased estimator of weighted population variance is given by :



s^2\ = \frac{ \sum_{i=1}^N{w_i} }{ \left(\sum_{i=1}^N{w_i}\right)^2 - \sum_{i=1}^N{{w_i}^2} }\ \sum_{i=1}^N{{w_i}\left(x_i - \mu\right)^2} $$

Which can also be written in terms of running sums for programming as:



s^2\ = \frac{ \sum_{i=1}^N{w_i {x_i}^2} \sum_{i=1}^N{w_i} - \left(\sum_{i=1}^N{w_i x_i}\right)^2 }{ \left(\sum_{i=1}^N{w_i}\right)^2 - \sum_{i=1}^N{{w_i}^2} } $$

The standard deviation is simply the square root of the variance above.