Skip to content

Instantly share code, notes, and snippets.

@munro
Created March 7, 2013 22:11
Show Gist options
  • Save munro/5112300 to your computer and use it in GitHub Desktop.
Save munro/5112300 to your computer and use it in GitHub Desktop.
var set = [[0, 0], [1, 1], [2, 3], [3, 4.3], [4, 4.2], [5, 4.6], [6, 4.1], [6, 1.6], [8, 0.5], [9, 0]];
var mean = (function (v) {
return v[0] / v[1];
}(set.reduce(function (a, b) {
return [a[0] + b[0] * b[1], a[1] + b[1]];
})));
var stdv = (function (v) {
return Math.sqrt(v[0] / v[1]);
}(set.map(function (v) {
return [Math.pow(v[0] - mean, 2), v[1]];
}).reduce(function (a, b) {
return [a[0] + b[0] * b[1], a[1] + b[1]];
})));
console.log('mean', mean); // 4.201716738197424
console.log('stdv', stdv); // 1.822679662909687
console.log('-1σ', mean - stdv); // 2.3790370752877372
console.log(' 1σ', mean + stdv); // 6.024396401107111
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment