Meant to be used with client-side graphing/analysis tools, as a more dynamic means of rendering graphical reports and charts.
(function(l,j){var o={},b="sum",r="min",t="max",u="mean",h="median",p="range",g="variance",m="stdev",k="quartiles",e="covariance",s="corrcoeff",c=l.min,f=l.max,a=l.floor,q=l.pow,n=l.sqrt,d=l.round,i=function(w,v){return w-v};o[b]=function(v){var x=0,w=v.length-1;for(;w>=0;w--){x+=v[w]}return x};o[r]=function(v){return c.apply(l,v)};o[t]=function(v){return f.apply(l,v)};o[u]=function(v){return o[b](v)/v.length};o[h]=function(v){var x=v.length,w=v.slice(0).sort(i);return !(x&1)?(w[(x/2)-1]+w[(x/2)])/2:w[a(x/2)]};o[p]=function(v){var w=v.slice(0).sort(i);return w[w.length-1]-w[0]};o[g]=function(v){var z=o[u](v),w=0,x=v.length,y=x;for(x--;x>=0;x--){w+=q((v[x]-z),2)}return w/(y-1)};o[m]=function(v){return n(o[g](v))};o[k]=function(v){var x=v.length,w=v.slice(0).sort(i);return[w[d((x)/4)-1],w[d((x)/2)-1],w[d((x)*3/4)-1]]};o[e]=function(A,y){var z=o[u](A),x=o[u](y),C=[],w=A.length,B=0;for(;B<w;B++){C[B]=(A[B]-z)*(y[B]-x)}return o[b](C)/w};o[s]=function(w,v){return o[e](w,v)/o[m](w)/o[m](v)};j.jStat=o})(Math,window);
KGZ1bmN0aW9uKGwsail7dmFyIG89e30sYj0ic3VtIixyPSJtaW4iLHQ9Im1heCIsdT0ibWVhbiIsaD0ibWVkaWFuIixwPSJyYW5nZSIsZz0idmFyaWFuY2UiLG09InN0ZGV2IixrPSJxdWFydGlsZXMiLGU9ImNvdmFyaWFuY2UiLHM9ImNvcnJjb2VmZiIsYz1sLm1pbixmPWwubWF4LGE9bC5mbG9vcixxPWwucG93LG49bC5zcXJ0LGQ9bC5yb3VuZCxpPWZ1bmN0aW9uKHcsdil7cmV0dXJuIHctdn07b1tiXT1mdW5jdGlvbih2KXt2YXIgeD0wLHc9di5sZW5ndGgtMTtmb3IoO3c+PTA7dy0tKXt4Kz12W3ddfXJldHVybiB4fTtvW3JdPWZ1bmN0aW9uKHYpe3JldHVybiBjLmFwcGx5KGwsdil9O29bdF09ZnVuY3Rpb24odil7cmV0dXJuIGYuYXBwbHkobCx2KX07b1t1XT1mdW5jdGlvbih2KXtyZXR1cm4gb1tiXSh2KS92Lmxlbmd0aH07b1toXT1mdW5jdGlvbih2KXt2YXIgeD12Lmxlbmd0aCx3PXYuc2xpY2UoMCkuc29ydChpKTtyZXR1cm4gISh4JjEpPyh3Wyh4LzIpLTFdK3dbKHgvMildKS8yOndbYSh4LzIpXX07b1twXT1mdW5jdGlvbih2KXt2YXIgdz12LnNsaWNlKDApLnNvcnQoaSk7cmV0dXJuIHdbdy5sZW5ndGgtMV0td1swXX07b1tnXT1mdW5jdGlvbih2KXt2YXIgej1vW3VdKHYpLHc9MCx4PXYubGVuZ3RoLHk9eDtmb3IoeC0tO3g+PTA7eC0tKXt3Kz1xKCh2W3hdLXopLDIpfXJldHVybiB3Lyh5LTEpfTtvW21dPWZ1bmN0aW9uKHYpe3JldHVybiBuKG9bZ10odikpfTtvW2tdPWZ1bmN0aW9uKHYpe3ZhciB4PXYubGVuZ3RoLHc9di5zbGljZSgwKS5zb3J0KGkpO3JldHVyblt3W2QoKHgpLzQpLTFdLHdbZCgoeCkvMiktMV0sd1tkKCh4KSozLzQpLTFdXX07b1tlXT1mdW5jdGlvbihBLHkpe3ZhciB6PW9bdV0oQSkseD1vW3VdKHkpLEM9W10sdz1BLmxlbmd0aCxCPTA7Zm9yKDtCPHc7QisrKXtDW0JdPShBW0JdLXopKih5W0JdLXgpfXJldHVybiBvW2JdKEMpL3d9O29bc109ZnVuY3Rpb24odyx2KXtyZXR1cm4gb1tlXSh3LHYpL29bbV0odykvb1ttXSh2KX07ai5qU3RhdD1vfSkoTWF0aCx3aW5kb3cpOw==
/* jStat - Statistical Library
* Copyright (c) 2010, Trevor Norris
* This document is licensed as free software under the terms of the
* MIT License: http://www.opensource.org/licenses/mit-license.php */
(function(Math,window){
var jStat = {},
sum = 'sum',
min = 'min',
max = 'max',
mean = 'mean',
median = 'median',
range = 'range',
variance = 'variance',
stdev = 'stdev',
quartiles = 'quartiles',
covariance = 'covariance',
corrcoeff = 'corrcoeff',
Mmin = Math.min,
Mmax = Math.max,
Mfloor = Math.floor,
Mpow = Math.pow,
Msqrt = Math.sqrt,
Mround = Math.round,
arrSortF = function(a, b){ return a - b; };
/**
* Sum of an array
* @return {Number}
* @param {Array} arr
* @example
* jStat.sum([1,2,3])
*/
jStat[sum] = function(arr){
var sm = 0,
i = arr.length - 1;
for(; i >= 0; i--){
sm += arr[i];
};
return sm;
};
/**
* Minimum value of an array
* @return {Number}
* @param {Array} arr
* @example
* jStat.min([1,2,3])
*/
jStat[min] = function(arr){
return Mmin.apply(Math,arr);
};
/**
* Maximum value of an array
* @return {Number}
* @param {Array} arr
* @example
* jStat.max([1,2,3])
*/
jStat[max] = function(arr){
return Mmax.apply(Math,arr);
};
/**
* Mean value of an array
* @return {Number}
* @param {Array} arr
* @example
* jStat.mean([1,2,3])
*/
jStat[mean] = function(arr){
return jStat[sum](arr) / arr.length;
};
/**
* Median of an array
* @return {Number}
* @param {Array} arr
* @example
* jStat.median([1,2,3,4,5,6])
*/
jStat[median] = function(arr){
var arrlen = arr.length,
_arr = arr.slice(0).sort(arrSortF);
// Check if array is even or odd, then return the appropriate
return !(arrlen & 1) ? (_arr[(arrlen / 2) - 1] + _arr[(arrlen / 2)]) / 2 : _arr[Mfloor(arrlen / 2)];
};
/**
* Range of an array
* @return {Number}
* @param {Array} arr
* @example
* jStat.range([1,6,5,3,8,6])
*/
jStat[range] = function(arr){
var _arr = arr.slice(0).sort(arrSortF);
return _arr[_arr.length - 1] - _arr[0];
};
/**
* Variance of an array
* @return {Number}
* @param {Array} arr
* @example
* jStat.variance([1,6,8,5,4,9,5,3])
*/
jStat[variance] = function(arr){
var meen = jStat[mean](arr),
stSum = 0,
i = arr.length,
arrlen = i;
for(i--; i >= 0; i--){
stSum += Mpow((arr[i] - meen), 2);
};
return stSum / (arrlen - 1);
};
/**
* Standard deviation of an array
* @return {Number}
* @param {Array} arr
* @example
* jStat.stdev([4,5,9,7,5,3,4])
*/
jStat[stdev] = function(arr){
return Msqrt(jStat[variance](arr));
};
/**
* Quartiles of an array
* @return {Array}
* @param {Array} arr
* @example
* jStat.quartiles([1,2,3,6,9,3,1,2,5])
*/
jStat[quartiles] = function(arr){
var arrlen = arr.length,
_arr = arr.slice(0).sort(arrSortF);
return [_arr[Mround((arrlen) / 4) - 1], _arr[Mround((arrlen) / 2) - 1], _arr[Mround((arrlen) * 3 / 4) - 1]];
};
/**
* Covariance of two arrays
* @return {Number}
* @param {Array} arr1
* @param {Array} arr2
* @example
* jStat.covariance([1,2,3,6,9,3,1,2,5],[2,3,5,2,5,7,8,9,6])
*/
jStat[covariance] = function(arr1, arr2){
var u = jStat[mean](arr1),
v = jStat[mean](arr2),
sq_dev = [],
arr1Len = arr1.length,
i = 0;
for(; i < arr1Len; i++){
sq_dev[i] = (arr1[i] - u) * (arr2[i] - v);
};
return jStat[sum](sq_dev) / arr1Len;
};
/**
* Correlation coefficient of two arrays
* @return {Number}
* @param {Array} arr1
* @param {Array} arr2
* @example
* jStat.corrcoeff([1,2,3,6,9,3,1,2,5], [2,3,5,2,5,7,8,9,6])
*/
jStat[corrcoeff] = function(arr1, arr2){
return jStat[covariance](arr1,arr2) / jStat[stdev](arr1) / jStat[stdev](arr2);
};
// Exposing jStat
window.jStat = jStat;
})(Math,window);