All formulas

Statistics

Sample variance

stats.sample-varianceAlso called sample variance, variance, var, unbiased variance

s2=1n−1∑i=1n(xi−xˉ)2s^2 = \frac{1}{n-1}\sum_{i=1}^{n} (x_i - \bar{x})^2

wherexˉ=1n∑j=1nxj\bar{x} = \frac{1}{n}\sum_{j=1}^{n} x_j

Unbiased estimate of a population's variance from a sample: squared deviations from the sample mean, divided by n − 1 (Bessel's correction).

Notation

xx

observations

x ∈ ℝn

nn

number of observations

Size, at least 2

s2s^2

Result: the sample variance

scalar

Worked examples

Each result is solved exactly and confirmed by two independent computations.

  1. Given

    x=[2,4,4,4,5,5,7,9]x = [2, 4, 4, 4, 5, 5, 7, 9]

    Result

    s2=327\displaystyle s^2 = \frac{32}{7}

    ≈ 4.57142857143

  2. Given

    x=[1,2,3,4]x = [1, 2, 3, 4]

    Result

    s2=53\displaystyle s^2 = \frac{5}{3}

    ≈ 1.66666666667

  3. Given

    x=[0.1,0.2,0.4]x = [0.1, 0.2, 0.4]

    Result

    s2=7300\displaystyle s^2 = \frac{7}{300}

    ≈ 0.0233333333333

Common mistakes

  • Divides by n instead of n − 1

    This is the population variance (NumPy's default, ddof=0); the result is (n − 1)/n times the sample variance.

    Fix: Divide the sum of squared deviations by n − 1, or call np.var(x, ddof=1).

  • Returns the standard deviation

    The result is the square root of the sample variance.

    Fix: Drop the final square root.

Sources

  • NIST/SEMATECH e-Handbook, §1.3.5.6

    NIST/SEMATECH e-Handbook of Statistical Methods. https://www.itl.nist.gov/div898/handbook/ Last updated 2022-04-27. doi:10.18434/M32189

    doi.org
  • Casella and Berger 2002

    Casella, George; Berger, Roger L. Statistical Inference. 2nd ed. Duxbury, 2002.