4 ms·
The formula you give for variance is the sample variance, with N - 1 as the divisor. It gives an unbiased estimate of the variance of a population, assuming X i
by bckygldstn 9y ago
The formula you give for variance is the sample variance, with N - 1 as the divisor. It gives an unbiased estimate of the variance of a population, assuming X is a sample of that much larger population.
However, by default numpy gives you population variance, with N as the divisor. This assumes X is the entire population, which is different from R and Matlab.
As a result, the answer in your code is different from the formula just above it.
np.var(X)
gives 207, while
np.sum((np.array(X) - np.mean(X))**2) / (len(X) - 1)
gives 236. If you want the N-1 corrected sample variance from numpy you'd use
np.var(X, ddof=1)
- aisofteng 9y agoSee: Bessel's correction. https://en.m.wikipedia.org/wiki/Bessel%27s_correction https://en.m.wikipedia.org/wiki/Bessel%27s_correction