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