fun variance(vector: DoubleArray): Double { var sum = 0.0 var sumSquared = 0.0 if (vector.size <= 1) { return 0 } for (i in vector.indices) { sum += vector[i] sumSquared += vector[i] * vector[i]  } val result = sumSquared - sum * sum / vector.size.toDouble() / (vector.size - 1).toDouble() return if (result < 0) { 0 } else { result } }
Computes the variance for an array of doubles .