Compute Population Variance
Problem statement
Given a nonempty array of real-valued observations values, return their population variance.
Population variance is the average squared distance from the population mean. Compute the result in one pass over the input.
Function
populationVariance(values: double[]) → doubleExamples
Example 1
values = [1.0,2.0,3.0,4.0]return = 1.25The mean is 2.5. The squared deviations sum to 5, and 5 / 4 = 1.25.
Example 2
values = [2.0,2.0,2.0]return = 0.0Every observation equals the mean, so every squared deviation is zero.
Constraints
1 <= values.length <= 100000-10^6 <= values[i] <= 10^6- The answer is accepted within
10^-9absolute error.