-
-
Save mxriverlynn/b7c6615ecf1fe9ad60a8 to your computer and use it in GitHub Desktop.
Standard deviation, in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sum = values.reduce(function(sum, value){ | |
return sum + value; | |
}, 0); | |
var avg = sum / data.length; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var diffs = values.map(function(value){ | |
var diff = value - avg; | |
return diff; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var squareDiffs = values.map(function(value){ | |
var diff = value - avg; | |
var sqr = diff * diff; | |
return sqr; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function average(data){ | |
var sum = data.reduce(function(sum, value){ | |
return sum + value; | |
}, 0); | |
var avg = sum / data.length; | |
return avg; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var avgSquareDiff = average(squareDiffs); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var stdDev = Math.sqrt(avgSquareDiff); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function standardDeviation(values){ | |
var avg = average(values); | |
var squareDiffs = values.map(function(value){ | |
var diff = value - avg; | |
var sqrDiff = diff * diff; | |
return sqrDiff; | |
}); | |
var avgSquareDiff = average(squareDiffs); | |
var stdDev = Math.sqrt(avgSquareDiff); | |
return stdDev; | |
} | |
function average(data){ | |
var sum = data.reduce(function(sum, value){ | |
return sum + value; | |
}, 0); | |
var avg = sum / data.length; | |
return avg; | |
} |
es6 version. each fn - lambda
standardDeviation(values) {
const average = (data) => data.reduce((sum, value) => sum + value, 0) / data.length;
const avg = average(values);
const diffs = values.map((value) => value - avg);
const squareDiffs = diffs.map((diff) => diff * diff);
const avgSquareDiff = average(squareDiffs);
return Math.sqrt(avgSquareDiff);
};
const average = data => data.reduce((sum, value) => sum + value) / data.length
const standardDeviation = values => Math.sqrt(average(values.map(value => (value - average(values)) ** 2)))
standardDeviation([4, 8, 2, 4, 5])
2 liner ES6
Array.prototype.stDev = function stDev() {
const average = data => data.reduce((sum, value) => sum + value) / data.length
return Math.sqrt(average(this.map(value => (value - average(this)) ** 2)))
};
[4, 8, 2, 4, 5].stDev()
This doesn't compute standard deviation, it computes the uncorrected standard deviation, because it uses 1/N
. The denominator for stdev should be N-1
, not N
. The squared differences should not be averaged.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1.js should read: