Population vs. Sample Variance (ddof=0 vs. ddof=1)

Basics
BasicsVarianceBessel CorrectionGoogleMetaJane Street

Problem

Implement pop_and_sample_variance(x) returning (population_variance, sample_variance) using ddof=0 (dividing by nn) and ddof=1 (Bessel's correction, dividing by n−1n - 1).

Why It Matters: Distinguishing ddof=0 vs. ddof=1 is a classic Data Science interview question: dividing by n−1n - 1 makes sample variance an unbiased estimator of the true population variance.

Function Signature

import numpy as np

def pop_and_sample_variance(x) -> tuple[float, float]:

Sample Input & Output

Example 1: Simple 4-Number Sample

Input:

x = [2.0, 4.0, 4.0, 6.0]

Expected Output:

(2.0, 2.666667)

Explanation: Mean is 4.0, sum of squared deviations is 8.0 → population var is 8/4 = 2.0, sample var is 8/3 ≈ 2.666667.

Example 2: Constant Values

Input:

x = [5.0, 5.0, 5.0]

Expected Output:

(0.0, 0.0)

Explanation: Zero spread gives 0.0 for both population and sample variance.

Constraints & Edge Cases

  • Input has length >= 2.
  • Return (pop_var, sample_var).
  • Input has length n >= 2.
  • Sample variance (ddof=1) is always strictly larger than population variance (ddof=0) when values are not all identical.

Constraints & Evaluation Criteria

  • Input has length `>= 2`.
  • Return `(pop_var, sample_var)`.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[2, 4, 6, 8]
Expected Output / Criteria
(5.0, 6.6667)