From 911610949818bbd9b27d492bc551baff3506e89d Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:00:21 +1100 Subject: [PATCH] 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 + ); +```