@numero-uno-jpn/rstats 0.15.1 → 0.15.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +147 -0
- package/README.ja.md +8 -8
- package/README.md +8 -8
- package/dist/chunks/{chunk-735NNFAS.js → chunk-3THFTB36.js} +2 -2
- package/dist/chunks/chunk-3TSWYE5L.js +2 -0
- package/dist/chunks/{chunk-7J4HW6VF.js → chunk-3WPU6LGK.js} +2 -2
- package/dist/chunks/{chunk-Z3C3G5XI.js → chunk-5ITR67MB.js} +2 -2
- package/dist/chunks/chunk-5NRFU6F6.js +2 -0
- package/dist/chunks/{chunk-SAZAEPZC.js → chunk-E2LCSVQV.js} +1 -1
- package/dist/chunks/chunk-FCNVTIPJ.js +2 -0
- package/dist/chunks/chunk-FKAKALOZ.js +2 -0
- package/dist/chunks/{chunk-5OSQ76OO.js → chunk-HD73UKPT.js} +2 -2
- package/dist/chunks/{chunk-4ILI2FSB.js → chunk-IUULP3LE.js} +1 -1
- package/dist/chunks/{chunk-LY2T6DA6.js → chunk-JQG3YQOC.js} +2 -2
- package/dist/chunks/chunk-KT66SB7R.js +2 -0
- package/dist/chunks/{chunk-IQOVOHZG.js → chunk-MAUAOQG3.js} +2 -2
- package/dist/chunks/{chunk-GEJCBCMP.js → chunk-MDYE7QP2.js} +2 -2
- package/dist/chunks/{chunk-5SQFDLNG.js → chunk-NM3Z2UBQ.js} +2 -2
- package/dist/chunks/{chunk-5XQZNKHZ.js → chunk-PMJI5QO6.js} +2 -2
- package/dist/chunks/chunk-UF7KOTHF.js +3 -0
- package/dist/chunks/chunk-UOE57JGT.js +2 -0
- package/dist/chunks/{chunk-3QTVXDUM.js → chunk-USPEBXSR.js} +2 -2
- package/dist/chunks/{chunk-H4CHJMFU.js → chunk-VLUL2HFW.js} +1 -1
- package/dist/chunks/{chunk-VOIJKNL2.js → chunk-ZIX7NYQF.js} +1 -1
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +48 -4
- package/dist/distributions/beta.d.ts +1 -1
- package/dist/distributions/beta.js +75 -2
- package/dist/distributions/cauchy.js +76 -5
- package/dist/distributions/exponential.js +16 -2
- package/dist/distributions/f.js +143 -24
- package/dist/distributions/gamma.d.ts +13 -2
- package/dist/distributions/gamma.js +122 -12
- package/dist/distributions/geometric.js +19 -3
- package/dist/distributions/invgamma.js +60 -10
- package/dist/distributions/kumaraswamy.js +116 -17
- package/dist/distributions/laplace.js +5 -2
- package/dist/distributions/logistic.js +8 -2
- package/dist/distributions/lognormal.js +40 -5
- package/dist/distributions/nbeta.js +597 -52
- package/dist/distributions/nchisq.d.ts +25 -0
- package/dist/distributions/nchisq.js +381 -96
- package/dist/distributions/negbinomial.js +26 -6
- package/dist/distributions/normal.js +29 -1
- package/dist/distributions/nt.d.ts +28 -3
- package/dist/distributions/nt.js +401 -63
- package/dist/distributions/pareto.js +73 -19
- package/dist/distributions/t.js +102 -12
- package/dist/distributions/triangular.js +93 -27
- package/dist/distributions/tukey.js +110 -50
- package/dist/distributions/uniform.js +14 -9
- package/dist/distributions/weibull.js +65 -10
- package/dist/distributions.js +2 -2
- package/dist/internal/ddexp.d.ts +13 -0
- package/dist/internal/ddexp.js +55 -3
- package/dist/internal/discrete.js +30 -7
- package/dist/internal/invert.d.ts +5 -0
- package/dist/internal/invert.js +59 -5
- package/dist/internal/noncentral.d.ts +41 -0
- package/dist/internal/noncentral.js +124 -2
- package/dist/internal/poissonMixture.d.ts +7 -2
- package/dist/internal/poissonMixture.js +142 -38
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +14 -14
- package/dist/special/beta.js +7 -0
- package/dist/special/dd.d.ts +1 -1
- package/dist/special/dd.js +4 -1
- package/dist/special/gamma.js +4 -0
- package/dist/special/igam.js +126 -21
- package/dist/special/incbet.d.ts +10 -0
- package/dist/special/incbet.js +218 -48
- package/dist/special/incbetLargeA.js +53 -14
- package/dist/special/incbetPrefactor.d.ts +57 -0
- package/dist/special/incbetPrefactor.js +103 -0
- package/dist/special/incbetTemme.js +15 -3
- package/dist/special/loader.js +97 -22
- package/dist/special.js +2 -2
- package/dist/tests.js +2 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-5YMP4WAJ.js +0 -2
- package/dist/chunks/chunk-BDJPSEA7.js +0 -2
- package/dist/chunks/chunk-KO6QQPC6.js +0 -2
- package/dist/chunks/chunk-OLPTFKIZ.js +0 -3
- package/dist/chunks/chunk-TYFGVCVQ.js +0 -2
- package/dist/chunks/chunk-UWBYDIOH.js +0 -2
- package/dist/chunks/chunk-UZHY5DWN.js +0 -2
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! rStats.js 0.15.3 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{f as T,g as kt,h as Y}from"./chunk-IUULP3LE.js";import{a as Kt,b as Jt,c as zt,d as $}from"./chunk-5NRFU6F6.js";import{a as R,b as _,c as L,d as S,e as $t,h as Vt,i as Zt,l as Yt}from"./chunk-FCNVTIPJ.js";import{a as P,b as v}from"./chunk-FKAKALOZ.js";import{e as y,g as F,h as G,p as Ut,q as Xt,t as Qt}from"./chunk-3TSWYE5L.js";import{c as pt,d as et,e as Ot,f as Bt,g as Z,i as nt,k as Gt}from"./chunk-KT66SB7R.js";function Un(t,e,n={}){let{log:r=!1,ncp:o}=n;return o!==void 0?y(t,i=>Kt(i,e,o,r)):y(t,i=>Vt(i,e/2,2,r))}function Xn(t,e,n={}){let{lowerTail:r=!0,logP:o=!1,ncp:i}=n;return i!==void 0?y(t,a=>Jt(a,e,i,r,o)):y(t,a=>Zt(a,e/2,2,r,o))}function Qn(t,e,n={}){let{lowerTail:r=!0,logP:o=!1,ncp:i}=n;return i!==void 0?y(t,a=>zt(a,e,i,r,o)):y(t,a=>Yt(a,e/2,2,r,o))}function mt(t,e,n,r){if(t===0&&e===0)return{e:[0,0],logDenom:0};if(t===0||e===0){let o=t+e,[i,a]=t===0?[r,n]:[n,r];return{e:a[0]<.1?et(Xt(o,Z([o,0],i)),Z([o,0],a)):Ot(Z([o,0],Gt(i))),logDenom:0}}return Qt(t,e,n,r)}function ne(t,e,n,r){let o=e+n,i=nt([r,0],[o,0]),a=nt([o-r,0],[o,0]),s=mt(t,e-t,i,a),c=mt(r-t,n-r+t,i,a),l=mt(r,o-r,i,a);return{e:Bt(et(s.e,c.e),l.e),logDenom:s.logDenom+c.logDenom-l.logDenom}}function re(t,e){let[n,r]=t.e,o=e*Math.exp(-.5*t.logDenom);return n<700?Math.exp(-n)*(1-r)*o:Math.exp(700-n)*(1-r)*o*Math.exp(-700)}function oe(t){return-t.e[0]-t.e[1]-.5*t.logDenom}function rt(t,e,n){return[Math.max(0,n-e),Math.min(n,t)]}function $e(t,e,n,r,o){if([t,e,n,r].some(Number.isNaN))return NaN;if(![e,n,r].every(Number.isFinite)||P(e)||P(n)||P(r))return NaN;if(e=v(e),n=v(n),r=v(r),e<0||n<0||r<0||r>e+n)return NaN;if(P(t))return L(o);t=v(t);let[i,a]=rt(e,n,r);if(t<i||t>a)return L(o);if(r===0||r===e+n)return o?0:1;let s=ne(t,e,n,r);return o?oe(s):re(s,1)}function te(t,e,n,r){return nt(pt(t,e),pt(n,r))}function Ue(t,e,n,r,o,i,a){let s=[1,0],c=[1,0],l=t;for(;!(o?l<=i:l>=a);){let u=o?te(l,n-r+l,e-l+1,r-l+1):te(e-l,r-l,l+1,n-r+l+1);s=Z(s,u),c=et(c,s),l+=o?-1:1;let f=u[0];if(f<1&&s[0]*f/(1-f)<c[0]*2**-60)break}return c[0]+c[1]}function ie(t,e,n,r,o,i){let[a,s]=rt(e,n,r),c=e+n,l=t*c<r*e,u=ee(t,e,n,r,l,a,s);if(u.value>.5){let f=ee(t,e,n,r,!l,a,s);f.value<u.value&&(u=f,l=!l)}return l===o?i?u.value>1e-300?Math.log(u.value):oe(u.form)+Math.log(u.sum):u.value:i?Math.log1p(-u.value):1-u.value}function ee(t,e,n,r,o,i,a){let s=o?t:t+1,c=ne(s,e,n,r),l=Ue(s,e,n,r,o,i,a);return{form:c,sum:l,value:re(c,l)}}function ae(t,e,n){if([t,e,n].every(Number.isFinite)&&(t=v(t),e=v(e),n=v(n),!(t<0||e<0||n<0||n>t+e)))return[t,e,n]}function Xe(t,e,n,r,o,i){if([t,e,n,r].some(Number.isNaN))return NaN;let a=ae(e,n,r);if(a===void 0)return NaN;[e,n,r]=a;let s=Math.floor(t+1e-7),[c,l]=rt(e,n,r);return s<c?R(o,i):s>=l?_(o,i):ie(s,e,n,r,o,i)}function Qe(t,e,n,r,o,i){if([t,e,n,r].some(Number.isNaN))return NaN;let a=ae(e,n,r);if(a===void 0)return NaN;[e,n,r]=a;let[s,c]=rt(e,n,r),l=S(t,s,c,o,i);if(l!==void 0)return l;if(s===c)return s;let u=e+n,f=r*e/u,h=Math.sqrt(r*e/u*(n/u)*(u-r)/Math.max(u-1,1)),p=G(t,{lowerTail:o,logP:i}),M=f+h*p;return $({p:t,lowerTail:o,logP:i,cdf:(x,N,g)=>x<s?R(N,g):x>=c?_(N,g):ie(x,e,n,r,N,g),y0:Number.isFinite(M)?M:f,lo:s,hi:c})}function er(t,e,n,r,o={}){let i=o.log??!1;return y(t,a=>$e(a,e,n,r,i))}function nr(t,e,n,r,o={}){let{lowerTail:i=!0,logP:a=!1}=o;return y(t,s=>Xe(s,e,n,r,i,a))}function rr(t,e,n,r,o={}){let{lowerTail:i=!0,logP:a=!1}=o;return y(t,s=>Qe(s,e,n,r,i,a))}var Ve=2**900,se=-900,q=new Map,Ze=2**21,ot=0,Ye=2**25;function ue(t,e){if(e+1>Ye)throw new RangeError(`signrank: n = ${t} needs too large a table (${e+1} entries)`);let n=new Float64Array(e+1);n[0]=1;let r=0,o=-1/0,i=Math.min(t,e);for(let l=1;l<=i;l++){for(let u=e;u>=l;u--)n[u]+=n[u-l];if(n[Math.min(e,Math.floor(l*(l+1)/4))]>Ve){let u=2**se;for(let f=0;f<=e;f++)n[f]*=u;r-=se,o=Math.max(o,r-1015+i-l)}}let a=new Float64Array(e+1),s=0,c=0;for(let l=0;l<=e;l++){let u=n[l]-c,f=s+u;c=f-s-u,s=f,a[l]=s}return{n:t,len:e+1,count:n,cum:a,scale:r,minTrusted:2**(o-r)}}function ce(t,e,n=!1){let r=n?-t:t,o=q.get(r);if(o!==void 0&&o.len>e)return q.delete(r),q.set(r,o),o;let i=Math.floor(t*(t+1)/4),a=Math.min(i,Math.max(e,o===void 0?0:2*o.len));return le(r,ue(t,a),o)}function le(t,e,n){n!==void 0&&(q.delete(t),ot-=n.len),q.set(t,e),ot+=e.len;for(let[r,o]of q){if(ot<=Ze||r===t)break;q.delete(r),ot-=o.len}return e}function gt(t,e,n){let r=(a,s)=>e===a.len-1||s>=(n?(e+1)*a.minTrusted:a.minTrusted),o=ce(t,e),i=(n?o.cum:o.count)[e];return r(o,i)?[i,o]:(o=ce(t,e,!0),i=(n?o.cum:o.count)[e],r(o,i)?[i,o]:(o=le(-t,ue(t,e),q.get(-t)),[(n?o.cum:o.count)[e],o]))}function dt(t,e,n){let r=e.scale-e.n,o=Math.trunc(r/2),i=t*2**o*2**(r-o);if(!n)return i;if(i>=1e-300)return Math.log(i);let a=Math.floor(Math.log2(t)),s=Math.trunc(a/2),c=t*2**-s*2**-(a-s);return c>=2?(c/=2,a++):c<1&&(c*=2,a--),Math.log(c)+(a+r)*Math.LN2}function Nt(t){return Number.isFinite(t)?(t=v(t),t<=0?NaN:t):NaN}function Ke(t,e,n){if(Number.isNaN(t)||Number.isNaN(e))return NaN;if(e=Nt(e),Number.isNaN(e))return NaN;if(P(t))return L(n);t=v(t);let r=e*(e+1)/2;if(t<0||t>r)return L(n);let o=Math.min(t,r-t),[i,a]=gt(e,o,!1);return dt(i,a,n)}function fe(t,e,n,r){let o=e*(e+1)/2,i=t,a=o-t-1,s=n?i:a,c=n?a:i;if(s<=c){let[h,p]=gt(e,s,!0);return dt(h,p,r)}let[l,u]=gt(e,c,!0),f=dt(l,u,!1);return r?Math.log1p(-f):1-f}function Je(t,e,n,r){if(Number.isNaN(t)||Number.isNaN(e))return NaN;if(e=Nt(e),Number.isNaN(e))return NaN;let o=v(t+1e-7),i=e*(e+1)/2;return o<0?R(n,r):o>=i?_(n,r):fe(o,e,n,r)}function ze(t,e,n,r){if(Number.isNaN(t)||Number.isNaN(e))return NaN;if(e=Nt(e),Number.isNaN(e))return NaN;let o=e*(e+1)/2,i=S(t,0,o,n,r);if(i!==void 0)return i;let a=o/2,s=Math.sqrt(e*(e+1)*(2*e+1)/24),c=a+s*G(t,{lowerTail:n,logP:r});return $({p:t,lowerTail:n,logP:r,cdf:(l,u,f)=>l<0?R(u,f):l>=o?_(u,f):fe(l,e,u,f),y0:Number.isFinite(c)?c:a,lo:0,hi:o})}function lr(t,e,n={}){let r=n.log??!1;return y(t,o=>Ke(o,e,r))}function fr(t,e,n={}){let{lowerTail:r=!0,logP:o=!1}=n;return y(t,i=>Je(i,e,r,o))}function hr(t,e,n={}){let{lowerTail:r=!0,logP:o=!1}=n;return y(t,i=>ze(i,e,r,o))}var st=960,ke=2**25,it=134217729,j=new Map,tn=2**21,at=0;function en(t,e,n){if(n+1>ke)throw new RangeError(`wilcox: m = ${t}, n = ${e} needs too large a table (${n+1} entries)`);let r=new Float64Array(n+1),o=new Float64Array(n+1);r[0]=2**st;for(let c=1;c<=t;c++){let l=Math.min(n,c*e),u=e+c,f=it*c,h=f-(f-c),p=c-h;f=it*u;let M=f-(f-u),x=u-M;for(let g=0;g<=l;g++){let d=r[g];if(d===0)continue;let m=d*c;f=it*d;let b=f-(f-d),I=d-b,E=b*h-m+b*p+I*h+I*p+o[g]*c,w=m/u,C=w*u;f=it*w;let A=f-(f-w),B=w-A,Ge=A*M-C+A*x+B*M+B*x,jt=(m-C-Ge+E)/u,Ht=w+jt;o[g]=jt-(Ht-w),r[g]=Ht}let N=e+c;for(let g=l;g>=N;g--)he(r,o,g,g-N,-1);for(let g=c;g<=l;g++)he(r,o,g,g-c,1)}let i=new Float64Array(n+1),a=0,s=0;for(let c=0;c<=n;c++){let l=r[c],u=a+l,f=u-a,h=a-(u-f)+(l-f);h+=s+o[c],a=u+h,s=h-(a-u),i[c]=a}return{m:t,n:e,len:n+1,prob:r,cum:i}}function he(t,e,n,r,o){let i=t[n],a=o*t[r],s=i+a,c=s-i,l=i-(s-c)+(a-c);l+=e[n]+o*e[r];let u=s+l;e[n]=l-(u-s),t[n]=u}function bt(t,e,n){t>e&&([t,e]=[e,t]);let r=`${t},${e}`,o=j.get(r);if(o!==void 0&&o.len>n)return j.delete(r),j.set(r,o),o;let i=Math.floor(t*e/2),a=Math.min(i,Math.max(n,o===void 0?0:2*o.len)),s=en(t,e,a);o!==void 0&&(j.delete(r),at-=o.len),j.set(r,s),at+=s.len;for(let[c,l]of j){if(at<=tn||c===r)break;j.delete(c),at-=l.len}return s}function xt(t,e){let n=t*2**-(st/2)*2**-(st/2);return e?n>=1e-300?Math.log(n):Math.log(t)-st*Math.LN2:n}function yt(t,e){if(!(!Number.isFinite(t)||!Number.isFinite(e)))return t=v(t),e=v(e),t<=0||e<=0?void 0:[t,e]}function nn(t,e,n,r){if(Number.isNaN(t)||Number.isNaN(e)||Number.isNaN(n))return NaN;let o=yt(e,n);if(o===void 0)return NaN;if([e,n]=o,P(t))return L(r);t=v(t);let i=e*n;if(t<0||t>i)return L(r);let a=Math.min(t,i-t);return xt(bt(e,n,a).prob[a],r)}function Me(t,e,n,r,o){let i=e*n,a=r?t:i-t-1,s=r?i-t-1:t;if(a<=s)return xt(bt(e,n,a).cum[a],o);let c=xt(bt(e,n,s).cum[s],!1);return o?Math.log1p(-c):1-c}function rn(t,e,n,r,o){if(Number.isNaN(t)||Number.isNaN(e)||Number.isNaN(n))return NaN;let i=yt(e,n);if(i===void 0)return NaN;[e,n]=i;let a=Math.floor(t+1e-7);return a<0?R(r,o):a>=e*n?_(r,o):Me(a,e,n,r,o)}function on(t,e,n,r,o){if(Number.isNaN(t)||Number.isNaN(e)||Number.isNaN(n))return NaN;let i=yt(e,n);if(i===void 0)return NaN;[e,n]=i;let a=e*n,s=S(t,0,a,r,o);if(s!==void 0)return s;let c=a/2,l=Math.sqrt(a*(e+n+1)/12),u=c+l*G(t,{lowerTail:r,logP:o});return $({p:t,lowerTail:r,logP:o,cdf:(f,h,p)=>f<0?R(h,p):f>=a?_(h,p):Me(f,e,n,h,p),y0:Number.isFinite(u)?u:c,lo:0,hi:a})}function br(t,e,n,r={}){let o=r.log??!1;return y(t,i=>nn(i,e,n,o))}function xr(t,e,n,r={}){let{lowerTail:o=!0,logP:i=!1}=r;return y(t,a=>rn(a,e,n,o,i))}function yr(t,e,n,r={}){let{lowerTail:o=!0,logP:i=!1}=r;return y(t,a=>on(a,e,n,o,i))}var D=128,z=Number.EPSILON,an=256,Ct=Float64Array.from({length:2*D+1},(t,e)=>Math.cos(Math.PI*e/D));function sn(t){let e=new Float64Array(t+1);for(let n=0;n<=t;n++){let r=0;for(let o=0;o<=t;o+=2)r+=(o===0||o===t?.5:1)*Ct[o*n%(2*t)*(D/t)]*2/(1-o*o);e[n]=(n===0||n===t?.5:1)*2*r/t}return e}var cn=new Map([16,32,64,128].map(t=>[t,sn(t)]));function un(t,e){let n=cn.get(e),r=D/e,o=0,i=0;for(let s=0;s<=e;s++)o+=n[s]*t[s],i+=Math.abs(n[s]*t[s]);let a=0;for(let s=e-7;s<=e;s++){let c=.5*(t[0]+(s%2===0?t[e]:-t[e]));for(let l=1;l<e;l++)c+=t[l]*Ct[s*l%(2*e)*r];c*=2/e,s===e&&(c*=.5),a=Math.max(a,Math.abs(c))}return{integral:o,tail:a,scale:i}}var J=class{f;a;b;noise;c;h;values=new Float64Array(D+1);have=new Uint8Array(D+1);N=0;integral=0;error=1/0;constructor(e,n,r,o){this.f=e,this.a=n,this.b=r,this.noise=o,this.c=.5*(n+r),this.h=.5*(r-n)}advance(e){let n=D/e,r=new Float64Array(e+1);for(let i=0;i<=e;i++){let a=i*n;this.have[a]||(this.values[a]=this.f(this.c+this.h*Ct[a]),this.have[a]=1),r[i]=this.values[a]}let o=un(r,e);this.N=e,this.integral=o.integral*this.h,this.error=Math.max(o.tail*4/e,this.noise*o.scale)*Math.abs(this.h)}};function lt(t,e,n,r=4*z){let o=[];for(let a=0;a+1<e.length;a++){let s=new J(t,e[a],e[a+1],r);s.advance(16),o.push(s)}for(let a=0;a<40;a++){let s=0;for(let h of o)s+=h.integral;let c=n*Math.abs(s)/o.length,l=!0,u=o.length,f=[];for(let h of o){if(h.error<=c||h.error<=r*Math.abs(h.integral)){f.push(h);continue}if(l=!1,h.N<D)h.advance(2*h.N),f.push(h);else if(u>=an)f.push(h);else{u++;let p=new J(t,h.a,h.c,r),M=new J(t,h.c,h.b,r);p.advance(16),M.advance(16),f.push(p,M)}}if(o=f,l)break}let i=0;for(let a of o)i+=a.integral;return i}function ge(t,e,n){let r=Math.max(1,Math.ceil((e-t)/n));return Array.from({length:r+1},(o,i)=>i===r?e:t+(e-t)*i/r)}var de=2e-15,Ne=1e-16,ln={lowerTail:!1},fn={logP:!0},hn={lowerTail:!1,logP:!0},tt=.9189385332046728,ft=t=>F(t),X=t=>F(t,ln),Lt=t=>F(t,fn),Pt=t=>F(t,hn),Mn=.3989422804014327;function Q(t){let e=Math.round(t*16)/16,n=t-e;return Mn*Math.exp(-.5*e*e)*Math.exp(-.5*n*(t+e))}var It=[.1834346424956498,.525532409916329,.7966664774136267,.9602898564975363],be=[.362683783378362,.31370664587788727,.22238103445337448,.10122853629037626];function pn(t,e){let n=t-.5*e,r=.5*e,o=0;for(let i=0;i<4;i++)o+=be[i]*(Q(n-r*It[i])+Q(n+r*It[i]));return o*r}function mn(t,e){let n=t-.5*e,r=.5*e,o=0;for(let i=0;i<4;i++){let a=r*It[i];o+=be[i]*(Math.exp(n*a-a*a/2)+Math.exp(-n*a-a*a/2))}return-tt-n*n/2+Math.log(o*r)}var St=(t,e)=>e*Math.max(1,Math.abs(t),Math.abs(t-e))<.5;function V(t,e){let n=t-e,r=n-t,o=t-(n-r)+(-e-r);return{ah:n,al:o}}function k(t,e){return ft(t)+Q(t)*e}function gn(t,e){return X(t)-Q(t)*e}function xe(t,e){if(St(t,e))return pn(t,e);let{ah:n,al:r}=V(t,e);return n>=0?gn(n,r)-X(t):t<=0?ft(t)-k(n,r):1-k(n,r)-X(t)}function dn(t,e,n){if(t>0&&t-e<0&&!St(t,e)){let{ah:o,al:i}=V(t,e);return Math.exp(n*Math.log1p(-(k(o,i)+X(t))))}let r=xe(t,e);return r>0?r**n:0}function Nn(t,e){return t>0?Math.exp(e*Math.log1p(-X(t))):ft(t)**e}function ye(t,e){let{ah:n,al:r}=V(t,e),o=Lt(n);return o+r*Math.exp(-tt-n*n/2-o)}function bn(t,e){let{ah:n,al:r}=V(t,e),o=Pt(n);return o-r*Math.exp(-tt-n*n/2-o)}function we(t,e){if(St(t,e))return mn(t,e);if(t-e>=0){let i=bn(t,e);return i+Math.log(-Math.expm1(Pt(t)-i))}if(t<=0){let i=Lt(t);return i+Math.log(-Math.expm1(ye(t,e)-i))}let{ah:r,al:o}=V(t,e);return Math.log1p(-k(r,o)-X(t))}var ve=2,wt=1e-18;function Ie(t,e,n){let r=ge(e,n,ve),o=r.map(t),i=0;for(let h=1;h<o.length;h++)o[h]>o[i]&&(i=h);let a=o[i];if(!(a>0))return 0;let s=Math.max(i-1,0),c=Math.min(i+1,o.length-1);for(;s>0&&o[s]>a*wt;)s--;for(;c<o.length-1&&o[c]>a*wt;)c++;let l=a*wt,u=r[s],f=r[c];if(o[s]<=l){let h=r[s+1];for(let p=0;p<2;p++){let M=.5*(u+h);t(M)<=l?u=M:h=M}}if(o[c]<=l){let h=r[c-1];for(let p=0;p<2;p++){let M=.5*(f+h);t(M)<=l?f=M:h=M}}return lt(t,[u,f],Ne)}function Ee(t,e,n){let r=ge(e,n,ve),o=r.map(t),i=0;for(let m=1;m<o.length;m++)o[m]>o[i]&&(i=m);if(!(o[i]>-1/0))return-1/0;let a=r[Math.max(i-1,0)],s=r[Math.min(i+1,r.length-1)],c=(Math.sqrt(5)-1)/2,l=s-c*(s-a),u=a+c*(s-a),f=t(l),h=t(u);for(let m=0;m<80&&s-a>1e-12*Math.max(1,Math.abs(a));m++)f<h?(a=l,l=u,f=h,u=a+c*(s-a),h=t(u)):(s=u,u=l,h=f,l=s-c*(s-a),f=t(l));let p=f>h?l:u,M=Math.max(f,h);o[i]>M&&(p=r[i],M=o[i]);let x=m=>{let b=m<0?e:n,I=Math.max(1e-6,.001*(r[1]-r[0])),E=p;for(;;){let w=p+m*I;if(m<0?w<=b:w>=b)return b;if(t(w)<M-46){let C=w;for(let A=0;A<6;A++){let B=.5*(E+C);t(B)<M-46?C=B:E=B}return C}E=w,I*=2}},N=x(-1),g=x(1),d=[...new Set([N,p,g])].sort((m,b)=>m-b);return M+Math.log(lt(m=>Math.exp(t(m)-M),d,Ne,4*z*Math.max(1,Math.abs(M))))}var Tt=-10,qt=10;function xn(t,e){return t<=0?0:e*Ie(n=>Q(n)*dn(n,t,e-1),Tt,qt)}function Ae(t,e){if(t<=0)return 1;let n=i=>{let a=ft(i);if(a===0)return 0;let{ah:s,al:c}=V(i,t),l=k(s,c),u=l<.5*a?-Math.expm1((e-1)*Math.log1p(-l/a)):-Math.expm1((e-1)*Math.log(xe(i,t)/a));return Q(i)*Nn(i,e-1)*u};if(t>60)return 0;let[r,o]=Fe(t);return e*Ie(n,r,o)}function Fe(t){return t>40?[t/2-15,t/2+15]:[Tt,t+qt]}function yn(t,e){if(t>100)return-Math.exp(Re(t,e));let n=r=>-tt-r*r/2+(e-1)*we(r,t);return Math.log(e)+Ee(n,Tt,qt)}function Re(t,e){if(t>100)return Math.log(e*(e-1))+Pt(t/Math.SQRT2);let n=i=>{let a=Lt(i),s=ye(i,t)-a,c;return s<-40?c=Math.log(e-1)+s:s<-Math.LN2?c=Math.log(-Math.expm1((e-1)*Math.log1p(-Math.exp(s)))):c=Math.log(-Math.expm1((e-1)*(we(i,t)-a))),-tt-i*i/2+(e-1)*a+c},[r,o]=Fe(t);return Math.log(e)+Ee(n,r,o)}function wn(t,e){let n=-1/0;for(let r of e)n=Math.max(n,t(r));for(let r=0;r+1<e.length;r++)n=Math.max(n,t(.5*(e[r]+e[r+1])));if(n===-1/0)return-1/0;if(z*Math.abs(n)>1)return n;for(let r=0;r<4;r++){let o=n,i=lt(a=>{let s=t(a);return s>o&&(o=s),Math.exp(s-n)},e,de,8*z*Math.max(1,Math.abs(n)));if(Number.isFinite(i)&&o-n<300)return n+Math.log(i);n=o}return NaN}var W=32,vn=1e-15,U=Math.log(.01),ht=Math.log(20),_e=Math.log(40),Ce=Float64Array.from({length:2*W+1},(t,e)=>Math.cos(Math.PI*e/W));function In(t,e){let n=new Float64Array(e+1);for(let r=0;r<=e;r++){let o=.5*(t[0]+(r%2===0?t[e]:-t[e]));for(let i=1;i<e;i++)o+=t[i]*Ce[r*i%(2*e)];o*=2/e,(r===0||r===e)&&(o*=.5),n[r]=o}return n}function vt(t,e,n){let r=[],o=[[e,n,0]];for(;o.length>0;){let[i,a,s]=o.pop(),c=.5*(i+a),l=.5*(a-i),u=new Float64Array(W+1),f=1/0;for(let M=0;M<=W;M++)u[M]=t(c+l*Ce[M]),f=Math.min(f,Math.max(1,Math.abs(u[M])));let h=In(u,W),p=0;for(let M=W-3;M<=W;M++)p=Math.max(p,Math.abs(h[M]));p<=vn*f||s>=14||!u.every(Number.isFinite)?r.push({a:i,b:a,coef:h}):o.push([c,a,s+1],[i,c,s+1])}return r.sort((i,a)=>i.a-a.a)}function Et(t,e){let n=0,r=t.length-1;for(;n<r;){let u=n+r+1>>1;t[u].a<=e?n=u:r=u-1}let{a:o,b:i,coef:a}=t[n],s=(2*e-o-i)/(i-o),c=0,l=0;for(let u=W;u>=1;u--){let f=2*s*c-l+a[u];l=c,c=f}return s*c-l+a[0]}var K=new Map,En=64,Le=Math.log(1e-10);function Pe(t){let e=K.get(t);if(e!==void 0)return e;let n=o=>{let i=xn(o,t);return i>1e-290?Math.log(i):yn(o,t)},r=o=>{let i=Math.exp(o);return Ae(i,t)*Se(i)};return e={logW:vt(o=>n(Math.exp(o)),U,ht),scaledWc:vt(r,U,_e),smallW:vt(o=>n(Math.exp(o))-(t-1)*o,Le,U)},K.size>=En&&K.delete(K.keys().next().value),K.set(t,e),e}function Se(t){let e=t*t,n=134217729*t,r=n-(n-t),o=t-r,i=r*r-e+2*r*o+o*o;return Math.exp(e/4)*(1+i/4)}function Te(t,e){let n=Math.log(t);if(n>=ht)return 0;let r=Pe(e);return n>=U?Et(r.logW,n):Et(r.smallW,Math.max(n,Le))+(e-1)*n}function At(t,e){return Math.log(t)>=ht?1:Math.exp(Te(t,e))}function Ft(t,e){let n=Math.log(t);return n>=U&&n<=_e?Et(Pe(e).scaledWc,n)/Se(t):n<U?1-At(t,e):Ae(t,e)}function ct(t,e,n,r){if(!r){let i=Math.min(At(t,e),1);return n===1?i:i**n}let o=Math.min(Ft(t,e),1);return o<=.5?n===1?o:-Math.expm1(n*Math.log1p(-o)):1-Math.min(At(t,e),1)**n}function Rt(t,e,n,r){if(!r)return n*(Math.log(t)<ht?Te(t,e):Math.log1p(-Math.min(Ft(t,e),1)));let o=Math.min(Ft(t,e),1),i=o>1e-290?Math.log(o):Re(t,e);return n===1?i:i<-700?Math.log(n)+i:Math.log(-Math.expm1(n*Math.log1p(-Math.exp(i))))}function _t(t,e,n,r,o){if(n===1/0||We(t,e,n,r))return ct(t,e,r,o);let i=n/2,a=qe(i);return lt(c=>{let l=Math.exp(a(c));return l===0?0:l*ct(t*Math.exp(.5*c),e,r,o)},De(t,e,n,r,o),de)}function qe(t){let e=.5*Math.log(t/(2*Math.PI))-Ut(t);return n=>{let r;if(Math.abs(n)<.5){let o=n*n/2;r=o;for(let i=3;i<60&&(o*=n/i,r+=o,!(Math.abs(o)<1e-17*r));i++);}else r=Math.expm1(n)-n;return e-t*r}}function We(t,e,n,r){let o=Math.max((e-1)*r,t*t,1);return o*o<1e-17*n}function An(t,e,n,r,o){if(n===1/0||We(t,e,n,r))return Rt(t,e,r,o);let i=qe(n/2),a=Math.log(t);return wn(c=>i(c)+Rt(Math.exp(a+.5*c),e,r,o),De(t,e,n,r,o,!0))}function De(t,e,n,r,o,i=!1){let a=n/2,s=a,c;if(o){let m=2*Math.log(t)-Math.log(4*a);c=-(m>30?m:Math.log1p(Math.exp(m)))}else s=a+(e-1)*r/2,c=Math.log1p((e-1)*r/(2*a));let l=(m,b)=>[m-9/Math.sqrt(b)-41/b,m+9/Math.sqrt(b)+Math.log1p(41/b)],[u,f]=l(0,a),[h,p]=l(c,s),M=i?Math.min(u,h):Math.max(Math.min(u,h),-740-Math.log(a)),x=Math.max(f,p),N=1/Math.sqrt(a),g=1/Math.sqrt(s),d=[M,Math.min(-4*N,c-4*g),Math.max(4*N,c+4*g),x];return[...new Set(d.map(m=>Math.min(Math.max(m,M),x)))].sort((m,b)=>m-b)}function je(t,e,n){return t>=2&&e>=2&&n>=1&&Number.isFinite(t)&&Number.isFinite(n)&&!Number.isNaN(e)}function ut(t,e,n,r,o,i){if(t<pe){let s=ut(pe,e,n,r,!0,!0)+(e-1)*r*Math.log(t*10715086071862673e285);return o?i?s:Math.exp(s):i?-Math.exp(s):-Math.expm1(s)}let a;return H!==void 0&&H.q===t&&H.k===e&&H.df===n&&H.r===r?a=H.tail:(a=Fn(t,e,n,r,!o),H={q:t,k:e,df:n,r,tail:a}),a.upper===!o?i?a.log:a.value:i?Math.log1p(-a.value):1-a.value}var H,pe=2**-1e3;function Fn(t,e,n,r,o){let i=o;n!==1/0&&ct(t,e,r,i)>.5&&(i=!i);let a=_t(t,e,n,r,i);if(a>.5){let s=_t(t,e,n,r,!i);s<a&&(a=s,i=!i)}if(a<1e-290){let s=An(t,e,n,r,i);return{upper:i,value:Math.exp(s),log:s}}return{upper:i,value:a,log:Math.log(a)}}function Rn(t,e,n,r,o,i){return[t,e,n,r].some(Number.isNaN)?NaN:je(e,n,r)?t<=0?R(o,i):t===1/0?_(o,i):ut(t,e,n,r,o,i):NaN}function _n(t,e,n,r,o,i){if([t,e,n,r].some(Number.isNaN))return NaN;if(!je(e,n,r))return NaN;let a=S(t,0,1/0,o,i);if(a!==void 0)return a;let{lower:s,logTarget:c,linearTarget:l}=$t(t,o,i),u=h=>p=>{let M=Math.exp(p);if(l!==void 0&&l>1e-290){let N=h===1/0?_t(M,e,h,r,!s):ut(M,e,h,r,s,!1);if(N>0)return Math.log(N/l)}return(h===1/0?Cn(M,e,r,s):ut(M,e,h,r,s,!0))-c},f=me(u(1/0),Math.log(3),s);return Math.exp(n===1/0?f:me(u(n),f,s))}function Cn(t,e,n,r){let o=ct(t,e,n,!r);return o>1e-290?Math.log(o):Rt(t,e,n,!r)}function me(t,e,n){let r=e,o=t(r);if(o===0)return r;let i=o>0===n?-1:1,a=.05,s=r+i*a,c=t(s),l=NaN;for(let u=0;u<200&&Math.sign(c)===Math.sign(o)&&c!==0;u++){if(Math.abs(l-s)<.001)return s;for(r=s,o=c,a*=2,s=(r+i*a-l)*i>=0?.5*(r+l):r+i*a,c=t(s);!Number.isFinite(c);){if(l=s,Math.abs(l-r)<.001)return r;s=.5*(r+l),c=t(s)}}return c===0?s:!Number.isFinite(c)||Math.sign(c)===Math.sign(o)?Number.isFinite(c)?s:r:Ln(t,r,s,o,c)}function Ln(t,e,n,r,o){let i=e,a=r,s=n-e,c=s;for(let l=0;l<100;l++){Math.sign(o)===Math.sign(a)&&(i=e,a=r,s=n-e,c=s),Math.abs(a)<Math.abs(o)&&(e=n,n=i,i=e,r=o,o=a,a=r);let u=2*z*Math.abs(n)+1e-300,f=.5*(i-n);if(Math.abs(f)<=u||o===0)return n;if(Math.abs(c)>=u&&Math.abs(r)>Math.abs(o)){let h=o/r,p,M;if(e===i)p=2*f*h,M=1-h;else{let x=r/a,N=o/a;p=h*(2*f*x*(x-N)-(n-e)*(N-1)),M=(x-1)*(N-1)*(h-1)}p>0?M=-M:p=-p,2*p<Math.min(3*f*M-Math.abs(u*M),Math.abs(c*M))?(c=s,s=p/M):(s=f,c=f)}else s=f,c=f;e=n,r=o,n+=Math.abs(s)>u?s:f>0?u:-u,o=t(n)}return n}function Fr(t,e,n,r={}){let{nranges:o=1,lowerTail:i=!0,logP:a=!1}=r;return y(t,s=>Rn(s,e,n,o,i,a))}function Rr(t,e,n,r={}){let{nranges:o=1,lowerTail:i=!0,logP:a=!1}=r;return y(t,s=>_n(s,e,n,o,i,a))}function Wt(t){let e=[],n=[];for(let r=1;r<=t/2;r++){let o=Math.cos(Math.PI*(r-.25)/(t+.5)),i=0;for(let a=0;a<100;a++){let s=1,c=o;for(let u=2;u<=t;u++)[s,c]=[c,((2*u-1)*o*c-(u-1)*s)/u];i=t*(o*c-s)/(o*o-1);let l=c/i;if(o-=l,Math.abs(l)<=1e-17)break}e.push(o),n.push(2/((1-o*o)*i*i))}return{x:e,w:n}}var Pn=[Wt(6),Wt(12),Wt(20)],Dt=2*Math.PI,O=t=>F(t,{lowerTail:!1});function Sn(t,e,n){if(n===1)return O(Math.max(t,e));if(n===-1)return Math.max(0,O(t)-F(-e));let{x:r,w:o}=Pn[Math.abs(n)<.3?0:Math.abs(n)<.75?1:2],i=t*e;if(Math.abs(n)<.925){let N=(t*t+e*e)/2,g=Math.asin(n),d=0;for(let m=0;m<r.length;m++)for(let b of[1-r[m],1+r[m]]){let I=Math.sin(g*b/2);d+=o[m]*Math.exp((I*i-N)/(1-I*I))}return d*g/(2*Dt)+O(t)*O(e)}let a=n<0?-e:e,s=n<0?-i:i,c=(1-n)*(1+n),l=Math.sqrt(c),u=(t-a)**2,f=(4-s)/8,h=(12-s)/16,p=l*Math.exp(-(u/c+s)/2)*(1-f*(u-c)*(1-h*u/5)/3+f*h*c*c/5);if(s>-160){let N=Math.sqrt(u);p-=Math.exp(-s/2)*Math.sqrt(Dt)*F(-N/l)*N*(1-f*u*(1-h*u/5)/3)}let M=l/2;for(let N=0;N<r.length;N++)for(let g of[1-r[N],1+r[N]]){let d=(M*g)**2,m=Math.sqrt(1-d);p+=M*o[N]*(Math.exp(-u/(2*d)-s/(1+m))/m-Math.exp(-(u/d+s)/2)*(1+f*d*(1+h*d)))}let x=-p/Dt;return n>0?x+O(Math.max(t,a)):-x+(a>t?F(a)-F(t):0)}function Mt(t,e,n){return t===1/0||e===1/0?0:t===-1/0?e===-1/0?1:O(e):e===-1/0?O(t):Sn(t,e,n)}function Lr(t,e,n){if(t.length!==2||e.length!==2)throw new TypeError("'lower' and 'upper' must have length 2");if(!(n>=-1&&n<=1))throw new RangeError("'rho' must be in [-1, 1]");let[r,o]=t,[i,a]=e;if([r,o,i,a].some(Number.isNaN))return NaN;if(!(r<i&&o<a))return 0;r+i+o+a<0&&([r,o,i,a]=[-i,-a,-r,-o]);let s=Mt(r,o,n)-Mt(r,a,n)-Mt(i,o,n)+Mt(i,a,n);return Math.max(0,s)}function Tr(t,e={}){return He(t,e).map(n=>n-1)}function He(t,e){if(!Number.isFinite(t)||t<0||t>45e14)throw new RangeError("n must be a non-negative number up to 4.5e15");t=Math.trunc(t);let{replace:n=!1,prob:r}=e,o=e.size!==void 0,i=o?e.size:t;if(!Number.isFinite(i)||i<0)throw new RangeError("size must be a non-negative number");let a=Math.trunc(i);if(r!==void 0){if(t>=2**31)throw new RangeError("n must be less than 2^31 with prob");if(r.length!==t)throw new TypeError("prob must have length n");let u=Tn(r,a,n);if(n){let f=0;for(let h=0;h<t;h++)t*u[h]>.1&&f++;return f>200?jn(u,a):Wn(u,a)}if(a>t)throw new RangeError("cannot take a sample larger than the population when replace is false");return Dn(u,a)}if(!n&&a>t)throw new RangeError("cannot take a sample larger than the population when replace is false");if(!n&&t>1e7&&o&&a<=t/2){let u=new Set,f=[];for(;f.length<a;){let h=Y(t);u.has(h)||(u.add(h),f.push(h+1))}return f}let s=new Array(a);if(n||a<2){for(let u=0;u<a;u++)s[u]=Y(t)+1;return s}if(t>=2**31)throw new RangeError("n must be less than 2^31 to sample without replacement with size above n / 2");let c=new Int32Array(t);for(let u=0;u<t;u++)c[u]=u;let l=t;for(let u=0;u<a;u++){let f=Y(l);s[u]=c[f]+1,c[f]=c[--l]}return s}function qr(t,e={}){return He(t.length,e).map(n=>t[n-1])}function Tn(t,e,n){let r=0,o=0;for(let i of t){if(!Number.isFinite(i))throw new RangeError("prob contains NaN or infinite values");if(i<0)throw new RangeError("prob contains negative values");i>0&&(o++,r+=i)}if(o===0||!n&&e>o)throw new RangeError("too few positive probabilities");return Float64Array.from(t,i=>i/r)}function qn(t,e){let n=t.length;if(n<=1)return;let r=c=>t[c-1],o=(n>>1)+1,i=n,a,s;for(;;){if(o>1)o=o-1,a=r(o),s=e[o-1];else if(a=r(i),s=e[i-1],t[i-1]=r(1),e[i-1]=e[0],--i===1){t[0]=a,e[0]=s;return}let c=o,l=o<<1;for(;l<=i;)l<i&&r(l)>r(l+1)&&++l,a>r(l)?(t[c-1]=r(l),e[c-1]=e[l-1],c=l,l+=l):l=i+1;t[c-1]=a,e[c-1]=s}}function Oe(t){let e=new Int32Array(t.length);for(let n=0;n<t.length;n++)e[n]=n+1;return qn(t,e),e}function Wn(t,e){let n=Float64Array.from(t),r=n.length,o=Oe(n);for(let a=1;a<r;a++)n[a]+=n[a-1];let i=new Array(e);for(let a=0;a<e;a++){let s=T(),c=0;for(;c<r-1&&!(s<=n[c]);c++);i[a]=o[c]}return i}function Dn(t,e){let n=Float64Array.from(t),r=n.length,o=Oe(n),i=new Array(e),a=1;for(let s=0,c=r-1;s<e;s++,c--){let l=a*T(),u=0,f=0;for(;f<c&&(u+=n[f],!(l<=u));f++);i[s]=o[f],a-=n[f];for(let h=f;h<c;h++)n[h]=n[h+1],o[h]=o[h+1]}return i}function jn(t,e){let n=t.length,r=new Float64Array(n),o=new Int32Array(n),i=new Int32Array(n),a=-1,s=n;for(let u=0;u<n;u++)r[u]=t[u]*n,r[u]<1?i[++a]=u:i[--s]=u;if(a>=0&&s<n)for(let u=0;u<n-1;u++){let f=i[u],h=i[s];if(o[f]=h,r[h]+=r[f]-1,r[h]<1&&s++,s>=n)break}let c=kt(),l=new Array(e);for(let u=0;u<e;u++){let f,h;c?(h=T()*n,f=Math.trunc(h)):(f=Y(n),h=f+T()),l[u]=h<r[f]+f?f+1:o[f]+1}return l}function Hn(t){let e=new Float64Array(t+1);for(let n=2;n<=t;n++)e[n]=e[n-1]+Math.log(n);return e}function On(t,e,n,r){let o=t.length,i=e.length,a=Array.from(e),s=0;for(let l of t)s+=l;for(let l=0;l<o-1;l++){let u=t[l],f=s;s-=u;let h=0;for(;h<i-1;h++){let p=a[h],M=f;f-=p;let x=M-u,N=x-p;if(M===0){for(let m=h;m<i-1;m++)r[l+m*o]=0;u=0;break}let g=T(),d;t:for(;;){d=Math.floor(u*(p/M)+.5);let m=Math.exp(n[u]+n[x]+n[f]+n[p]-n[M]-n[d]-n[p-d]-n[u-d]-n[N+d]);if(m>=g)break;if(m===0)throw new Error(`r2dtable: probability underflow at cell [${l}, ${h}]`);let b=m,I=m,E=d,w=!1,C=!1;for(;!(w&&C);){if(!w){let A=(p-d)*(u-d);if(A===0)w=!0;else if(d++,m=m*A/(d*(N+d)),b+=m,b>=g)break t}do{let A=E*(N+E);if(A===0){C=!0;break}if(E--,I=I*A/((p-E)*(u-E)),b+=I,b>=g){d=E;break t}}while(w)}g=b*T()}r[l+h*o]=d,u-=d,a[h]=a[h]-d}r[l+(i-1)*o]=u}let c=e[i-1];for(let l=0;l<o-1;l++)c-=r[l+(i-1)*o];for(let l=0;l<i-1;l++)r[o-1+l*o]=a[l];r[o-1+(i-1)*o]=c}function Be(t,e){let n=Array.from(t,r=>Math.trunc(r));if(n.length<=1||n.some(r=>!(r>=0)||r>2147483647))throw new RangeError(`invalid argument '${e}'`);return n}function jr(t,e,n){let r=Math.trunc(t);if(!(r>=0))throw new RangeError("invalid argument 'n'");let o=Be(e,"r"),i=Be(n,"c"),a=o.reduce((u,f)=>u+f,0);if(a!==i.reduce((u,f)=>u+f,0))throw new RangeError("arguments 'r' and 'c' must have the same sums");let s=Hn(a),c=new Int32Array(o.length*i.length),l=[];for(let u=0;u<r;u++)On(o,i,s,c),l.push(o.map((f,h)=>i.map((p,M)=>c[h+M*o.length])));return l}export{Un as a,Xn as b,Qn as c,$e as d,Xe as e,Qe as f,er as g,nr as h,rr as i,Je as j,ze as k,lr as l,fr as m,hr as n,nn as o,rn as p,on as q,br as r,xr as s,yr as t,Fr as u,Rr as v,Lr as w,Tr as x,qr as y,Hn as z,On as A,jr as B};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 0.15.
|
|
2
|
-
import{f as o,i as N,j as a}from"./chunk-
|
|
1
|
+
/*! rStats.js 0.15.3 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{f as o,i as N,j as a}from"./chunk-IUULP3LE.js";function u(t,e){if(!Number.isFinite(t)||t<0)throw new RangeError("n must be a non-negative finite number");let r=Math.trunc(t),n=new Array(r);for(let i=0;i<r;i++)n[i]=e();return n}function s(t,e){let r=1;if(Number.isNaN(t))return t;if(e!==0){if(!Number.isFinite(t))return Math.pow(t,e);for(e<0&&(e=-e,t=1/t);e&1&&(r*=t),e>>=1,e;)t*=t}return r}function l(t,e={}){let{min:r=0,max:n=1}=e;return u(t,()=>{if(!Number.isFinite(r)||!Number.isFinite(n)||n<r)return NaN;if(r===n)return r;let i=o();return r+(n-r)*i})}function f(t,e){return Number.isNaN(t)||!Number.isFinite(e)||e<0?NaN:e===0||!Number.isFinite(t)?t:t+e*N()}function p(t,e={}){let{mean:r=0,sd:n=1}=e;return u(t,()=>f(r,n))}function F(t,e={}){let r=1/(e.rate??1);return u(t,()=>!Number.isFinite(r)||r<=0?r===0?0:NaN:r*a())}function g(t,e={}){let{meanlog:r=0,sdlog:n=1}=e;return u(t,()=>Number.isNaN(r)||!Number.isFinite(n)||n<0?NaN:Math.exp(f(r,n)))}function h(t,e={}){let{location:r=0,scale:n=1}=e;return u(t,()=>{if(Number.isNaN(r)||!Number.isFinite(n))return NaN;if(n===0||!Number.isFinite(r))return r;let i=o();return r+n*Math.log(i/(1-i))})}function w(t,e={}){let{location:r=0,scale:n=1}=e;return u(t,()=>Number.isNaN(r)||!Number.isFinite(n)||n<0?NaN:n===0||!Number.isFinite(r)?r:r+n*Math.tan(Math.PI*o()))}function M(t,e,r={}){let{scale:n=1}=r;return u(t,()=>!Number.isFinite(e)||!Number.isFinite(n)||e<=0||n<=0?n===0?0:NaN:n*Math.pow(-Math.log(o()),1/e))}export{u as a,s as b,l as c,f as d,p as e,F as f,g,h,w as i,M as j};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/*! rStats.js 0.15.
|
|
1
|
+
/*! rStats.js 0.15.3 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
2
|
function a(t){return` with simulated p-value
|
|
3
3
|
(based on ${u(t)} replicates)`}function u(t){if(!Number.isFinite(t)||t===0)return String(t);let i=t.toExponential(14).replace(/\.?0+e/,"e"),[o,n]=i.split("e"),s=Number(n),e=`${o}e${s<0?"-":"+"}${String(Math.abs(s)).padStart(2,"0")}`,r=String(Number(t.toPrecision(15)));return r.length<=e.length?r:e}function f(t){let i=[...new Set(Array.from(t,e=>e===0?0:e))].filter(e=>!Number.isNaN(e)).sort((e,r)=>e-r),o=new Map,n,s=NaN;for(let e of i){let r=u(e);r!==n&&(n=r,s=e),o.set(e,s)}return o}function m(t){let i=Array.from(t),o=f(i.filter(n=>typeof n=="number"));return i.map(n=>typeof n=="number"&&!Number.isNaN(n)?o.get(n):n)}function p(t){return typeof t!="number"?String(t):t===1/0?"Inf":t===-1/0?"-Inf":u(t)}export{a,u as b,m as c,p as d};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 0.15.
|
|
1
|
+
/*! rStats.js 0.15.3 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
2
|
var q=2220446049250313e-31,b0=22250738585072014e-324,z=17976931348623157e292,v0=[.06667134430868814,.1494513491505806,.21908636251598204,.26926671930999635,.29552422471475287],f0=[.9956571630258081,.9739065285171717,.9301574913557082,.8650633666889845,.7808177265864169,.6794095682990244,.5627571346686047,.4333953941292472,.2943928627014602,.14887433898163122,0],I=[.011694638867371874,.032558162307964725,.054755896574351995,.07503967481091996,.0931254545836976,.10938715880229764,.12349197626206584,.13470921731147334,.14277593857706009,.14773910490133849,.1494455540029169];function h0(a,o,i,s,t){let r=Math.abs((a-o)*i);return t!==0&&r!==0&&(r=t*Math.min(1,Math.pow(200*r/t,1.5))),s>b0/(50*q)&&(r=Math.max(q*50*s,r)),r}function g0(a,o,i){let s=.5*(o+i),t=.5*(i-o),r=Math.abs(t),w=new Array(10),u=new Array(10),p=a(s),M=0,c=I[10]*p,b=Math.abs(c);for(let e=0;e<5;e++){let n=2*e+1,l=t*f0[n],h=a(s-l),v=a(s+l);w[n]=h,u[n]=v;let g=h+v;M+=v0[e]*g,c+=I[n]*g,b+=I[n]*(Math.abs(h)+Math.abs(v))}for(let e=0;e<5;e++){let n=2*e,l=t*f0[n],h=a(s-l),v=a(s+l);w[n]=h,u[n]=v;let g=h+v;c+=I[n]*g,b+=I[n]*(Math.abs(h)+Math.abs(v))}let A=c*.5,f=I[10]*Math.abs(p-A);for(let e=0;e<10;e++)f+=I[e]*(Math.abs(w[e]-A)+Math.abs(u[e]-A));return b*=r,f*=r,{result:c*t,abserr:h0(c,M,t,b,f),resabs:b,resasc:f}}var l0=[0,.1294849661688697,0,.27970539148927664,0,.3818300505051189,0,.4179591836734694],x0=[.9914553711208126,.9491079123427585,.8648644233597691,.7415311855993945,.5860872354676911,.4058451513773972,.20778495500789848,0],U=[.022935322010529224,.06309209262997856,.10479001032225019,.14065325971552592,.1690047266392679,.19035057806478542,.20443294007529889,.20948214108472782];function A0(a,o,i,s,t){let r=i>1?1:i,w=.5*(s+t),u=.5*(t-s),p=new Array(7),M=new Array(7),c=h=>{let v=o+r*(1-h)/h,g=a(v);return i===2&&(g+=a(-v)),g},b=c(w)/w/w,A=b*l0[7],f=b*U[7],e=Math.abs(f);for(let h=0;h<7;h++){let v=u*x0[h],g=w-v,W=w+v,m=c(g),x=c(W);m=m/g/g,x=x/W/W,p[h]=m,M[h]=x;let K=m+x;A+=l0[h]*K,f+=U[h]*K,e+=U[h]*(Math.abs(m)+Math.abs(x))}let n=f*.5,l=U[7]*Math.abs(b-n);for(let h=0;h<7;h++)l+=U[h]*(Math.abs(p[h]-n)+Math.abs(M[h]-n));return e*=u,l*=u,{result:f*u,abserr:h0(f,A,u,e,l),resabs:e,resasc:l}}function j0(a){let o=a.epstab,i=a.n;a.nres+=1;let s=z,t=o[a.n];if(a.n<2)return s=Math.max(s,5*q*Math.abs(t)),[t,s];let r=49;o[a.n+2]=o[a.n];let w=Math.trunc(a.n/2);o[a.n]=z;let u=a.n;for(let c=0;c<w;c++){let b=u-1,A=u-2,f=o[u+2],e=o[A],n=o[b],l=f,h=Math.abs(n),v=l-n,g=Math.abs(v),W=Math.max(Math.abs(l),h)*q,m=n-e,x=Math.abs(m),K=Math.max(h,Math.abs(e))*q;if(!(g>W||x>K))return t=f,s=g+x,s=Math.max(s,5*q*Math.abs(t)),[t,s];let k=o[u];o[u]=n;let j=n-k,F=Math.abs(j),_=Math.max(h,Math.abs(k))*q;if(F<=_||g<=W||x<=K){a.n=c+c;break}let L=1/j+1/v-1/m;if(!(Math.abs(L*n)>1e-4)){a.n=c+c;break}f=n+1/L,o[u]=f,u-=2;let X=g+Math.abs(f-l)+x;X>s||(s=X,t=f)}a.n===r&&(a.n=2*Math.trunc(r/2));let p=i%2;for(let c=0;c<=w;c++)o[2*c+p]=o[2*c+2+p];if(a.n!==i){let c=i-a.n;for(let b=0;b<=a.n;b++)o[b]=o[c],c++}let M=a.res3la;return a.nres<4?(M[a.nres-1]=t,s=z):(s=Math.abs(t-M[2])+Math.abs(t-M[1])+Math.abs(t-M[0]),M[0]=M[1],M[1]=M[2],M[2]=t),s=Math.max(s,5*q*Math.abs(t)),[t,s]}function y0(a,o,i,s,t,r){if(o<=2){t[0]=0,t[1]=1;let e=t[r];return[e,s[e],r]}let w=s[i];for(;r>0&&w>s[t[r-1]];)t[r]=t[r-1],r-=1;let u=o>Math.trunc(a/2)+2?a-o+2:o-1,p=s[o-1],M=u-1,c=r+1,b=c,A=!1;if(c<=M)for(b=c;b<=M;b++){if(w>=s[t[b]]){A=!0;break}t[b-1]=t[b]}if(!A)t[M]=i,t[u]=o-1;else{t[b-1]=i;let e=M;for(let n=b;n<=M&&!(p<s[t[e]]);n++)t[e+1]=t[e],e--;p<s[t[e]]?t[e+1]=o-1:t[b]=o-1}let f=t[r];return[f,s[f],r]}function M0(a,o,i,s,t,r,w,u){let p=new Float64Array(r+1),M=new Float64Array(r+1),c=new Float64Array(r+1),b=new Float64Array(r+1),A=new Int32Array(r+1),f=0,e=0,n=0,l=0;if(p[0]=o,M[0]=i,s<=0&&t<Math.max(50*q,5e-29))return{result:0,abserr:0,neval:0,ier:6,last:0};let h=a(o,i);n=h.result,l=h.abserr;let v=h.resabs,g=h.resasc,W=Math.abs(n),m=Math.max(s,t*W);if(e=1,c[0]=n,b[0]=l,A[0]=0,l<=100*q*v&&l>m&&(f=2),r===1&&(f=1),f!==0||l<=m&&l!==g||l===0)return{result:n,abserr:l,neval:u(e),ier:f,last:e};let x={epstab:new Float64Array(52),res3la:new Float64Array(3),n:1,nres:0};x.epstab[0]=n;let K=l,k=0,j=n,F=l;l=z;let _=0,L=0,E=!1,X=!1,B=0,t0=0,R=0,s0=0,C=0,O=0,D=0,e0=0,u0=W>=(1-50*q)*v?1:-1,a0=!1,y=1;for(y=1;y<r;y++){e=y+1;let d=p[k],H=.5*(p[k]+M[k]),J=H,S=M[k],i0=K,T=a(d,H),V=a(J,S),Y=T.result,N=T.abserr,k0=T.resasc,Z=V.result,P=V.abserr,w0=V.resasc,$=Y+Z,Q=N+P;if(F=F+Q-K,j=j+$-c[k],k0!==N&&w0!==P&&(Math.abs(c[k]-$)>1e-5*Math.abs($)||Q<.99*K||(E?R++:t0++),y>9&&Q>K&&s0++),c[k]=Y,c[y]=Z,m=Math.max(s,t*Math.abs(j)),(t0+R>=10||s0>=20)&&(f=2),R>=5&&(B=3),e===r&&(f=1),Math.max(Math.abs(d),Math.abs(S))<=(1+100*q)*(Math.abs(J)+1e3*b0)&&(f=4),P>N?(p[k]=J,p[y]=d,M[y]=H,c[k]=Z,c[y]=Y,b[k]=P,b[y]=N):(p[y]=J,M[k]=H,M[y]=S,b[k]=N,b[y]=P),[k,K,_]=y0(r,e,k,b,A,_),F<=m){a0=!0;break}if(f!==0)break;if(y===1){C=w,O=F,D=m,x.epstab[1]=j;continue}if(X)continue;if(O-=i0,Math.abs(H-d)>C&&(O+=Q),!E){if(Math.abs(M[k]-p[k])>C)continue;E=!0,_=1}if(!(B===3||O<=D)){let p0=e>2+Math.trunc(r/2)?r+3-e:e,o0=!1;for(let c0=_;c0<p0;c0++){if(k=A[_],K=b[k],Math.abs(M[k]-p[k])>C){o0=!0;break}_++}if(o0)continue}x.n+=1,x.epstab[x.n]=j;let[n0,r0]=j0(x);if(L+=1,L>5&&l<.001*F&&(f=5),r0<l&&(L=0,l=r0,n=n0,e0=O,D=Math.max(s,t*Math.abs(n0)),l<=D)||(x.n===0&&(X=!0),f===5))break;k=A[0],K=b[k],_=0,E=!1,C*=.5,O=F}let G;if(a0||l===z?G=115:f+B===0?G=110:(B===3&&(l+=e0),f===0&&(f=3),n!==0&&j!==0?G=105:l>F?G=115:j===0?G=130:G=110),G===105&&(G=l/Math.abs(n)>F/Math.abs(j)?115:110),G===110)u0===-1&&Math.max(Math.abs(n),Math.abs(j))<=v*.01||(.01>n/j||n/j>100||F>Math.abs(j))&&(f=6);else if(G===115){n=0;for(let d=0;d<=y;d++)n+=c[d];l=F}return f>2&&(f-=1),{result:n,abserr:l,neval:u(e),ier:f,last:e}}function m0(a,o,i,s,t,r){return M0((w,u)=>g0(a,w,u),o,i,s,t,r,Math.abs(i-o)*.375,w=>42*w-21)}function K0(a,o,i,s,t,r){let w=i===2?0:o;return M0((u,p)=>A0(a,w,i,u,p),0,1,s,t,r,.375,u=>(30*u-15)*(i===2?2:1))}export{m0 as a,K0 as b};
|
package/dist/descriptive.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 0.15.
|
|
2
|
-
import{l as me,m as he,n as pe}from"./chunks/chunk-Z3C3G5XI.js";import"./chunks/chunk-3QTVXDUM.js";import{A as Fe,B as je,C as Se,D as Te,E as _e,F as Ie,a as V,b as L,c as z,d as k,e as K,f as ge,g as de,j as ye,k as we,l as Y,m as be,n as Ne,o as Ee,p as Ae,q as Me,r as C,s as F,t as S,u as xe,v as $e,w as ve,x as Oe,y as Re,z as ke}from"./chunks/chunk-LY2T6DA6.js";import{a as ee,b as Le,c as Ce,d as Pe,e as Ue,f as qe}from"./chunks/chunk-IQOVOHZG.js";import"./chunks/chunk-4ILI2FSB.js";import"./chunks/chunk-7J4HW6VF.js";import{a as G,e as I,f as J}from"./chunks/chunk-735NNFAS.js";import"./chunks/chunk-TYFGVCVQ.js";import{a as H}from"./chunks/chunk-5OSQ76OO.js";import{b as _,c as X}from"./chunks/chunk-H4CHJMFU.js";import"./chunks/chunk-UZHY5DWN.js";import"./chunks/chunk-UWBYDIOH.js";var B=4*Number.EPSILON,Ve={4:[0,1],5:[.5,.5],6:[0,0],8:[1/3,1/3],9:[3/8,3/8]};function ze(e,n,t){let r=e.length,o=i=>e[Math.min(Math.max(i,1),r)-1];if(t===7){let i=1+(r-1)*n,h=Math.floor(i),m=o(h),y=o(Math.ceil(i));if(i>h&&y!==m){let g=i-h;return(1-g)*m+g*y}return m}let a,s;if(t<=3){let i=t===3?r*n-.5:r*n;a=Math.floor(i*(1+B)),t===1?s=i>a?1:0:t===2?s=i>a?1:.5:s=i!==a||Math.abs(a%2)===1?1:0}else{let[i,h]=Ve[t],m=i+n*(r+1-i-h);a=Math.floor(m*(1+B)),s=m-a,Math.abs(s)<B&&(s=0)}let l=o(a),f=o(a+1);return s===1?f:s>0&&s<1&&l!==f?(1-s)*l+s*f:l}function P(e,n=[0,.25,.5,.75,1],t={}){let r=t.type??7;if(!(Number.isInteger(r)&&r>=1&&r<=9))throw new RangeError("'type' must be an integer in 1..9");let o=Y(e,t.naRm);if(o===void 0)throw new RangeError("missing values and NaN's not allowed if 'na.rm' is FALSE");let a=o.sort((i,h)=>i-h),s=typeof n=="number"?[n]:Array.from(n),l=100*Number.EPSILON;if(s.some(i=>i<-l||i>1+l))throw new RangeError("'probs' outside [0,1]");let f=s.map(i=>Number.isNaN(i)||a.length===0?NaN:ze(a,Math.min(Math.max(i,0),1),r));return typeof n=="number"?f[0]:f}function W(e,n={}){let[t,r]=P(e,[.25,.75],n);return r-t}function Be(e){let n=Array.from(e).filter(a=>!Number.isNaN(a)).sort((a,s)=>a-s),t=n.length;if(t===0)return[NaN,NaN,NaN,NaN,NaN];let r=Math.floor((t+3)/2)/2;return[1,r,(t+1)/2,t+1-r,t].map(a=>.5*(n[Math.floor(a)-1]+n[Math.ceil(a)-1]))}function We(e,n={}){let t=Array.from(e),r=t.filter(m=>!Number.isNaN(m)),o=n.naRm?r:t,a=n.center??(o.length>r.length?NaN:F(r)),s=n.constant??1.4826,l=n.low??!1,f=n.high??!1,i=o.map(m=>Math.abs(m-a)),h=i.length;if((l||f)&&h%2===0){if(l&&f)throw new RangeError("'low' and 'high' cannot be both TRUE");let m=h/2+(f?1:0),y=i.filter(g=>!Number.isNaN(g)).sort((g,d)=>g-d);if(m===0||y.length<m)throw new RangeError(`'partial' index ${m} outside bounds`);return s*y[m-1]}return s*F(i)}var ne=e=>e===null||typeof e=="number"&&Number.isNaN(e),te=e=>Array.isArray(e)||ArrayBuffer.isView(e);function Ze(e){if(te(e))return{columns:[e],rows:e.length};if(typeof e!="object"||e===null)throw new TypeError("the arguments must be arrays or data frames");let n=Object.values(e),t=n[0]?.length??0;if(n.some(r=>r.length!==t))throw new RangeError("not all arguments have the same length");return{columns:n,rows:t}}function re(...e){if(e.length===0)throw new RangeError("no input has determined the number of cases");let n=e.map(Ze),t=n[0].rows;if(n.some(o=>o.rows!==t))throw new RangeError("not all arguments have the same length");let r=n.flatMap(o=>o.columns);return Array.from({length:t},(o,a)=>r.every(s=>!ne(s[a])))}function De(e){if(te(e))return Array.from(e).filter(r=>!ne(r));let n=re(e),t={};for(let[r,o]of Object.entries(e)){let a=Array.from(o).filter((s,l)=>n[l]);t[r]=o instanceof Float64Array?Float64Array.from(a):a}return t}function oe(e,n={}){let t=Array.from(e).filter(Number.isFinite);if(t.length===0)return[];let r=n.n??5;if(Number.isNaN(r)||Math.abs(r)>=2**31||Math.trunc(r)<0)throw new RangeError("invalid 'n' argument");let o=Math.trunc(r),a=n.minN??Math.floor(r/3);if(Number.isNaN(a)||Math.trunc(a)<0||Math.trunc(a)>o)throw new RangeError("invalid 'min.n' argument");let s=Math.trunc(a),l=n.shrinkSml??.75,f=n.highUBias??1.5,i=n.u5Bias??.5+1.5*f,h=1/0,m=-1/0;for(let A of t)A<h&&(h=A),A>m&&(m=A);let y=m-h,g,d;if(y===0&&m===0)g=1,d=!0;else{g=Math.max(Math.abs(h),Math.abs(m));let O=(1+(i>=1.5*f+.5?1/(1+f):1.5/(1+i)))*Math.max(1,o)*Number.EPSILON;d=y<g*O*3}d?(g>10&&(g=9+g/10),g*=l,s>1&&(g/=s)):(g=o>1?y/o:y,y===1/0&&o>=2&&(g=m/o-h/o));let x=2**-20*Qe;g<x?g=x:g>Number.MAX_VALUE/1.25&&(g=Number.MAX_VALUE/1.25);let b=10**Math.floor(Math.log10(g)),w=[[2*b,f],[5*b,i],[10*b,f]],c=b;for(let[A,O]of w)if(A-g<O*(g-c))c=A;else break;let u=1e-10,p=Math.floor(h/c+u);for(;p*c>h+u*c;)p--;for(;!Number.isFinite(p*c);)p++;let N=Math.ceil(m/c-u);for(;N*c<m-u*c;)N++;for(;!Number.isFinite(N*c);)N--;let E=Math.trunc(.5+N-p);if(E<s){let A=s-E,O=Math.floor(A/2),T=A%2;h===0&&p===0&&m!==0?N+=A:m===0&&N===0&&h!==0?p-=A:p>=0?(N+=O,p-=O+T):(p-=O,N+=O+T),E=s}let $=p*c<h?p*c:h,M=N*c>m?N*c:m;if(E<=0)return[$];let v;if(Number.isFinite(M-$))v=ee($,M,E+1);else{let A=M/E-$/E;v=Array.from({length:E+1},(O,T)=>$+T*A),v[E]=M}let R=M/E-$/E;return v.map(A=>Math.abs(A)<1e-14*R?0:A)}var Qe=22250738585072014e-324;function Ge(e){return e===0||!Number.isFinite(e)?e:Number(e.toPrecision(5))}function Je(e,n){let t=e.length,[r,o]=S(e),a=o-r;switch(n.toLowerCase()){case"sturges":return Math.ceil(Math.log2(t)+1);case"scott":{let s=3.5*C(e)*t**-.3333333333333333;return s>0?Math.max(1,Math.ceil(a/s)):1}case"fd":case"freedman-diaconis":{let s=e.map(Ge),l=2*W(s);for(let f=1/8;l===0&&f>=1/512;f/=2){let[i,h]=P(s,[f,1-f]);l=(h-i)/(1-2*f)}return l===0&&(l=3.5*C(e)),l>0?Math.ceil(a/l*t**(1/3)):1}default:throw new RangeError("unknown 'breaks' algorithm")}}function Xe(e,n){let t=e.slice(1).map((r,o)=>r-e[o]);return e.length>5?F(t):e.length<=3?n:Math.min(...t.filter(r=>r>0))}function He(e,n={}){let t=Array.from(e).filter(Number.isFinite),r=t.length,o=n.right??!0,a=n.includeLowest??!0,s=n.breaks??"Sturges",l;if(typeof s!="string"&&typeof s!="number"&&s.length>1)l=[...s].sort((w,c)=>w-c);else{a=!0;let w=typeof s=="string"?Je(t,s):typeof s=="number"?s:s[0];if(!Number.isFinite(w)||w<1)throw new RangeError("invalid number of 'breaks'");w=Math.min(w,1e6);let[c,u]=S(t);l=oe([c,u],{n:w,minN:1});for(let p=1;p<l.length;p++)if(!(l[p]-l[p-1]>0))throw new RangeError("'breaks' are not strictly increasing")}let f=l.length,[i,h]=r>0?S(t):[0,0],m=1e-7*Xe(l,h-i),y=l.map((w,c)=>o?c===0&&a?w-m:w+m:c<f-1?w-m:a?w+m:w-m),g=new Array(Math.max(0,f-1)).fill(0),d=0;for(let w of t){let c=Ke(w,y,o,a);c>=0&&(g[c]++,d++)}if(d<r)throw new RangeError("some 'x' not counted; maybe 'breaks' do not span range of 'x'");let x=g.map((w,c)=>w/(r*(l[c+1]-l[c]))),b=g.map((w,c)=>.5*(l[c+1]+l[c]));return{breaks:l,counts:g,density:x,mids:b}}function Ke(e,n,t,r){let o=n.length-1;if(o<1)return-1;if(t){if(e===n[0]&&r)return 0;if(!(e>n[0]&&e<=n[o]))return-1;let l=1,f=o;for(;l<f;){let i=l+f>>1;e<=n[i]?f=i:l=i+1}return l-1}if(e===n[o]&&r)return o-1;if(!(e>=n[0]&&e<n[o]))return-1;let a=0,s=o-1;for(;a<s;){let l=a+s+1>>1;n[l]<=e?a=l:s=l-1}return a}function Ye(e){let n=Array.from(e).filter(r=>!Number.isNaN(r)).sort((r,o)=>r-o),t=n.length;if(t<1)throw new RangeError("'x' must have 1 or more non-missing values");return r=>{if(Number.isNaN(r))return NaN;let o=0,a=t;for(;o<a;){let s=o+a>>1;n[s]<=r?o=s+1:a=s}return o/t}}var U=e=>e===null||typeof e=="number"&&Number.isNaN(e),en=(e,n)=>typeof e=="number"?e-n:typeof e=="string"?H(e,n):Number(e)-Number(n),nn=e=>typeof e=="boolean"?e?"TRUE":"FALSE":typeof e=="string"?e:Number.isFinite(e)?_(e):e>0?"Inf":"-Inf";function tn(e,n){let t=X(n),r=new Set(t.filter(l=>!U(l)).map(l=>typeof l));if(r.size>1)throw new TypeError(`${e} mixes ${[...r].join(" and ")} labels`);let o=[...r][0];if(o!==void 0&&o!=="number"&&o!=="string"&&o!=="boolean")throw new TypeError(`${e} has labels of an unsupported type`);let a=[...new Set(t.filter(l=>!U(l)))].sort(en),s=new Map(a.map((l,f)=>[l,f]));return{levels:a,codes:t.map(l=>U(l)?-1:s.get(l))}}var j=e=>Array.isArray(e)||e instanceof Float64Array;function ie(e){if(e instanceof Float64Array)return[e];if(!Array.isArray(e))throw new TypeError("the group labels must be an array or an array of arrays");if(e.length>0&&e.every(j))return[...e];if(e.some(j))throw new TypeError("the group labels must be an array or an array of arrays");return[e]}function Z(e,n,t){e.forEach((s,l)=>{if(s.length!==t)throw new RangeError(`${n[l]} must have one label per row (${t}), not ${s.length}`)});let r=e.map((s,l)=>tn(n[l],s)),o=r.reduce((s,l)=>s*l.levels.length,1),a=new Array(t);for(let s=0;s<t;s++){let l=0,f=1;for(let i of r){let h=i.codes[s];if(h<0){l=-1;break}l+=h*f,f*=i.levels.length}a[s]=l}return{factors:r,size:o,cells:a}}function D(e){let n=Array.from({length:e.size},()=>[]);return e.cells.forEach((t,r)=>{t>=0&&n[t].push(r)}),n}function rn(e,n){return e.factors.map(t=>{let r=t.levels.length,o=t.levels[n%r];return n=Math.floor(n/r),o})}var ce=(e,n)=>Array.from({length:e},(t,r)=>e===1?n:`${n}[${r}]`);function on(e,n,t){let r=Array.from(e),o=ie(n),a=Z(o,ce(o.length,"index"),r.length),s=D(a).map(i=>i.length===0?null:{value:t(i.map(h=>r[h]))}),f=s.every(i=>i===null||typeof i.value=="number")?s.map(i=>i===null?NaN:i.value):s.map(i=>i===null?null:i.value);return{dim:a.factors.map(i=>i.levels.length),dimnames:a.factors.map(i=>[...i.levels]),values:f}}function sn(e,n){let t={};for(let[r,o]of Object.entries(e))t[r]=o instanceof Float64Array?Float64Array.from(n,a=>o[a]):n.map(a=>o[a]);return t}function ln(e){let t=Object.values(e)[0]?.length??0;for(let[r,o]of Object.entries(e)){if(!j(o))throw new TypeError(`column '${r}' must be an array`);if(o.length!==t)throw new RangeError(`the columns of the data frame have different lengths (found for '${r}')`)}return t}function an(e,n,t={}){let r=t.sep??".",o=ie(n),a=!j(e);if(a&&(typeof e!="object"||e===null))throw new TypeError("x must be an array or a data frame");let s=a?ln(e):e.length,l=Z(o,ce(o.length,"f"),s),f=a?[]:Array.from(e),i=new Map;return D(l).forEach((h,m)=>{if(h.length===0&&(t.drop||l.factors.length===1))return;let y=rn(l,m).map(nn).join(r);i.set(y,a?sn(e,h):h.map(g=>f[g]))}),i}function se(e,n){if(n===void 0)throw new Error(`object '${e}' not found`);if(!j(n))throw new TypeError(`column '${e}' must be an array`);return Array.from(n,t=>{if(t===null)return NaN;if(typeof t!="number")throw new TypeError(`column '${e}' to summarise must be numeric`);return t})}function le(e,n){let t=Object.prototype.hasOwnProperty.call(e,n)?e[n]:void 0;if(t===void 0)throw new Error(`object '${n}' not found`);if(!j(t))throw new TypeError(`column '${n}' must be an array`);return t}function cn(e,n){if(typeof e!="string")throw new TypeError("the formula must be a string");let t=e.indexOf("~");if(t<0)throw new SyntaxError("invalid formula: missing '~'");let r=e.slice(0,t).trim(),{rhs:o}=G(`~ ${e.slice(t+1)}`),a=/^[A-Za-z.][A-Za-z0-9._]*$/,s;if(r!=="."){let i=/^cbind\s*\((.*)\)$/s.exec(r);if(s=i===null?[r]:i[1].split(",").map(h=>h.trim()),s.some(h=>!a.test(h)||h==="."))throw new SyntaxError("invalid formula: the left-hand side must be a variable, cbind(...) of variables or '.'")}else if(I(o,[]).length<I(o,["."]).length)throw new SyntaxError("invalid formula: '.' cannot be on both sides");let l=s===void 0?[]:n.filter(i=>!s.includes(i)),f=I(o,l);return J(o,()=>0,l),{values:s??n.filter(i=>!f.includes(i)),groups:f}}function ae(e,n,t,r,o,a){let s=r[0]?.length??n[0]?.length??0;e.forEach((h,m)=>{if(n[m].length!==s)throw new RangeError(`variable lengths differ (found for '${h}')`)});let l=Z(r,t,s),f=D(l).map((h,m)=>({cell:m,rows:h.filter(o)})).filter(h=>h.rows.length>0),i={};return t.forEach((h,m)=>{let y=r[m];i[h]=f.map(g=>{let d=y[g.rows[0]];return d===0?0:d})}),e.forEach((h,m)=>{let y=n[m],d=f.map(b=>a(b.rows.map(w=>y[w]))).map(b=>{if(typeof b=="number")return{names:void 0,values:[b]};if(Array.isArray(b))return{names:void 0,values:[...b]};let w=b;return{names:Object.keys(w),values:Object.values(w)}}),x=d[0]?.values.length??1;if(d.some(b=>b.values.length!==x))throw new RangeError("fun must return values of the same length for every group");if(x===1&&d[0]?.names===void 0){i[h]=d.map(b=>b.values[0]);return}for(let b=0;b<x;b++){let w=d[0].names?.[b]??String(b+1);i[`${h}.${w}`]=d.map(c=>c.values[b])}}),i}function un(e,n,t){if(typeof n!="object"||n===null)throw new TypeError("the data must be an object of columns");if(typeof e=="string"){let l=n,{values:f,groups:i}=cn(e,Object.keys(l)),h=d=>Object.prototype.hasOwnProperty.call(l,d),m=f.map(d=>se(d,h(d)?l[d]:void 0)),y=i.map(d=>le(l,d));return ae(f,m,i,y,d=>m.every(x=>!Number.isNaN(x[d]))&&y.every(x=>!U(x[d])),t)}if(typeof e!="object"||e===null)throw new TypeError("x must be a data frame");let r=Object.keys(e),o=r.map(l=>se(l,e[l])),a=Object.keys(n);if(a.length===0)throw new RangeError("'by' must name at least one grouping column");let s=a.map(l=>le(n,l));return ae(r,o,a,s,()=>!0,t)}var q=e=>typeof e=="string"?[e]:[...e];function fe(e,n){for(let t of n)if(!Object.hasOwn(e,t))throw new Error(`Column \`${t}\` doesn't exist.`)}function fn(e,n){if(n instanceof Float64Array)return"double";let t=null;for(let r of n){if(r===null)continue;let o=typeof r=="number"?"double":typeof r=="string"?"character":"logical";if(t!==null&&o!==t)throw new TypeError(`column '${e}' mixes ${t} and ${o} values`);t=o}return t}function mn(e,n){let t=null;for(let r of n){let o=fn(r,e[r]);if(o!==null){if(t===null)t={type:o,column:r};else if(t.type!==o){if(t.type==="character"||o==="character")throw new TypeError(`Can't combine \`${t.column}\` <${t.type}> and \`${r}\` <${o}>.`);o==="double"&&(t={type:o,column:r})}}}return t?.type??null}function hn(e,n,t){let r=new RegExp(`${n}|`).exec("").length-1;if(r!==t)throw new Error(`\`regex\` should define ${t} groups; ${r} found.`);let o=new RegExp(n);return e.map(a=>{let s=o.exec(a);return Array.from({length:t},(l,f)=>s===null?null:s[f+1]??null)})}function pn(e,n,t,r){let o=new RegExp(n,"g"),a=[],s=[],l=e.map((i,h)=>{let m=[],y=0;for(let g of i.matchAll(o))g[0].length!==0&&(m.push(i.slice(y,g.index)),y=g.index+g[0].length);return m.push(i.slice(y)),m.length>t&&a.push(h+1),m.length<t&&s.push(h+1),Array.from({length:t},(g,d)=>m[d]??null)}),f=i=>`${i.length} rows [${i.join(", ")}]`;return a.length>0&&r.push(`Expected ${t} pieces. Additional pieces discarded in ${f(a)}.`),s.length>0&&r.push(`Expected ${t} pieces. Missing pieces filled with \`NA\` in ${f(s)}.`),l}function gn(e,n,t,r){let{namesSep:o,namesPattern:a}=t;if(n.length===1){if(o!==void 0)throw new Error("`names_sep` can't be used with a length 1 `names_to`.");if(a===void 0)return e.map(l=>[l]);let s=new RegExp(a);return e.map(l=>[s.exec(l)?.[1]??null])}if(o===void 0==(a===void 0))throw new Error("If you supply multiple names in `names_to` you must also supply one of `names_sep` or `names_pattern`.");return o!==void 0?pn(e,o,n.length,r):hn(e,a,n.length)}function dn(e,n){let t=V(e),r=Object.keys(e),o=typeof n.cols=="function"?r.filter(n.cols):[...new Set(n.cols)];if(fe(e,o),o.length===0)throw new Error("`cols` must select at least one column.");let a=q(n.namesTo??"name"),s=a.indexOf(".value");if(a.lastIndexOf(".value")!==s)throw new Error('`names_to` may contain ".value" only once');let l=[],f=n.namesPrefix===void 0?null:new RegExp(`^(?:${n.namesPrefix})`),i=o.map(c=>f===null?c:c.replace(f,"")),h=gn(i,a,n,l),m=[],y=[];if(s<0)m.push(...h),y.push({name:n.valuesTo??"value",sources:[...o]});else{let c=new Map,u=new Map;h.forEach((p,N)=>{let E=p.filter((A,O)=>O!==s),$=JSON.stringify(E),M=c.get($);if(M===void 0){M=m.length,c.set($,M),m.push(E);for(let A of y)A.sources.push(null)}let v=p[s]??"NA",R=u.get(v);if(R===void 0&&(R={name:v,sources:m.map(()=>null)},u.set(v,R),y.push(R)),R.sources[M]!==null)throw new Error(`the names give the value column '${v}' twice for one key`);R.sources[M]=o[N]})}let g=y.map(c=>mn(e,c.sources.filter(u=>u!==null))),d=[];if(n.colsVary==="slowest")for(let c=0;c<m.length;c++)for(let u=0;u<t;u++)d.push([u,c]);else for(let c=0;c<t;c++)for(let u=0;u<m.length;u++)d.push([c,u]);let x=(c,u,p)=>{let N=c.sources[p];return N==null?null:e[N][u]};n.valuesDropNa===!0&&(d=d.filter(([c,u])=>y.some(p=>!L(x(p,c,u)))));let b={},w=d.map(([c])=>c);for(let c of r)o.includes(c)||k(b,c,z(e[c],w));if(a.forEach((c,u)=>{if(u===s)return;let p=n.namesTransform?.[c],N=E=>{let $=m[E][u<s||s<0?u:u-1];return $===null||p===void 0?$:p($)};k(b,c,d.map(([,E])=>N(E)))}),y.forEach((c,u)=>{let p=g[u]==="double";if(p&&c.sources.every(N=>N!==null&&e[N]instanceof Float64Array))k(b,c.name,Float64Array.from(d,([N,E])=>x(c,N,E)));else{let N=d.map(([E,$])=>{let M=x(c,E,$);return p&&typeof M=="boolean"?Number(M):M});k(b,c.name,N)}}),n.onWarning!==void 0)for(let c of new Set(l))n.onWarning(c);return b}var yn=e=>L(e)?"NA":typeof e=="number"?`n${e}`:typeof e=="string"?`s${e}`:`b${String(e)}`;function ue(e,n){let t=new Map,r=[];return{codes:Array.from({length:n},(a,s)=>{let l=JSON.stringify(e.map(i=>yn(i[s]))),f=t.get(l);return f===void 0&&(f=r.length,t.set(l,f),r.push(s)),f}),firsts:r}}function wn(e){return L(e)?"NA":typeof e=="number"?Number.isFinite(e)?_(e):e>0?"Inf":"-Inf":typeof e=="boolean"?e?"TRUE":"FALSE":e}function Q(e,n,t){if(e!==void 0)return n(e)?e:e[t]}function bn(e,n={}){let t=V(e),r=q(n.namesFrom??"name"),o=q(n.valuesFrom??"value"),a=n.idCols===void 0?Object.keys(e).filter(u=>!r.includes(u)&&!o.includes(u)):q(n.idCols);fe(e,[...a,...r,...o]);let s=n.namesPrefix??"",l=n.namesSep??"_",f=ue(a.map(u=>e[u]),t),i=ue(r.map(u=>e[u]),t),h=i.firsts.map((u,p)=>p);n.namesSort===!0&&h.sort((u,p)=>{for(let N of r){let E=K(e[N][i.firsts[u]],e[N][i.firsts[p]]);if(E!==0)return E}return 0});let m=i.firsts.map(u=>s+r.map(p=>wn(e[p][u])).join(l)),y=i.firsts.length,g=Array.from({length:f.firsts.length*y},()=>[]);for(let u=0;u<t;u++)g[f.codes[u]*y+i.codes[u]].push(u);let d=u=>typeof u=="function",x=u=>typeof u!="object"||u===null,b=(u,p)=>{let N=e[u],E=Q(n.valuesFn,d,u),$=Q(n.valuesFill,x,u)??null,M=f.firsts.map((v,R)=>{let A=g[R*y+p];return A.length===0?$:E!==void 0?E(A.map(O=>N[O])):N[A[0]]});return N instanceof Float64Array&&M.every(v=>v===null||typeof v=="number")?Float64Array.from(M,v=>v===null?NaN:v):M};for(let u of o)if(Q(n.valuesFn,d,u)===void 0&&g.some(p=>p.length>1))throw new Error(`values from '${u}' are not uniquely identified; use valuesFn to summarise them`);let w={};for(let u of a)k(w,u,z(e[u],f.firsts));let c=(u,p)=>o.length===1?m[p]:`${u}${l}${m[p]}`;if(n.namesVary==="slowest")for(let u of h)for(let p of o)k(w,c(p,u),b(p,u));else for(let u of o)for(let p of h)k(w,c(u,p),b(u,p));return w}export{W as IQR,un as aggregate,re as completeCases,pe as cor,he as cov,ve as cummax,Oe as cummin,$e as cumprod,xe as cumsum,Ue as cut,_e as cv,Re as diff,Ye as ecdf,qe as findInterval,Be as fivenum,ye as fromRows,je as geometricMean,He as hist,Fe as kurtosis,We as mad,Ee as mean,Te as meanAD,F as median,ge as merge,Se as modes,De as naOmit,dn as pivotLonger,bn as pivotWider,oe as pretty,Ne as prod,P as quantile,S as range,me as rank,Ie as scale,C as sd,ke as skewness,an as split,de as subset,be as sum,Ce as table,Le as tabulate,on as tapply,we as toRows,Pe as unique,Me as variance,Ae as weightedMean};
|
|
1
|
+
/*! rStats.js 0.15.3 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{l as me,m as he,n as pe}from"./chunks/chunk-5ITR67MB.js";import"./chunks/chunk-USPEBXSR.js";import{A as Fe,B as je,C as Se,D as Te,E as _e,F as Ie,a as V,b as L,c as z,d as k,e as K,f as ge,g as de,j as ye,k as we,l as Y,m as be,n as Ne,o as Ee,p as Ae,q as Me,r as C,s as F,t as S,u as xe,v as $e,w as ve,x as Oe,y as Re,z as ke}from"./chunks/chunk-JQG3YQOC.js";import{a as ee,b as Le,c as Ce,d as Pe,e as Ue,f as qe}from"./chunks/chunk-MAUAOQG3.js";import"./chunks/chunk-IUULP3LE.js";import"./chunks/chunk-3WPU6LGK.js";import{a as G,e as I,f as J}from"./chunks/chunk-3THFTB36.js";import"./chunks/chunk-FCNVTIPJ.js";import{a as H}from"./chunks/chunk-HD73UKPT.js";import{b as _,c as X}from"./chunks/chunk-VLUL2HFW.js";import"./chunks/chunk-3TSWYE5L.js";import"./chunks/chunk-KT66SB7R.js";var B=4*Number.EPSILON,Ve={4:[0,1],5:[.5,.5],6:[0,0],8:[1/3,1/3],9:[3/8,3/8]};function ze(e,n,t){let r=e.length,o=i=>e[Math.min(Math.max(i,1),r)-1];if(t===7){let i=1+(r-1)*n,h=Math.floor(i),m=o(h),y=o(Math.ceil(i));if(i>h&&y!==m){let g=i-h;return(1-g)*m+g*y}return m}let a,s;if(t<=3){let i=t===3?r*n-.5:r*n;a=Math.floor(i*(1+B)),t===1?s=i>a?1:0:t===2?s=i>a?1:.5:s=i!==a||Math.abs(a%2)===1?1:0}else{let[i,h]=Ve[t],m=i+n*(r+1-i-h);a=Math.floor(m*(1+B)),s=m-a,Math.abs(s)<B&&(s=0)}let l=o(a),f=o(a+1);return s===1?f:s>0&&s<1&&l!==f?(1-s)*l+s*f:l}function P(e,n=[0,.25,.5,.75,1],t={}){let r=t.type??7;if(!(Number.isInteger(r)&&r>=1&&r<=9))throw new RangeError("'type' must be an integer in 1..9");let o=Y(e,t.naRm);if(o===void 0)throw new RangeError("missing values and NaN's not allowed if 'na.rm' is FALSE");let a=o.sort((i,h)=>i-h),s=typeof n=="number"?[n]:Array.from(n),l=100*Number.EPSILON;if(s.some(i=>i<-l||i>1+l))throw new RangeError("'probs' outside [0,1]");let f=s.map(i=>Number.isNaN(i)||a.length===0?NaN:ze(a,Math.min(Math.max(i,0),1),r));return typeof n=="number"?f[0]:f}function W(e,n={}){let[t,r]=P(e,[.25,.75],n);return r-t}function Be(e){let n=Array.from(e).filter(a=>!Number.isNaN(a)).sort((a,s)=>a-s),t=n.length;if(t===0)return[NaN,NaN,NaN,NaN,NaN];let r=Math.floor((t+3)/2)/2;return[1,r,(t+1)/2,t+1-r,t].map(a=>.5*(n[Math.floor(a)-1]+n[Math.ceil(a)-1]))}function We(e,n={}){let t=Array.from(e),r=t.filter(m=>!Number.isNaN(m)),o=n.naRm?r:t,a=n.center??(o.length>r.length?NaN:F(r)),s=n.constant??1.4826,l=n.low??!1,f=n.high??!1,i=o.map(m=>Math.abs(m-a)),h=i.length;if((l||f)&&h%2===0){if(l&&f)throw new RangeError("'low' and 'high' cannot be both TRUE");let m=h/2+(f?1:0),y=i.filter(g=>!Number.isNaN(g)).sort((g,d)=>g-d);if(m===0||y.length<m)throw new RangeError(`'partial' index ${m} outside bounds`);return s*y[m-1]}return s*F(i)}var ne=e=>e===null||typeof e=="number"&&Number.isNaN(e),te=e=>Array.isArray(e)||ArrayBuffer.isView(e);function Ze(e){if(te(e))return{columns:[e],rows:e.length};if(typeof e!="object"||e===null)throw new TypeError("the arguments must be arrays or data frames");let n=Object.values(e),t=n[0]?.length??0;if(n.some(r=>r.length!==t))throw new RangeError("not all arguments have the same length");return{columns:n,rows:t}}function re(...e){if(e.length===0)throw new RangeError("no input has determined the number of cases");let n=e.map(Ze),t=n[0].rows;if(n.some(o=>o.rows!==t))throw new RangeError("not all arguments have the same length");let r=n.flatMap(o=>o.columns);return Array.from({length:t},(o,a)=>r.every(s=>!ne(s[a])))}function De(e){if(te(e))return Array.from(e).filter(r=>!ne(r));let n=re(e),t={};for(let[r,o]of Object.entries(e)){let a=Array.from(o).filter((s,l)=>n[l]);t[r]=o instanceof Float64Array?Float64Array.from(a):a}return t}function oe(e,n={}){let t=Array.from(e).filter(Number.isFinite);if(t.length===0)return[];let r=n.n??5;if(Number.isNaN(r)||Math.abs(r)>=2**31||Math.trunc(r)<0)throw new RangeError("invalid 'n' argument");let o=Math.trunc(r),a=n.minN??Math.floor(r/3);if(Number.isNaN(a)||Math.trunc(a)<0||Math.trunc(a)>o)throw new RangeError("invalid 'min.n' argument");let s=Math.trunc(a),l=n.shrinkSml??.75,f=n.highUBias??1.5,i=n.u5Bias??.5+1.5*f,h=1/0,m=-1/0;for(let A of t)A<h&&(h=A),A>m&&(m=A);let y=m-h,g,d;if(y===0&&m===0)g=1,d=!0;else{g=Math.max(Math.abs(h),Math.abs(m));let O=(1+(i>=1.5*f+.5?1/(1+f):1.5/(1+i)))*Math.max(1,o)*Number.EPSILON;d=y<g*O*3}d?(g>10&&(g=9+g/10),g*=l,s>1&&(g/=s)):(g=o>1?y/o:y,y===1/0&&o>=2&&(g=m/o-h/o));let x=2**-20*Qe;g<x?g=x:g>Number.MAX_VALUE/1.25&&(g=Number.MAX_VALUE/1.25);let b=10**Math.floor(Math.log10(g)),w=[[2*b,f],[5*b,i],[10*b,f]],c=b;for(let[A,O]of w)if(A-g<O*(g-c))c=A;else break;let u=1e-10,p=Math.floor(h/c+u);for(;p*c>h+u*c;)p--;for(;!Number.isFinite(p*c);)p++;let N=Math.ceil(m/c-u);for(;N*c<m-u*c;)N++;for(;!Number.isFinite(N*c);)N--;let E=Math.trunc(.5+N-p);if(E<s){let A=s-E,O=Math.floor(A/2),T=A%2;h===0&&p===0&&m!==0?N+=A:m===0&&N===0&&h!==0?p-=A:p>=0?(N+=O,p-=O+T):(p-=O,N+=O+T),E=s}let $=p*c<h?p*c:h,M=N*c>m?N*c:m;if(E<=0)return[$];let v;if(Number.isFinite(M-$))v=ee($,M,E+1);else{let A=M/E-$/E;v=Array.from({length:E+1},(O,T)=>$+T*A),v[E]=M}let R=M/E-$/E;return v.map(A=>Math.abs(A)<1e-14*R?0:A)}var Qe=22250738585072014e-324;function Ge(e){return e===0||!Number.isFinite(e)?e:Number(e.toPrecision(5))}function Je(e,n){let t=e.length,[r,o]=S(e),a=o-r;switch(n.toLowerCase()){case"sturges":return Math.ceil(Math.log2(t)+1);case"scott":{let s=3.5*C(e)*t**-.3333333333333333;return s>0?Math.max(1,Math.ceil(a/s)):1}case"fd":case"freedman-diaconis":{let s=e.map(Ge),l=2*W(s);for(let f=1/8;l===0&&f>=1/512;f/=2){let[i,h]=P(s,[f,1-f]);l=(h-i)/(1-2*f)}return l===0&&(l=3.5*C(e)),l>0?Math.ceil(a/l*t**(1/3)):1}default:throw new RangeError("unknown 'breaks' algorithm")}}function Xe(e,n){let t=e.slice(1).map((r,o)=>r-e[o]);return e.length>5?F(t):e.length<=3?n:Math.min(...t.filter(r=>r>0))}function He(e,n={}){let t=Array.from(e).filter(Number.isFinite),r=t.length,o=n.right??!0,a=n.includeLowest??!0,s=n.breaks??"Sturges",l;if(typeof s!="string"&&typeof s!="number"&&s.length>1)l=[...s].sort((w,c)=>w-c);else{a=!0;let w=typeof s=="string"?Je(t,s):typeof s=="number"?s:s[0];if(!Number.isFinite(w)||w<1)throw new RangeError("invalid number of 'breaks'");w=Math.min(w,1e6);let[c,u]=S(t);l=oe([c,u],{n:w,minN:1});for(let p=1;p<l.length;p++)if(!(l[p]-l[p-1]>0))throw new RangeError("'breaks' are not strictly increasing")}let f=l.length,[i,h]=r>0?S(t):[0,0],m=1e-7*Xe(l,h-i),y=l.map((w,c)=>o?c===0&&a?w-m:w+m:c<f-1?w-m:a?w+m:w-m),g=new Array(Math.max(0,f-1)).fill(0),d=0;for(let w of t){let c=Ke(w,y,o,a);c>=0&&(g[c]++,d++)}if(d<r)throw new RangeError("some 'x' not counted; maybe 'breaks' do not span range of 'x'");let x=g.map((w,c)=>w/(r*(l[c+1]-l[c]))),b=g.map((w,c)=>.5*(l[c+1]+l[c]));return{breaks:l,counts:g,density:x,mids:b}}function Ke(e,n,t,r){let o=n.length-1;if(o<1)return-1;if(t){if(e===n[0]&&r)return 0;if(!(e>n[0]&&e<=n[o]))return-1;let l=1,f=o;for(;l<f;){let i=l+f>>1;e<=n[i]?f=i:l=i+1}return l-1}if(e===n[o]&&r)return o-1;if(!(e>=n[0]&&e<n[o]))return-1;let a=0,s=o-1;for(;a<s;){let l=a+s+1>>1;n[l]<=e?a=l:s=l-1}return a}function Ye(e){let n=Array.from(e).filter(r=>!Number.isNaN(r)).sort((r,o)=>r-o),t=n.length;if(t<1)throw new RangeError("'x' must have 1 or more non-missing values");return r=>{if(Number.isNaN(r))return NaN;let o=0,a=t;for(;o<a;){let s=o+a>>1;n[s]<=r?o=s+1:a=s}return o/t}}var U=e=>e===null||typeof e=="number"&&Number.isNaN(e),en=(e,n)=>typeof e=="number"?e-n:typeof e=="string"?H(e,n):Number(e)-Number(n),nn=e=>typeof e=="boolean"?e?"TRUE":"FALSE":typeof e=="string"?e:Number.isFinite(e)?_(e):e>0?"Inf":"-Inf";function tn(e,n){let t=X(n),r=new Set(t.filter(l=>!U(l)).map(l=>typeof l));if(r.size>1)throw new TypeError(`${e} mixes ${[...r].join(" and ")} labels`);let o=[...r][0];if(o!==void 0&&o!=="number"&&o!=="string"&&o!=="boolean")throw new TypeError(`${e} has labels of an unsupported type`);let a=[...new Set(t.filter(l=>!U(l)))].sort(en),s=new Map(a.map((l,f)=>[l,f]));return{levels:a,codes:t.map(l=>U(l)?-1:s.get(l))}}var j=e=>Array.isArray(e)||e instanceof Float64Array;function ie(e){if(e instanceof Float64Array)return[e];if(!Array.isArray(e))throw new TypeError("the group labels must be an array or an array of arrays");if(e.length>0&&e.every(j))return[...e];if(e.some(j))throw new TypeError("the group labels must be an array or an array of arrays");return[e]}function Z(e,n,t){e.forEach((s,l)=>{if(s.length!==t)throw new RangeError(`${n[l]} must have one label per row (${t}), not ${s.length}`)});let r=e.map((s,l)=>tn(n[l],s)),o=r.reduce((s,l)=>s*l.levels.length,1),a=new Array(t);for(let s=0;s<t;s++){let l=0,f=1;for(let i of r){let h=i.codes[s];if(h<0){l=-1;break}l+=h*f,f*=i.levels.length}a[s]=l}return{factors:r,size:o,cells:a}}function D(e){let n=Array.from({length:e.size},()=>[]);return e.cells.forEach((t,r)=>{t>=0&&n[t].push(r)}),n}function rn(e,n){return e.factors.map(t=>{let r=t.levels.length,o=t.levels[n%r];return n=Math.floor(n/r),o})}var ce=(e,n)=>Array.from({length:e},(t,r)=>e===1?n:`${n}[${r}]`);function on(e,n,t){let r=Array.from(e),o=ie(n),a=Z(o,ce(o.length,"index"),r.length),s=D(a).map(i=>i.length===0?null:{value:t(i.map(h=>r[h]))}),f=s.every(i=>i===null||typeof i.value=="number")?s.map(i=>i===null?NaN:i.value):s.map(i=>i===null?null:i.value);return{dim:a.factors.map(i=>i.levels.length),dimnames:a.factors.map(i=>[...i.levels]),values:f}}function sn(e,n){let t={};for(let[r,o]of Object.entries(e))t[r]=o instanceof Float64Array?Float64Array.from(n,a=>o[a]):n.map(a=>o[a]);return t}function ln(e){let t=Object.values(e)[0]?.length??0;for(let[r,o]of Object.entries(e)){if(!j(o))throw new TypeError(`column '${r}' must be an array`);if(o.length!==t)throw new RangeError(`the columns of the data frame have different lengths (found for '${r}')`)}return t}function an(e,n,t={}){let r=t.sep??".",o=ie(n),a=!j(e);if(a&&(typeof e!="object"||e===null))throw new TypeError("x must be an array or a data frame");let s=a?ln(e):e.length,l=Z(o,ce(o.length,"f"),s),f=a?[]:Array.from(e),i=new Map;return D(l).forEach((h,m)=>{if(h.length===0&&(t.drop||l.factors.length===1))return;let y=rn(l,m).map(nn).join(r);i.set(y,a?sn(e,h):h.map(g=>f[g]))}),i}function se(e,n){if(n===void 0)throw new Error(`object '${e}' not found`);if(!j(n))throw new TypeError(`column '${e}' must be an array`);return Array.from(n,t=>{if(t===null)return NaN;if(typeof t!="number")throw new TypeError(`column '${e}' to summarise must be numeric`);return t})}function le(e,n){let t=Object.prototype.hasOwnProperty.call(e,n)?e[n]:void 0;if(t===void 0)throw new Error(`object '${n}' not found`);if(!j(t))throw new TypeError(`column '${n}' must be an array`);return t}function cn(e,n){if(typeof e!="string")throw new TypeError("the formula must be a string");let t=e.indexOf("~");if(t<0)throw new SyntaxError("invalid formula: missing '~'");let r=e.slice(0,t).trim(),{rhs:o}=G(`~ ${e.slice(t+1)}`),a=/^[A-Za-z.][A-Za-z0-9._]*$/,s;if(r!=="."){let i=/^cbind\s*\((.*)\)$/s.exec(r);if(s=i===null?[r]:i[1].split(",").map(h=>h.trim()),s.some(h=>!a.test(h)||h==="."))throw new SyntaxError("invalid formula: the left-hand side must be a variable, cbind(...) of variables or '.'")}else if(I(o,[]).length<I(o,["."]).length)throw new SyntaxError("invalid formula: '.' cannot be on both sides");let l=s===void 0?[]:n.filter(i=>!s.includes(i)),f=I(o,l);return J(o,()=>0,l),{values:s??n.filter(i=>!f.includes(i)),groups:f}}function ae(e,n,t,r,o,a){let s=r[0]?.length??n[0]?.length??0;e.forEach((h,m)=>{if(n[m].length!==s)throw new RangeError(`variable lengths differ (found for '${h}')`)});let l=Z(r,t,s),f=D(l).map((h,m)=>({cell:m,rows:h.filter(o)})).filter(h=>h.rows.length>0),i={};return t.forEach((h,m)=>{let y=r[m];i[h]=f.map(g=>{let d=y[g.rows[0]];return d===0?0:d})}),e.forEach((h,m)=>{let y=n[m],d=f.map(b=>a(b.rows.map(w=>y[w]))).map(b=>{if(typeof b=="number")return{names:void 0,values:[b]};if(Array.isArray(b))return{names:void 0,values:[...b]};let w=b;return{names:Object.keys(w),values:Object.values(w)}}),x=d[0]?.values.length??1;if(d.some(b=>b.values.length!==x))throw new RangeError("fun must return values of the same length for every group");if(x===1&&d[0]?.names===void 0){i[h]=d.map(b=>b.values[0]);return}for(let b=0;b<x;b++){let w=d[0].names?.[b]??String(b+1);i[`${h}.${w}`]=d.map(c=>c.values[b])}}),i}function un(e,n,t){if(typeof n!="object"||n===null)throw new TypeError("the data must be an object of columns");if(typeof e=="string"){let l=n,{values:f,groups:i}=cn(e,Object.keys(l)),h=d=>Object.prototype.hasOwnProperty.call(l,d),m=f.map(d=>se(d,h(d)?l[d]:void 0)),y=i.map(d=>le(l,d));return ae(f,m,i,y,d=>m.every(x=>!Number.isNaN(x[d]))&&y.every(x=>!U(x[d])),t)}if(typeof e!="object"||e===null)throw new TypeError("x must be a data frame");let r=Object.keys(e),o=r.map(l=>se(l,e[l])),a=Object.keys(n);if(a.length===0)throw new RangeError("'by' must name at least one grouping column");let s=a.map(l=>le(n,l));return ae(r,o,a,s,()=>!0,t)}var q=e=>typeof e=="string"?[e]:[...e];function fe(e,n){for(let t of n)if(!Object.hasOwn(e,t))throw new Error(`Column \`${t}\` doesn't exist.`)}function fn(e,n){if(n instanceof Float64Array)return"double";let t=null;for(let r of n){if(r===null)continue;let o=typeof r=="number"?"double":typeof r=="string"?"character":"logical";if(t!==null&&o!==t)throw new TypeError(`column '${e}' mixes ${t} and ${o} values`);t=o}return t}function mn(e,n){let t=null;for(let r of n){let o=fn(r,e[r]);if(o!==null){if(t===null)t={type:o,column:r};else if(t.type!==o){if(t.type==="character"||o==="character")throw new TypeError(`Can't combine \`${t.column}\` <${t.type}> and \`${r}\` <${o}>.`);o==="double"&&(t={type:o,column:r})}}}return t?.type??null}function hn(e,n,t){let r=new RegExp(`${n}|`).exec("").length-1;if(r!==t)throw new Error(`\`regex\` should define ${t} groups; ${r} found.`);let o=new RegExp(n);return e.map(a=>{let s=o.exec(a);return Array.from({length:t},(l,f)=>s===null?null:s[f+1]??null)})}function pn(e,n,t,r){let o=new RegExp(n,"g"),a=[],s=[],l=e.map((i,h)=>{let m=[],y=0;for(let g of i.matchAll(o))g[0].length!==0&&(m.push(i.slice(y,g.index)),y=g.index+g[0].length);return m.push(i.slice(y)),m.length>t&&a.push(h+1),m.length<t&&s.push(h+1),Array.from({length:t},(g,d)=>m[d]??null)}),f=i=>`${i.length} rows [${i.join(", ")}]`;return a.length>0&&r.push(`Expected ${t} pieces. Additional pieces discarded in ${f(a)}.`),s.length>0&&r.push(`Expected ${t} pieces. Missing pieces filled with \`NA\` in ${f(s)}.`),l}function gn(e,n,t,r){let{namesSep:o,namesPattern:a}=t;if(n.length===1){if(o!==void 0)throw new Error("`names_sep` can't be used with a length 1 `names_to`.");if(a===void 0)return e.map(l=>[l]);let s=new RegExp(a);return e.map(l=>[s.exec(l)?.[1]??null])}if(o===void 0==(a===void 0))throw new Error("If you supply multiple names in `names_to` you must also supply one of `names_sep` or `names_pattern`.");return o!==void 0?pn(e,o,n.length,r):hn(e,a,n.length)}function dn(e,n){let t=V(e),r=Object.keys(e),o=typeof n.cols=="function"?r.filter(n.cols):[...new Set(n.cols)];if(fe(e,o),o.length===0)throw new Error("`cols` must select at least one column.");let a=q(n.namesTo??"name"),s=a.indexOf(".value");if(a.lastIndexOf(".value")!==s)throw new Error('`names_to` may contain ".value" only once');let l=[],f=n.namesPrefix===void 0?null:new RegExp(`^(?:${n.namesPrefix})`),i=o.map(c=>f===null?c:c.replace(f,"")),h=gn(i,a,n,l),m=[],y=[];if(s<0)m.push(...h),y.push({name:n.valuesTo??"value",sources:[...o]});else{let c=new Map,u=new Map;h.forEach((p,N)=>{let E=p.filter((A,O)=>O!==s),$=JSON.stringify(E),M=c.get($);if(M===void 0){M=m.length,c.set($,M),m.push(E);for(let A of y)A.sources.push(null)}let v=p[s]??"NA",R=u.get(v);if(R===void 0&&(R={name:v,sources:m.map(()=>null)},u.set(v,R),y.push(R)),R.sources[M]!==null)throw new Error(`the names give the value column '${v}' twice for one key`);R.sources[M]=o[N]})}let g=y.map(c=>mn(e,c.sources.filter(u=>u!==null))),d=[];if(n.colsVary==="slowest")for(let c=0;c<m.length;c++)for(let u=0;u<t;u++)d.push([u,c]);else for(let c=0;c<t;c++)for(let u=0;u<m.length;u++)d.push([c,u]);let x=(c,u,p)=>{let N=c.sources[p];return N==null?null:e[N][u]};n.valuesDropNa===!0&&(d=d.filter(([c,u])=>y.some(p=>!L(x(p,c,u)))));let b={},w=d.map(([c])=>c);for(let c of r)o.includes(c)||k(b,c,z(e[c],w));if(a.forEach((c,u)=>{if(u===s)return;let p=n.namesTransform?.[c],N=E=>{let $=m[E][u<s||s<0?u:u-1];return $===null||p===void 0?$:p($)};k(b,c,d.map(([,E])=>N(E)))}),y.forEach((c,u)=>{let p=g[u]==="double";if(p&&c.sources.every(N=>N!==null&&e[N]instanceof Float64Array))k(b,c.name,Float64Array.from(d,([N,E])=>x(c,N,E)));else{let N=d.map(([E,$])=>{let M=x(c,E,$);return p&&typeof M=="boolean"?Number(M):M});k(b,c.name,N)}}),n.onWarning!==void 0)for(let c of new Set(l))n.onWarning(c);return b}var yn=e=>L(e)?"NA":typeof e=="number"?`n${e}`:typeof e=="string"?`s${e}`:`b${String(e)}`;function ue(e,n){let t=new Map,r=[];return{codes:Array.from({length:n},(a,s)=>{let l=JSON.stringify(e.map(i=>yn(i[s]))),f=t.get(l);return f===void 0&&(f=r.length,t.set(l,f),r.push(s)),f}),firsts:r}}function wn(e){return L(e)?"NA":typeof e=="number"?Number.isFinite(e)?_(e):e>0?"Inf":"-Inf":typeof e=="boolean"?e?"TRUE":"FALSE":e}function Q(e,n,t){if(e!==void 0)return n(e)?e:e[t]}function bn(e,n={}){let t=V(e),r=q(n.namesFrom??"name"),o=q(n.valuesFrom??"value"),a=n.idCols===void 0?Object.keys(e).filter(u=>!r.includes(u)&&!o.includes(u)):q(n.idCols);fe(e,[...a,...r,...o]);let s=n.namesPrefix??"",l=n.namesSep??"_",f=ue(a.map(u=>e[u]),t),i=ue(r.map(u=>e[u]),t),h=i.firsts.map((u,p)=>p);n.namesSort===!0&&h.sort((u,p)=>{for(let N of r){let E=K(e[N][i.firsts[u]],e[N][i.firsts[p]]);if(E!==0)return E}return 0});let m=i.firsts.map(u=>s+r.map(p=>wn(e[p][u])).join(l)),y=i.firsts.length,g=Array.from({length:f.firsts.length*y},()=>[]);for(let u=0;u<t;u++)g[f.codes[u]*y+i.codes[u]].push(u);let d=u=>typeof u=="function",x=u=>typeof u!="object"||u===null,b=(u,p)=>{let N=e[u],E=Q(n.valuesFn,d,u),$=Q(n.valuesFill,x,u)??null,M=f.firsts.map((v,R)=>{let A=g[R*y+p];return A.length===0?$:E!==void 0?E(A.map(O=>N[O])):N[A[0]]});return N instanceof Float64Array&&M.every(v=>v===null||typeof v=="number")?Float64Array.from(M,v=>v===null?NaN:v):M};for(let u of o)if(Q(n.valuesFn,d,u)===void 0&&g.some(p=>p.length>1))throw new Error(`values from '${u}' are not uniquely identified; use valuesFn to summarise them`);let w={};for(let u of a)k(w,u,z(e[u],f.firsts));let c=(u,p)=>o.length===1?m[p]:`${u}${l}${m[p]}`;if(n.namesVary==="slowest")for(let u of h)for(let p of o)k(w,c(p,u),b(p,u));else for(let u of o)for(let p of h)k(w,c(u,p),b(u,p));return w}export{W as IQR,un as aggregate,re as completeCases,pe as cor,he as cov,ve as cummax,Oe as cummin,$e as cumprod,xe as cumsum,Ue as cut,_e as cv,Re as diff,Ye as ecdf,qe as findInterval,Be as fivenum,ye as fromRows,je as geometricMean,He as hist,Fe as kurtosis,We as mad,Ee as mean,Te as meanAD,F as median,ge as merge,Se as modes,De as naOmit,dn as pivotLonger,bn as pivotWider,oe as pretty,Ne as prod,P as quantile,S as range,me as rank,Ie as scale,C as sd,ke as skewness,an as split,de as subset,be as sum,Ce as table,Le as tabulate,on as tapply,we as toRows,Pe as unique,Me as variance,Ae as weightedMean};
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
// (the beta(1/2, 1/2) distribution is symmetric).
|
|
9
9
|
import { vectorize } from "../internal/vectorize.js";
|
|
10
10
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
11
|
-
import { ddAdd, ddLog, twoSum } from "../special/dd.js";
|
|
11
|
+
import { ddAdd, ddLog, ddMulNum, ddSub, twoSum } from "../special/dd.js";
|
|
12
|
+
import { ddExp } from "../internal/ddexp.js";
|
|
12
13
|
import { pbeta1, qbeta1 } from "./beta.js";
|
|
13
14
|
const valid = (a, b) => Number.isFinite(a) && Number.isFinite(b) && a < b;
|
|
14
15
|
export function darcsine1(x, a, b, log) {
|
|
@@ -26,6 +27,12 @@ export function darcsine1(x, a, b, log) {
|
|
|
26
27
|
return -LOGPI_DD[0] - 0.5 * l[0] - (LOGPI_DD[1] + 0.5 * l[1]);
|
|
27
28
|
}
|
|
28
29
|
const LOGPI_DD = [1.1447298858494002, 1.0265951162707826e-17];
|
|
30
|
+
/** log(2 / pi) and log(pi / 2) in double-double */
|
|
31
|
+
const LOG_2_OVER_PI_DD = [Math.LN2 - LOGPI_DD[0], 2.3190468138462996e-17 - LOGPI_DD[1]];
|
|
32
|
+
const LOG_HALF_PI_DD = [-LOG_2_OVER_PI_DD[0], -LOG_2_OVER_PI_DD[1]];
|
|
33
|
+
/** Below this z, asin(sqrt(z)) is sqrt(z) to double precision (the next term is z/6 relative) */
|
|
34
|
+
const TINY = 1e-20;
|
|
35
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
29
36
|
export function parcsine1(q, a, b, lowerTail, logP) {
|
|
30
37
|
if (Number.isNaN(q) || !valid(a, b))
|
|
31
38
|
return NaN;
|
|
@@ -34,9 +41,23 @@ export function parcsine1(q, a, b, lowerTail, logP) {
|
|
|
34
41
|
if (q >= b)
|
|
35
42
|
return dt1(lowerTail, logP);
|
|
36
43
|
const w = b - a;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
const nearA = q - a <= b - q;
|
|
45
|
+
// distance to the nearer end, and the tail on that side
|
|
46
|
+
const d = nearA ? q - a : b - q;
|
|
47
|
+
const z = d / w;
|
|
48
|
+
if (z < TINY) {
|
|
49
|
+
// (2/pi) asin(sqrt(z)) = (2/pi) sqrt(z) (1 + z/6 + ...): sqrt(d) / sqrt(w), and its log from log d - log w, keep the
|
|
50
|
+
// digits where z is subnormal or 0
|
|
51
|
+
const l = ddAdd(LOG_2_OVER_PI_DD, ddMulNum(ddSub(ddLog([d, 0]), ddLog([w, 0])), 0.5));
|
|
52
|
+
const sq = (Math.sqrt(d) / Math.sqrt(w)) * (2 / Math.PI);
|
|
53
|
+
const near = sq >= MIN_NORMAL ? sq : Math.exp(l[0]);
|
|
54
|
+
if (lowerTail === nearA)
|
|
55
|
+
return logP ? l[0] + l[1] : near;
|
|
56
|
+
return logP ? Math.log1p(-near) : 1 - near;
|
|
57
|
+
}
|
|
58
|
+
if (nearA)
|
|
59
|
+
return pbeta1(z, 0.5, 0.5, lowerTail, logP);
|
|
60
|
+
return pbeta1(z, 0.5, 0.5, !lowerTail, logP);
|
|
40
61
|
}
|
|
41
62
|
export function qarcsine1(p, a, b, lowerTail, logP) {
|
|
42
63
|
if (Number.isNaN(p) || !valid(a, b))
|
|
@@ -44,6 +65,29 @@ export function qarcsine1(p, a, b, lowerTail, logP) {
|
|
|
44
65
|
const bd = quantileBoundaries(p, a, b, lowerTail, logP);
|
|
45
66
|
if (bd !== undefined)
|
|
46
67
|
return bd;
|
|
68
|
+
// Where the lower or upper probability is below e^-40, the distance to that end is w sin^2(pi p / 2) = w (pi p / 2)^2
|
|
69
|
+
// (1 - O(p^2)), from logs (sin^2 underflowed: qarcsine(1.7e-208, 0, 2.6e187) was 0, not 1.9e-228). The log of a
|
|
70
|
+
// tiny probability in double-double from its linear value (log p rounded loses eps |log p| relative); 1 - p is exact
|
|
71
|
+
// for p >= 1/2
|
|
72
|
+
const given = logP ? [p, 0] : ddLog([p, 0]);
|
|
73
|
+
const other = logP
|
|
74
|
+
? p > -Math.LN2
|
|
75
|
+
? ddLog([-Math.expm1(p), 0])
|
|
76
|
+
: [Math.log1p(-Math.exp(p)), 0]
|
|
77
|
+
: p >= 0.5
|
|
78
|
+
? ddLog([1 - p, 0])
|
|
79
|
+
: [Math.log1p(-p), 0];
|
|
80
|
+
const [lower, upper] = lowerTail ? [given, other] : [other, given];
|
|
81
|
+
for (const [lt, fromA] of [
|
|
82
|
+
[lower, true],
|
|
83
|
+
[upper, false],
|
|
84
|
+
]) {
|
|
85
|
+
if (!(lt[0] < -40))
|
|
86
|
+
continue;
|
|
87
|
+
const e = ddAdd(ddMulNum(ddAdd(lt, LOG_HALF_PI_DD), 2), ddLog([b - a, 0]));
|
|
88
|
+
const dist = e[0] < -746 ? 0 : ddExp(e)[0];
|
|
89
|
+
return fromA ? Math.min(a + dist, b) : Math.max(b - dist, a);
|
|
90
|
+
}
|
|
47
91
|
const z = qbeta1(p, 0.5, 0.5, lowerTail, logP);
|
|
48
92
|
if (z <= 0.5)
|
|
49
93
|
return a + (b - a) * z;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Vectorizable, type VectorResult } from "../internal/vectorize.js";
|
|
2
2
|
import { type DensityOptions, type TailOptions } from "../internal/dpq.js";
|
|
3
3
|
import type { NoncentralOption } from "../internal/noncentral.js";
|
|
4
|
-
/** Log density (0 <
|
|
4
|
+
/** Log density (0 < a, b < Inf; also at x = 0 or 1, which a quantile search in log x reaches by underflow, as dbeta) */
|
|
5
5
|
export declare function dbetaLog(x: number, _xc: number, a: number, b: number): number;
|
|
6
6
|
export declare function dbeta1(x: number, a: number, b: number, log: boolean): number;
|
|
7
7
|
export declare function pbeta1(q: number, a: number, b: number, lowerTail: boolean, logP: boolean): number;
|
|
@@ -13,6 +13,7 @@ import { dbinomExponent } from "../special/loader.js";
|
|
|
13
13
|
import { ddAdd, ddLog, ddMul, ddNeg, twoSum } from "../special/dd.js";
|
|
14
14
|
import { dnbeta1, pnbeta1, qnbeta1 } from "./nbeta.js";
|
|
15
15
|
import { qnorm } from "./normal.js";
|
|
16
|
+
import { qgammaStd } from "./gamma.js";
|
|
16
17
|
/** Form x and 1 - x as a double-double pair based on the smaller of the two (no rounding of 1 - x brought in) */
|
|
17
18
|
function pair(x) {
|
|
18
19
|
return x <= 0.5 ? [[x, 0], twoSum(1, -x)] : [twoSum(1, -(1 - x)), [1 - x, 0]];
|
|
@@ -25,7 +26,15 @@ function pair(x) {
|
|
|
25
26
|
*/
|
|
26
27
|
function dbetaCore(x, a, b, log) {
|
|
27
28
|
const [xd, xcd] = pair(x);
|
|
29
|
+
if ((a + b) * Math.min(xd[0], xcd[0]) < 2.2250738585072014e-308) {
|
|
30
|
+
const l = dbetaDirectLog(xd, xcd, a, b);
|
|
31
|
+
return log ? l : Math.exp(l);
|
|
32
|
+
}
|
|
28
33
|
const { e } = dbinomExponent(a, b, xd, xcd);
|
|
34
|
+
// (a deviance beyond the doubles, from a shape near the largest double: the density is 0, where the double-double sums
|
|
35
|
+
// below were NaN: dbeta(0.998, 55.1, 6.3e307))
|
|
36
|
+
if (e[0] === Infinity)
|
|
37
|
+
return d0(log);
|
|
29
38
|
// With m = ab/(a+b), log(denom)/2 = log(2 pi m)/2 and log(ab/(a+b)) - log(denom)/2 = log(m/(2 pi))/2
|
|
30
39
|
const m = (a * b) / (a + b);
|
|
31
40
|
if (!log && e[0] < 700 && x > 1e-290 && xcd[0] > 1e-290) {
|
|
@@ -59,8 +68,20 @@ function dbetaShape1Log(x, a, b) {
|
|
|
59
68
|
const t = ddAdd(ddLog([s, 0]), ddMul(twoSum(s, -1), ddLog(l)));
|
|
60
69
|
return t[0] + t[1];
|
|
61
70
|
}
|
|
62
|
-
/**
|
|
71
|
+
/**
|
|
72
|
+
* log f = (a - 1) log x + (b - 1) log(1 - x) - log B(a, b) directly, in double-double, for (a + b) x or (a + b)(1 - x)
|
|
73
|
+
* below the normal range: the deviances of dbinomExponent take the log of that product, which has then lost its
|
|
74
|
+
* digits or underflowed to 0 (dbeta(2.7e-289, 7.5e-126, 7.6e-180) threw a TypeError). Such products need tiny shapes
|
|
75
|
+
* or x, where the terms are of moderate size and Loader's form brings nothing
|
|
76
|
+
*/
|
|
77
|
+
function dbetaDirectLog(xd, xcd, a, b) {
|
|
78
|
+
const t = ddAdd(ddMul(twoSum(a, -1), ddLog(xd)), ddMul(twoSum(b, -1), ddLog(xcd)));
|
|
79
|
+
return t[0] + (t[1] - lbeta(a, b));
|
|
80
|
+
}
|
|
81
|
+
/** Log density (0 < a, b < Inf; also at x = 0 or 1, which a quantile search in log x reaches by underflow, as dbeta) */
|
|
63
82
|
export function dbetaLog(x, _xc, a, b) {
|
|
83
|
+
if (!(x > 0 && x < 1))
|
|
84
|
+
return dbeta1(x, a, b, true);
|
|
64
85
|
return dbetaCore(x, a, b, true);
|
|
65
86
|
}
|
|
66
87
|
export function dbeta1(x, a, b, log) {
|
|
@@ -143,6 +164,41 @@ export function betaQuantileGuess(a, b, lower, logTarget) {
|
|
|
143
164
|
const x = a / (a + b * Math.exp(2 * w));
|
|
144
165
|
return x > 0 && x < 1 ? x : undefined;
|
|
145
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* The quantile where the relative spread of Beta(a, b) about its mean m = a/(a + b) (the coefficient of variation
|
|
169
|
+
* sqrt(b / (a (a + b + 1)))) is below 1e-15: the distribution function then rises from 0 to 1 within a few dozen ulps
|
|
170
|
+
* of m, and Newton's method on its log fails where the rise is within an ulp (qbeta(1.47e-15, 1.69e160, 1.62e175,
|
|
171
|
+
* { lowerTail: false }) returned 0, qbeta(exp(-226.4), 2.7e43, 8.3e95, { logP: true }) 2.7e-20 for m = 3.3e-53,
|
|
172
|
+
* qbeta(0.3, 1.3e32, 1.3e32) 0.49999998; measured to work down to a spread of 2e-16). Bisects the doubles in
|
|
173
|
+
* m (1 ± 1e-13) (100 times the spread) for the smallest x with I_x(a, b) >= target (lower) or 1 - I_x(a, b) <= target;
|
|
174
|
+
* undefined when the spread is larger or the target is not bracketed there
|
|
175
|
+
*/
|
|
176
|
+
function stepQuantile(a, b, lower, logTarget) {
|
|
177
|
+
const s = Math.min(a, b);
|
|
178
|
+
const l = Math.max(a, b);
|
|
179
|
+
// log of the coefficient of variation (a + b + 1 as l (1 + s/l) to the rounding, without overflow)
|
|
180
|
+
const lcv = 0.5 * (Math.log(b) - Math.log(a) - Math.log(l) - Math.log1p(s / l));
|
|
181
|
+
if (!(lcv < Math.log(1e-15)))
|
|
182
|
+
return undefined;
|
|
183
|
+
const ok = (x) => {
|
|
184
|
+
const v = betaInc(a, b, x, 1 - x, lower, true);
|
|
185
|
+
return lower ? v >= logTarget : v <= logTarget;
|
|
186
|
+
};
|
|
187
|
+
const m = 1 / (1 + b / a);
|
|
188
|
+
let lo = m * (1 - 1e-13);
|
|
189
|
+
let hi = m * (1 + 1e-13);
|
|
190
|
+
if (ok(lo) || !ok(hi))
|
|
191
|
+
return undefined;
|
|
192
|
+
for (;;) {
|
|
193
|
+
const mid = lo + (hi - lo) / 2;
|
|
194
|
+
if (mid === lo || mid === hi)
|
|
195
|
+
return hi;
|
|
196
|
+
if (ok(mid))
|
|
197
|
+
hi = mid;
|
|
198
|
+
else
|
|
199
|
+
lo = mid;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
146
202
|
/**
|
|
147
203
|
* Solve for the quantile in frame (a, b) when the answer lies in (0, 1/2].
|
|
148
204
|
* I_x(a, b) = target if lower, otherwise 1 - I_x(a, b) = target.
|
|
@@ -152,6 +208,9 @@ function solveLeftHalf(a, b, lower, logTarget, linearTarget) {
|
|
|
152
208
|
const atMin = betaInc(a, b, Number.MIN_VALUE, 1, lower, true);
|
|
153
209
|
if (lower ? atMin >= logTarget : atMin <= logTarget)
|
|
154
210
|
return 0;
|
|
211
|
+
const step = stepQuantile(a, b, lower, logTarget);
|
|
212
|
+
if (step !== undefined)
|
|
213
|
+
return step;
|
|
155
214
|
// Starting value: near 0, the tail approximation I_x(a, b) ≈ x^a / (a B(a, b)); otherwise the A&S 26.5.22 approximation
|
|
156
215
|
let x0 = 0.25;
|
|
157
216
|
let tail = Infinity;
|
|
@@ -165,6 +224,14 @@ function solveLeftHalf(a, b, lower, logTarget, linearTarget) {
|
|
|
165
224
|
if (g !== undefined && g < 0.5)
|
|
166
225
|
x0 = g;
|
|
167
226
|
}
|
|
227
|
+
if (b > 1e10 && b > 1e10 * a) {
|
|
228
|
+
// b X tends to Gamma(a): start from the gamma quantile. From 0.25 the log probability (about b log(3/4)) and the
|
|
229
|
+
// log density each round to 1e-16 of their size, and their difference, the Newton slope, was noise for b = 4e204
|
|
230
|
+
// (qbeta(9.3e-85, 1, 4e204, { lowerTail: false }) returned 0)
|
|
231
|
+
const g = qgammaStd(logTarget, a, lower, true) / b;
|
|
232
|
+
if (g > 0 && g < 0.5)
|
|
233
|
+
x0 = g;
|
|
234
|
+
}
|
|
168
235
|
return invertContinuous({
|
|
169
236
|
logTarget,
|
|
170
237
|
...(linearTarget !== undefined && { linearTarget }),
|
|
@@ -212,7 +279,13 @@ export function qbeta1(p, a, b, lowerTail, logP) {
|
|
|
212
279
|
const leftHalf = lower ? logTarget <= atHalf : logTarget >= atHalf;
|
|
213
280
|
if (leftHalf)
|
|
214
281
|
return solveLeftHalf(a, b, lower, logTarget, linearTarget);
|
|
215
|
-
// Right half: y = 1 - x lies in (0, 1/2] in frame (b, a), with the tails swapped
|
|
282
|
+
// Right half: y = 1 - x lies in (0, 1/2] in frame (b, a), with the tails swapped. Where y is below 2^-55, x rounds to
|
|
283
|
+
// 1 (the search for such a y took seconds for a huge a: qbeta(8.7e-285, 1.3e304, 5.9), where y is about 5e-302; R
|
|
284
|
+
// does not converge there)
|
|
285
|
+
const yMin = 2 ** -55;
|
|
286
|
+
const atMinY = betaInc(b, a, yMin, 1 - yMin, !lower, true);
|
|
287
|
+
if (lower ? atMinY <= logTarget : atMinY >= logTarget)
|
|
288
|
+
return 1;
|
|
216
289
|
return 1 - solveLeftHalf(b, a, !lower, logTarget, linearTarget);
|
|
217
290
|
}
|
|
218
291
|
/**
|
|
@@ -7,6 +7,44 @@
|
|
|
7
7
|
import { vectorize } from "../internal/vectorize.js";
|
|
8
8
|
import { locationScale } from "../internal/ddexp.js";
|
|
9
9
|
const LOG_PI = 1.1447298858494002;
|
|
10
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
11
|
+
/** [f, e] with v = f 2^e, f in [1, 2) up to the rounding of log2 (v > 0 finite; exact, subnormal v included) */
|
|
12
|
+
function frexp(v) {
|
|
13
|
+
const s = v < 2 ** -1000 ? 600 : 0;
|
|
14
|
+
const e = Math.floor(Math.log2(v * 2 ** s));
|
|
15
|
+
return [v * 2 ** s * 2 ** -e, e - s];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The density at a subnormal scale, from d = x - location (finite or infinite) without rounding a product on the
|
|
19
|
+
* subnormal grid: π scale had lost digits (dcauchy(-2.165e-309, { scale: 1.578e-312 }) was 3.3e-13 off), and z = d /
|
|
20
|
+
* scale overflowed where the density is still normal (dcauchy(1e-10, { scale: 5e-324 }) was 0, 1.57e-304 expected).
|
|
21
|
+
* For |z| > 1, f = scale / (π d^2 (1 + y^2)) with y = scale / |d|, its powers of two taken out of scale and d and
|
|
22
|
+
* applied last (in two steps, the first within the normal range)
|
|
23
|
+
*/
|
|
24
|
+
function tinyScaleDensity(d, scale, az, log) {
|
|
25
|
+
if (az <= 1) {
|
|
26
|
+
if (log)
|
|
27
|
+
return -(LOG_PI + Math.log(scale)) - Math.log1p(az * az);
|
|
28
|
+
return 1 / (Math.PI * (1 + az * az)) / scale;
|
|
29
|
+
}
|
|
30
|
+
const ad = Math.abs(d);
|
|
31
|
+
const y = scale / ad;
|
|
32
|
+
if (log)
|
|
33
|
+
return -LOG_PI + Math.log(scale) - 2 * Math.log(ad) - Math.log1p(y * y);
|
|
34
|
+
if (ad === Infinity)
|
|
35
|
+
return 0;
|
|
36
|
+
const [fs, es] = frexp(scale);
|
|
37
|
+
const [fa, ea] = frexp(ad);
|
|
38
|
+
const n = es - 2 * ea;
|
|
39
|
+
const h = Math.trunc(n / 2);
|
|
40
|
+
return (fs / (fa * fa) / (Math.PI * (1 + y * y))) * 2 ** h * 2 ** (n - h);
|
|
41
|
+
}
|
|
42
|
+
/** |x - location| / 2 where the difference of the finite x and location overflows, else undefined */
|
|
43
|
+
function halfDistance(x, location) {
|
|
44
|
+
if (Number.isFinite(x - location) || !Number.isFinite(x) || !Number.isFinite(location))
|
|
45
|
+
return undefined;
|
|
46
|
+
return Math.abs(x / 2 - location / 2);
|
|
47
|
+
}
|
|
10
48
|
export function dcauchy1(x, location, scale, log) {
|
|
11
49
|
if (Number.isNaN(x) || Number.isNaN(location) || Number.isNaN(scale))
|
|
12
50
|
return NaN;
|
|
@@ -15,7 +53,18 @@ export function dcauchy1(x, location, scale, log) {
|
|
|
15
53
|
const z = (x - location) / scale;
|
|
16
54
|
if (Number.isNaN(z))
|
|
17
55
|
return NaN;
|
|
56
|
+
const ah = halfDistance(x, location);
|
|
57
|
+
if (ah !== undefined) {
|
|
58
|
+
// x - location overflows (both finite): |z| = 2 |x/2 - location/2| / scale, in logs, and 1/|z| from the halves
|
|
59
|
+
// (the density was 0 and its log -Infinity: dcauchy(1.79e308, { location: -1.1e307, scale: 2.7e296 }))
|
|
60
|
+
const y = scale / ah / 2;
|
|
61
|
+
if (log)
|
|
62
|
+
return -(LOG_PI + Math.log(scale)) - 2 * (Math.log(ah) + Math.LN2 - Math.log(scale)) - Math.log1p(y * y);
|
|
63
|
+
return (y / (Math.PI * scale)) * (y / (1 + y * y));
|
|
64
|
+
}
|
|
18
65
|
const az = Math.abs(z);
|
|
66
|
+
if (scale < MIN_NORMAL)
|
|
67
|
+
return tinyScaleDensity(x - location, scale, az, log);
|
|
19
68
|
if (az <= 1) {
|
|
20
69
|
if (log)
|
|
21
70
|
return -(LOG_PI + Math.log(scale)) - Math.log1p(z * z);
|
|
@@ -39,6 +88,20 @@ export function pcauchy1(q, location, scale, lowerTail, logP) {
|
|
|
39
88
|
const d = q - location;
|
|
40
89
|
if (Number.isNaN(d))
|
|
41
90
|
return NaN;
|
|
91
|
+
const ah = halfDistance(q, location);
|
|
92
|
+
if (ah !== undefined) {
|
|
93
|
+
// q - location overflows (both finite): the smaller tail from the halves, scale / |d| = (scale / |d/2|) / 2 (it
|
|
94
|
+
// was 0 although of order scale / 1.8e308: pcauchy(1.79e308, { location: -1.1e307, scale: 2.7e296, lowerTail:
|
|
95
|
+
// false }) is 4.5e-13)
|
|
96
|
+
const w = scale / ah / 2;
|
|
97
|
+
const v = Math.atan(w) / Math.PI;
|
|
98
|
+
if (lowerTail === d < 0) {
|
|
99
|
+
if (!logP)
|
|
100
|
+
return v;
|
|
101
|
+
return w < 1e-150 ? Math.log(scale) - Math.log(ah) - Math.LN2 - LOG_PI : Math.log(v);
|
|
102
|
+
}
|
|
103
|
+
return logP ? Math.log1p(-v) : 1 - v;
|
|
104
|
+
}
|
|
42
105
|
if (!(Math.abs(d) > scale)) {
|
|
43
106
|
const z = d / scale;
|
|
44
107
|
const v = 0.5 + Math.atan(lowerTail ? z : -z) / Math.PI;
|
|
@@ -61,13 +124,21 @@ const TINY_LOG = -460;
|
|
|
61
124
|
/**
|
|
62
125
|
* scale |T| = scale / tan(π s) ≈ scale / (π s) when the probability on that side is tiny.
|
|
63
126
|
* When π s is subnormal, 1 / (π s) overflows and loses accuracy, so change the order of division.
|
|
64
|
-
*
|
|
127
|
+
* Without logP, divide scale by s first (scale / π could underflow to a subnormal for a tiny scale) and then by π;
|
|
128
|
+
* if scale / s overflows although the quantile is still finite, divide by π first instead (then scale exceeds about
|
|
129
|
+
* 9e-16, so scale / π is a normal number).
|
|
130
|
+
* With logP (s is a log), multiply by e^-s split into four quarters (s / 4 is exact; taking log and exp back would
|
|
131
|
+
* amplify the rounding of the exponent). As s < -460, a quarter is at least e^115, so scale times it is a normal number
|
|
132
|
+
* before the division by π even for a subnormal scale (scale / π lost bits there); the later products only grow, so one
|
|
133
|
+
* overflows only where the quantile does, and quarters (not halves) keep e^(-s/4) finite wherever the quantile is.
|
|
65
134
|
*/
|
|
66
135
|
function tinyTail(scale, s, logP) {
|
|
67
|
-
if (!logP)
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
136
|
+
if (!logP) {
|
|
137
|
+
const r = scale / s;
|
|
138
|
+
return Number.isFinite(r) ? r / Math.PI : scale / Math.PI / s;
|
|
139
|
+
}
|
|
140
|
+
const h = Math.exp(-s / 4);
|
|
141
|
+
return ((h * scale) / Math.PI) * h * h * h;
|
|
71
142
|
}
|
|
72
143
|
/** Quantile tan(π (s - 1/2)) of the standard Cauchy for lower probability s (0 < s <= 1/2) */
|
|
73
144
|
function quantileSmall(s) {
|
|
@@ -5,7 +5,10 @@
|
|
|
5
5
|
// Argument handling (rate = 0, Inf) is matched to R's output.
|
|
6
6
|
import { vectorize } from "../internal/vectorize.js";
|
|
7
7
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
8
|
-
import { expNeg, log1mExp, logOneMinusExpNeg, mulDD } from "../internal/ddexp.js";
|
|
8
|
+
import { ddExp, expNeg, expNegOver, log1mExp, logOneMinusExpNeg, mulDD } from "../internal/ddexp.js";
|
|
9
|
+
import { ddLog, ddSub } from "../special/dd.js";
|
|
10
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
11
|
+
const LOG_MIN_NORMAL = -1022 * Math.LN2;
|
|
9
12
|
export function dexp1(x, rate, log) {
|
|
10
13
|
if (Number.isNaN(x) || Number.isNaN(rate))
|
|
11
14
|
return NaN;
|
|
@@ -18,7 +21,14 @@ export function dexp1(x, rate, log) {
|
|
|
18
21
|
return NaN;
|
|
19
22
|
if (log)
|
|
20
23
|
return Math.log(rate) - t[0] - t[1];
|
|
21
|
-
|
|
24
|
+
const e = expNeg(t);
|
|
25
|
+
// exp(-rate x) subnormal while a large rate brings the density back: from the log in double-double (it had lost
|
|
26
|
+
// digits: dexp(2.5e-294, 3e296) was 8e-8 off)
|
|
27
|
+
if (e < MIN_NORMAL && rate > 1 && t[0] < 1500) {
|
|
28
|
+
const l = ddSub(ddLog([rate, 0]), t);
|
|
29
|
+
return l[0] < -746 ? 0 : ddExp(l)[0];
|
|
30
|
+
}
|
|
31
|
+
return rate * e;
|
|
22
32
|
}
|
|
23
33
|
/** Log or linear value of P[X > x], according to the tail direction */
|
|
24
34
|
function expTail(t, lowerTail, logP) {
|
|
@@ -51,6 +61,10 @@ export function qexp1(p, rate, lowerTail, logP) {
|
|
|
51
61
|
if (Number.isNaN(b) || b === 0)
|
|
52
62
|
return b;
|
|
53
63
|
// Log of the upper probability, log P[X > x] = -rate x
|
|
64
|
+
// A lower tail below the normal range, given as its log: -log(1 - P) is P to the double, but exp(p) would be rounded
|
|
65
|
+
// on the subnormal grid before the division by the rate (qexp(-720.6, { rate: 7.4e-258, logP: true }) was 4e-14 off)
|
|
66
|
+
if (logP && lowerTail && p < LOG_MIN_NORMAL && rate > 0)
|
|
67
|
+
return expNegOver([-p, 0], rate);
|
|
54
68
|
let lu;
|
|
55
69
|
if (logP)
|
|
56
70
|
lu = lowerTail ? log1mExp(p) : p;
|