5
$\begingroup$

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

ClearAll["Global`*"]
$Path = Append[$Path, "c:"]
grades = OpenRead["grades.dat"]
data = ReadList[grades, {Number, Number}]
Close[grades]
w = 1/(data[[All, 2]]^2) // N
data2 = data[[All, 1]] // N
wd = WeightedData[data2, w]
$\endgroup$
2
  • 1
    $\begingroup$ why not Variance[wd]? $\endgroup$
    – kglr
    Feb 6, 2018 at 19:14
  • 2
    $\begingroup$ @kglr I believe that gives the sample variance, not the variance of the (weighted) mean $\endgroup$
    – Valerio
    Aug 22, 2018 at 18:13

4 Answers 4

5
$\begingroup$
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

$\endgroup$
1
  • $\begingroup$ Nice contraction of the formula. $\endgroup$ Feb 7, 2018 at 11:19
5
$\begingroup$

I think you can use this variance definition.

From https://en.wikipedia.org/wiki/Reduced_chi-squared_statistic

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

enter image description here

where

enter image description here

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
$\endgroup$
4
$\begingroup$

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.

$\endgroup$
1
  • 3
    $\begingroup$ +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). $\endgroup$
    – JimB
    Oct 21, 2018 at 17:37
0
$\begingroup$

The weighted mean can alternatively be written as

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

the variance follows to

var=(data2-wd)^2.(w/Total[w])
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.