!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(t.simpleStatistics={})}(this,function(t){function r(t){if(0===t.length)return 0;for(var r,n=t[0],e=0,a=1;a=Math.abs(t[a])?e+=n-r+t[a]:e+=t[a]-r+n,n=r;return n+e}function n(t){if(0===t.length)throw new Error("mean requires at least one data point");return r(t)/t.length}function e(t,r){var e,a,o=n(t),i=0;if(2===r)for(a=0;ae&&(e=a,n=r),a=1,r=t[o]):a++;return n}function h(t){return t.slice().sort(function(t,r){return t-r})}function u(t){if(0===t.length)throw new Error("min requires at least one data point");for(var r=t[0],n=1;nr&&(r=t[n]);return r}function s(t,r){var n=t.length*r;if(0===t.length)throw new Error("quantile requires at least one data point.");if(r<0||r>1)throw new Error("quantiles must be between 0 and 1");return 1===r?t[t.length-1]:0===r?t[0]:n%1!=0?t[Math.ceil(n)-1]:t.length%2==0?(t[n-1]+t[n])/2:t[n]}function l(t,r,n,e){for(n=n||0,e=e||t.length-1;e>n;){if(e-n>600){var a=e-n+1,o=r-n+1,i=Math.log(a),h=.5*Math.exp(2*i/3),u=.5*Math.sqrt(i*h*(a-h)/a);o-a/2<0&&(u*=-1),l(t,r,Math.max(n,Math.floor(r-o*h/a+u)),Math.min(e,Math.floor(r+(a-o)*h/a+u)))}var f=t[r],s=n,g=e;for(c(t,n,r),t[e]>f&&c(t,n,e);sf;)g--}t[n]===f?c(t,n,g):c(t,++g,e),g<=r&&(n=g+1),r<=g&&(e=g-1)}}function c(t,r,n){var e=t[r];t[r]=t[n],t[n]=e}function g(t,r){var n=t.slice();if(Array.isArray(r)){!function(t,r){for(var n=[0],e=0;et[t.length-1])return 1;var n=function(t,r){var n=0,e=0,a=t.length;for(;e>>1]?a=n:e=-~n;return e}(t,r);if(t[n]!==r)return n/t.length;n++;var e=function(t,r){var n=0,e=0,a=t.length;for(;e=t[n=e+a>>>1]?e=-~n:a=n;return e}(t,r);if(e===n)return n/t.length;var a=e-n+1;return a*(e+n)/2/a/t.length}function m(t){var r=g(t,.75),n=g(t,.25);if("number"==typeof r&&"number"==typeof n)return r-n}function d(t){return+g(t,.5)}function b(t){for(var r=d(t),n=[],e=0;e0;)e=Math.floor(r()*a--),n=t[a],t[a]=t[e],t[e]=n;return t}function E(t,r){return q(t.slice().slice(),r)}function y(t){for(var r,n=0,e=0;e0){var o=(n[r]-n[t-1])/(r-t+1);a=e[r]-e[t-1]-(r-t+1)*o*o}else a=e[r]-n[r]*n[r]/(r+1);return a<0?0:a}function k(t,r,n,e,a,o,i){if(!(t>r)){var h=Math.floor((t+r)/2);e[n][h]=e[n-1][h-1],a[n][h]=h;var u=n;t>n&&(u=Math.max(u,a[n][t-1]||0)),u=Math.max(u,a[n-1][h]||0);var f,s,l,c=h-1;r=u&&!((f=x(g,h,o,i))+e[n-1][u-1]>=e[n][h]);--g)(s=x(u,h,o,i)+e[n-1][u-1])0?1:0},N.prototype.train=function(t,r){if(0!==r&&1!==r)return null;t.length!==this.weights.length&&(this.weights=t,this.bias=1);var n=this.predict(t);if(n!==r){for(var e=r-n,a=0;a=0?1-n:n-1}function Q(t){var r=8*(Math.PI-3)/(3*Math.PI*(4-Math.PI)),n=Math.sqrt(Math.sqrt(Math.pow(2/(Math.PI*r)+Math.log(1-t*t)/2,2)-Math.log(1-t*t)/r)-(2/(Math.PI*r)+Math.log(1-t*t)/2));return t>=0?n:-n}function X(t){if("number"==typeof t)return t<0?-1:0===t?0:1;throw new TypeError("not a number")}t.linearRegression=function(t){var r,n,e=t.length;if(1===e)r=0,n=t[0][1];else{for(var a,o,i,h=0,u=0,f=0,s=0,l=0;le&&(r=t[a],e=o),n.set(t[a],o)}if(0===e)throw new Error("mode requires at last one data point");return r},t.modeSorted=i,t.min=u,t.max=f,t.extent=function(t){if(0===t.length)throw new Error("extent requires at least one data point");for(var r=t[0],n=t[0],e=1;en&&(n=t[e]),t[e]t.length)throw new Error("cannot generate more classes than there are data values");var n=h(t);if(1===y(n))return[n];var e=S(r,n.length),a=S(r,n.length);!function(t,r,n){for(var e=r[0].length,a=t[Math.floor(e/2)],o=[],i=[],h=0,u=void 0;h=0;u--){var f=a[u][i];o[u]=n.slice(f,i+1),u>0&&(i=f-1)}return o},t.uniqueCountSorted=y,t.sumNthPowerDeviations=e,t.equalIntervalBreaks=function(t,r){if(t.length<2)return t;for(var n=u(t),e=f(t),a=[n],o=(e-n)/r,i=1;i1)throw new Error("bernoulliDistribution requires probability to be between 0 and 1 inclusive");return[1-t,t]},t.binomialDistribution=function(t,r){if(!(r<0||r>1||t<=0||t%1!=0)){var n=0,e=0,a=[],o=1;do{a[n]=o*Math.pow(r,n)*Math.pow(1-r,t-n),e+=a[n],o=o*(t-++n+1)/n}while(e<1-R);return a}},t.poissonDistribution=function(t){if(!(t<=0)){var r=0,n=0,e=[],a=1;do{e[r]=Math.exp(-t)*Math.pow(t,r)/a,n+=e[r],a*=++r}while(n<1-R);return e}},t.chiSquaredDistributionTable=B,t.chiSquaredGoodnessOfFit=function(t,r,e){for(var a=0,o=r(n(t)),i=[],h=[],u=0;u=0;l--)h[l]<3&&(h[l-1]+=h[l],h.pop(),i[l-1]+=i[l],i.pop());for(var c=0;c=0?O[n]:+(1-O[n]).toFixed(4)},t.standardNormalTable=O,t.errorFunction=J,t.erf=J,t.inverseErrorFunction=Q,t.probit=function(t){return 0===t?t=R:t>=1&&(t=1-R),Math.sqrt(2)*Q(2*t-1)},t.permutationTest=function(t,r,e,a){if(void 0===a&&(a=1e4),void 0===e&&(e="two_side"),"two_side"!==e&&"greater"!==e&&"less"!==e)throw new Error("`alternative` must be either 'two_side', 'greater', or 'less'");for(var o=n(t)-n(r),i=new Array(a),h=t.concat(r),u=Math.floor(h.length/2),f=0;f=Math.abs(o)&&(g+=1);else if("greater"===e)for(var p=0;p<=a;p++)i[p]>=o&&(g+=1);else for(var w=0;w<=a;w++)i[w]<=o&&(g+=1);return g/a},t.bisect=function(t,r,n,e,a){if("function"!=typeof t)throw new TypeError("func must be a function");for(var o=0;o