From be8d285faf1b898a9e48fc410dda04f608684811 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 11:29:20 +0200 Subject: [PATCH] Update standard-deviation.md --- snippets/standard-deviation.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index f07cb4f4c..e559972bb 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -1,17 +1,15 @@ ### Standard deviation -Use `Array.reduce()` to calculate the mean of the values, the variance of the values, and the sum of the variance -of the values to determine the standard deviation of an array of numbers. - -Since there are two types of standard deviation, population and sample, you can use a flag to switch to population (sample is default). +Use `Array.reduce()` to calculate the mean, variance and the sum of the variance of the values, the variance of the values, then +determine the standard deviation. +You can omit the second argument to get the sample standard deviation or set it to `true` to get the population standard deviation. ```js const standardDeviation = (arr, usePopulation = false) => { const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length; return Math.sqrt( arr.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), []) - .reduce((acc, val) => acc + val, 0) - / (arr.length - (usePopulation ? 0 : 1)) + .reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1)) ); } // standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample)