Section 8.3 Estimating Variance
As another example, suppose we want to estimate variance in the penguinsβ weights. In Chapter 1, we saw that there are two ways to compute the variance of a sample. I promised to explain the difference later -- and later is now.
The reason there are two ways to compute the variance of a sample is that one is a biased estimator of the population variance, and the other is unbiased. The following function computes the biased estimator, which is the sum of the squared deviations divided by
n.
def biased_var(xs):
# Compute variance with n in the denominator
n = len(xs)
deviations = xs - np.mean(xs)
return np.sum(deviations**2) / n
To test it, weβll simulate many samples with size 10, compute the biased variance of each sample, and then compute the average of the variances.
$ biased_vars = [biased_var(make_sample(n=10)) for i in range(10001)]
np.mean(biased_vars)
np.float64(0.19049277659404473)
The result is about 0.19, but in this case, we know that the actual population variance is about 0.21, so this version of the sample variance is too low on average -- which confirms that it is biased.
$ actual_var = sigma**2
actual_var
0.2116
The following function computes the unbiased estimator, which is the sum of the squared deviations divided by
n-1.
def unbiased_var(xs):
# Compute variance with n-1 in the denominator
n = len(xs)
deviations = xs - np.mean(xs)
return np.sum(deviations**2) / (n - 1)
We can test it by generating many samples and computing the unbiased variance for each one.
$ unbiased_vars = [unbiased_var(make_sample(n=10)) for i in range(10001)]
np.mean(unbiased_vars)
np.float64(0.21159109492300626)
The average of the unbiased sample variances is very close to the actual value -- which is what we expect if it is unbiased.
With sample size 10, the difference between the biased and unbiased estimators is about 10%, which might be non-negligible. With sample size 100, the difference is only 1%, which is small enough that it probably doesnβt matter in practice.
$ n = 10
1 - (n - 1) / n
0.09999999999999998
$ n = 100
1 - (n - 1) / n
0.010000000000000009
