From 911610949818bbd9b27d492bc551baff3506e89d Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:00:21 +1100 Subject: [PATCH 1/9] Create standard-deviation.md http://www.calculator.net/standard-deviation-calculator.html As a one-liner it's really long, feel free to optimize the formatting here (or shorten it further somehow). --- snippets/standard-deviation.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 snippets/standard-deviation.md diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md new file mode 100644 index 000000000..3ee95a147 --- /dev/null +++ b/snippets/standard-deviation.md @@ -0,0 +1,16 @@ +### 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. + +NOTE: This is **population standard deviation**. Use `/ (arr.length - 1)` at the end to +calculate **sample standard deviation**. + +```js +const standardDeviation = (arr, val) => + Math.sqrt( + arr.reduce((acc, val) => acc.concat(Math.pow(val - arr.reduce((acc, val) => acc + val, 0) / arr.length, 2)), []) + .reduce((acc, val) => acc + val, 0) + / arr.length + ); +``` From 635adbc61799bfb7659448f38099adb496a80b6a Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:02:21 +1100 Subject: [PATCH 2/9] Update standard-deviation.md --- snippets/standard-deviation.md | 1 + 1 file changed, 1 insertion(+) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index 3ee95a147..63485f91e 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -13,4 +13,5 @@ const standardDeviation = (arr, val) => .reduce((acc, val) => acc + val, 0) / arr.length ); +// standardDeviation([10,2,38,23,38,23,21]) -> 12.298996142875 ``` From 731df313959b3b47e4c9ed4da46c9fa941947608 Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:12:42 +1100 Subject: [PATCH 3/9] Update standard-deviation.md --- snippets/standard-deviation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index 63485f91e..0ae7f56af 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -7,7 +7,7 @@ NOTE: This is **population standard deviation**. Use `/ (arr.length - 1)` at the calculate **sample standard deviation**. ```js -const standardDeviation = (arr, val) => +const standardDeviation = arr => Math.sqrt( arr.reduce((acc, val) => acc.concat(Math.pow(val - arr.reduce((acc, val) => acc + val, 0) / arr.length, 2)), []) .reduce((acc, val) => acc + val, 0) From b844b0f765dbdb69783d544a39f067bb708f8744 Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:41:28 +1100 Subject: [PATCH 4/9] Add usePopulation flag param --- snippets/standard-deviation.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index 0ae7f56af..a2b9a64fe 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -3,15 +3,15 @@ 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. -NOTE: This is **population standard deviation**. Use `/ (arr.length - 1)` at the end to -calculate **sample standard deviation**. +Since there are two types of standard deviation, population and sample, you can use a flag to switch to population (sample is default). ```js -const standardDeviation = arr => +const standardDeviation = (arr, usePopulation) => Math.sqrt( arr.reduce((acc, val) => acc.concat(Math.pow(val - arr.reduce((acc, val) => acc + val, 0) / arr.length, 2)), []) .reduce((acc, val) => acc + val, 0) - / arr.length + / (arr.length - (usePopulation ? 0 : 1)) ); -// standardDeviation([10,2,38,23,38,23,21]) -> 12.298996142875 +// standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample) +// standardDeviation([10,2,38,23,38,23,21], true) -> 12.29899614287479 (population) ``` From 3d7b322a9d9defaf99a3f3f27aec45857697156f Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:48:29 +1100 Subject: [PATCH 5/9] Cache the mean --- snippets/standard-deviation.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index a2b9a64fe..b1dd0b9c6 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -6,12 +6,14 @@ 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). ```js -const standardDeviation = (arr, usePopulation) => - Math.sqrt( - arr.reduce((acc, val) => acc.concat(Math.pow(val - arr.reduce((acc, val) => acc + val, 0) / arr.length, 2)), []) +const standardDeviation = (arr, usePopulation) => { + const mean = arr.reduce((acc, val) => acc + val, 0); + return Math.sqrt( + arr.reduce((acc, val) => acc.concat(Math.pow(val - mean / arr.length, 2)), []) .reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1)) ); + } // standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample) // standardDeviation([10,2,38,23,38,23,21], true) -> 12.29899614287479 (population) ``` From 418069551b528e3855d2a39a6375f70a88b36a2e Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:54:33 +1100 Subject: [PATCH 6/9] Update standard-deviation.md --- snippets/standard-deviation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index b1dd0b9c6..3641cbe14 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -7,7 +7,7 @@ Since there are two types of standard deviation, population and sample, you can ```js const standardDeviation = (arr, usePopulation) => { - const mean = arr.reduce((acc, val) => acc + val, 0); + const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length; return Math.sqrt( arr.reduce((acc, val) => acc.concat(Math.pow(val - mean / arr.length, 2)), []) .reduce((acc, val) => acc + val, 0) From 98f56cbf2394b59053f14b4e4ef382c456965128 Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:55:08 +1100 Subject: [PATCH 7/9] Update standard-deviation.md --- snippets/standard-deviation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index 3641cbe14..442052761 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -9,7 +9,7 @@ Since there are two types of standard deviation, population and sample, you can const standardDeviation = (arr, usePopulation) => { const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length; return Math.sqrt( - arr.reduce((acc, val) => acc.concat(Math.pow(val - mean / arr.length, 2)), []) + arr.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), []) .reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1)) ); From bede528c5265c304f14f0064030b310f40ac9536 Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 14 Dec 2017 20:24:07 +1100 Subject: [PATCH 8/9] Use explicit default value --- snippets/standard-deviation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index 442052761..f07cb4f4c 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -6,7 +6,7 @@ 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). ```js -const standardDeviation = (arr, usePopulation) => { +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)), []) From 2c30c0afdefb9fc194bd0142d2734e24ec6dc689 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 11:29:20 +0200 Subject: [PATCH 9/9] 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)