User:Vossman/Weighted running sums


 * 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.

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.