UNPKG

@stdlib/esm

Version:

ES module distribution for Stdlib, a standard library for JavaScript and Node.js.

3 lines 4.22 kB
// This file is a part of stdlib. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 import r from"./../utils/define-read-only-property.js";import e from"./../assert/is-nonnegative-integer-array.js";import t from"./../assert/is-matrix-like.js";import i from"./../assert/is-array-array.js";import a from"./../ndarray/array.js";import n from"./incr/min.js";import o from"./../blas/ext/base/gsum.js";import s from"./../math/base/special/min.js";import p from"./../utils/copy.js";import l from"./base/dists/chisquare/cdf.js";import m from"./../assert/is-positive-integer.js";import f from"./../assert/is-plain-object.js";import u from"./../assert/is-boolean.js";import h from"./../assert/has-own-property.js";import c from"./../math/base/special/roundn.js";import d from"./../array/float64.js";import v from"./../math/base/special/abs.js";import g from"./../assert/is-number.js";import j from"./../assert/is-nan.js";var b=m,w=f,y=u.isPrimitive,E=h,T=c;function V(r){return function(e){var t,i,a;if(i=4,t=!0,arguments.length>0){if(!w(e))throw new TypeError("invalid argument. Must provide an object. Value: `"+e+"`.");if(E(e,"digits")){if(!b(e.digits))throw new TypeError("invalid option. `digits` option must be a positive integer. Option: `"+e.digits+"`.");i=e.digits}if(E(e,"decision")){if(!y(e.decision))throw new TypeError("invalid option. `decision` option must be a boolean primitive. Option: `"+e.decision+"`.");t=e.decision}}a="",a+=r.method,a+="\n\n",a+="Null hypothesis: the two variables are independent",a+="\n\n",a+=" pValue: "+T(r.pValue,-i)+"\n",a+=" statistic: "+T(r.statistic,-i)+"\n",a+=" degrees of freedom: "+r.df+"\n",a+="\n",t&&(a+="Test Decision: ",r.rejected?a+="Reject null in favor of alternative at "+100*r.alpha+"% significance level":a+="Fail to reject null in favor of alternative at "+100*r.alpha+"% significance level",a+="\n");return a}}var x=.05,O=!0,F={alpha:x,correct:O},P=d;function k(r,e){var t,i,a,n,o,s,p,l,m,f;if(1===e?(o=r.shape[1],s=r.shape[0],i=r.strides[1],a=r.strides[0]):(o=r.shape[0],s=r.shape[1],i=r.strides[0],a=r.strides[1]),0===o||0===s)return 0;for(t=new P(o),p=r.offset,l=0;l<o;l++){for(f=p+l*i,n=0,m=0;m<s;m++)n+=r.data[f+m*a];t[l]=n}return t}var q=a,R=d;function C(r,e){var t,i,a=r.length,n=e.length,o=q(new R(a*n),{shape:[a,n]});for(t=0;t<a;t++)for(i=0;i<n;i++)o.set(t,i,r[t]*e[i]);return o}var D=d,M=v;function N(r,e){var t,i,a,n,o;for(t=new D(r.length),n=r.shape[0],o=r.shape[1],i=0;i<n;i++)for(a=0;a<o;a++)t[i*n+a]=M(r.get(i,a)-e.get(i,a));return t}var z=u.isPrimitive,A=g.isPrimitive,B=f,G=j,H=h;function I(r,e){if(!B(e))return new TypeError("invalid argument. Options argument must be an object. Value: `"+e+"`.");if(H(e,"alpha")){if(r.alpha=e.alpha,!A(r.alpha)||G(r.alpha))return new TypeError("invalid option. `alpha` option must be a number primitive. Option: `"+r.alpha+"`.");if(r.alpha<0||r.alpha>1)return new RangeError("invalid option. `alpha` option must be a number on the interval `[0,1]`. Value: `"+r.alpha+"`.")}return H(e,"correct")&&(r.correct=e.correct,!z(r.correct))?new TypeError("invalid option. `correct` option must be a boolean primitive. Option: `"+r.simulate+"`."):null}var J=r,K=e,L=t,Q=i,S=a,U=n,W=o,X=s,Y=p,Z=l,$=V,_=F,rr=k,er=C,tr=N,ir=I;function ar(r,e){var t,i,a,n,o,s,p,l,m,f,u,h,c,d,v,g;if(Q(r)&&(r=S(r)),!L(r))throw new TypeError("invalid argument. First argument `x` must be an array of arrays or ndarray-like object with dimension two. Value: `"+r+"`.");if(!K(r.data))throw new TypeError("invalid argument. First argument `x` must contain nonnegative integers. Value: `"+r+"`.");if(f=Y(_),arguments.length>1&&(c=ir(f,e)))throw c;for(v=W(r.length,r.data,1),l=r.shape[0],m=r.shape[1],i=rr(r,1),a=rr(r,2),s=er(a,i),g=0;g<s.length;g++)s.data[g]/=v;if(t=tr(r,s),f.correct&&2===l&&2===m){for(n=U(),g=0;g<t.length;g++)n(t[g]);for(o=X(.5,n()),g=0;g<t.length;g++)t[g]-=o}for(g=0;g<t.length;g++)t[g]*=t[g],t[g]/=s.data[g];return h=W(t.length,t,1),u=1-Z(h,p=(l-1)*(m-1)),J(d={},"rejected",u<=f.alpha),J(d,"alpha",f.alpha),J(d,"pValue",u),J(d,"df",p),J(d,"expected",s),J(d,"statistic",h),J(d,"method","Chi-square independence test"),J(d,"print",$(d)),d}var nr=ar;export default nr; //# sourceMappingURL=chi2test.js.map