varianceBy<T> function
Summarizing data
Returns an unbiased estimator of the population variance
of all values yielded by the accessor
function applied to each element in
the iterable
using
Welford’s algorithm.
This function ignores elements that yield values that do not satisfy any of the following conditions:
- The value is not
null
. - The value is not double.nan.
Useful for filtering and ignoring missing data in datasets.
If the iterable
has fewer than two elements that yield valid values, this
function returns null
.
Implementation
num? varianceBy<T>(Iterable<T> iterable, num? Function(T) accessor) {
var count = 0;
num delta, mean = 0, sum = 0;
for (var element in iterable) {
var value = accessor(element);
if (value != null && value == value) {
delta = value - mean;
mean += delta / ++count;
sum += delta * (value - mean);
}
}
return count > 1 ? sum / (count - 1) : null;
}