Skip to content

Instantly share code, notes, and snippets.

@akdubya
Created October 23, 2010 21:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save akdubya/642690 to your computer and use it in GitHub Desktop.
Save akdubya/642690 to your computer and use it in GitHub Desktop.
(function(uustats){
uustats.sdev = function(series) {
return Math.sqrt(uustats.variance(series));
};
uustats.variance = function(series) {
var t = 0, squares = 0, len = series.length;
for (var i=0; i<len; i++) {
var obs = series[i];
t += obs;
squares += Math.pow(obs, 2);
}
return (squares/len) - Math.pow(t/len, 2);
},
uustats.mean = function(series) {
var t = 0, len = series.length;
for (var i=0; i<len; i++) {
t += series[i];
}
return t / Math.max(len, 1);
}
uustats.summary = function(series) {
var q = uustats.quantile,
a = series.slice(0).sort(function(a, b) { return a - b });
return {
min: a[0],
p25: q(a, 0.25),
med: q(a, 0.5),
p75: q(a, 0.75),
max: a[a.length - 1],
p10: q(a, 0.1),
p90: q(a, 0.9),
avg: uustats.mean(a)
}
};
uustats.quantile = function(series, q) {
var len = series.length,
pos = q * (len - 1),
t = Math.ceil(pos),
f = t - 1;
if (f < 0) { return series[0] }
if (t >= len) { return series[len - 1] }
return series[f] * (t - pos) + series[t] * (pos - f);
};
uustats.round = function(x, n) {
return Math.round(x*Math.pow(10, n))/Math.pow(10, n);
};
})(typeof exports !== 'undefined' ? exports : window.uustats = {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment