# How do you calculate the variance on a weighted mean?

How do I calculate a variance for the weighted mean value in the below program?

ClearAll["Global*"]
$Path = Append[$Path, "c:"]
w = 1/(data[[All, 2]]^2) // N
data2 = data[[All, 1]] // N
wd = WeightedData[data2, w]

• why not Variance[wd]?
– kglr
Feb 6, 2018 at 19:14
• @kglr I believe that gives the sample variance, not the variance of the (weighted) mean Aug 22, 2018 at 18:13

SeedRandom[1]
dt = RandomInteger[10, 10];
wt = RandomInteger[10, 10];
wd = WeightedData[dt, wt];

Mean @ wd


80/33

Variance @ wd


4357/441

Verifying the formula in Chris Degnen's answer:

normalizedweights = Normalize[wt, Total];

mean = normalizedweights.dt


80/33

variance = (1/(1 - Total[normalizedweights^2])) normalizedweights .(dt - mean)^2


4357/441

• Nice contraction of the formula. Feb 7, 2018 at 11:19

I think you can use this variance definition.

The unbiased weighted estimator of the sample variance can be computed as follows:

where

Example

x = {1.21, 2.24, 1.2, 2.39, 1.1, 1.45, 2.29, 2.33, 1.13, 2.39};
w = {0.000977517, 0.00195503, 0.00391007, 0.00782014, 0.0156403,
0.0312805, 0.0625611, 0.125122, 0.250244, 0.500489};
Total[w]

1.

w = 2 w; (* denormalise the weights to check normalisation is working *)
n = Length[x];

mu = Sum[w[[i]]*x[[i]], {i, 1, n}]/Sum[w[[i]], {i, 1, n}];

var = (Sum[w[[i]], {i, 1, n}]*
Sum[w[[i]]*(x[[i]] - Sum[w[[i]]*x[[i]], {i, 1, n}]/
Sum[w[[i]], {i, 1, n}])^2, {i, 1, n}])/
(Sum[w[[i]], {i, 1, n}]^2 - Sum[w[[i]]^2, {i, 1, n}]);

wd = WeightedData[x, w];

Mean[wd] == mu
Variance[wd] == var

True
True


Let $$X$$ be a random variable with a given distribution, and fix a sample size $$n$$. Let $$\bar X$$ be the random variable representing the mean of a sample of size $$n$$. If $$\mathop{\text{Var}} (X)$$ is the variance of $$X$$, the variance of the mean, which is the variance of the distribution of sample means, is given by $$\mathop{\text{Var}} (\bar X) = \mathop{\text{Var}}(X)\,/\,n\;.$$ Given one sample wd, with a point estimate for $$\mathop{\text{Var}} (X)$$ given by

Variance[wd]


then the variance of the mean could be estimated with

Variance[wd] / n


where n is sample size. It's unclear to me whether the sample size is the same as Length[data2], or the weighted data wd represents an EmpiricalDistribution from which samples of another size are to be drawn.

• +1 This is the right answer. All of the others give the estimate of the variance of the sample values when the question (whether intended or not) is about the variance of the estimator of the sample mean given that one has weighted data (as mentioned by @Valerio).
– JimB
Oct 21, 2018 at 17:37

The weighted mean can alternatively be written as

wd=data2.(w/Total[w])


the variance follows to

var=(data2-wd)^2.(w/Total[w])
`