Skip to content

Instantly share code, notes, and snippets.

@kebman
Last active March 23, 2016 13:12
Show Gist options
  • Save kebman/c1eedde47c50aaf18def to your computer and use it in GitHub Desktop.
Save kebman/c1eedde47c50aaf18def to your computer and use it in GitHub Desktop.
Tutorial on how to get the Standard Deviation of a set of numbers using JavaScript
<!DOCTYPE html>
<html>
<head>
<title>Standard Deviation</title>
<meta charset="utf-8">
<style type="text/css"></style>
</head>
<body>
<header>
<h1>Standard Deviation</h1>
<h3>JavaScript Tutorial</h3>
</header>
<article>
<h1>Explanation</h1>
<p>The Standard Deviation, signified by the Greek letter sigma (σ), is a measure of how spread out numbers in a data set are. It's a really great tool to use as a kind of measuring stick to find out what parts of the data set is high, low or in-between, as compared to the mean.</p>
<p>Formula: The Standard Deviation is the Square Root of the Variance.</p>
<p>Variance is the average of the squared difference of the mean.</p>
<p>So to get the Standard Deviation, follow the formula. Get the Variance, and then calculate the Square Root of the Variance.</p>
<p>For a tutorial, see the source code and the console.</p>
<p>Source: <a href="https://www.mathsisfun.com/data/standard-deviation.html">Math is Fun</a></p>
</article>
</body>
<script>
// Standard Deviation
// Formula: The Standard Deviation is the Square Root of the Variance.
// Variance is the average of the squared difference of the mean.
// Here's an exhaustive explanation for JavaScript:
// make an array to holde some numbers
var data = [];
// and fill it with 20 randomly generated numbers
// just so we have something to play around with
for (var i = 20 - 1; i >= 0; i--) {
data[i] = Math.floor(Math.random() * 100) + 0;
}
// print the array
console.log("data set: " + data);
// first, some preparations
// count the number of numbers in the array :p
var count = data.length;
console.log("count: " + count);
// sum all the numbers in the array
var sum = data.reduce((a, b) => a + b);
console.log("sum: " + sum); // sum numbers in array
/*
.reduce(callback) runs a callback function on each element in the array, and then accumulates what it returns.
This makes it great tool to quickly sum up the numbers in an array.
In this case the callback function is in the form of an arrow function that takes the first and second number in the array as arguments,
and then sums them up. Then it repeats the process until you get the total sum of numbers in the array.
*/
// find the variance
// calculate the mean
var mean = sum/count;
console.log("mean: " + mean);
// get the difference from the mean
var meanDiff = data.map((n) => n-mean);
console.log("difference from mean: " + meanDiff);
/*
.map(callback) iterates trough each element in an array, and then makes a new array out of it containing all the answers
based on the callback function thar was fed to it.
In this case the callback is an arrow function that calculates the difference from the mean on each number in the array.
*/
// square the new array
var meanDiffSqrd = meanDiff.map((n) => Math.pow(n, 2));
console.log("squared: " + meanDiffSqrd);
// while I could also write n*n to square the number here, I opted to use Math.pow(number, exponent) for clarity.
// sum the new array
var sumDiffSqrd = meanDiffSqrd.reduce((a, b) => a + b);
console.log("sum " + sumDiffSqrd);
// get the mean, and thus the variance
var variance = sumDiffSqrd/count;
console.log("mean " + variance);
// then, finally, find the Standard Deviation by finding the Square Root of the Variance:
var sd = Math.sqrt(variance);
console.log("standard deviation: " + sd);
// here's the compacted function to do the same thing:
function sdev(array) {
var mean = array.reduce((a, b) => a + b)/array.length;
var meanDiffSqrd = array.map((n) => Math.pow((n-mean), 2));
var variance = meanDiffSqrd.reduce((a, b) => a + b)/array.length;
return Math.sqrt(variance);
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment