@numero-uno-jpn/rstats 0.5.0 → 0.7.0
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 +125 -0
- package/README.ja.md +36 -20
- package/README.md +34 -18
- package/THIRD_PARTY_NOTICES.md +25 -5
- package/dist/chunks/chunk-6R7YDHA2.js +2 -0
- package/dist/chunks/chunk-AFKYWGKN.js +2 -0
- package/dist/chunks/chunk-ANYGLFT4.js +2 -0
- package/dist/chunks/chunk-BI5RSUZ2.js +2 -0
- package/dist/chunks/chunk-J3SNI7KY.js +2 -0
- package/dist/chunks/chunk-JCZAB6C5.js +2 -0
- package/dist/chunks/chunk-MVG5LUZU.js +2 -0
- package/dist/chunks/chunk-PTPPP4TI.js +2 -0
- package/dist/chunks/chunk-QNQJA6KZ.js +2 -0
- package/dist/chunks/chunk-SXSJXDD3.js +2 -0
- package/dist/chunks/chunk-UIOAWLAN.js +2 -0
- package/dist/chunks/chunk-XYXHHF5L.js +2 -0
- package/dist/chunks/chunk-YKN6OGJL.js +2 -0
- package/dist/chunks/chunk-ZW52Z3GY.js +2 -0
- package/dist/chunks/chunk-ZYA4ISAT.js +2 -0
- package/dist/descriptive/basic.d.ts +73 -0
- package/dist/descriptive/basic.js +256 -0
- package/dist/descriptive/cov.d.ts +39 -0
- package/dist/descriptive/cov.js +105 -0
- package/dist/descriptive/hist.d.ts +64 -0
- package/dist/descriptive/hist.js +304 -0
- package/dist/descriptive/moments.d.ts +63 -0
- package/dist/descriptive/moments.js +158 -0
- package/dist/descriptive/quantile.d.ts +50 -0
- package/dist/descriptive/quantile.js +131 -0
- package/dist/descriptive/rank.d.ts +23 -0
- package/dist/descriptive/rank.js +72 -0
- package/dist/descriptive/table.d.ts +54 -0
- package/dist/descriptive/table.js +160 -0
- package/dist/descriptive.js +2 -0
- package/dist/distributions/arcsine.d.ts +24 -0
- package/dist/distributions/arcsine.js +73 -0
- package/dist/distributions/invgamma.d.ts +29 -0
- package/dist/distributions/invgamma.js +85 -0
- package/dist/distributions/kumaraswamy.d.ts +17 -0
- package/dist/distributions/kumaraswamy.js +96 -0
- package/dist/distributions/laplace.d.ts +23 -0
- package/dist/distributions/laplace.js +69 -0
- package/dist/distributions/multinomial.d.ts +24 -0
- package/dist/distributions/multinomial.js +70 -0
- package/dist/distributions/nbeta.js +28 -10
- package/dist/distributions/nchisq.js +20 -3
- package/dist/distributions/pareto.d.ts +23 -0
- package/dist/distributions/pareto.js +76 -0
- package/dist/distributions/summary.d.ts +25 -0
- package/dist/distributions/summary.js +289 -0
- package/dist/distributions/triangular.d.ts +25 -0
- package/dist/distributions/triangular.js +87 -0
- package/dist/distributions.js +2 -0
- package/dist/entries/descriptive.d.ts +7 -0
- package/dist/entries/descriptive.js +8 -0
- package/dist/entries/distributions.d.ts +42 -0
- package/dist/entries/distributions.js +42 -0
- package/dist/entries/linalg.d.ts +11 -0
- package/dist/entries/linalg.js +11 -0
- package/dist/entries/numerics.d.ts +8 -0
- package/dist/entries/numerics.js +6 -0
- package/dist/entries/special.d.ts +10 -0
- package/dist/entries/special.js +10 -0
- package/dist/entries/tests.d.ts +21 -0
- package/dist/entries/tests.js +20 -0
- package/dist/htest/ansariTest.d.ts +30 -0
- package/dist/htest/ansariTest.js +253 -0
- package/dist/htest/aov.d.ts +85 -0
- package/dist/htest/aov.js +178 -0
- package/dist/htest/binomTest.d.ts +36 -0
- package/dist/htest/binomTest.js +121 -0
- package/dist/htest/chisqTest.d.ts +34 -2
- package/dist/htest/chisqTest.js +140 -37
- package/dist/htest/cor.d.ts +14 -0
- package/dist/htest/cor.js +4 -3
- package/dist/htest/core/ansari.d.ts +25 -0
- package/dist/htest/core/ansari.js +186 -0
- package/dist/htest/core/kolmogorov.d.ts +26 -0
- package/dist/htest/core/kolmogorov.js +221 -0
- package/dist/htest/core/mantelhaen.d.ts +26 -0
- package/dist/htest/core/mantelhaen.js +145 -0
- package/dist/htest/core/shapiro.d.ts +24 -0
- package/dist/htest/core/shapiro.js +142 -0
- package/dist/htest/core/simulate.d.ts +2 -0
- package/dist/htest/core/simulate.js +16 -0
- package/dist/htest/core/smirnov.d.ts +20 -0
- package/dist/htest/core/smirnov.js +75 -0
- package/dist/htest/fisherTest.d.ts +12 -1
- package/dist/htest/fisherTest.js +187 -127
- package/dist/htest/groups.d.ts +20 -0
- package/dist/htest/groups.js +62 -0
- package/dist/htest/ksTest.d.ts +31 -0
- package/dist/htest/ksTest.js +106 -0
- package/dist/htest/mantelhaenTest.d.ts +26 -0
- package/dist/htest/mantelhaenTest.js +196 -0
- package/dist/htest/mcnemarTest.d.ts +26 -0
- package/dist/htest/mcnemarTest.js +99 -0
- package/dist/htest/onewayTest.d.ts +26 -0
- package/dist/htest/onewayTest.js +97 -0
- package/dist/htest/pairwise.d.ts +64 -0
- package/dist/htest/pairwise.js +152 -0
- package/dist/htest/poissonTest.d.ts +35 -0
- package/dist/htest/poissonTest.js +170 -0
- package/dist/htest/propTest.d.ts +37 -0
- package/dist/htest/propTest.js +164 -0
- package/dist/htest/rankTests.d.ts +52 -0
- package/dist/htest/rankTests.js +234 -0
- package/dist/htest/types.d.ts +2 -0
- package/dist/htest/types.js +6 -0
- package/dist/htest/varTest.d.ts +48 -0
- package/dist/htest/varTest.js +118 -0
- package/dist/htest/wilcoxTest.js +136 -174
- package/dist/index.d.ts +6 -50
- package/dist/index.js +8 -50
- package/dist/integrate/quadpack.d.ts +29 -0
- package/dist/integrate/quadpack.js +525 -0
- package/dist/internal/seq.d.ts +5 -0
- package/dist/internal/seq.js +14 -0
- package/dist/internal/uniroot.js +10 -71
- package/dist/linalg/lu.d.ts +45 -0
- package/dist/linalg/lu.js +88 -0
- package/dist/linalg/ops.d.ts +90 -0
- package/dist/linalg/ops.js +320 -0
- package/dist/linalg.js +2 -0
- package/dist/models/lm.d.ts +98 -0
- package/dist/models/lm.js +187 -0
- package/dist/numerics/deriv.d.ts +37 -0
- package/dist/numerics/deriv.js +150 -0
- package/dist/numerics/integrate.d.ts +91 -0
- package/dist/numerics/integrate.js +264 -0
- package/dist/numerics/interp.d.ts +103 -0
- package/dist/numerics/interp.js +581 -0
- package/dist/numerics/ode.d.ts +23 -0
- package/dist/numerics/ode.js +59 -0
- package/dist/numerics.js +2 -0
- package/dist/optim/localmin.d.ts +16 -0
- package/dist/optim/localmin.js +97 -0
- package/dist/random/beta.js +1 -1
- package/dist/random/binomial.js +1 -1
- package/dist/random/continuous.js +1 -1
- package/dist/random/extra.d.ts +33 -0
- package/dist/random/extra.js +62 -0
- package/dist/random/gamma.js +1 -1
- package/dist/random/hyper.js +1 -1
- package/dist/random/mt.js +0 -1
- package/dist/random/poisson.js +0 -1
- package/dist/random/r2dtable.d.ts +26 -0
- package/dist/random/r2dtable.js +153 -0
- package/dist/random/rank.js +1 -1
- package/dist/random/rng.js +1 -1
- package/dist/random/sample.js +1 -1
- package/dist/rstats.js +3 -2
- package/dist/special/besselIK.d.ts +38 -0
- package/dist/special/besselIK.js +315 -0
- package/dist/special/besselJY.d.ts +32 -0
- package/dist/special/besselJY.js +508 -0
- package/dist/special/besselSeries.d.ts +8 -0
- package/dist/special/besselSeries.js +24 -0
- package/dist/special/beta.js +22 -5
- package/dist/special/choose.d.ts +75 -0
- package/dist/special/choose.js +233 -0
- package/dist/special/erf.d.ts +28 -2
- package/dist/special/erf.js +37 -10
- package/dist/special/extra.d.ts +47 -0
- package/dist/special/extra.js +220 -0
- package/dist/special/igam.d.ts +4 -0
- package/dist/special/igam.js +57 -2
- package/dist/special/incbet.js +77 -22
- package/dist/special/lgammaDiff.d.ts +7 -0
- package/dist/special/lgammaDiff.js +16 -3
- package/dist/special/log1p.d.ts +53 -0
- package/dist/special/log1p.js +93 -0
- package/dist/special/psigamma.d.ts +48 -0
- package/dist/special/psigamma.js +318 -0
- package/dist/special/rCompatible.d.ts +8 -0
- package/dist/special/rCompatible.js +1 -0
- package/dist/special.js +2 -0
- package/dist/tests.js +3 -0
- package/package.json +26 -1
package/dist/numerics.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! rStats.js 0.7.0 | 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{a as H,b as T,c as D}from"./chunks/chunk-QNQJA6KZ.js";import"./chunks/chunk-ZYA4ISAT.js";import{a as M,f as j}from"./chunks/chunk-XYXHHF5L.js";import{b as S}from"./chunks/chunk-MVG5LUZU.js";import"./chunks/chunk-ZW52Z3GY.js";function x(o,l,t="mean"){if(o.length!==l.length)throw new RangeError("'x' and 'y' lengths differ");let n=[];for(let r=0;r<o.length;r++)!Number.isNaN(o[r])&&!Number.isNaN(l[r])&&n.push(r);n.sort((r,s)=>o[r]-o[s]);let e=typeof t=="function"?t:t==="min"?r=>Math.min(...r):t==="max"?r=>Math.max(...r):S,a=[],f=[];for(let r=0;r<n.length;){let s=o[n[r]],c=r+1;for(;c<n.length&&o[n[c]]===s;)c++;a.push(s),f.push(c-r===1?l[n[r]]:e(n.slice(r,c).map(i=>l[i]))),r=c}return{x:a,y:f}}function F(o,l,t,n){let e=(n-o[t])/(o[t+1]-o[t]);return l[t]+(l[t+1]-l[t])*e}function O(o,l,t){let n=(e,a)=>e===0?0:e*a;return n(1-t,o[l])+n(t,o[l+1])}function X(o,l,t={}){let{method:n="linear",n:e=50,f:a=0,rule:f=1,ties:r="mean"}=t,s=x(o,l,r),c=s.x.length;if(c<=1&&n==="linear")throw new RangeError("need at least two non-NA values to interpolate");if(c===0)throw new RangeError("zero non-NA points");let[i,p]=typeof f=="number"?[f,f]:f,d=t.yleft??(i===1?NaN:s.y[0]),h=t.yright??(p===1?NaN:s.y[c-1]),m;if(t.xout!==void 0)m=Array.from(t.xout);else{if(e<=0)throw new RangeError("'approx' requires n >= 1");m=M(s.x[0],s.x[c-1],e)}let g=s.x,u=s.y,w=j(m,g),y=m.map((b,A)=>{let R=w[A];if(Number.isNaN(R))return NaN;if(R===0)return d;let E=R-1;return b===g[E]?u[E]:R===c?h:n==="linear"?F(g,u,E,b):O(u,E,a)});return{x:m,y}}function v(o,l){let t=o.length,n=new Array(t).fill(0),e=new Array(t).fill(0),a=new Array(t).fill(0);if(t<2)return{b:n,c:e,d:a};if(t<3)return n[0]=(l[1]-l[0])/(o[1]-o[0]),n[1]=n[0],{b:n,c:e,d:a};let f=t-1;a[0]=o[1]-o[0],e[1]=(l[1]-l[0])/a[0];for(let r=1;r<f;r++)a[r]=o[r+1]-o[r],n[r]=2*(a[r-1]+a[r]),e[r+1]=(l[r+1]-l[r])/a[r],e[r]=e[r+1]-e[r];n[0]=-a[0],n[f]=-a[t-2],e[0]=0,e[f]=0,t>3&&(e[0]=e[2]/(o[3]-o[1])-e[1]/(o[2]-o[0]),e[f]=e[t-2]/(o[f]-o[t-3])-e[t-3]/(o[t-2]-o[t-4]),e[0]=e[0]*a[0]*a[0]/(o[3]-o[0]),e[f]=-e[f]*a[t-2]*a[t-2]/(o[f]-o[t-4]));for(let r=1;r<t;r++){let s=a[r-1]/n[r-1];n[r]=n[r]-s*a[r-1],e[r]=e[r]-s*e[r-1]}e[f]=e[f]/n[f];for(let r=t-2;r>=0;r--)e[r]=(e[r]-a[r]*e[r+1])/n[r];n[f]=(l[f]-l[t-2])/a[t-2]+a[t-2]*(e[t-2]+2*e[f]);for(let r=0;r<f;r++)n[r]=(l[r+1]-l[r])/a[r]-a[r]*(e[r+1]+2*e[r]),a[r]=(e[r+1]-e[r])/a[r],e[r]=3*e[r];return e[f]=3*e[f],a[f]=a[t-2],{b:n,c:e,d:a}}function z(o,l,t){let n=o.length,e=new Array(n).fill(0),a=new Array(n).fill(0),f=new Array(n).fill(0);for(let r=0;r<n-1;r++){let s=o[r+1]-o[r];e[r]=(l[r+1]-l[r])/s-s*(t[r+1]+2*t[r]),a[r]=3*t[r],f[r]=(t[r+1]-t[r])/s}return{b:e,c:a,d:f}}function J(o,l){let t=o.length;if(t<3)return v(o,l);let n=Array.from({length:t-1},(c,i)=>o[i+1]-o[i]),e=n.map((c,i)=>(l[i+1]-l[i])/c),a=new Array(t).fill(0),f=new Array(t).fill(0);for(let c=1;c<t-1;c++)a[c]=2*(n[c-1]+n[c]),f[c]=e[c]-e[c-1];for(let c=2;c<t-1;c++){let i=n[c-1]/a[c-1];a[c]=a[c]-i*n[c-1],f[c]=f[c]-i*f[c-1]}let r=new Array(t).fill(0);r[t-2]=f[t-2]/a[t-2];for(let c=t-3;c>=1;c--)r[c]=(f[c]-n[c]*r[c+1])/a[c];let s=z(o,l,r);return s.b[t-1]=e[t-2]+n[t-2]*r[t-2],s}function P(o,l){let t=o.length;if(l[0]!==l[t-1])throw new RangeError("periodic spline needs y[1] == y[n]");if(t<3)return{b:new Array(t).fill(0),c:new Array(t).fill(0),d:new Array(t).fill(0)};let n=t-1,e=Array.from({length:n},(u,w)=>o[w+1]-o[w]),a=e.map((u,w)=>(l[w+1]-l[w])/u),f=u=>u===0?n-1:u-1,r=Array.from({length:n},(u,w)=>2*(e[f(w)]+e[w])),s=Array.from({length:n},(u,w)=>a[w]-a[f(w)]),c=e.slice(),i=new Array(n).fill(0);i[0]=e[n-1],i[n-2]=i[n-2]+e[n-2];let p=i[0],d=r[n-1],h=s[n-1];for(let u=0;u<n-1;u++){if(u+1<n-1){let y=e[u]/r[u];r[u+1]=r[u+1]-y*c[u],i[u+1]=i[u+1]-y*i[u],s[u+1]=s[u+1]-y*s[u]}let w=p/r[u];d-=w*i[u],h-=w*s[u],u+1<n-1&&(p=(u+1===n-2?e[n-2]:0)-w*c[u])}let m=new Array(t).fill(0);m[n-1]=h/d;for(let u=n-2;u>=0;u--){let w=s[u]-i[u]*m[n-1]-(u<n-2?c[u]*m[u+1]:0);m[u]=w/r[u]}m[t-1]=m[0];let g=z(o,l,m);return g.b[t-1]=g.b[0],g.c[t-1]=g.c[0],g.d[t-1]=g.d[0],g}function Y(o,l){let t=o.length,{b:n,c:e,d:a}=v(o,l);if(t<2)return{b:n,c:e,d:a};let f=Array.from({length:t-1},(s,c)=>(l[c+1]-l[c])/(o[c+1]-o[c]));for(let s=0;s<t;s++){let c=f[s===0?0:s-1],i=f[s===t-1?t-2:s],p=Math.min(Math.abs(c),Math.abs(i)),d=c*i>0?i>0:n[s]>=0;n[s]=d?Math.min(Math.max(0,n[s]),3*p):Math.max(Math.min(0,n[s]),-3*p)}for(let s=0;s<t-1;s++){let c=o[s+1]-o[s],i=(l[s+1]-l[s])/c;e[s]=(3*i-2*n[s]-n[s+1])/c,a[s]=(n[s]+n[s+1]-2*i)/(c*c)}let r=o[t-1]-o[t-2];return e[t-1]=(n[t-2]+2*n[t-1]-3*(l[t-1]-l[t-2])/r)/r,a[t-1]=a[t-2],{b:n,c:e,d:a}}function q(o,l,t,n){let e=x(o,l,n),a=e.x.length;if(a===0)throw new RangeError("zero non-NA points");t==="periodic"&&e.y[a-1]!==e.y[0]&&(e.y[a-1]=e.y[0]),t==="hyman"&&B(e.y);let f=t==="fmm"?v(e.x,e.y):t==="natural"?J(e.x,e.y):t==="periodic"?P(e.x,e.y):t==="hyman"?Y(e.x,e.y):(()=>{throw new RangeError(`invalid spline method '${String(t)}'`)})();return{method:t,...e,...f}}function B(o){let l=!0,t=!0;for(let n=1;n<o.length;n++){let e=o[n]-o[n-1];e>=0||(l=!1),e<=0||(t=!1)}if(!l&&!t)throw new RangeError("'y' must be increasing or decreasing")}function C(o){let l=Math.trunc(o);if(!(l>=0&&l<=3))throw new RangeError("'deriv' must be between 0 and 3");return l}function I(o,l,t){let n=C(t),{x:e,y:a,b:f,c:r,d:s,method:c}=o,i=e.length,p=c==="natural",d=e[i-1]-e[0],h=0;return Array.from(l,m=>{let g=m;if(c==="periodic"&&i>1){let y=(g-e[0])%d;y<0&&(y+=d),g=e[0]+y}if(p&&n>0&&g<=e[0])return n===1?f[0]:0;if(!(e[h]<=g&&(h===i-1||g<=e[h+1]))){let y=0,b=i;for(;b-y>1;){let A=y+b>>1;e[A]<=g?y=A:b=A}h=y}let u=g-e[h],w=p&&g<e[0]?0:s[h];switch(n){case 0:return a[h]+u*(f[h]+u*(r[h]+u*w));case 1:return f[h]+u*(2*r[h]+u*(3*w));case 2:return 2*r[h]+u*(6*w);default:return 6*w}})}function G(o,l,t={}){let{method:n="fmm",ties:e="mean"}=t,a=q(o,l,n,e),f;if(t.xout!==void 0)f=Array.from(t.xout);else{let r=a.x.length,s=t.n??3*r;if(s<=0)throw new RangeError("'spline' requires n >= 1");f=M(t.xmin??a.x[0],t.xmax??a.x[r-1],s)}return{x:f,y:I(a,f,0)}}function K(o,l,t={}){let{method:n="fmm",ties:e="mean"}=t;if(n==="monoH.FC"){let f=x(o,l,e);if(f.x.length===0)throw new RangeError("zero non-NA points");let r=Q(f.x,f.y);return(s,c=0)=>L(f.x,f.y,r,s,c)}let a=q(o,l,n,e);return(f,r=0)=>I(a,f,r)}function Q(o,l){let t=o.length;if(t<2)throw new RangeError("length(m) must be at least two");let n=Array.from({length:t-1},(a,f)=>(l[f+1]-l[f])/(o[f+1]-o[f])),e=Array.from({length:t},(a,f)=>f===0?n[0]:f===t-1?n[t-2]:(n[f-1]+n[f])/2);for(let a=0;a<t-1;a++){let f=n[a];if(f===0){e[a]=0,e[a+1]=0;continue}let r=e[a]/f,s=e[a+1]/f,c=2*r+s-3,i=r+2*s-3;if(c>0&&i>0&&r*(c+i)<c*c){let p=3*f/Math.sqrt(r*r+s*s);e[a]=p*r,e[a+1]=p*s}}return e}function L(o,l,t,n,e){let a=C(e),f=o.length;return Array.from(n,r=>{if(Number.isNaN(r))return NaN;let s=0,c=f;for(;s<c;){let A=s+c>>1;o[A]<=r?s=A+1:c=A}let i=s;if(i===0||i===f){let A=i===0?0:f-1;return a===0?l[A]+t[A]*(r-o[A]):a===1?t[A]:0}let p=o[i-1],d=o[i]-p,h=(r-p)/d,m=h-1,g=l[i-1],u=l[i],w=t[i-1],y=t[i],b=(u-g)/d;switch(a){case 0:{let A=h*h*(3-2*h);return g*(1-A)+d*w*(h*m*m)+u*A+d*y*(h*h*m)}case 1:return b*(-6*h*m)+w*(m*(3*h-1))+y*(h*(3*h-2));case 2:return(b*(6-12*h)+w*(6*h-4)+y*(6*h-2))/d;default:return(-12*b+6*w+6*y)/(d*d)}})}function U(o,l,t){let n=o.length;if(l.length!==n||t.length!==n)throw new RangeError("'x', 'y' and 'm' must have the same length");let e=Array.from({length:n},(s,c)=>c).sort((s,c)=>o[s]-o[c]),a=e.map(s=>o[s]),f=e.map(s=>l[s]),r=e.map(s=>t[s]);for(let s=1;s<n;s++)if(!(a[s]-a[s-1]>0))throw new RangeError("'x' must be *strictly* increasing (non - NA)");return(s,c=0)=>L(a,f,r,s,c)}function V(o,l,t){let n=Array.from(o),e=Array.from(l),a=n.length;if(e.length!==a)throw new RangeError("'xi' and 'yi' must have the same length");let f=Array.from(t),r=Math.min(...n),s=Math.max(...n);if(f.some(i=>i<r||i>s))throw new RangeError("Some interpolation points outside the nodes.");let c=n.map((i,p)=>{let d=1;for(let h=0;h<a;h++)h!==p&&(d*=i-n[h]);return 1/d});return f.map(i=>{let p=0,d=0,h=n.lastIndexOf(i);if(h>=0)return e[h];for(let m=0;m<a;m++){let g=i-n[m],u=c[m]/g;p+=u*e[m],d+=u}return p/d})}function W(o,l,t,n){let e=Array.from(o),a=Array.from(l),f=Array.from(t),r=e.length;if(a.length!==r||f.length!==r)throw new RangeError("'xi', 'yi' and 'di' must have the same length");let s=e.flatMap(h=>[h,h]),c=2*r,i=a.flatMap(h=>[h,h]),p=[i[0]],d=i.slice();for(let h=1;h<c;h++){let m=[];for(let g=0;g<c-h;g++)h===1&&s[g+1]===s[g]?m.push(f[g>>1]):m.push((d[g+1]-d[g])/(s[g+h]-s[g]));p.push(m[0]),d=m}return Array.from(n,h=>{let m=p[c-1];for(let g=c-2;g>=0;g--)m=m*(h-s[g])+p[g];return m})}function Z(o,l,t,n=[],e={}){let a=e.method??"rk4";if(a!=="euler"&&a!=="rk2"&&a!=="rk4")throw new RangeError(`unknown method '${String(a)}'`);let f=[...n],r=o.length,s=(d,h)=>{let m=Array.from(t(d,h,f));if(m.length!==r)throw new RangeError(`The number of derivatives returned by func() (${m.length}) must equal the length of the initial conditions vector (${r})`);return m},c=(d,h,m)=>d.map((g,u)=>g+h*m[u]),i=[...o],p=[];if(l.length===0)return p;p.push([l[0],...i]);for(let d=1;d<l.length;d++){let h=l[d-1],m=l[d]-h,g=s(h,i);if(a==="euler")i=c(i,m,g);else if(a==="rk2"){let u=s(h+m,c(i,m,g));i=i.map((w,y)=>w+m*(.5*g[y]+.5*u[y]))}else{let u=s(h+m/2,c(i,m/2,g)),w=s(h+m/2,c(i,m/2,u)),y=s(h+m,c(i,m,w));i=i.map((b,A)=>b+m/6*(g[A]+2*u[A]+2*w[A]+y[A]))}p.push([l[d],...i])}return p}function _(o,l,t){let n=l.eps??1e-4,e=l.d??t,a=l.zeroTol??Math.sqrt(Number.EPSILON/7e-7),f=l.r??4;if(!(Number.isInteger(f)&&f>=1))throw new RangeError("'r' must be a positive integer");return{eps:n,r:f,h0:o.map(r=>Math.abs(e*r)+(Math.abs(r)<a?n:0))}}function k(o){let l=[...o];for(let t=1;t<l.length;t++){let n=4**t;for(let e=0;e<l.length-t;e++)l[e]=(l[e+1]*n-l[e])/(n-1)}return l[0]}function N(o,l,t){let n=[...o];return n[l]=n[l]+t,n}function $(o){let l=o.method??"Richardson";if(l!=="Richardson"&&l!=="simple")throw new RangeError(`unknown method "${String(l)}" (expected "Richardson" or "simple")`);return l}function tt(o,l,t={}){let n=[...l],e=n.length;if($(t)==="simple"){let s=t.eps??1e-4,c=o([...n]);return n.map((i,p)=>(o(N(n,p,s))-c)/s)}let{r:a,h0:f}=_(n,t,1e-4),r=[];for(let s=0;s<e;s++){let c=[],i=f[s];for(let p=0;p<a;p++){if(p>0&&Math.abs(c[p-1])<1e-20?c.push(0):c.push((o(N(n,s,i))-o(N(n,s,-i)))/(2*i)),Number.isNaN(c[p]))throw new RangeError(`func returned NaN at step ${i} from x`);i/=2}r.push(k(c))}return r}function nt(o,l,t={}){let n=[...l],e=n.length,a=Array.from(o([...n])),f=a.map(()=>new Array(e).fill(NaN));if($(t)==="simple"){let c=t.eps??1e-4;for(let i=0;i<e;i++){let p=o(N(n,i,c));a.forEach((d,h)=>f[h][i]=(p[h]-d)/c)}return f}let{r,h0:s}=_(n,t,1e-4);for(let c=0;c<e;c++){let i=a.map(()=>[]),p=s[c];for(let d=0;d<r;d++){let h=o(N(n,c,p)),m=o(N(n,c,-p));a.forEach((g,u)=>i[u].push((h[u]-m[u])/(2*p))),p/=2}a.forEach((d,h)=>f[h][c]=k(i[h]))}return f}function rt(o,l,t={}){if((t.method??"Richardson")!=="Richardson")throw new RangeError('hessian supports only method "Richardson"');let n=[...l],e=n.length,{r:a,h0:f}=_(n,t,.1),r=o([...n]),s=[];for(let i=0;i<e;i++){let p=[],d=f[i];for(let h=0;h<a;h++)p.push((o(N(n,i,d))-2*r+o(N(n,i,-d)))/d**2),d/=2;s.push(k(p))}let c=n.map(()=>new Array(e).fill(NaN));for(let i=0;i<e;i++){c[i][i]=s[i];for(let p=0;p<i;p++){let d=[],h=f[i],m=f[p];for(let g=0;g<a;g++){let u=N(N(n,i,h),p,m),w=N(N(n,i,-h),p,-m);d.push((o(u)-2*r+o(w)-s[i]*h**2-s[p]*m**2)/(2*h*m)),h/=2,m/=2}c[i][p]=c[p][i]=k(d)}}return c}export{X as approx,V as barylag,tt as grad,W as hermiteInterp,rt as hessian,H as integrate,nt as jacobian,Z as ode,T as optimize,G as spline,K as splinefun,U as splinefunH,D as uniroot};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Default tolerance .Machine$double.eps^0.25, as in R's optimize */
|
|
2
|
+
export declare const LOCALMIN_TOL: number;
|
|
3
|
+
export interface LocalMinResult {
|
|
4
|
+
/** Location of the minimum */
|
|
5
|
+
x: number;
|
|
6
|
+
/** f at x (the last value computed there) */
|
|
7
|
+
fx: number;
|
|
8
|
+
/** Number of evaluations of f */
|
|
9
|
+
evaluations: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A local minimum of f in [lower, upper], to within about 3 tol + 2 sqrt(eps) |x| (Brent's localmin).
|
|
13
|
+
*
|
|
14
|
+
* @param tol - Tolerance (default .Machine$double.eps^0.25).
|
|
15
|
+
*/
|
|
16
|
+
export declare function localMin(f: (x: number) => number, lower: number, upper: number, tol?: number): LocalMinResult;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// One-dimensional minimisation without derivatives (localmin from Brent 1973, "Algorithms for Minimization without
|
|
2
|
+
// Derivatives", chapter 5), the algorithm behind R's optimize(). Golden-section search combined with successive
|
|
3
|
+
// parabolic interpolation. The procedure follows the ALGOL 60 procedure in Brent's book (the same as netlib's
|
|
4
|
+
// fmin.f, public domain), written for rStats.js.
|
|
5
|
+
/** Default tolerance .Machine$double.eps^0.25, as in R's optimize */
|
|
6
|
+
export const LOCALMIN_TOL = Math.pow(Number.EPSILON, 0.25);
|
|
7
|
+
/**
|
|
8
|
+
* A local minimum of f in [lower, upper], to within about 3 tol + 2 sqrt(eps) |x| (Brent's localmin).
|
|
9
|
+
*
|
|
10
|
+
* @param tol - Tolerance (default .Machine$double.eps^0.25).
|
|
11
|
+
*/
|
|
12
|
+
export function localMin(f, lower, upper, tol = LOCALMIN_TOL) {
|
|
13
|
+
const c = 0.5 * (3 - Math.sqrt(5));
|
|
14
|
+
const eps = Math.sqrt(Number.EPSILON);
|
|
15
|
+
let a = lower;
|
|
16
|
+
let b = upper;
|
|
17
|
+
let v = a + c * (b - a);
|
|
18
|
+
let w = v;
|
|
19
|
+
let x = v;
|
|
20
|
+
let d = 0;
|
|
21
|
+
let e = 0;
|
|
22
|
+
let fx = f(x);
|
|
23
|
+
let fv = fx;
|
|
24
|
+
let fw = fx;
|
|
25
|
+
let evaluations = 1;
|
|
26
|
+
const tol3 = tol / 3;
|
|
27
|
+
for (;;) {
|
|
28
|
+
const xm = 0.5 * (a + b);
|
|
29
|
+
const tol1 = eps * Math.abs(x) + tol3;
|
|
30
|
+
const t2 = 2 * tol1;
|
|
31
|
+
// stopping criterion
|
|
32
|
+
if (Math.abs(x - xm) <= t2 - 0.5 * (b - a))
|
|
33
|
+
break;
|
|
34
|
+
let golden = true;
|
|
35
|
+
if (Math.abs(e) > tol1) {
|
|
36
|
+
// fit a parabola through (v, fv), (w, fw), (x, fx)
|
|
37
|
+
let r = (x - w) * (fx - fv);
|
|
38
|
+
let q = (x - v) * (fx - fw);
|
|
39
|
+
let p = (x - v) * q - (x - w) * r;
|
|
40
|
+
q = 2 * (q - r);
|
|
41
|
+
if (q > 0)
|
|
42
|
+
p = -p;
|
|
43
|
+
else
|
|
44
|
+
q = -q;
|
|
45
|
+
r = e;
|
|
46
|
+
e = d;
|
|
47
|
+
if (Math.abs(p) < Math.abs(0.5 * q * r) && p > q * (a - x) && p < q * (b - x)) {
|
|
48
|
+
// parabolic interpolation step
|
|
49
|
+
d = p / q;
|
|
50
|
+
const u = x + d;
|
|
51
|
+
// f must not be evaluated too close to a or b
|
|
52
|
+
if (u - a < t2 || b - u < t2)
|
|
53
|
+
d = x <= xm ? tol1 : -tol1; // sign(tol1, xm - x), + for 0
|
|
54
|
+
golden = false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (golden) {
|
|
58
|
+
// golden-section step
|
|
59
|
+
e = x < xm ? b - x : a - x;
|
|
60
|
+
d = c * e;
|
|
61
|
+
}
|
|
62
|
+
// f must not be evaluated too close to x
|
|
63
|
+
const u = Math.abs(d) >= tol1 ? x + d : d >= 0 ? x + tol1 : x - tol1; // x + sign(tol1, d)
|
|
64
|
+
const fu = f(u);
|
|
65
|
+
evaluations++;
|
|
66
|
+
// update a, b, v, w and x
|
|
67
|
+
if (fu <= fx) {
|
|
68
|
+
if (u < x)
|
|
69
|
+
b = x;
|
|
70
|
+
else
|
|
71
|
+
a = x;
|
|
72
|
+
v = w;
|
|
73
|
+
fv = fw;
|
|
74
|
+
w = x;
|
|
75
|
+
fw = fx;
|
|
76
|
+
x = u;
|
|
77
|
+
fx = fu;
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
if (u < x)
|
|
81
|
+
a = u;
|
|
82
|
+
else
|
|
83
|
+
b = u;
|
|
84
|
+
if (fu <= fw || w === x) {
|
|
85
|
+
v = w;
|
|
86
|
+
fv = fw;
|
|
87
|
+
w = u;
|
|
88
|
+
fw = fu;
|
|
89
|
+
}
|
|
90
|
+
else if (fu <= fv || v === x || v === w) {
|
|
91
|
+
v = u;
|
|
92
|
+
fv = fu;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return { x, fx, evaluations };
|
|
97
|
+
}
|
package/dist/random/beta.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// Cheng (1978) "Generating beta variates with nonintegral shape parameters"
|
|
4
4
|
// (Comm. ACM 21(4)): algorithm BB (min(a, b) > 1) and BC (min(a, b) <= 1).
|
|
5
5
|
// These are the algorithms cited in R's ?rbeta, with the constants from the paper. The order in which uniforms
|
|
6
|
-
// are consumed and the parameter handling follow observed R output.
|
|
6
|
+
// are consumed and the parameter handling follow observed R output.
|
|
7
7
|
import { unifRand } from "./rng.js";
|
|
8
8
|
import { generate } from "./common.js";
|
|
9
9
|
import { rchisq } from "./gamma.js";
|
package/dist/random/binomial.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// Kachitvichyanukul & Schmeiser (1988) "Binomial random variate generation"
|
|
4
4
|
// (Comm. ACM 31(2)): algorithm BTPE. When n min(p, 1-p) < 30, inversion (BINV in the paper) is used.
|
|
5
5
|
// These are the algorithms cited in R's ?rbinom, with the constants from the paper. The order in which uniforms
|
|
6
|
-
// are consumed and the parameter handling follow observed R output.
|
|
6
|
+
// are consumed and the parameter handling follow observed R output.
|
|
7
7
|
import { unifRand } from "./rng.js";
|
|
8
8
|
import { generate, powInt } from "./common.js";
|
|
9
9
|
import { qbinom } from "../distributions/binomial.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Continuous random variates generated by inversion and similar: runif, rnorm, rexp, rlnorm, rlogis, rcauchy, rweibull.
|
|
2
2
|
//
|
|
3
3
|
// Parameter handling (whether a uniform is consumed for NaN, 0 or Inf) follows observed R output
|
|
4
|
-
// (verified by next in test/fixtures/random*.json).
|
|
4
|
+
// (verified by next in test/fixtures/random*.json).
|
|
5
5
|
import { expRand, normRand, unifRand } from "./rng.js";
|
|
6
6
|
import { generate } from "./common.js";
|
|
7
7
|
/**
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Random numbers from the inverse gamma distribution. Equivalent to `extraDistr::rinvgamma(n, alpha, beta = 1)`.
|
|
3
|
+
*
|
|
4
|
+
* @param n Number of values to generate.
|
|
5
|
+
* @param alpha Shape.
|
|
6
|
+
* @param options.beta Scale. Default `1`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function rinvgamma(n: number, alpha: number, options?: {
|
|
9
|
+
beta?: number;
|
|
10
|
+
}): number[];
|
|
11
|
+
/** Random numbers from the Kumaraswamy distribution. Equivalent to `extraDistr::rkumar(n, a, b)`. */
|
|
12
|
+
export declare function rkumar(n: number, a: number, b: number): number[];
|
|
13
|
+
/** Random numbers from the Pareto distribution. Equivalent to `extraDistr::rpareto(n, a = 1, b = 1)`. */
|
|
14
|
+
export declare function rpareto(n: number, options?: {
|
|
15
|
+
a?: number;
|
|
16
|
+
b?: number;
|
|
17
|
+
}): number[];
|
|
18
|
+
/** Random numbers from the triangular distribution. Equivalent to `extraDistr::rtriang(n, a = -1, b = 1, c = (a + b)/2)`. */
|
|
19
|
+
export declare function rtriang(n: number, options?: {
|
|
20
|
+
a?: number;
|
|
21
|
+
b?: number;
|
|
22
|
+
c?: number;
|
|
23
|
+
}): number[];
|
|
24
|
+
/** Random numbers from the Laplace distribution. Equivalent to `extraDistr::rlaplace(n, mu = 0, sigma = 1)`. */
|
|
25
|
+
export declare function rlaplace(n: number, options?: {
|
|
26
|
+
mu?: number;
|
|
27
|
+
sigma?: number;
|
|
28
|
+
}): number[];
|
|
29
|
+
/** Random numbers from the arcsine distribution on (a, b), by inversion: `qarcsine(runif(n), a, b)`. */
|
|
30
|
+
export declare function rarcsine(n: number, options?: {
|
|
31
|
+
a?: number;
|
|
32
|
+
b?: number;
|
|
33
|
+
}): number[];
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Random variates of the distributions outside base R: rinvgamma, rkumar, rpareto, rtriang, rlaplace (as extraDistr
|
|
2
|
+
// draws them, so the same seed gives the same numbers) and rarcsine.
|
|
3
|
+
//
|
|
4
|
+
// Written for rStats.js (no ported code); the ways of drawing were taken from extraDistr's output and documentation:
|
|
5
|
+
// rinvgamma is 1 / rgamma(alpha, rate = beta); rkumar and rpareto invert the distribution function at 1 - u; rtriang
|
|
6
|
+
// mixes the minimum and maximum of two uniforms; rlaplace draws an exponential and then a uniform for the sign.
|
|
7
|
+
// Invalid parameters give NaN without drawing. rarcsine (not in extraDistr) inverts the distribution function at u.
|
|
8
|
+
import { qarcsine1 } from "../distributions/arcsine.js";
|
|
9
|
+
import { qkumar1 } from "../distributions/kumaraswamy.js";
|
|
10
|
+
import { qpareto1 } from "../distributions/pareto.js";
|
|
11
|
+
import { generate } from "./common.js";
|
|
12
|
+
import { rgamma1 } from "./gamma.js";
|
|
13
|
+
import { expRand, unifRand } from "./rng.js";
|
|
14
|
+
/**
|
|
15
|
+
* Random numbers from the inverse gamma distribution. Equivalent to `extraDistr::rinvgamma(n, alpha, beta = 1)`.
|
|
16
|
+
*
|
|
17
|
+
* @param n Number of values to generate.
|
|
18
|
+
* @param alpha Shape.
|
|
19
|
+
* @param options.beta Scale. Default `1`.
|
|
20
|
+
*/
|
|
21
|
+
export function rinvgamma(n, alpha, options = {}) {
|
|
22
|
+
const { beta = 1 } = options;
|
|
23
|
+
return generate(n, () => (alpha > 0 && beta > 0 && Number.isFinite(alpha) && Number.isFinite(beta) ? 1 / rgamma1(alpha, 1 / beta) : NaN));
|
|
24
|
+
}
|
|
25
|
+
/** Random numbers from the Kumaraswamy distribution. Equivalent to `extraDistr::rkumar(n, a, b)`. */
|
|
26
|
+
export function rkumar(n, a, b) {
|
|
27
|
+
return generate(n, () => (a > 0 && b > 0 && Number.isFinite(a) && Number.isFinite(b) ? qkumar1(1 - unifRand(), a, b, true, false) : NaN));
|
|
28
|
+
}
|
|
29
|
+
/** Random numbers from the Pareto distribution. Equivalent to `extraDistr::rpareto(n, a = 1, b = 1)`. */
|
|
30
|
+
export function rpareto(n, options = {}) {
|
|
31
|
+
const { a = 1, b = 1 } = options;
|
|
32
|
+
return generate(n, () => (a > 0 && b > 0 && Number.isFinite(a) && Number.isFinite(b) ? qpareto1(1 - unifRand(), a, b, true, false) : NaN));
|
|
33
|
+
}
|
|
34
|
+
/** Random numbers from the triangular distribution. Equivalent to `extraDistr::rtriang(n, a = -1, b = 1, c = (a + b)/2)`. */
|
|
35
|
+
export function rtriang(n, options = {}) {
|
|
36
|
+
const { a = -1, b = 1 } = options;
|
|
37
|
+
const c = options.c ?? (a + b) / 2;
|
|
38
|
+
return generate(n, () => {
|
|
39
|
+
if (!(Number.isFinite(a) && Number.isFinite(b) && a < b && a <= c && c <= b))
|
|
40
|
+
return NaN;
|
|
41
|
+
const u = unifRand();
|
|
42
|
+
const v = unifRand();
|
|
43
|
+
const cc = (c - a) / (b - a);
|
|
44
|
+
return a + (b - a) * ((1 - cc) * Math.min(u, v) + cc * Math.max(u, v));
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/** Random numbers from the Laplace distribution. Equivalent to `extraDistr::rlaplace(n, mu = 0, sigma = 1)`. */
|
|
48
|
+
export function rlaplace(n, options = {}) {
|
|
49
|
+
const { mu = 0, sigma = 1 } = options;
|
|
50
|
+
return generate(n, () => {
|
|
51
|
+
if (!(Number.isFinite(mu) && sigma > 0 && Number.isFinite(sigma)))
|
|
52
|
+
return NaN;
|
|
53
|
+
const e = expRand();
|
|
54
|
+
const s = unifRand() > 0.5 ? 1 : -1;
|
|
55
|
+
return mu + sigma * s * e;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/** Random numbers from the arcsine distribution on (a, b), by inversion: `qarcsine(runif(n), a, b)`. */
|
|
59
|
+
export function rarcsine(n, options = {}) {
|
|
60
|
+
const { a = 0, b = 1 } = options;
|
|
61
|
+
return generate(n, () => (Number.isFinite(a) && Number.isFinite(b) && a < b ? qarcsine1(unifRand(), a, b, true, false) : NaN));
|
|
62
|
+
}
|
package/dist/random/gamma.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// technique" (Comm. ACM 25(1)); for shape < 1, algorithm GS from Ahrens & Dieter (1974)
|
|
5
5
|
// "Computer methods for sampling from gamma, beta, Poisson and binomial distributions"
|
|
6
6
|
// (Computing 12). These are the algorithms cited in R's ?rgamma, with the constants from the paper.
|
|
7
|
-
// The order in which uniforms are consumed and the parameter handling follow observed R output.
|
|
7
|
+
// The order in which uniforms are consumed and the parameter handling follow observed R output.
|
|
8
8
|
import { expRand, normRand, unifRand } from "./rng.js";
|
|
9
9
|
import { gammaScale } from "../distributions/gamma.js";
|
|
10
10
|
import { generate } from "./common.js";
|
package/dist/random/hyper.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// Kachitvichyanukul & Schmeiser (1985) "Computer generation of hypergeometric random
|
|
4
4
|
// variates" (J. Statist. Comput. Simul. 22): algorithm HIN (inversion when the mode is near the lower end) and
|
|
5
5
|
// H2PE (rejection over three regions). These are the algorithms cited in R's ?rhyper, with the constants from the paper.
|
|
6
|
-
// The order in which uniforms are consumed and the parameter handling follow observed R output.
|
|
6
|
+
// The order in which uniforms are consumed and the parameter handling follow observed R output.
|
|
7
7
|
import { unifRand } from "./rng.js";
|
|
8
8
|
import { generate } from "./common.js";
|
|
9
9
|
import { rbinom1 } from "./binomial.js";
|
package/dist/random/mt.js
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
// equidistributed uniform pseudo-random number generator" (ACM TOMACS 8(1)).
|
|
5
5
|
// How set.seed() expands the seed and maps to [0, 1) was verified against R's ?RNG documentation and
|
|
6
6
|
// observations of R's .Random.seed and runif() output (test/fixtures/random*.json).
|
|
7
|
-
// R's source was not consulted.
|
|
8
7
|
const N = 624;
|
|
9
8
|
const M = 397;
|
|
10
9
|
const MATRIX_A = 0x9908b0df;
|
package/dist/random/poisson.js
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
// distributions" (ACM TOMS 8(2)): algorithm PD. For mu < 10, table-based inversion (Case B in the paper);
|
|
5
5
|
// for mu >= 10, normal approximation with rejection (Case A). These are the algorithms cited in R's ?rpois,
|
|
6
6
|
// with the constants from the paper. The order in which uniforms are consumed and the parameter handling follow observed R output.
|
|
7
|
-
// R's source was not consulted.
|
|
8
7
|
import { expRand, normRand, unifRand } from "./rng.js";
|
|
9
8
|
import { generate } from "./common.js";
|
|
10
9
|
import { rgamma1 } from "./gamma.js";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** log k! for k = 0..n, accumulated as sums of logs (the table AS 159 uses) */
|
|
2
|
+
export declare function logFactorialTable(n: number): Float64Array;
|
|
3
|
+
/**
|
|
4
|
+
* One random table with row totals `r` and column totals `c` (equal sums, non-negative integers), filled into `out`
|
|
5
|
+
* column by column (out[i + j * nr]). `fact` is {@link logFactorialTable} of at least the grand total.
|
|
6
|
+
*/
|
|
7
|
+
export declare function rcont2(r: readonly number[], c: readonly number[], fact: Float64Array, out: Int32Array): void;
|
|
8
|
+
/**
|
|
9
|
+
* Random two-way tables with given row and column totals. Equivalent to R's `r2dtable()`.
|
|
10
|
+
*
|
|
11
|
+
* Uses Patefield's (1981) algorithm and consumes the same uniforms as R, so after `setSeed` the tables are R's.
|
|
12
|
+
* Each table is returned as an array of rows (R returns a list of matrices).
|
|
13
|
+
*
|
|
14
|
+
* @param n Number of tables (fractions are truncated, as in R).
|
|
15
|
+
* @param r Row totals (non-negative integers, at least two).
|
|
16
|
+
* @param c Column totals (non-negative integers, at least two), with the same sum as `r`.
|
|
17
|
+
* @returns An array of `n` tables, each `r.length` rows of `c.length` counts.
|
|
18
|
+
* @throws RangeError for invalid arguments or totals that differ.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* setSeed(1);
|
|
23
|
+
* r2dtable(1, [3, 2], [2, 3]); // [[[1, 2], [1, 1]]] (R: set.seed(1); r2dtable(1, c(3, 2), c(2, 3)))
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function r2dtable(n: number, r: readonly number[], c: readonly number[]): number[][][];
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Random two-way tables with given margins: r2dtable, and the generator behind the simulated p-values of chisqTest
|
|
2
|
+
// and fisherTest.
|
|
3
|
+
//
|
|
4
|
+
// Written for rStats.js from Patefield, W. M. (1981), "Algorithm AS 159: An efficient method of generating random
|
|
5
|
+
// R x C tables with given row and column totals", Applied Statistics 30, 91-97 (no ported code). The table is filled
|
|
6
|
+
// row by row, left to right. Given what is left of the row total (ia), of the column total (id) and of the grand total
|
|
7
|
+
// over this and the later columns of the remaining rows (ie), the cell follows a hypergeometric distribution. One
|
|
8
|
+
// uniform u is drawn per cell; the search starts at the mode floor(ia id / ie + 1/2) and moves alternately one step up
|
|
9
|
+
// and one step down, adding the probabilities (updated by their ratios), until the sum reaches u. If both ends of the
|
|
10
|
+
// support are reached first (the sum fell short of u by rounding), u is replaced by sum * (a new uniform) and the
|
|
11
|
+
// search starts again at the mode. The last column of each row and the last row follow from the margins. A cell
|
|
12
|
+
// whose remaining total ie is 0 (all later rows and columns empty) is 0 without a draw. This consumes uniforms as R's
|
|
13
|
+
// r2dtable does (checked against R's output).
|
|
14
|
+
import { unifRand } from "./rng.js";
|
|
15
|
+
/** log k! for k = 0..n, accumulated as sums of logs (the table AS 159 uses) */
|
|
16
|
+
export function logFactorialTable(n) {
|
|
17
|
+
const f = new Float64Array(n + 1);
|
|
18
|
+
for (let i = 2; i <= n; i++)
|
|
19
|
+
f[i] = f[i - 1] + Math.log(i);
|
|
20
|
+
return f;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* One random table with row totals `r` and column totals `c` (equal sums, non-negative integers), filled into `out`
|
|
24
|
+
* column by column (out[i + j * nr]). `fact` is {@link logFactorialTable} of at least the grand total.
|
|
25
|
+
*/
|
|
26
|
+
export function rcont2(r, c, fact, out) {
|
|
27
|
+
const nr = r.length;
|
|
28
|
+
const nc = c.length;
|
|
29
|
+
const colLeft = Array.from(c);
|
|
30
|
+
let rest = 0; // grand total of the rows not yet filled
|
|
31
|
+
for (const v of r)
|
|
32
|
+
rest += v;
|
|
33
|
+
for (let l = 0; l < nr - 1; l++) {
|
|
34
|
+
let ia = r[l];
|
|
35
|
+
let ic = rest; // total of the cells to the right of (and including) the current column, in rows l..nr-1
|
|
36
|
+
rest -= ia;
|
|
37
|
+
let m = 0;
|
|
38
|
+
for (; m < nc - 1; m++) {
|
|
39
|
+
const id = colLeft[m];
|
|
40
|
+
const ie = ic;
|
|
41
|
+
ic -= id;
|
|
42
|
+
const ib = ie - ia;
|
|
43
|
+
const ii = ib - id;
|
|
44
|
+
if (ie === 0) {
|
|
45
|
+
// nothing left in this block: the rest of the row is 0
|
|
46
|
+
for (let j = m; j < nc - 1; j++)
|
|
47
|
+
out[l + j * nr] = 0;
|
|
48
|
+
ia = 0;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
let u = unifRand();
|
|
52
|
+
let nlm;
|
|
53
|
+
search: for (;;) {
|
|
54
|
+
// mode of the conditional distribution and its probability
|
|
55
|
+
nlm = Math.floor(ia * (id / ie) + 0.5);
|
|
56
|
+
let x = Math.exp(fact[ia] + fact[ib] + fact[ic] + fact[id] - fact[ie] - fact[nlm] - fact[id - nlm] - fact[ia - nlm] - fact[ii + nlm]);
|
|
57
|
+
if (x >= u)
|
|
58
|
+
break;
|
|
59
|
+
if (x === 0)
|
|
60
|
+
throw new Error(`r2dtable: probability underflow at cell [${l}, ${m}]`);
|
|
61
|
+
let sum = x;
|
|
62
|
+
let y = x;
|
|
63
|
+
let low = nlm;
|
|
64
|
+
let upDone = false;
|
|
65
|
+
let downDone = false;
|
|
66
|
+
while (!(upDone && downDone)) {
|
|
67
|
+
if (!upDone) {
|
|
68
|
+
const j = (id - nlm) * (ia - nlm);
|
|
69
|
+
if (j === 0)
|
|
70
|
+
upDone = true;
|
|
71
|
+
else {
|
|
72
|
+
nlm++;
|
|
73
|
+
x = (x * j) / (nlm * (ii + nlm));
|
|
74
|
+
sum += x;
|
|
75
|
+
if (sum >= u)
|
|
76
|
+
break search;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// a step down after each step up; only down once the top is reached
|
|
80
|
+
do {
|
|
81
|
+
const j = low * (ii + low);
|
|
82
|
+
if (j === 0) {
|
|
83
|
+
downDone = true;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
low--;
|
|
87
|
+
y = (y * j) / ((id - low) * (ia - low));
|
|
88
|
+
sum += y;
|
|
89
|
+
if (sum >= u) {
|
|
90
|
+
nlm = low;
|
|
91
|
+
break search;
|
|
92
|
+
}
|
|
93
|
+
} while (upDone);
|
|
94
|
+
}
|
|
95
|
+
u = sum * unifRand();
|
|
96
|
+
}
|
|
97
|
+
out[l + m * nr] = nlm;
|
|
98
|
+
ia -= nlm;
|
|
99
|
+
colLeft[m] = colLeft[m] - nlm;
|
|
100
|
+
}
|
|
101
|
+
out[l + (nc - 1) * nr] = ia;
|
|
102
|
+
}
|
|
103
|
+
// the last row is what is left of the column totals
|
|
104
|
+
let last = c[nc - 1];
|
|
105
|
+
for (let l = 0; l < nr - 1; l++)
|
|
106
|
+
last -= out[l + (nc - 1) * nr];
|
|
107
|
+
for (let m = 0; m < nc - 1; m++)
|
|
108
|
+
out[nr - 1 + m * nr] = colLeft[m];
|
|
109
|
+
out[nr - 1 + (nc - 1) * nr] = last;
|
|
110
|
+
}
|
|
111
|
+
/** Margins as integers (R's as.integer truncates), checked as R's r2dtable does */
|
|
112
|
+
function margins(v, name) {
|
|
113
|
+
const out = Array.from(v, (e) => Math.trunc(e));
|
|
114
|
+
if (out.length <= 1 || out.some((e) => !(e >= 0) || e > 2147483647))
|
|
115
|
+
throw new RangeError(`invalid argument '${name}'`);
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Random two-way tables with given row and column totals. Equivalent to R's `r2dtable()`.
|
|
120
|
+
*
|
|
121
|
+
* Uses Patefield's (1981) algorithm and consumes the same uniforms as R, so after `setSeed` the tables are R's.
|
|
122
|
+
* Each table is returned as an array of rows (R returns a list of matrices).
|
|
123
|
+
*
|
|
124
|
+
* @param n Number of tables (fractions are truncated, as in R).
|
|
125
|
+
* @param r Row totals (non-negative integers, at least two).
|
|
126
|
+
* @param c Column totals (non-negative integers, at least two), with the same sum as `r`.
|
|
127
|
+
* @returns An array of `n` tables, each `r.length` rows of `c.length` counts.
|
|
128
|
+
* @throws RangeError for invalid arguments or totals that differ.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts
|
|
132
|
+
* setSeed(1);
|
|
133
|
+
* r2dtable(1, [3, 2], [2, 3]); // [[[1, 2], [1, 1]]] (R: set.seed(1); r2dtable(1, c(3, 2), c(2, 3)))
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
export function r2dtable(n, r, c) {
|
|
137
|
+
const count = Math.trunc(n);
|
|
138
|
+
if (!(count >= 0))
|
|
139
|
+
throw new RangeError("invalid argument 'n'");
|
|
140
|
+
const rows = margins(r, "r");
|
|
141
|
+
const cols = margins(c, "c");
|
|
142
|
+
const total = rows.reduce((a, b) => a + b, 0);
|
|
143
|
+
if (total !== cols.reduce((a, b) => a + b, 0))
|
|
144
|
+
throw new RangeError("arguments 'r' and 'c' must have the same sums");
|
|
145
|
+
const fact = logFactorialTable(total);
|
|
146
|
+
const out = new Int32Array(rows.length * cols.length);
|
|
147
|
+
const tables = [];
|
|
148
|
+
for (let k = 0; k < count; k++) {
|
|
149
|
+
rcont2(rows, cols, fact, out);
|
|
150
|
+
tables.push(rows.map((_, i) => cols.map((_, j) => out[i + j * rows.length])));
|
|
151
|
+
}
|
|
152
|
+
return tables;
|
|
153
|
+
}
|
package/dist/random/rank.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Random Wilcoxon rank statistics: rsignrank and rwilcox.
|
|
2
2
|
// Generated by definition (signs chosen by uniforms; ranks sampled without replacement).
|
|
3
|
-
// Uniform consumption follows observed R output.
|
|
3
|
+
// Uniform consumption follows observed R output.
|
|
4
4
|
import { unifIndex, unifRand } from "./rng.js";
|
|
5
5
|
import { generate } from "./common.js";
|
|
6
6
|
/**
|
package/dist/random/rng.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
//
|
|
3
3
|
// As in R, there is a single state (set by setSeed), corresponding to R's default
|
|
4
4
|
// RNGkind("Mersenne-Twister", "Inversion", "Rejection").
|
|
5
|
-
// The procedures were verified against R's ?RNG and ?Random documentation and observed output.
|
|
5
|
+
// The procedures were verified against R's ?RNG and ?Random documentation and observed output.
|
|
6
6
|
import { MersenneTwister } from "./mt.js";
|
|
7
7
|
import { ppnd16 } from "./as241.js";
|
|
8
8
|
/** Value of .Random.seed[1]: uniform 3 (Mersenne-Twister) + 100 × normal 4 (Inversion) + 10000 × sample 1 (Rejection) */
|
package/dist/random/sample.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// Weighted sampling sorts the weights in decreasing order and selects by cumulative probability (with replacement,
|
|
5
5
|
// when more than 200 candidates have large weights, Walker's (1977) alias method). The sort is a heapsort,
|
|
6
6
|
// and the order of equal weights also matches R.
|
|
7
|
-
// All of this was verified against observed R output.
|
|
7
|
+
// All of this was verified against observed R output.
|
|
8
8
|
import { unifIndex, unifRand } from "./rng.js";
|
|
9
9
|
/**
|
|
10
10
|
* Random sample of integers from `0..n-1`. Equivalent to R's `sample.int()`, but 0-based.
|