@stdlib/esm
Version:
ES module distribution for Stdlib, a standard library for JavaScript and Node.js.
3 lines • 4.51 kB
JavaScript
// This file is a part of stdlib. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0
import e from"./../utils/define-read-only-property.js";import i from"./../assert/is-number-array.js";import t from"./../assert/is-nonnegative-integer.js";import r from"./base/dists/beta/quantile.js";import a from"./../math/base/special/floor.js";import n from"./../math/base/special/ceil.js";import o from"./base/dists/binomial/cdf.js";import s from"./base/dists/binomial/pmf.js";import l from"./../assert/is-number.js";import p from"./../assert/is-plain-object.js";import m from"./../assert/is-string.js";import u from"./../assert/is-nan.js";import f from"./../assert/has-own-property.js";import v from"./../assert/is-positive-integer.js";import h from"./../assert/is-boolean.js";import d from"./../math/base/special/roundn.js";var c=l.isPrimitive,b=p,g=m.isPrimitive,w=u,j=f;function y(e,i){return b(i)?j(i,"alpha")&&(e.alpha=i.alpha,!c(e.alpha)||w(e.alpha))?new TypeError("invalid option. `alpha` option must be a number primitive. Option: `"+e.alpha+"`."):j(i,"alternative")&&(e.alternative=i.alternative,!g(e.alternative))?new TypeError("invalid option. `alternative` option must be a string primitive. Option: `"+e.alternative+"`."):j(i,"p")&&(e.p=i.p,!c(e.p)||w(e.p))?new TypeError("invalid option. `p` option must be a number primitive. Option: `"+e.p+"`."):null:new TypeError("invalid argument. Options argument must be an object. Value: `"+i+"`.")}var E=v,V=p,T=h.isPrimitive,O=f,k=d;function x(e){var i,t,r;if(t=4,i=!0,arguments.length>0){if(!V(e))throw new TypeError("invalid argument. First argument must be an options object. Value: `"+e+"`.");if(O(e,"digits")){if(!E(e.digits))throw new TypeError("invalid option. `digits` option must be a positive integer. Option: `"+e.digits+"`.");t=e.digits}if(O(e,"decision")){if(!T(e.decision))throw new TypeError("invalid option. `decision` option must be a boolean primitive. Option: `"+e.decision+"`.");i=e.decision}}switch(r="",r+=this.method,r+="\n\n",r+="Alternative hypothesis: ",r+="True correlation coefficient is ",this.alternative){case"less":r+="less than ";break;case"greater":r+="greater than ";break;case"two-sided":default:r+="not equal to "}return r+=this.nullValue,r+="\n\n",r+=" pValue: "+k(this.pValue,-t)+"\n",r+=" statistic: "+k(this.statistic,-t)+"\n",r+=" "+100*(1-this.alpha)+"% confidence interval: ["+k(this.ci[0],-t)+","+k(this.ci[1],-t)+"]",r+="\n\n",i&&(r+="Test Decision: ",this.rejected?r+="Reject null in favor of alternative at "+100*this.alpha+"% significance level":r+="Fail to reject null in favor of alternative at "+100*this.alpha+"% significance level",r+="\n"),r}var P=e,R=i,q=t,F=r,I=a,M=n,A=o,D=s,z=y,B=x,C=1+1e-7;function G(e,i,t){return 0===e?0:F(t,e,i-e+1)}function H(e,i,t){return e===i?1:F(1-t,e+1,i-e)}function J(){var e,i,t,r,a,n,o,s,l,p,m,u,f,v,h;if(i={},R(arguments[0])){if(2!==(f=arguments[0]).length)throw new Error("invalid argument. If provided an array, it must have two elements. Value: `"+f+"`.");m=f[1]+f[0],f=f[0],arguments[1]&&(o=z(i,arguments[1]))}else{if(m=arguments[1],!q(f=arguments[0]))throw new TypeError("invalid argument. Must provide a nonnegative integer or a two-element array. Value: `"+f+"`.");if(!q(m))throw new TypeError("invalid argument. Must provide a nonnegative integer. Value: `"+m+"`.");if(f>m)throw new TypeError("invalid arguments. `x` cannot be larger than `n`. `x:"+f+", n:"+m+"`.");arguments[2]&&(o=z(i,arguments[2]))}if(o)throw o;if((e=void 0===i.alpha?.05:i.alpha)<0||e>1)throw new RangeError("invalid argument. Option `alpha` must be a number in the range 0 to 1. Value: `"+e+"`.");if((u=void 0===i.p?.5:i.p)<0||u>1)throw new RangeError("invalid argument. Option `p` must be a probability. Value: `"+u+"`.");switch(a=f/m,n=i.alternative||"two-sided"){case"less":r=A(f,m,u),t=[0,H(f,m,e)];break;case"greater":r=1-A(f-1,m,u),t=[G(f,m,e),1];break;case"two-sided":if(l=D(f,m,u),f===(p=m*u))r=1;else if(f<p){for(v=0,h=M(p);h<=m;h++)D(h,m,u)<=l*C&&(v+=1);r=A(f,m,u)+(1-A(m-v,m,u))}else{for(v=0,h=0;h<=I(p);h++)D(h,m,u)<=l*C&&(v+=1);r=A(v-1,m,u)+(1-A(f-1,m,u))}t=[G(f,m,e/2),H(f,m,e/2)];break;default:throw new Error("Invalid option. `alternative` must be either `two-sided`, `less` or `greater`. Value: `"+n+"`")}return P(s={},"rejected",r<=e),P(s,"alpha",e),P(s,"pValue",r),P(s,"statistic",a),P(s,"ci",t),P(s,"nullValue",u),P(s,"alternative",n),P(s,"method","Exact binomial test"),P(s,"print",B),s}var K=J;export default K;
//# sourceMappingURL=binomial-test.js.map