standardDeviation static method

double standardDeviation(
  1. Iterable<num> ns, [
  2. num? mean
])

Standard deviation of ns entries.

mean an already calculated mean for ns.

Implementation

static double standardDeviation(Iterable<num> ns, [num? mean]) {
  if (ns.length == 1) {
    return 0.0;
  } else {
    var meanD = (mean ?? Math.mean(ns)).toDouble();
    var sum = 0.0;

    for (var n in ns) {
      var v = (n - meanD);
      v *= v;
      sum += v;
    }

    var variation = sum / (ns.length - 1);
    var deviation = Math.sqrt(variation);

    return deviation;
  }
}