Comparing version 0.2.1 to 0.2.2
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.probably=t.probably||{})}(this,function(t){"use strict";function e(t,e){if(e>t)return NaN;for(var r=1,n=1;e>=n;n++)r*=t--,r/=n;return r}function r(t){var e=1/(1+.5*Math.abs(t)),r=e*Math.exp(-(t*t)-1.26551223+1.00002368*e+.37409196*Math.pow(e,2)+.09678418*Math.pow(e,3)-.18628806*Math.pow(e,4)+.27886807*Math.pow(e,5)-1.13520398*Math.pow(e,6)+1.48851587*Math.pow(e,7)-.82215223*Math.pow(e,8)+.17087277*Math.pow(e,9));return t>=0?1-r:r-1}function n(t){return 0>t?NaN:0==t?1:t*n(t-1)}function a(t){if(0>t)return NaN;for(var e=C[0],r=C.length-1;r>0;--r)e+=C[r]/(t+r);var n=t+N+.5;return.5*Math.log(2*Math.PI)+(t+.5)*Math.log(n)-n+Math.log(e)-Math.log(t)}function o(t){var e=t;if(0>e&&e%1==0)return 1/0;if(.5>e)return Math.PI/(Math.sin(Math.PI*e)*o(1-e));if(e>100)return Math.exp(a(e));e-=1;for(var r=E[0],n=1;O+2>n;n++)r+=E[n]/(e+n);var i=e+O+.5;return Math.sqrt(2*Math.PI)*Math.pow(i,e+.5)*Math.exp(-i)*r}function i(t,e){var r=void 0,n=void 0,o=void 0;for(o=1,r=1,n=1;100>o&&(r+=n*=e/(t+o),!(q>n/r));++o);return Math.exp(t*Math.log(e)-e-a(t+1)+Math.log(r))}function u(t){if(0==t.length)return 0;var e=0;return t.reduce(function(t,r){var n=r-e,a=t+n;return e=a-t-n,a},0)}function s(t){return 0==t.length?NaN:u(t)/t.length}function l(t){if(0==t.length)return NaN;if(1==t.length)return t;var e=function(){var e=t.reduce(function(t,e){return t[e]?t[e]++:t[e]=1,t},{}),r=Object.keys(e),n=r.map(function(t){return e[t]}).reduce(function(t,e){return e>t?e:t});return{v:r.filter(function(t){return e[t]==n}).map(function(t){return Number(t)})}}();return"object"===("undefined"==typeof e?"undefined":g["typeof"](e))?e.v:void 0}function f(t,e,r,n){var a=t.slice(0);if(0==a.length||e>=a.length)return NaN;var o=r||0,i=n||a.length-1;if(o==i)return a[o];for(;i>o;){if(i-o>D){var u=i-o+1,s=e-o+1,l=Math.log(u),h=j*Math.exp(2*l/3),c=j*Math.sqrt(l*h*(u-h)/u),d=Math.max(o,Math.floor(e-s*h/u+c)),m=Math.min(i,Math.floor(e+(u-s)*h/u+c));f(a,e,d,m)}var p=a[e],y=o,b=i;for(v(a,y,e),a[b]>p&&v(a,y,b);b>y;){for(v(a,y,b),y++,b--;a[y]<p;)y++;for(;a[b]>p;)b--}a[o]===p?v(a,o,b):(b++,v(a,b,i)),b>e?i=b-1:(o=b+1,i=b-1)}return a[e]}function v(t,e,r){var n=t[e];t[e]=t[r],t[r]=n}function h(t){var e=void 0;if(0==t.length)e=NaN;else{var r=t.length%2==0;r?(e=f(t,t.length/2),e+=f(t,t.length/2-1),e/=2):e=f(t,(t.length-1)/2)}return e}function c(t,e){if(void 0==e||e>1||0>e)throw new Error("p must be between zero and one inclusive.");if(void 0==t)throw new Error("need a list to provide a percentile.");if(0==t.length)return NaN;var r=Math.floor(t.length*e);return r>=t.length&&(r=t.length-1),f(t,r)}function d(t,e){if(!Array.isArray(t)||0==t.length)throw new Error("you must provide an array.");if(!Array.isArray(e)||0==e.length)throw new Error("you must provide some quantiles.");var r=e.reduce(function(t,e){return e>=0&&1>=e&&t},!0);if(!r)throw new Error("quantiles must be between zero and one inclusive.");if(e.length>1)return e.map(function(e){return d(t,[e])});var n=e[0];if(0==n)return t[0];if(1==n)return t[t.length-1];var a=t.length*n;return a%1?f(t,Math.floor(a)):t.length%2?f(t,a):(f(t,a-1)+f(t,a))/2}function m(t){return 0==t.length?NaN:Math.max.apply(Math,g.toConsumableArray(t))-Math.min.apply(Math,g.toConsumableArray(t))}function p(t,e,r){if(0==t.length)return NaN;var n=function(){var n=s(t);return{v:t.reduce(function(t,a){var o=a-n;return r&&(o=Math.abs(o)),t+Math.pow(o,e)},0)}}();return"object"===("undefined"==typeof n?"undefined":g["typeof"](n))?n.v:void 0}function y(t){return 0==t.length?NaN:p(t,2)/t.length}function b(t){var e=y(t);return isNaN(e)?0:Math.sqrt(e)}function k(t){return(t+.5)*Math.log(t)-t+Math.log(2*Math.PI)/2}function M(t,e,r){return(t-e)/r}function w(t,e,r){if(!t)return NaN;if(e){var n=r||0,a=((t.size-1)*t.variance+(e.size-1)*e.variance)/(t.size+e.size-2);return(t.mean-e.mean-n)/Math.sqrt(a*(1/t.size+1/e.size))}return(t.mean-r)/(t.std/Math.sqrt(t.size))}var g={};g["typeof"]="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol?"symbol":typeof t},g.classCallCheck=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},g.createClass=function(){function t(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,r,n){return r&&t(e.prototype,r),n&&t(e,n),e}}(),g["extends"]=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},g.get=function Y(t,e,r){null===t&&(t=Function.prototype);var n=Object.getOwnPropertyDescriptor(t,e);if(void 0===n){var a=Object.getPrototypeOf(t);return null===a?void 0:Y(a,e,r)}if("value"in n)return n.value;var o=n.get;if(void 0!==o)return o.call(r)},g.inherits=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},g.possibleConstructorReturn=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},g.toConsumableArray=function(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e<t.length;e++)r[e]=t[e];return r}return Array.from(t)};var N=607/128,C=[.9999999999999971,57.15623566586292,-59.59796035547549,14.136097974741746,-.4919138160976202,3399464998481189e-20,4652362892704858e-20,-9837447530487956e-20,.0001580887032249125,-.00021026444172410488,.00021743961811521265,-.0001643181065367639,8441822398385275e-20,-26190838401581408e-21,36899182659531625e-22],O=7,E=[.9999999999998099,676.5203681218851,-1259.1392167224028,771.3234287776531,-176.6150291621406,12.507343278686905,-.13857109526572012,9984369578019572e-21,1.5056327351493116e-7],q=1e-14,D=600,j=.5,x=function(){function t(e){g.classCallCheck(this,t);var r=Object.assign({},e);this.params=this.constructor.validate(r),this.mean=this.constructor.mean(r),this.stdDev=this.constructor.stdDev(r),this.relStdDev=this.constructor.relStdDev(r),this.variance=this.constructor.variance(r),this.skewness=this.constructor.skewness(r),this.kurtosis=this.constructor.kurtosis(r)}return g.createClass(t,null,[{key:"validate",value:function(){throw new Error("You must define a validation function for your distribution.")}},{key:"mean",value:function(){}},{key:"variance",value:function(){}},{key:"stdDev",value:function(){}},{key:"relStdDev",value:function(){}},{key:"skewness",value:function(){}},{key:"kurtosis",value:function(){}},{key:"pdf",value:function(){}},{key:"cdf",value:function(){}},{key:"random",value:function(){return Math.random()}},{key:"sample",value:function(t,e){var r=this;return Array.apply(null,Array(t)).map(function(){return r.random(e)})}}]),g.createClass(t,[{key:"pdf",value:function(t){return this.constructor.pdf(t,this.params)}},{key:"cdf",value:function(t){return this.constructor.cdf(t,this.params)}},{key:"random",value:function(){return this.constructor.random(this.params)}},{key:"sample",value:function(t){return this.constructor.sample(t,this.params)}}]),t}();x.covariates=0,x.discrete=!1;var P=function(t){function e(){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return g.inherits(e,t),g.createClass(e,null,[{key:"validate",value:function(t){if(!t||void 0===t.a||void 0===t.b)throw new Error("need a parameter object of shape { a: number, b: number }.");var e=t.a,r=t.b;if("number"!=typeof e)throw Error("a must be a number.");if("number"!=typeof r||0>=r)throw RangeError("b must be greater than zero.");return t}},{key:"random",value:function(t){for(var r=this.validate(t),n=r.a,a=r.b,o=void 0;!o||.5==o;)o=g.get(Object.getPrototypeOf(e),"random",this).call(this);return n+a*Math.tan(Math.PI*o)}},{key:"pdf",value:function(t,e){if("number"!=typeof t)throw new TypeError("x must be a number");var r=this.validate(e),n=r.a,a=r.b;return a/(Math.pow(t-n,2)+Math.pow(a,2))/Math.PI}},{key:"cdf",value:function(t,e){if("number"!=typeof t)throw new TypeError("x must be a number");var r=this.validate(e),n=r.a,a=r.b;return Math.atan((t-n)/a)/Math.PI+.5}},{key:"mean",value:function(t){return NaN}},{key:"variance",value:function(t){return NaN}},{key:"stdDev",value:function(t){return NaN}},{key:"relStdDev",value:function(t){return NaN}},{key:"skewness",value:function(t){return NaN}},{key:"kurtosis",value:function(t){return NaN}}]),e}(x);P.covariates=1,P.discrete=!1;var S=NaN,z=function(t){function e(){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return g.inherits(e,t),g.createClass(e,null,[{key:"validate",value:function(t){if(!t||void 0===t.mu||void 0==t.sigma)throw new Error("need a parameter object of shape { mu: number, simga: number }.");var e=t.mu,r=t.sigma;if("number"!=typeof e||"number"!=typeof r)throw new Error("Need mu and sigma for the normal distribution.");return t}},{key:"random",value:function(t){var r=this.validate(t),n=r.mu,a=r.sigma,o=S;if(S=NaN,!o){var i=2*g.get(Object.getPrototypeOf(e),"random",this).call(this)*Math.PI,u=Math.sqrt(-2*Math.log(1-g.get(Object.getPrototypeOf(e),"random",this).call(this)));o=Math.cos(i)*u,S=Math.sin(i)*u}return n+o*a}},{key:"pdf",value:function(t,e){if("number"!=typeof t)throw new Error("x must be a number.");var r=this.validate(e),n=r.mu,a=r.sigma;t/Math.abs(a);return 1/(Math.sqrt(2*Math.PI)*Math.abs(a))*Math.exp(-1*Math.pow(t-n,2)/(2*a*a))}},{key:"cdf",value:function(t,e){if("number"!=typeof t)throw new Error("x must be a number.");var n=this.validate(e),a=n.mu,o=n.sigma;return.5*(1+r((t-a)/(o*Math.sqrt(2))))}},{key:"mean",value:function(t){var e=this.validate(t),r=e.mu;e.sigma;return r}},{key:"variance",value:function(t){var e=this.validate(t),r=(e.mu,e.sigma);return r*r}},{key:"stdDev",value:function(t){var e=this.validate(t),r=(e.mu,e.sigma);return r}},{key:"relStdDev",value:function(t){var e=this.validate(t),r=e.mu,n=e.sigma;return n/r}},{key:"skewness",value:function(t){return 0}},{key:"kurtosis",value:function(t){return 3}}]),e}(x);z.covariates=2,z.discrete=!1;var A=function(t){function e(){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return g.inherits(e,t),g.createClass(e,null,[{key:"validate",value:function(t){if(!t||void 0===t.mu)throw new Error("need a parameter object of shape { mu: number }.");var e=t.mu;if("number"!=typeof e||0>=e)throw RangeError("mu must be greater than zero.");return t}},{key:"random",value:function(t){var r=this.validate(t),n=r.mu;return-n*Math.log1p(-g.get(Object.getPrototypeOf(e),"random",this).call(this))}},{key:"pdf",value:function(t,e){var r=this.validate(e),n=r.mu;if("number"!=typeof t)throw new Error("x must be a number.");return 0>t?0:Math.exp(-t/n)/n}},{key:"cdf",value:function(t,e){var r=this.validate(e),n=r.mu;if("number"!=typeof t)throw new Error("x must be a number.");return 0>=t?0:1-Math.exp(-t/n)}},{key:"mean",value:function(t){var e=this.validate(t),r=e.mu;return r}},{key:"variance",value:function(t){var e=this.validate(t),r=e.mu;return r*r}},{key:"stdDev",value:function(t){var e=this.validate(t),r=e.mu;return r}},{key:"relStdDev",value:function(t){return 1}},{key:"skewness",value:function(t){return 2}},{key:"kurtosis",value:function(t){return 9}}]),e}(x);A.covariates=1;var R=function(t){function e(){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return g.inherits(e,t),g.createClass(e,null,[{key:"validate",value:function(t){if(!t||void 0===t.a||void 0===t.b)throw new Error("need a parameter object of shape { a: number, b: number }.");var e=t.a,r=t.b;if("number"!=typeof e||"number"!=typeof r)throw new Error("Need a and b for the gamma distribution.");if(0>=e)throw new Error("a must be greater than zero.");if(0>=r)throw new Error("b must be greater than zero.");return t}},{key:"random",value:function(t){var r=this.validate(t),n=r.a,a=r.b;if(1>n){var o=g.get(Object.getPrototypeOf(e),"random",this).call(this);return this.random({a:1+n,b:a})*Math.pow(o,1/n)}for(var i=void 0,u=void 0,s=void 0,l=n-1/3,f=1/Math.sqrt(9*l);;){do i=z.random({mu:0,sigma:1}),u=1+f*i;while(0>=u);for(u=u*u*u;!s;)s=g.get(Object.getPrototypeOf(e),"random",this).call(this);if(1-.0331*i*i*i*i>s)break;if(Math.log(s)<.5*i*i+l*(1-u+Math.log(u)))break}return l*u/a}},{key:"pdf",value:function(t,e){var r=this.validate(e),n=r.a,o=r.b;if("number"!=typeof t)throw new Error("x must be a number.");return 0>t?0:0==t?1==n?o:0:1==n?Math.exp(-t*o)*o:Math.exp((n-1)*Math.log(t*o)-t*o-a(n))*o}},{key:"cdf",value:function(t,e){var r=this.validate(e),n=r.a,a=r.b;if("number"!=typeof t)throw new Error("x must be a number.");return 0>=t?0:i(n,t*a)}},{key:"mean",value:function(t){var e=this.validate(t),r=e.a,n=e.b;return r/n}},{key:"variance",value:function(t){var e=this.validate(t),r=e.a,n=e.b;return r/(n*n)}},{key:"stdDev",value:function(t){var e=this.validate(t),r=e.a,n=e.b;return Math.sqrt(r/(n*n))}},{key:"relStdDev",value:function(t){var e=this.validate(t),r=e.a;e.b;return 1/Math.sqrt(r)}},{key:"skewness",value:function(t){var e=this.validate(t),r=e.a;e.b;return 2/Math.sqrt(r)}},{key:"kurtosis",value:function(t){var e=this.validate(t),r=e.a;e.b;return 6/r}}]),e}(x);R.covariates=2;var I=14,T=function(t){function r(){return g.classCallCheck(this,r),g.possibleConstructorReturn(this,Object.getPrototypeOf(r).apply(this,arguments))}return g.inherits(r,t),g.createClass(r,null,[{key:"validate",value:function(t){if(!t||void 0===t.n||void 0===t.p)throw new Error("need a parameter object of shape { n: number, p: number }.");var e=t.n,r=t.p;if("number"!=typeof r||r>1||0>r)throw new Error("p must be between zero and one inclusive.");if("number"!=typeof e||0>e)throw new Error("n must be positive or zero.");return t}},{key:"random",value:function(t){var e=this,n=this.validate(t),a=n.n,o=n.p;if(0==a)return 0;var i=!1,u=0,s=void 0;o>.5?(i=!0,s=1-o):s=o;var l=1-s,f=s/l,v=a*s;if(I>v)for(var h=Math.pow(l,a),c=g.get(Object.getPrototypeOf(r),"random",this).call(this);a>=u&&c>=h;)c-=h,h*=f*(a-u)/(u+1),u++;else!function(){var t=v+s,n=Math.floor(t),o=n+.5,i=v*l,f=Math.floor(2.195*Math.sqrt(i)-4.6*l)+.5,h=o-f,c=o+f,d=.134+20.5/(15.3+n),m=f*(1+d+d),p=(t-h)/(t-h*s),y=p*(1+.5*p),b=(c-t)/(c*l),M=b*(1.5+b),w=m+d/y,N=w+d/M,C=void 0,O=void 0,E=void 0,q=void 0,D=function j(){if(E=g.get(Object.getPrototypeOf(r),"random",e).call(e)*N,q=g.get(Object.getPrototypeOf(r),"random",e).call(e),f>=E)u=Math.floor(o-f*q+E);else if(m>=E){var t=h+(E-f)/d;q=q*d+1-Math.abs(t-o)/f,q>1||0>=q?j():u=t}else w>=E?(u=Math.floor(h+Math.log(q)/y),q*=(E-m)*y):(u=Math.floor(c-Math.log(q)/M),q*=(E-w)*M);C=Math.log(q);var i=u+1,v=a-u-1,p=n+1,b=a+1-n;O=o*Math.log(p/i)+(a-n+.5)*Math.log(b/v)+(u-n)*Math.log(v*s/(i*l))+k(p)+k(b)-k(i)-k(v),C>O&&j()};D()}();return Math.floor(i?a-u:u)}},{key:"pdf",value:function(t,r){var n=this.validate(r),a=n.n,o=n.p;if("number"!=typeof t)throw new Error("k must be a number.");if(0>t||t>a)return 0;var i=void 0;if(0==o)i=0==t?1:0;else{if(1!=o){var u=e(a,t),s=Math.pow(o,t)*Math.pow(1-o,a-t);return"Infinity"==u?0==s?0:u:u*s}i=t==a?1:0}return i}},{key:"cdf",value:function(t,r){var n=this.validate(r),a=n.n,o=n.p;if("number"!=typeof t)throw new Error("k must be a number.");return 0>t?0:t>a?1:Array.apply(null,Array(Math.floor(t)+1)).map(function(t,r){return e(a,r)*Math.pow(o,r)*Math.pow(1-o,a-r)}).reduce(function(t,e){return t+e},0)}},{key:"mean",value:function(t){var e=this.validate(t),r=e.n,n=e.p;return r*n}},{key:"variance",value:function(t){var e=this.validate(t),r=e.n,n=e.p;return r*n*(1-n)}},{key:"stdDev",value:function(t){var e=this.validate(t),r=e.n,n=e.p;return Math.sqrt(r*n*(1-n))}},{key:"relStdDev",value:function(t){var e=this.validate(t),r=e.n,n=e.p;return Math.sqrt((1-n)/(r*n))}},{key:"skewness",value:function(t){var e=this.validate(t),r=e.n,n=e.p;return(1-2*n)/Math.sqrt(r*n*(1-n))}},{key:"kurtosis",value:function(t){var e=this.validate(t),r=e.n,n=e.p;return 3-6/r+1/(r*n*(1-n))}}]),r}(x);T.covariates=2,T.discrete=!0;var _=function(t){function e(t){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).call(this,g["extends"]({},t,{n:1})))}return g.inherits(e,t),g.createClass(e,null,[{key:"random",value:function(t){var e=this.validate(g["extends"]({},t,{n:1})),r=e.p,n=x.random();return r>n?1:0}},{key:"pdf",value:function(t,e){var r=this.validate(g["extends"]({},e,{n:1})),n=r.p;if("number"!=typeof t)throw new Error("k must be a number.");return 0==t?1-n:1==t?n:0}},{key:"cdf",value:function(t,e){var r=this.validate(g["extends"]({},e,{n:1})),n=r.p;if("number"!=typeof t)throw new Error("k must be a number.");return 1==t?n:0==t?1-n:NaN}}]),e}(T);_.covariates=1;var B=function(t){function e(){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return g.inherits(e,t),g.createClass(e,null,[{key:"validate",value:function(t){if(!t||void 0===t.df)throw new Error("need a parameter object of shape { df: number }.");var e=t.df;if("number"!=typeof e||0>=e)throw RangeError("df must be greater than zero.");return t}},{key:"random",value:function(t){var e=this.validate(t),r=e.df;return 2*R.random({a:r/2,b:1})}},{key:"pdf",value:function(t,e){var r=this.validate(e),n=r.df;if("number"!=typeof t)throw new TypeError("x must be a number");return 0>t?0:2==n?Math.exp(-t/2)/2:Math.exp((n/2-1)*Math.log(t/2)-t/2-a(n/2))/2}},{key:"cdf",value:function(t,e){var r=this.validate(e),n=r.df;if("number"!=typeof t)throw new TypeError("x must be a number");return 0>t?0:2==n?1-Math.exp(-t/2):i(n/2,t/2)}},{key:"mean",value:function(t){var e=this.validate(t),r=e.df;return r}},{key:"variance",value:function(t){var e=this.validate(t),r=e.df;return 2*r}},{key:"stdDev",value:function(t){var e=this.validate(t),r=e.df;return Math.sqrt(2*r)}},{key:"relStdDev",value:function(t){var e=this.validate(t),r=e.df;return Math.sqrt(2/r)}},{key:"skewness",value:function(t){var e=this.validate(t),r=e.df;return Math.pow(2,1.5)/Math.sqrt(r)}},{key:"kurtosis",value:function(t){var e=this.validate(t),r=e.df;return 3+12/r}}]),e}(x);B.covariates=1;var F=function(t){function e(){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return g.inherits(e,t),g.createClass(e,null,[{key:"validate",value:function(t){if(!t||void 0===t.df)throw new Error("need a parameter object of shape { df: number }.");var e=t.df;if("number"!=typeof e||0>=e)throw RangeError("df must be greater than zero.");return t}},{key:"random",value:function(t){var e=this.validate(t),r=e.df;if(2>=r){var n=z.random({mu:0,sigma:1}),a=B.random({df:r});return n/Math.sqrt(a/r)}var o=void 0,i=void 0,u=void 0;do o=z.random({mu:0,sigma:1}),i=A.random({mu:1/(r/2-1)}),u=o*i/(r-2);while(0>1-u||Math.exp(-i-u)>1-u);return o/Math.sqrt((1-2/r)*(1-u))}},{key:"pdf",value:function(t,e){if("number"!=typeof t)throw TypeError("t must be a number.");var r=this.validate(e),n=r.df,o=a(n/2),i=a((n+1)/2);return Math.exp(i-o)/Math.sqrt(Math.PI*n)*Math.pow(1+t*t/n,-(n+1)/2)}},{key:"cdf",value:function(t,e){if("number"!=typeof t)throw TypeError("t must be a number.");var r=this.validate(e),n=r.df,a=void 0,o=void 0,i=void 0,u=void 0,s=void 0,l=void 0,f=void 0,v=void 0,h=void 0,c=void 0,d=1/Math.PI;if(i=n,a=t/Math.sqrt(i),o=i/(i+t*t),u=n-2,s=i%2,l=1,f=1,i=1,v=2+s,h=v,u>=2)for(c=v;u>=c;c+=2)f=f*o*(h-1)/h,l+=f,i=l,h+=2;return 1!=s?.5+.5*a*Math.sqrt(o)*l:(1==n&&(l=0),.5+(a*o*l+Math.atan(a))*d)}},{key:"mean",value:function(t){var e=this.validate(t),r=e.df;return 1>=r?NaN:0}},{key:"variance",value:function(t){var e=this.validate(t),r=e.df;return 1>r?NaN:2>=r?1/0:r/(r-2)}},{key:"stdDev",value:function(t){var e=this.validate(t),r=e.df;return 1>r?NaN:2>=r?1/0:Math.sqrt(r/(r-2))}},{key:"relStdDev",value:function(t){var e=this.validate(t);e.df;return NaN}},{key:"skewness",value:function(t){var e=this.validate(t),r=e.df;return r>3?0:NaN}},{key:"kurtosis",value:function(t){var e=this.validate(t),r=e.df;return 4>=r?NaN:3*(r-2)/(r-4)}}]),e}(x);F.covariates=1;var G=function(){function t(e){var r=this;if(g.classCallCheck(this,t),this.covariance=function(t){return r.constructor.covariance(r,t)},this.correlation=function(t){return r.constructor.correlation(r,t)},0==e.length)throw new Error("Sample of size 0 not allowed.");this.data=e,this.size=e.length,this.mean=s(e),this.std=this.constructor.stdDev(e),this.variance=this.constructor.variance(e),this.skewness=this.constructor.skewness(e),this.kurtosis=this.constructor.kurtosis(e),this.sqrdMeanDev=this.constructor.sqrdMeanDev(e),this.meanDev=this.constructor.meanDev(e),this.rootMeanSqrd=this.constructor.rootMeanSqrd(e),this.stdMeanDev=this.constructor.stdMeanDev(e),this.relStdDev=this.constructor.relativeStdDev(e),this.quartiles=this.constructor.quartiles(e)}return g.createClass(t,null,[{key:"variance",value:function(t){return p(t,2)/(t.length-1)}},{key:"sqrdMeanDev",value:function(t){return p(t,2)}},{key:"meanDev",value:function(t){return p(t,1,!0)/t.length}},{key:"stdDev",value:function(t){var e=this.variance(t);return isNaN(e)?0:Math.sqrt(e)}},{key:"rootMeanSqrd",value:function(t){return Math.sqrt(t.map(function(t){return t*t}).reduce(function(t,e){return t+e})/t.length)}},{key:"stdMeanDev",value:function(t){return this.stdDev(t)/Math.sqrt(t.length)}},{key:"relativeStdDev",value:function(t){return this.stdDev(t)/s(t)}},{key:"quartiles",value:function(t){return d(t,[.25,.5,.75])}},{key:"skewness",value:function(t){var e=this.stdDev(t);if(isNaN(e)||t.length<3)return NaN;var r=t.length,n=Math.pow(e,3),a=p(t,3);return r*a/((r-1)*(r-2)*n)}},{key:"kurtosis",value:function(t){var e=this.stdDev(t);if(isNaN(e)||t.length<3)return NaN;var r=(t.length,p(t,2)),n=p(t,4);return n/Math.pow(r,2)}},{key:"covariance",value:function(t,e){if(!t||!e||t.size<=1||t.size!==e.size)return NaN;var r=t.data.map(function(r,n){return(t.data[n]-t.mean)*(e.data[n]-e.mean)}).reduce(function(t,e){return t+e},0);return r/(t.size-1)}},{key:"correlation",value:function(t,e){if(!(t&&t.std&&e&&e.std))return NaN;var r=this.covariance(t,e);return r/t.std/e.std}}]),t}();t.choose=e,t.error=r,t.factorial=n,t.gamma=o,t.gammainc_lower=i,t.lngamma=a,t.median=h,t.mean=s,t.mode=l,t.percentile=c,t.quantile=d,t.range=m,t.select=f,t.std=b,t.stirling=k,t.sum=u,t.sumNthPowerDev=p,t.variance=y,t.zscore=M,t.Normal=z,t.Binomial=T,t.Bernoulli=_,t.StudentsT=F,t.Gamma=R,t.ChiSquared=B,t.Exponential=A,t.Cauchy=P,t.Sample=G,t.ttest=w}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.sampson=t.sampson||{})}(this,function(t){"use strict";function e(t,e){if(e>t)return NaN;for(var r=1,n=1;e>=n;n++)r*=t--,r/=n;return r}function r(t){var e=1/(1+.5*Math.abs(t)),r=e*Math.exp(-(t*t)-1.26551223+1.00002368*e+.37409196*Math.pow(e,2)+.09678418*Math.pow(e,3)-.18628806*Math.pow(e,4)+.27886807*Math.pow(e,5)-1.13520398*Math.pow(e,6)+1.48851587*Math.pow(e,7)-.82215223*Math.pow(e,8)+.17087277*Math.pow(e,9));return t>=0?1-r:r-1}function n(t){return 0>t?NaN:0==t?1:t*n(t-1)}function a(t){if(0>t)return NaN;for(var e=C[0],r=C.length-1;r>0;--r)e+=C[r]/(t+r);var n=t+N+.5;return.5*Math.log(2*Math.PI)+(t+.5)*Math.log(n)-n+Math.log(e)-Math.log(t)}function o(t){var e=t;if(0>e&&e%1==0)return 1/0;if(.5>e)return Math.PI/(Math.sin(Math.PI*e)*o(1-e));if(e>100)return Math.exp(a(e));e-=1;for(var r=E[0],n=1;O+2>n;n++)r+=E[n]/(e+n);var i=e+O+.5;return Math.sqrt(2*Math.PI)*Math.pow(i,e+.5)*Math.exp(-i)*r}function i(t,e){var r=void 0,n=void 0,o=void 0;for(o=1,r=1,n=1;100>o&&(r+=n*=e/(t+o),!(q>n/r));++o);return Math.exp(t*Math.log(e)-e-a(t+1)+Math.log(r))}function u(t){if(0==t.length)return 0;var e=0;return t.reduce(function(t,r){if(isNaN(r))return t;var n=r-e,a=t+n;return e=a-t-n,a},0)}function s(t){return 0==t.length?NaN:u(t)/t.filter(function(t){return!isNaN(t)}).length}function l(t){if(0==t.length)return NaN;if(1==t.length)return t;var e=function(){var e=t.reduce(function(t,e){return t[e]?t[e]++:t[e]=1,t},{}),r=Object.keys(e),n=r.map(function(t){return e[t]}).reduce(function(t,e){return e>t?e:t});return{v:r.filter(function(t){return e[t]==n}).map(function(t){return Number(t)})}}();return"object"===("undefined"==typeof e?"undefined":g["typeof"](e))?e.v:void 0}function f(t,e,r,n){var a=t.slice(0);if(0==a.length||e>=a.length)return NaN;var o=r||0,i=n||a.length-1;if(o==i)return a[o];for(;i>o;){if(i-o>D){var u=i-o+1,s=e-o+1,l=Math.log(u),h=j*Math.exp(2*l/3),c=j*Math.sqrt(l*h*(u-h)/u),d=Math.max(o,Math.floor(e-s*h/u+c)),m=Math.min(i,Math.floor(e+(u-s)*h/u+c));f(a,e,d,m)}var p=a[e],y=o,b=i;for(v(a,y,e),a[b]>p&&v(a,y,b);b>y;){for(v(a,y,b),y++,b--;a[y]<p;)y++;for(;a[b]>p;)b--}a[o]===p?v(a,o,b):(b++,v(a,b,i)),b>e?i=b-1:(o=b+1,i=b-1)}return a[e]}function v(t,e,r){var n=t[e];t[e]=t[r],t[r]=n}function h(t){var e=void 0;if(0==t.length)e=NaN;else{var r=t.length%2==0;r?(e=f(t,t.length/2),e+=f(t,t.length/2-1),e/=2):e=f(t,(t.length-1)/2)}return e}function c(t,e){if(void 0==e||e>1||0>e)throw new Error("p must be between zero and one inclusive.");if(void 0==t)throw new Error("need a list to provide a percentile.");if(0==t.length)return NaN;var r=Math.floor(t.length*e);return r>=t.length&&(r=t.length-1),f(t,r)}function d(t,e){if(!Array.isArray(t)||0==t.length)throw new Error("you must provide an array.");if(!Array.isArray(e)||0==e.length)throw new Error("you must provide some quantiles.");var r=e.reduce(function(t,e){return e>=0&&1>=e&&t},!0);if(!r)throw new Error("quantiles must be between zero and one inclusive.");if(e.length>1)return e.map(function(e){return d(t,[e])});var n=e[0];if(0==n)return t[0];if(1==n)return t[t.length-1];var a=t.length*n;return a%1?f(t,Math.floor(a)):t.length%2?f(t,a):(f(t,a-1)+f(t,a))/2}function m(t){return 0==t.length?NaN:Math.max.apply(Math,g.toConsumableArray(t))-Math.min.apply(Math,g.toConsumableArray(t))}function p(t,e,r){if(0==t.length)return NaN;var n=function(){var n=s(t);return{v:t.reduce(function(t,a){var o=a-n;return r&&(o=Math.abs(o)),t+Math.pow(o,e)},0)}}();return"object"===("undefined"==typeof n?"undefined":g["typeof"](n))?n.v:void 0}function y(t){return 0==t.length?NaN:p(t,2)/t.length}function b(t){var e=y(t);return isNaN(e)?0:Math.sqrt(e)}function k(t){return(t+.5)*Math.log(t)-t+Math.log(2*Math.PI)/2}function M(t,e,r){return(t-e)/r}function w(t,e,r){if(!t)return NaN;if(e){var n=r||0,a=((t.size-1)*t.variance+(e.size-1)*e.variance)/(t.size+e.size-2);return(t.mean-e.mean-n)/Math.sqrt(a*(1/t.size+1/e.size))}return(t.mean-r)/(t.std/Math.sqrt(t.size))}var g={};g["typeof"]="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol?"symbol":typeof t},g.classCallCheck=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},g.createClass=function(){function t(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,r,n){return r&&t(e.prototype,r),n&&t(e,n),e}}(),g["extends"]=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},g.get=function Y(t,e,r){null===t&&(t=Function.prototype);var n=Object.getOwnPropertyDescriptor(t,e);if(void 0===n){var a=Object.getPrototypeOf(t);return null===a?void 0:Y(a,e,r)}if("value"in n)return n.value;var o=n.get;if(void 0!==o)return o.call(r)},g.inherits=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},g.possibleConstructorReturn=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},g.toConsumableArray=function(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e<t.length;e++)r[e]=t[e];return r}return Array.from(t)};var N=607/128,C=[.9999999999999971,57.15623566586292,-59.59796035547549,14.136097974741746,-.4919138160976202,3399464998481189e-20,4652362892704858e-20,-9837447530487956e-20,.0001580887032249125,-.00021026444172410488,.00021743961811521265,-.0001643181065367639,8441822398385275e-20,-26190838401581408e-21,36899182659531625e-22],O=7,E=[.9999999999998099,676.5203681218851,-1259.1392167224028,771.3234287776531,-176.6150291621406,12.507343278686905,-.13857109526572012,9984369578019572e-21,1.5056327351493116e-7],q=1e-14,D=600,j=.5,x=function(){function t(e){g.classCallCheck(this,t);var r=Object.assign({},e);this.params=this.constructor.validate(r),this.mean=this.constructor.mean(r),this.stdDev=this.constructor.stdDev(r),this.relStdDev=this.constructor.relStdDev(r),this.variance=this.constructor.variance(r),this.skewness=this.constructor.skewness(r),this.kurtosis=this.constructor.kurtosis(r)}return g.createClass(t,null,[{key:"validate",value:function(){throw new Error("You must define a validation function for your distribution.")}},{key:"mean",value:function(){}},{key:"variance",value:function(){}},{key:"stdDev",value:function(){}},{key:"relStdDev",value:function(){}},{key:"skewness",value:function(){}},{key:"kurtosis",value:function(){}},{key:"pdf",value:function(){}},{key:"cdf",value:function(){}},{key:"random",value:function(){return Math.random()}},{key:"sample",value:function(t,e){var r=this;return Array.apply(null,Array(t)).map(function(){return r.random(e)})}}]),g.createClass(t,[{key:"pdf",value:function(t){return this.constructor.pdf(t,this.params)}},{key:"cdf",value:function(t){return this.constructor.cdf(t,this.params)}},{key:"random",value:function(){return this.constructor.random(this.params)}},{key:"sample",value:function(t){return this.constructor.sample(t,this.params)}}]),t}();x.covariates=0,x.discrete=!1;var P=function(t){function e(){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return g.inherits(e,t),g.createClass(e,null,[{key:"validate",value:function(t){if(!t||void 0===t.a||void 0===t.b)throw new Error("need a parameter object of shape { a: number, b: number }.");var e=t.a,r=t.b;if("number"!=typeof e)throw Error("a must be a number.");if("number"!=typeof r||0>=r)throw RangeError("b must be greater than zero.");return t}},{key:"random",value:function(t){for(var r=this.validate(t),n=r.a,a=r.b,o=void 0;!o||.5==o;)o=g.get(Object.getPrototypeOf(e),"random",this).call(this);return n+a*Math.tan(Math.PI*o)}},{key:"pdf",value:function(t,e){if("number"!=typeof t)throw new TypeError("x must be a number");var r=this.validate(e),n=r.a,a=r.b;return a/(Math.pow(t-n,2)+Math.pow(a,2))/Math.PI}},{key:"cdf",value:function(t,e){if("number"!=typeof t)throw new TypeError("x must be a number");var r=this.validate(e),n=r.a,a=r.b;return Math.atan((t-n)/a)/Math.PI+.5}},{key:"mean",value:function(t){return NaN}},{key:"variance",value:function(t){return NaN}},{key:"stdDev",value:function(t){return NaN}},{key:"relStdDev",value:function(t){return NaN}},{key:"skewness",value:function(t){return NaN}},{key:"kurtosis",value:function(t){return NaN}}]),e}(x);P.covariates=1,P.discrete=!1;var S=NaN,z=function(t){function e(){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return g.inherits(e,t),g.createClass(e,null,[{key:"validate",value:function(t){if(!t||void 0===t.mu||void 0==t.sigma)throw new Error("need a parameter object of shape { mu: number, simga: number }.");var e=t.mu,r=t.sigma;if("number"!=typeof e||"number"!=typeof r)throw new Error("Need mu and sigma for the normal distribution.");return t}},{key:"random",value:function(t){var r=this.validate(t),n=r.mu,a=r.sigma,o=S;if(S=NaN,!o){var i=2*g.get(Object.getPrototypeOf(e),"random",this).call(this)*Math.PI,u=Math.sqrt(-2*Math.log(1-g.get(Object.getPrototypeOf(e),"random",this).call(this)));o=Math.cos(i)*u,S=Math.sin(i)*u}return n+o*a}},{key:"pdf",value:function(t,e){if("number"!=typeof t)throw new Error("x must be a number.");var r=this.validate(e),n=r.mu,a=r.sigma;t/Math.abs(a);return 1/(Math.sqrt(2*Math.PI)*Math.abs(a))*Math.exp(-1*Math.pow(t-n,2)/(2*a*a))}},{key:"cdf",value:function(t,e){if("number"!=typeof t)throw new Error("x must be a number.");var n=this.validate(e),a=n.mu,o=n.sigma;return.5*(1+r((t-a)/(o*Math.sqrt(2))))}},{key:"mean",value:function(t){var e=this.validate(t),r=e.mu;e.sigma;return r}},{key:"variance",value:function(t){var e=this.validate(t),r=(e.mu,e.sigma);return r*r}},{key:"stdDev",value:function(t){var e=this.validate(t),r=(e.mu,e.sigma);return r}},{key:"relStdDev",value:function(t){var e=this.validate(t),r=e.mu,n=e.sigma;return n/r}},{key:"skewness",value:function(t){return 0}},{key:"kurtosis",value:function(t){return 3}}]),e}(x);z.covariates=2,z.discrete=!1;var A=function(t){function e(){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return g.inherits(e,t),g.createClass(e,null,[{key:"validate",value:function(t){if(!t||void 0===t.mu)throw new Error("need a parameter object of shape { mu: number }.");var e=t.mu;if("number"!=typeof e||0>=e)throw RangeError("mu must be greater than zero.");return t}},{key:"random",value:function(t){var r=this.validate(t),n=r.mu;return-n*Math.log1p(-g.get(Object.getPrototypeOf(e),"random",this).call(this))}},{key:"pdf",value:function(t,e){var r=this.validate(e),n=r.mu;if("number"!=typeof t)throw new Error("x must be a number.");return 0>t?0:Math.exp(-t/n)/n}},{key:"cdf",value:function(t,e){var r=this.validate(e),n=r.mu;if("number"!=typeof t)throw new Error("x must be a number.");return 0>=t?0:1-Math.exp(-t/n)}},{key:"mean",value:function(t){var e=this.validate(t),r=e.mu;return r}},{key:"variance",value:function(t){var e=this.validate(t),r=e.mu;return r*r}},{key:"stdDev",value:function(t){var e=this.validate(t),r=e.mu;return r}},{key:"relStdDev",value:function(t){return 1}},{key:"skewness",value:function(t){return 2}},{key:"kurtosis",value:function(t){return 9}}]),e}(x);A.covariates=1;var R=function(t){function e(){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return g.inherits(e,t),g.createClass(e,null,[{key:"validate",value:function(t){if(!t||void 0===t.a||void 0===t.b)throw new Error("need a parameter object of shape { a: number, b: number }.");var e=t.a,r=t.b;if("number"!=typeof e||"number"!=typeof r)throw new Error("Need a and b for the gamma distribution.");if(0>=e)throw new Error("a must be greater than zero.");if(0>=r)throw new Error("b must be greater than zero.");return t}},{key:"random",value:function(t){var r=this.validate(t),n=r.a,a=r.b;if(1>n){var o=g.get(Object.getPrototypeOf(e),"random",this).call(this);return this.random({a:1+n,b:a})*Math.pow(o,1/n)}for(var i=void 0,u=void 0,s=void 0,l=n-1/3,f=1/Math.sqrt(9*l);;){do i=z.random({mu:0,sigma:1}),u=1+f*i;while(0>=u);for(u=u*u*u;!s;)s=g.get(Object.getPrototypeOf(e),"random",this).call(this);if(1-.0331*i*i*i*i>s)break;if(Math.log(s)<.5*i*i+l*(1-u+Math.log(u)))break}return l*u/a}},{key:"pdf",value:function(t,e){var r=this.validate(e),n=r.a,o=r.b;if("number"!=typeof t)throw new Error("x must be a number.");return 0>t?0:0==t?1==n?o:0:1==n?Math.exp(-t*o)*o:Math.exp((n-1)*Math.log(t*o)-t*o-a(n))*o}},{key:"cdf",value:function(t,e){var r=this.validate(e),n=r.a,a=r.b;if("number"!=typeof t)throw new Error("x must be a number.");return 0>=t?0:i(n,t*a)}},{key:"mean",value:function(t){var e=this.validate(t),r=e.a,n=e.b;return r/n}},{key:"variance",value:function(t){var e=this.validate(t),r=e.a,n=e.b;return r/(n*n)}},{key:"stdDev",value:function(t){var e=this.validate(t),r=e.a,n=e.b;return Math.sqrt(r/(n*n))}},{key:"relStdDev",value:function(t){var e=this.validate(t),r=e.a;e.b;return 1/Math.sqrt(r)}},{key:"skewness",value:function(t){var e=this.validate(t),r=e.a;e.b;return 2/Math.sqrt(r)}},{key:"kurtosis",value:function(t){var e=this.validate(t),r=e.a;e.b;return 6/r}}]),e}(x);R.covariates=2;var I=14,T=function(t){function r(){return g.classCallCheck(this,r),g.possibleConstructorReturn(this,Object.getPrototypeOf(r).apply(this,arguments))}return g.inherits(r,t),g.createClass(r,null,[{key:"validate",value:function(t){if(!t||void 0===t.n||void 0===t.p)throw new Error("need a parameter object of shape { n: number, p: number }.");var e=t.n,r=t.p;if("number"!=typeof r||r>1||0>r)throw new Error("p must be between zero and one inclusive.");if("number"!=typeof e||0>e)throw new Error("n must be positive or zero.");return t}},{key:"random",value:function(t){var e=this,n=this.validate(t),a=n.n,o=n.p;if(0==a)return 0;var i=!1,u=0,s=void 0;o>.5?(i=!0,s=1-o):s=o;var l=1-s,f=s/l,v=a*s;if(I>v)for(var h=Math.pow(l,a),c=g.get(Object.getPrototypeOf(r),"random",this).call(this);a>=u&&c>=h;)c-=h,h*=f*(a-u)/(u+1),u++;else!function(){var t=v+s,n=Math.floor(t),o=n+.5,i=v*l,f=Math.floor(2.195*Math.sqrt(i)-4.6*l)+.5,h=o-f,c=o+f,d=.134+20.5/(15.3+n),m=f*(1+d+d),p=(t-h)/(t-h*s),y=p*(1+.5*p),b=(c-t)/(c*l),M=b*(1.5+b),w=m+d/y,N=w+d/M,C=void 0,O=void 0,E=void 0,q=void 0,D=function j(){if(E=g.get(Object.getPrototypeOf(r),"random",e).call(e)*N,q=g.get(Object.getPrototypeOf(r),"random",e).call(e),f>=E)u=Math.floor(o-f*q+E);else if(m>=E){var t=h+(E-f)/d;q=q*d+1-Math.abs(t-o)/f,q>1||0>=q?j():u=t}else w>=E?(u=Math.floor(h+Math.log(q)/y),q*=(E-m)*y):(u=Math.floor(c-Math.log(q)/M),q*=(E-w)*M);C=Math.log(q);var i=u+1,v=a-u-1,p=n+1,b=a+1-n;O=o*Math.log(p/i)+(a-n+.5)*Math.log(b/v)+(u-n)*Math.log(v*s/(i*l))+k(p)+k(b)-k(i)-k(v),C>O&&j()};D()}();return Math.floor(i?a-u:u)}},{key:"pdf",value:function(t,r){var n=this.validate(r),a=n.n,o=n.p;if("number"!=typeof t)throw new Error("k must be a number.");if(0>t||t>a)return 0;var i=void 0;if(0==o)i=0==t?1:0;else{if(1!=o){var u=e(a,t),s=Math.pow(o,t)*Math.pow(1-o,a-t);return"Infinity"==u?0==s?0:u:u*s}i=t==a?1:0}return i}},{key:"cdf",value:function(t,r){var n=this.validate(r),a=n.n,o=n.p;if("number"!=typeof t)throw new Error("k must be a number.");return 0>t?0:t>a?1:Array.apply(null,Array(Math.floor(t)+1)).map(function(t,r){return e(a,r)*Math.pow(o,r)*Math.pow(1-o,a-r)}).reduce(function(t,e){return t+e},0)}},{key:"mean",value:function(t){var e=this.validate(t),r=e.n,n=e.p;return r*n}},{key:"variance",value:function(t){var e=this.validate(t),r=e.n,n=e.p;return r*n*(1-n)}},{key:"stdDev",value:function(t){var e=this.validate(t),r=e.n,n=e.p;return Math.sqrt(r*n*(1-n))}},{key:"relStdDev",value:function(t){var e=this.validate(t),r=e.n,n=e.p;return Math.sqrt((1-n)/(r*n))}},{key:"skewness",value:function(t){var e=this.validate(t),r=e.n,n=e.p;return(1-2*n)/Math.sqrt(r*n*(1-n))}},{key:"kurtosis",value:function(t){var e=this.validate(t),r=e.n,n=e.p;return 3-6/r+1/(r*n*(1-n))}}]),r}(x);T.covariates=2,T.discrete=!0;var _=function(t){function e(t){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).call(this,g["extends"]({},t,{n:1})))}return g.inherits(e,t),g.createClass(e,null,[{key:"random",value:function(t){var e=this.validate(g["extends"]({},t,{n:1})),r=e.p,n=x.random();return r>n?1:0}},{key:"pdf",value:function(t,e){var r=this.validate(g["extends"]({},e,{n:1})),n=r.p;if("number"!=typeof t)throw new Error("k must be a number.");return 0==t?1-n:1==t?n:0}},{key:"cdf",value:function(t,e){var r=this.validate(g["extends"]({},e,{n:1})),n=r.p;if("number"!=typeof t)throw new Error("k must be a number.");return 1==t?n:0==t?1-n:NaN}}]),e}(T);_.covariates=1;var B=function(t){function e(){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return g.inherits(e,t),g.createClass(e,null,[{key:"validate",value:function(t){if(!t||void 0===t.df)throw new Error("need a parameter object of shape { df: number }.");var e=t.df;if("number"!=typeof e||0>=e)throw RangeError("df must be greater than zero.");return t}},{key:"random",value:function(t){var e=this.validate(t),r=e.df;return 2*R.random({a:r/2,b:1})}},{key:"pdf",value:function(t,e){var r=this.validate(e),n=r.df;if("number"!=typeof t)throw new TypeError("x must be a number");return 0>t?0:2==n?Math.exp(-t/2)/2:Math.exp((n/2-1)*Math.log(t/2)-t/2-a(n/2))/2}},{key:"cdf",value:function(t,e){var r=this.validate(e),n=r.df;if("number"!=typeof t)throw new TypeError("x must be a number");return 0>t?0:2==n?1-Math.exp(-t/2):i(n/2,t/2)}},{key:"mean",value:function(t){var e=this.validate(t),r=e.df;return r}},{key:"variance",value:function(t){var e=this.validate(t),r=e.df;return 2*r}},{key:"stdDev",value:function(t){var e=this.validate(t),r=e.df;return Math.sqrt(2*r)}},{key:"relStdDev",value:function(t){var e=this.validate(t),r=e.df;return Math.sqrt(2/r)}},{key:"skewness",value:function(t){var e=this.validate(t),r=e.df;return Math.pow(2,1.5)/Math.sqrt(r)}},{key:"kurtosis",value:function(t){var e=this.validate(t),r=e.df;return 3+12/r}}]),e}(x);B.covariates=1;var F=function(t){function e(){return g.classCallCheck(this,e),g.possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return g.inherits(e,t),g.createClass(e,null,[{key:"validate",value:function(t){if(!t||void 0===t.df)throw new Error("need a parameter object of shape { df: number }.");var e=t.df;if("number"!=typeof e||0>=e)throw RangeError("df must be greater than zero.");return t}},{key:"random",value:function(t){var e=this.validate(t),r=e.df;if(2>=r){var n=z.random({mu:0,sigma:1}),a=B.random({df:r});return n/Math.sqrt(a/r)}var o=void 0,i=void 0,u=void 0;do o=z.random({mu:0,sigma:1}),i=A.random({mu:1/(r/2-1)}),u=o*i/(r-2);while(0>1-u||Math.exp(-i-u)>1-u);return o/Math.sqrt((1-2/r)*(1-u))}},{key:"pdf",value:function(t,e){if("number"!=typeof t)throw TypeError("t must be a number.");var r=this.validate(e),n=r.df,o=a(n/2),i=a((n+1)/2);return Math.exp(i-o)/Math.sqrt(Math.PI*n)*Math.pow(1+t*t/n,-(n+1)/2)}},{key:"cdf",value:function(t,e){if("number"!=typeof t)throw TypeError("t must be a number.");var r=this.validate(e),n=r.df,a=void 0,o=void 0,i=void 0,u=void 0,s=void 0,l=void 0,f=void 0,v=void 0,h=void 0,c=void 0,d=1/Math.PI;if(i=n,a=t/Math.sqrt(i),o=i/(i+t*t),u=n-2,s=i%2,l=1,f=1,i=1,v=2+s,h=v,u>=2)for(c=v;u>=c;c+=2)f=f*o*(h-1)/h,l+=f,i=l,h+=2;return 1!=s?.5+.5*a*Math.sqrt(o)*l:(1==n&&(l=0),.5+(a*o*l+Math.atan(a))*d)}},{key:"mean",value:function(t){var e=this.validate(t),r=e.df;return 1>=r?NaN:0}},{key:"variance",value:function(t){var e=this.validate(t),r=e.df;return 1>r?NaN:2>=r?1/0:r/(r-2)}},{key:"stdDev",value:function(t){var e=this.validate(t),r=e.df;return 1>r?NaN:2>=r?1/0:Math.sqrt(r/(r-2))}},{key:"relStdDev",value:function(t){var e=this.validate(t);e.df;return NaN}},{key:"skewness",value:function(t){var e=this.validate(t),r=e.df;return r>3?0:NaN}},{key:"kurtosis",value:function(t){var e=this.validate(t),r=e.df;return 4>=r?NaN:3*(r-2)/(r-4)}}]),e}(x);F.covariates=1;var G=function(){function t(e){var r=this;if(g.classCallCheck(this,t),this.covariance=function(t){return r.constructor.covariance(r,t)},this.correlation=function(t){return r.constructor.correlation(r,t)},0==e.length)throw new Error("Sample of size 0 not allowed.");this.data=e,this.size=e.length,this.mean=s(e),this.std=this.constructor.stdDev(e),this.variance=this.constructor.variance(e),this.skewness=this.constructor.skewness(e),this.kurtosis=this.constructor.kurtosis(e),this.sqrdMeanDev=this.constructor.sqrdMeanDev(e),this.meanDev=this.constructor.meanDev(e),this.rootMeanSqrd=this.constructor.rootMeanSqrd(e),this.stdMeanDev=this.constructor.stdMeanDev(e),this.relStdDev=this.constructor.relativeStdDev(e),this.quartiles=this.constructor.quartiles(e)}return g.createClass(t,null,[{key:"variance",value:function(t){return p(t,2)/(t.length-1)}},{key:"sqrdMeanDev",value:function(t){return p(t,2)}},{key:"meanDev",value:function(t){return p(t,1,!0)/t.length}},{key:"stdDev",value:function(t){var e=this.variance(t);return isNaN(e)?0:Math.sqrt(e)}},{key:"rootMeanSqrd",value:function(t){return Math.sqrt(t.map(function(t){return t*t}).reduce(function(t,e){return t+e})/t.length)}},{key:"stdMeanDev",value:function(t){return this.stdDev(t)/Math.sqrt(t.length)}},{key:"relativeStdDev",value:function(t){return this.stdDev(t)/s(t)}},{key:"quartiles",value:function(t){return d(t,[.25,.5,.75])}},{key:"skewness",value:function(t){var e=this.stdDev(t);if(isNaN(e)||t.length<3)return NaN;var r=t.length,n=Math.pow(e,3),a=p(t,3);return r*a/((r-1)*(r-2)*n)}},{key:"kurtosis",value:function(t){var e=this.stdDev(t);if(isNaN(e)||t.length<3)return NaN;var r=(t.length,p(t,2)),n=p(t,4);return n/Math.pow(r,2)}},{key:"covariance",value:function(t,e){if(!t||!e||t.size<=1||t.size!==e.size)return NaN;var r=t.data.map(function(r,n){return(t.data[n]-t.mean)*(e.data[n]-e.mean)}).reduce(function(t,e){return t+e},0);return r/(t.size-1)}},{key:"correlation",value:function(t,e){if(!(t&&t.std&&e&&e.std))return NaN;var r=this.covariance(t,e);return r/t.std/e.std}}]),t}();t.choose=e,t.error=r,t.factorial=n,t.gamma=o,t.gammainc_lower=i,t.lngamma=a,t.median=h,t.mean=s,t.mode=l,t.percentile=c,t.quantile=d,t.range=m,t.select=f,t.std=b,t.stirling=k,t.sum=u,t.sumNthPowerDev=p,t.variance=y,t.zscore=M,t.Normal=z,t.Binomial=T,t.Bernoulli=_,t.StudentsT=F,t.Gamma=R,t.ChiSquared=B,t.Exponential=A,t.Cauchy=P,t.Sample=G,t.ttest=w}); |
@@ -11,3 +11,3 @@ /* @flow */ | ||
export default function mean(x: Array<number>): number { | ||
return ( x.length == 0) ? NaN : sum(x) / x.length; | ||
return ( x.length == 0) ? NaN : sum(x) / x.filter(d => !isNaN(d) ).length; | ||
}; |
@@ -16,7 +16,9 @@ /* @flow */ | ||
return x.reduce( (p,n) => { | ||
let corrected = n - errorComp; | ||
let next = p + corrected; | ||
errorComp = next - p - corrected; | ||
return next; | ||
if (!isNaN((n))) { | ||
let corrected = n - errorComp; | ||
let next = p + corrected; | ||
errorComp = next - p - corrected; | ||
return next; | ||
} else return p; | ||
}, 0); | ||
}; |
@@ -5,4 +5,8 @@ { | ||
"author": "Jack Shira <jack@atg.io> (http://www.atg.io)", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"license": "ISC", | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "https://github.com/atgJack/sampson.git" | ||
}, | ||
"engine": "node >= 5.8.0", | ||
@@ -27,2 +31,3 @@ "main": "lib.js", | ||
"blue-tape": "^0.2.0", | ||
"codecov": "^1.0.1", | ||
"esdoc": "^0.4.6", | ||
@@ -29,0 +34,0 @@ "esdoc-es7-plugin": "0.0.3", |
@@ -1,6 +0,3 @@ | ||
# Sampson.js ```v0.2.0``` | ||
# Sampson.js | ||
[![npm](https://img.shields.io/npm/v/sampson.svg?maxAge=2592000)](https://www.npmjs.com/package/sampson) | ||
[![npm](https://img.shields.io/npm/l/sampson.svg?maxAge=2592000)]() | ||
A Statistical Sampling and Distribution Library. | ||
@@ -12,3 +9,3 @@ Named after the [largest horse ever](https://en.wikipedia.org/wiki/Sampson_(horse)), | ||
### Distributions | ||
#### Distributions | ||
@@ -24,3 +21,3 @@ - Bernoulli | ||
### Statistics | ||
#### Statistics | ||
@@ -30,3 +27,3 @@ - Sample | ||
### General Functions | ||
#### General Functions | ||
@@ -53,3 +50,3 @@ - n Choose k | ||
#### Major references: | ||
### Major references: | ||
@@ -56,0 +53,0 @@ - https://github.com/ampl/gsl |
@@ -16,3 +16,3 @@ var rollup = require('rollup'); | ||
format: 'umd', | ||
moduleName: 'probably', | ||
moduleName: 'sampson', | ||
dest: 'dist/lib.js' | ||
@@ -28,5 +28,5 @@ }); | ||
format: 'umd', | ||
moduleName: 'probably', | ||
moduleName: 'sampson', | ||
dest: 'dist/lib.min.js' | ||
}); | ||
}).catch( function(err) { console.log(err) }); |
@@ -24,2 +24,9 @@ import test from 'blue-tape'; | ||
}); | ||
t.test('can handle NaNs in the mix', (t) => { | ||
let answer = 3; | ||
let data = [1, NaN, 2, 3, NaN, 4, 5]; | ||
t.equal( mean(data), answer ); | ||
t.end(); | ||
}); | ||
}); |
@@ -34,2 +34,9 @@ import test from 'blue-tape'; | ||
}); | ||
t.test('can handle NaNs in the mix', (t) => { | ||
let answer = 15; | ||
let data = [1, NaN, 2, 3, NaN, 4, 5]; | ||
t.equal( sum(data), answer ); | ||
t.end(); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
287533
7281
0
16
54