Welford Online Mean and Variance

Easy
~20 min
code completion

Welford Online Mean and Variance

Welford's algorithm computes mean and variance in a single pass, updating incrementally as each new value arrives. This is essential when data cannot be stored — it never materializes the full stream.

For each new value (1-indexed count n):

At the end, population variance .

Example: values = [2, 4, 4, 4, 5, 5, 7, 9]

  • Mean = 5.0, variance = 4.0 (population)
  • Your task:

    Implement welford_mean_var(values) that returns (mean, variance) as a tuple of two floats.

    Example Tests

    Classic Welford example: mean=5.0, variance=4.0

    Input: {"values":[2,4,4,4,5,5,7,9]}

    Expected: [5,4]

    All identical values: variance is 0

    Input: {"values":[3,3,3,3]}

    Expected: [3,0]

    Two values: mean=3.0, variance=1.0 (population)

    Input: {"values":[2,4]}

    Expected: [3,1]

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.