@numero-uno-jpn/rstats 0.0.0-stage → 0.6.1
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 +144 -0
- package/LICENSE +21 -0
- package/README.ja.md +197 -0
- package/README.md +188 -2
- package/THIRD_PARTY_NOTICES.md +317 -0
- package/dist/chunks/chunk-2JCIB5H2.js +2 -0
- package/dist/chunks/chunk-7JVSDGQQ.js +2 -0
- package/dist/chunks/chunk-7NK4SA6B.js +2 -0
- package/dist/chunks/chunk-AJYQNFXK.js +2 -0
- package/dist/chunks/chunk-DRNMIEYH.js +2 -0
- package/dist/chunks/chunk-NAYDS7CL.js +2 -0
- package/dist/chunks/chunk-NOCEAM44.js +2 -0
- package/dist/chunks/chunk-QNAIK4PY.js +2 -0
- package/dist/chunks/chunk-TPC6MTYC.js +2 -0
- package/dist/chunks/chunk-UGUQYWRQ.js +2 -0
- package/dist/chunks/chunk-XM2TVD63.js +2 -0
- package/dist/chunks/chunk-Y4IZV7XK.js +2 -0
- package/dist/chunks/chunk-ZWJ42QN6.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/beta.d.ts +62 -0
- package/dist/distributions/beta.js +246 -0
- package/dist/distributions/binomial.d.ts +41 -0
- package/dist/distributions/binomial.js +136 -0
- package/dist/distributions/cauchy.d.ts +34 -0
- package/dist/distributions/cauchy.js +139 -0
- package/dist/distributions/chisq.d.ts +42 -0
- package/dist/distributions/chisq.js +60 -0
- package/dist/distributions/exponential.d.ts +34 -0
- package/dist/distributions/exponential.js +93 -0
- package/dist/distributions/f.d.ts +47 -0
- package/dist/distributions/f.js +214 -0
- package/dist/distributions/gamma.d.ts +61 -0
- package/dist/distributions/gamma.js +188 -0
- package/dist/distributions/geometric.d.ts +38 -0
- package/dist/distributions/geometric.js +116 -0
- package/dist/distributions/hypergeometric.d.ts +38 -0
- package/dist/distributions/hypergeometric.js +234 -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/logistic.d.ts +36 -0
- package/dist/distributions/logistic.js +94 -0
- package/dist/distributions/lognormal.d.ts +39 -0
- package/dist/distributions/lognormal.js +158 -0
- package/dist/distributions/multinomial.d.ts +24 -0
- package/dist/distributions/multinomial.js +70 -0
- package/dist/distributions/nbeta.d.ts +7 -0
- package/dist/distributions/nbeta.js +330 -0
- package/dist/distributions/nchisq.d.ts +3 -0
- package/dist/distributions/nchisq.js +234 -0
- package/dist/distributions/negbinomial.d.ts +54 -0
- package/dist/distributions/negbinomial.js +325 -0
- package/dist/distributions/normal.d.ts +66 -0
- package/dist/distributions/normal.js +184 -0
- package/dist/distributions/nt.d.ts +3 -0
- package/dist/distributions/nt.js +491 -0
- package/dist/distributions/pareto.d.ts +23 -0
- package/dist/distributions/pareto.js +76 -0
- package/dist/distributions/poisson.d.ts +41 -0
- package/dist/distributions/poisson.js +104 -0
- package/dist/distributions/signrank.d.ts +38 -0
- package/dist/distributions/signrank.js +216 -0
- package/dist/distributions/summary.d.ts +25 -0
- package/dist/distributions/summary.js +289 -0
- package/dist/distributions/t.d.ts +49 -0
- package/dist/distributions/t.js +235 -0
- package/dist/distributions/triangular.d.ts +25 -0
- package/dist/distributions/triangular.js +87 -0
- package/dist/distributions/tukey.d.ts +43 -0
- package/dist/distributions/tukey.js +985 -0
- package/dist/distributions/uniform.d.ts +36 -0
- package/dist/distributions/uniform.js +90 -0
- package/dist/distributions/weibull.d.ts +37 -0
- package/dist/distributions/weibull.js +125 -0
- package/dist/distributions/wilcox.d.ts +47 -0
- package/dist/distributions/wilcox.js +270 -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 +41 -0
- package/dist/entries/distributions.js +41 -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 +9 -0
- package/dist/entries/special.js +9 -0
- package/dist/entries/tests.d.ts +21 -0
- package/dist/entries/tests.js +20 -0
- package/dist/htest/ansariTest.d.ts +13 -0
- package/dist/htest/ansariTest.js +80 -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 +55 -0
- package/dist/htest/chisqTest.js +160 -0
- package/dist/htest/cor.d.ts +95 -0
- package/dist/htest/cor.js +437 -0
- package/dist/htest/core/ansari.d.ts +18 -0
- package/dist/htest/core/ansari.js +121 -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/smirnov.d.ts +20 -0
- package/dist/htest/core/smirnov.js +75 -0
- package/dist/htest/fisherTest.d.ts +48 -0
- package/dist/htest/fisherTest.js +482 -0
- 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/pAdjust.d.ts +34 -0
- package/dist/htest/pAdjust.js +147 -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/tTest.d.ts +57 -0
- package/dist/htest/tTest.js +153 -0
- package/dist/htest/types.d.ts +51 -0
- package/dist/htest/types.js +26 -0
- package/dist/htest/varTest.d.ts +48 -0
- package/dist/htest/varTest.js +118 -0
- package/dist/htest/wilcoxTest.d.ts +61 -0
- package/dist/htest/wilcoxTest.js +433 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +8 -0
- package/dist/integrate/quadpack.d.ts +29 -0
- package/dist/integrate/quadpack.js +525 -0
- package/dist/internal/ddexp.d.ts +19 -0
- package/dist/internal/ddexp.js +94 -0
- package/dist/internal/discrete.d.ts +14 -0
- package/dist/internal/discrete.js +85 -0
- package/dist/internal/dpq.d.ts +43 -0
- package/dist/internal/dpq.js +54 -0
- package/dist/internal/integer.d.ts +4 -0
- package/dist/internal/integer.js +17 -0
- package/dist/internal/invert.d.ts +36 -0
- package/dist/internal/invert.js +124 -0
- package/dist/internal/noncentral.d.ts +12 -0
- package/dist/internal/noncentral.js +2 -0
- package/dist/internal/poissonMixture.d.ts +31 -0
- package/dist/internal/poissonMixture.js +401 -0
- package/dist/internal/seq.d.ts +5 -0
- package/dist/internal/seq.js +14 -0
- package/dist/internal/summary.d.ts +21 -0
- package/dist/internal/summary.js +125 -0
- package/dist/internal/uniroot.d.ts +11 -0
- package/dist/internal/uniroot.js +14 -0
- package/dist/internal/vectorize.d.ts +12 -0
- package/dist/internal/vectorize.js +7 -0
- package/dist/linalg/basic.d.ts +95 -0
- package/dist/linalg/basic.js +152 -0
- package/dist/linalg/chol.d.ts +83 -0
- package/dist/linalg/chol.js +200 -0
- package/dist/linalg/eigen.d.ts +52 -0
- package/dist/linalg/eigen.js +753 -0
- package/dist/linalg/kappa.d.ts +51 -0
- package/dist/linalg/kappa.js +199 -0
- package/dist/linalg/lu.d.ts +45 -0
- package/dist/linalg/lu.js +88 -0
- package/dist/linalg/matrix.d.ts +29 -0
- package/dist/linalg/matrix.js +65 -0
- package/dist/linalg/norm.d.ts +32 -0
- package/dist/linalg/norm.js +92 -0
- package/dist/linalg/ops.d.ts +90 -0
- package/dist/linalg/ops.js +320 -0
- package/dist/linalg/qr.d.ts +264 -0
- package/dist/linalg/qr.js +494 -0
- package/dist/linalg/solve.d.ts +123 -0
- package/dist/linalg/solve.js +380 -0
- package/dist/linalg/svd.d.ts +38 -0
- package/dist/linalg/svd.js +275 -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/as241.d.ts +2 -0
- package/dist/random/as241.js +58 -0
- package/dist/random/beta.d.ts +14 -0
- package/dist/random/beta.js +117 -0
- package/dist/random/binomial.d.ts +25 -0
- package/dist/random/binomial.js +224 -0
- package/dist/random/common.d.ts +4 -0
- package/dist/random/common.js +35 -0
- package/dist/random/continuous.d.ts +91 -0
- package/dist/random/continuous.js +138 -0
- package/dist/random/extra.d.ts +33 -0
- package/dist/random/extra.js +62 -0
- package/dist/random/gamma.d.ts +52 -0
- package/dist/random/gamma.js +217 -0
- package/dist/random/hyper.d.ts +12 -0
- package/dist/random/hyper.js +240 -0
- package/dist/random/mt.d.ts +16 -0
- package/dist/random/mt.js +96 -0
- package/dist/random/poisson.d.ts +33 -0
- package/dist/random/poisson.js +203 -0
- package/dist/random/rank.d.ts +19 -0
- package/dist/random/rank.js +62 -0
- package/dist/random/rng.d.ts +46 -0
- package/dist/random/rng.js +148 -0
- package/dist/random/sample.d.ts +45 -0
- package/dist/random/sample.js +255 -0
- package/dist/rstats.js +2 -0
- 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.d.ts +30 -0
- package/dist/special/beta.js +169 -0
- package/dist/special/choose.d.ts +75 -0
- package/dist/special/choose.js +233 -0
- package/dist/special/constants.d.ts +16 -0
- package/dist/special/constants.js +19 -0
- package/dist/special/dd.d.ts +29 -0
- package/dist/special/dd.js +693 -0
- package/dist/special/erf.d.ts +35 -0
- package/dist/special/erf.js +112 -0
- package/dist/special/extra.d.ts +47 -0
- package/dist/special/extra.js +220 -0
- package/dist/special/gamma.d.ts +32 -0
- package/dist/special/gamma.js +270 -0
- package/dist/special/igam.d.ts +15 -0
- package/dist/special/igam.js +346 -0
- package/dist/special/igamAsympCoeff.d.ts +4 -0
- package/dist/special/igamAsympCoeff.js +233 -0
- package/dist/special/igami.d.ts +2 -0
- package/dist/special/igami.js +135 -0
- package/dist/special/incbet.d.ts +25 -0
- package/dist/special/incbet.js +381 -0
- package/dist/special/lgammaDiff.d.ts +9 -0
- package/dist/special/lgammaDiff.js +47 -0
- package/dist/special/loader.d.ts +43 -0
- package/dist/special/loader.js +456 -0
- package/dist/special/log1pmx.d.ts +2 -0
- package/dist/special/log1pmx.js +20 -0
- package/dist/special/ndtri.d.ts +4 -0
- package/dist/special/ndtri.js +82 -0
- package/dist/special/polevl.d.ts +4 -0
- package/dist/special/polevl.js +17 -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 +2 -0
- package/package.json +93 -4
package/dist/numerics.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! rStats.js 0.6.1 | 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-TPC6MTYC.js";import"./chunks/chunk-DRNMIEYH.js";import{a as M,f as j}from"./chunks/chunk-NOCEAM44.js";import{b as S}from"./chunks/chunk-7NK4SA6B.js";import"./chunks/chunk-UGUQYWRQ.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
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Standard normal quantile (PPND16 from Wichura 1988, Algorithm AS 241 "The percentage points of the
|
|
2
|
+
// normal distribution", Applied Statistics 37(3)).
|
|
3
|
+
//
|
|
4
|
+
// Used only to transform uniforms into normal variates (inversion). As documented in ?qnorm, R's rnorm()
|
|
5
|
+
// transforms uniforms with this algorithm, so computing with the same formulas gives the same variates as R
|
|
6
|
+
// (qnorm in src/distributions/normal.ts uses a different algorithm and may differ in the last digit).
|
|
7
|
+
// The p used for random numbers exceeds 2^-54, so only the range covered by the paper (r <= 27) is handled here.
|
|
8
|
+
const A = [
|
|
9
|
+
3.387132872796366608, 133.14166789178437745, 1971.5909503065514427, 13731.693765509461125,
|
|
10
|
+
45921.953931549871457, 67265.770927008700853, 33430.575583588128105, 2509.0809287301226727,
|
|
11
|
+
];
|
|
12
|
+
const B = [
|
|
13
|
+
1, 42.313330701600911252, 687.1870074920579083, 5394.1960214247511077, 21213.794301586595867,
|
|
14
|
+
39307.89580009271061, 28729.085735721942674, 5226.495278852854561,
|
|
15
|
+
];
|
|
16
|
+
const C = [
|
|
17
|
+
1.42343711074968357734, 4.6303378461565452959, 5.7694972214606914055, 3.64784832476320460504,
|
|
18
|
+
1.27045825245236838258, 0.24178072517745061177, 0.0227238449892691845833, 7.7454501427834140764e-4,
|
|
19
|
+
];
|
|
20
|
+
const D = [
|
|
21
|
+
1, 2.05319162663775882187, 1.6763848301838038494, 0.68976733498510000455, 0.14810397642748007459,
|
|
22
|
+
0.0151986665636164571966, 5.475938084995344946e-4, 1.05075007164441684324e-9,
|
|
23
|
+
];
|
|
24
|
+
const E = [
|
|
25
|
+
6.6579046435011037772, 5.4637849111641143699, 1.7848265399172913358, 0.29656057182850489123,
|
|
26
|
+
0.026532189526576123093, 0.0012426609473880784386, 2.71155556874348757815e-5, 2.01033439929228813265e-7,
|
|
27
|
+
];
|
|
28
|
+
const F = [
|
|
29
|
+
1, 0.59983220655588793769, 0.13692988092273580531, 0.0148753612908506148525, 7.868691311456132591e-4,
|
|
30
|
+
1.8463183175100546818e-5, 1.4215117583164458887e-7, 2.04426310338993978564e-15,
|
|
31
|
+
];
|
|
32
|
+
/** c[7] r^7 + ... + c[0] by Horner's method */
|
|
33
|
+
function poly(c, r) {
|
|
34
|
+
let s = c[7];
|
|
35
|
+
for (let i = 6; i >= 0; i--)
|
|
36
|
+
s = s * r + c[i];
|
|
37
|
+
return s;
|
|
38
|
+
}
|
|
39
|
+
/** Standard normal quantile for 0 < p < 1 */
|
|
40
|
+
export function ppnd16(p) {
|
|
41
|
+
const q = p - 0.5;
|
|
42
|
+
if (Math.abs(q) <= 0.425) {
|
|
43
|
+
const r = 0.180625 - q * q;
|
|
44
|
+
return (q * poly(A, r)) / poly(B, r);
|
|
45
|
+
}
|
|
46
|
+
let r = q < 0 ? p : 1 - p;
|
|
47
|
+
r = Math.sqrt(-Math.log(r));
|
|
48
|
+
let val;
|
|
49
|
+
if (r <= 5) {
|
|
50
|
+
r -= 1.6;
|
|
51
|
+
val = poly(C, r) / poly(D, r);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
r -= 5;
|
|
55
|
+
val = poly(E, r) / poly(F, r);
|
|
56
|
+
}
|
|
57
|
+
return q < 0 ? -val : val;
|
|
58
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** One beta variate (shape1 aa, shape2 bb) */
|
|
2
|
+
export declare function rbeta1(aa: number, bb: number): number;
|
|
3
|
+
/**
|
|
4
|
+
* Random numbers from the beta distribution. Equivalent to R's `rbeta()`.
|
|
5
|
+
*
|
|
6
|
+
* @param n Number of values to generate (fractions are truncated, as in R).
|
|
7
|
+
* @param shape1 First shape parameter.
|
|
8
|
+
* @param shape2 Second shape parameter.
|
|
9
|
+
* @param options.ncp Non-centrality parameter. If omitted, the central distribution is used.
|
|
10
|
+
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
11
|
+
*/
|
|
12
|
+
export declare function rbeta(n: number, shape1: number, shape2: number, options?: {
|
|
13
|
+
ncp?: number;
|
|
14
|
+
}): number[];
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Beta random variates.
|
|
2
|
+
//
|
|
3
|
+
// Cheng (1978) "Generating beta variates with nonintegral shape parameters"
|
|
4
|
+
// (Comm. ACM 21(4)): algorithm BB (min(a, b) > 1) and BC (min(a, b) <= 1).
|
|
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.
|
|
7
|
+
import { unifRand } from "./rng.js";
|
|
8
|
+
import { generate } from "./common.js";
|
|
9
|
+
import { rchisq } from "./gamma.js";
|
|
10
|
+
/** The value of log(DBL_MAX) (1024 log 2) */
|
|
11
|
+
const EXPMAX = 1024 * Math.LN2;
|
|
12
|
+
let beta = 0, gamma = 0, delta = 0, k1 = 0, k2 = 0;
|
|
13
|
+
let olda = -1, oldb = -1;
|
|
14
|
+
/** v = beta log(u1 / (1 - u1)) and w = aa e^v (DBL_MAX on overflow) */
|
|
15
|
+
function vw(u1, aa) {
|
|
16
|
+
const v = beta * Math.log(u1 / (1 - u1));
|
|
17
|
+
if (v <= EXPMAX) {
|
|
18
|
+
let w = aa * Math.exp(v);
|
|
19
|
+
if (!Number.isFinite(w))
|
|
20
|
+
w = Number.MAX_VALUE;
|
|
21
|
+
return [v, w];
|
|
22
|
+
}
|
|
23
|
+
return [v, Number.MAX_VALUE];
|
|
24
|
+
}
|
|
25
|
+
/** One beta variate (shape1 aa, shape2 bb) */
|
|
26
|
+
export function rbeta1(aa, bb) {
|
|
27
|
+
if (Number.isNaN(aa) || Number.isNaN(bb) || aa < 0 || bb < 0)
|
|
28
|
+
return NaN;
|
|
29
|
+
if (!Number.isFinite(aa) && !Number.isFinite(bb))
|
|
30
|
+
return 0.5;
|
|
31
|
+
if (aa === 0 && bb === 0)
|
|
32
|
+
return unifRand() < 0.5 ? 0 : 1;
|
|
33
|
+
if (!Number.isFinite(aa) || bb === 0)
|
|
34
|
+
return 1;
|
|
35
|
+
if (!Number.isFinite(bb) || aa === 0)
|
|
36
|
+
return 0;
|
|
37
|
+
const qsame = olda === aa && oldb === bb;
|
|
38
|
+
if (!qsame) {
|
|
39
|
+
olda = aa;
|
|
40
|
+
oldb = bb;
|
|
41
|
+
}
|
|
42
|
+
const a = Math.min(aa, bb);
|
|
43
|
+
const b = Math.max(aa, bb);
|
|
44
|
+
const alpha = a + b;
|
|
45
|
+
let v, w;
|
|
46
|
+
if (a <= 1) {
|
|
47
|
+
// Algorithm BC
|
|
48
|
+
if (!qsame) {
|
|
49
|
+
beta = 1 / a;
|
|
50
|
+
delta = 1 + b - a;
|
|
51
|
+
k1 = (delta * (0.0138889 + 0.0416667 * a)) / (b * beta - 0.777778);
|
|
52
|
+
k2 = 0.25 + (0.5 + 0.25 / delta) * a;
|
|
53
|
+
}
|
|
54
|
+
for (;;) {
|
|
55
|
+
const u1 = unifRand();
|
|
56
|
+
const u2 = unifRand();
|
|
57
|
+
let z;
|
|
58
|
+
if (u1 < 0.5) {
|
|
59
|
+
const y = u1 * u2;
|
|
60
|
+
z = u1 * y;
|
|
61
|
+
if (0.25 * u2 + z - y >= k1)
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
z = u1 * u1 * u2;
|
|
66
|
+
if (z <= 0.25) {
|
|
67
|
+
[v, w] = vw(u1, b);
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
if (z >= k2)
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
[v, w] = vw(u1, b);
|
|
74
|
+
if (alpha * (Math.log(alpha / (a + w)) + v) - 1.3862944 >= Math.log(z))
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
return aa === a ? a / (a + w) : w / (a + w);
|
|
78
|
+
}
|
|
79
|
+
// Algorithm BB
|
|
80
|
+
if (!qsame) {
|
|
81
|
+
beta = Math.sqrt((alpha - 2) / (2 * a * b - alpha));
|
|
82
|
+
gamma = a + 1 / beta;
|
|
83
|
+
}
|
|
84
|
+
let r, t;
|
|
85
|
+
do {
|
|
86
|
+
const u1 = unifRand();
|
|
87
|
+
const u2 = unifRand();
|
|
88
|
+
[v, w] = vw(u1, a);
|
|
89
|
+
const z = u1 * u1 * u2;
|
|
90
|
+
r = gamma * v - 1.3862944;
|
|
91
|
+
const s = a + r - w;
|
|
92
|
+
if (s + 2.609438 >= 5 * z)
|
|
93
|
+
break;
|
|
94
|
+
t = Math.log(z);
|
|
95
|
+
if (s > t)
|
|
96
|
+
break;
|
|
97
|
+
} while (r + alpha * Math.log(alpha / (b + w)) < t);
|
|
98
|
+
return aa !== a ? b / (b + w) : w / (b + w);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Random numbers from the beta distribution. Equivalent to R's `rbeta()`.
|
|
102
|
+
*
|
|
103
|
+
* @param n Number of values to generate (fractions are truncated, as in R).
|
|
104
|
+
* @param shape1 First shape parameter.
|
|
105
|
+
* @param shape2 Second shape parameter.
|
|
106
|
+
* @param options.ncp Non-centrality parameter. If omitted, the central distribution is used.
|
|
107
|
+
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
108
|
+
*/
|
|
109
|
+
export function rbeta(n, shape1, shape2, options = {}) {
|
|
110
|
+
const { ncp } = options;
|
|
111
|
+
if (ncp === undefined)
|
|
112
|
+
return generate(n, () => rbeta1(shape1, shape2));
|
|
113
|
+
// Noncentral beta: as in R, generate n draws X ~ noncentral χ²(2 shape1, ncp), then n draws Y ~ χ²(2 shape2), and return X / (X + Y)
|
|
114
|
+
const x = rchisq(n, 2 * shape1, { ncp });
|
|
115
|
+
const y = rchisq(n, 2 * shape2);
|
|
116
|
+
return x.map((xi, i) => xi / (xi + y[i]));
|
|
117
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** One binomial variate (nin trials, success probability pp) */
|
|
2
|
+
export declare function rbinom1(nin: number, pp: number): number;
|
|
3
|
+
/**
|
|
4
|
+
* Random numbers from the binomial distribution. Equivalent to R's `rbinom()`.
|
|
5
|
+
*
|
|
6
|
+
* @param n Number of values to generate (fractions are truncated, as in R).
|
|
7
|
+
* @param size Number of trials.
|
|
8
|
+
* @param prob Probability of success in each trial.
|
|
9
|
+
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
10
|
+
*/
|
|
11
|
+
export declare function rbinom(n: number, size: number, prob: number): number[];
|
|
12
|
+
/**
|
|
13
|
+
* Random vectors from the multinomial distribution. Equivalent to R's `rmultinom()`.
|
|
14
|
+
*
|
|
15
|
+
* Unlike R, which returns a K x n matrix with one draw per column, this returns `n` rows
|
|
16
|
+
* with one draw per row (the transpose of R's result). The random stream is the same as R's.
|
|
17
|
+
*
|
|
18
|
+
* @param n Number of values to generate (fractions are truncated, as in R).
|
|
19
|
+
* @param size Number of objects distributed into the K categories in each draw.
|
|
20
|
+
* @param prob Category weights of length K. They need not sum to 1 (they are divided by their sum).
|
|
21
|
+
* @returns An array of `n` arrays, each of length K and summing to `size`.
|
|
22
|
+
* @throws RangeError if `size` is not a non-negative integer, or `prob` has a negative or
|
|
23
|
+
* non-finite value, or no positive value.
|
|
24
|
+
*/
|
|
25
|
+
export declare function rmultinom(n: number, size: number, prob: readonly number[]): number[][];
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// Binomial and multinomial random variates.
|
|
2
|
+
//
|
|
3
|
+
// Kachitvichyanukul & Schmeiser (1988) "Binomial random variate generation"
|
|
4
|
+
// (Comm. ACM 31(2)): algorithm BTPE. When n min(p, 1-p) < 30, inversion (BINV in the paper) is used.
|
|
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.
|
|
7
|
+
import { unifRand } from "./rng.js";
|
|
8
|
+
import { generate, powInt } from "./common.js";
|
|
9
|
+
import { qbinom } from "../distributions/binomial.js";
|
|
10
|
+
const INT_MAX = 2147483647;
|
|
11
|
+
// Values rebuilt only when the parameters change
|
|
12
|
+
let c = 0, fm = 0, npq = 0, p1 = 0, p2 = 0, p3 = 0, p4 = 0, qn = 0;
|
|
13
|
+
let xl = 0, xll = 0, xlr = 0, xm = 0, xr = 0;
|
|
14
|
+
let psave = -1, nsave = -1, m = 0;
|
|
15
|
+
/** Correction term of Stirling's formula (13860 - (462 - (132 - (99 - 140/z²)/z²)/z²)/z²)/z/166320 */
|
|
16
|
+
function stirlingTail(z) {
|
|
17
|
+
const z2 = z * z;
|
|
18
|
+
return (13860 - (462 - (132 - (99 - 140 / z2) / z2) / z2) / z2) / z / 166320;
|
|
19
|
+
}
|
|
20
|
+
/** One binomial variate (nin trials, success probability pp) */
|
|
21
|
+
export function rbinom1(nin, pp) {
|
|
22
|
+
if (!Number.isFinite(nin))
|
|
23
|
+
return NaN;
|
|
24
|
+
const rn = Math.round(nin);
|
|
25
|
+
if (rn !== nin)
|
|
26
|
+
return NaN;
|
|
27
|
+
if (!Number.isFinite(pp) || rn < 0 || pp < 0 || pp > 1)
|
|
28
|
+
return NaN;
|
|
29
|
+
if (rn === 0 || pp === 0)
|
|
30
|
+
return 0;
|
|
31
|
+
if (pp === 1)
|
|
32
|
+
return rn;
|
|
33
|
+
if (rn >= INT_MAX)
|
|
34
|
+
return qbinom(unifRand(), rn, pp, { lowerTail: false });
|
|
35
|
+
const n = rn;
|
|
36
|
+
const p = Math.min(pp, 1 - pp);
|
|
37
|
+
const q = 1 - p;
|
|
38
|
+
const np = n * p;
|
|
39
|
+
const r = p / q;
|
|
40
|
+
const g = r * (n + 1);
|
|
41
|
+
let small;
|
|
42
|
+
if (pp !== psave || n !== nsave) {
|
|
43
|
+
psave = pp;
|
|
44
|
+
nsave = n;
|
|
45
|
+
if (np < 30) {
|
|
46
|
+
qn = powInt(q, n);
|
|
47
|
+
small = true;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
small = false;
|
|
51
|
+
fm = np + p;
|
|
52
|
+
m = Math.trunc(fm);
|
|
53
|
+
npq = np * q;
|
|
54
|
+
p1 = Math.trunc(2.195 * Math.sqrt(npq) - 4.6 * q) + 0.5;
|
|
55
|
+
xm = m + 0.5;
|
|
56
|
+
xl = xm - p1;
|
|
57
|
+
xr = xm + p1;
|
|
58
|
+
c = 0.134 + 20.5 / (15.3 + m);
|
|
59
|
+
let al = (fm - xl) / (fm - xl * p);
|
|
60
|
+
xll = al * (1 + 0.5 * al);
|
|
61
|
+
al = (xr - fm) / (xr * q);
|
|
62
|
+
xlr = al * (1 + 0.5 * al);
|
|
63
|
+
p2 = p1 * (1 + c + c);
|
|
64
|
+
p3 = p2 + c / xll;
|
|
65
|
+
p4 = p3 + c / xlr;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
small = np < 30;
|
|
70
|
+
}
|
|
71
|
+
let ix;
|
|
72
|
+
if (small) {
|
|
73
|
+
// Inversion (mean below 30)
|
|
74
|
+
outer: for (;;) {
|
|
75
|
+
ix = 0;
|
|
76
|
+
let f = qn;
|
|
77
|
+
let u = unifRand();
|
|
78
|
+
for (;;) {
|
|
79
|
+
if (u < f)
|
|
80
|
+
break outer;
|
|
81
|
+
if (ix > 110)
|
|
82
|
+
break;
|
|
83
|
+
u -= f;
|
|
84
|
+
ix++;
|
|
85
|
+
f *= g / ix - r;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
for (;;) {
|
|
91
|
+
const u = unifRand() * p4;
|
|
92
|
+
let v = unifRand();
|
|
93
|
+
// Triangular region
|
|
94
|
+
if (u <= p1) {
|
|
95
|
+
ix = Math.trunc(xm - p1 * v + u);
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
if (u <= p2) {
|
|
99
|
+
// Parallelogram region
|
|
100
|
+
const x = xl + (u - p1) / c;
|
|
101
|
+
v = v * c + 1 - Math.abs(xm - x) / p1;
|
|
102
|
+
if (v > 1 || v <= 0)
|
|
103
|
+
continue;
|
|
104
|
+
ix = Math.trunc(x);
|
|
105
|
+
}
|
|
106
|
+
else if (u > p3) {
|
|
107
|
+
// Right tail
|
|
108
|
+
ix = Math.trunc(xr - Math.log(v) / xlr);
|
|
109
|
+
if (ix > n)
|
|
110
|
+
continue;
|
|
111
|
+
v = v * (u - p3) * xlr;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
// Left tail
|
|
115
|
+
ix = Math.trunc(xl + Math.log(v) / xll);
|
|
116
|
+
if (ix < 0)
|
|
117
|
+
continue;
|
|
118
|
+
v = v * (u - p2) * xll;
|
|
119
|
+
}
|
|
120
|
+
// Acceptance/rejection test
|
|
121
|
+
const k = Math.abs(ix - m);
|
|
122
|
+
if (k <= 20 || k >= npq / 2 - 1) {
|
|
123
|
+
// Compute the probability ratio directly
|
|
124
|
+
let f = 1;
|
|
125
|
+
if (m < ix) {
|
|
126
|
+
for (let i = m + 1; i <= ix; i++)
|
|
127
|
+
f *= g / i - r;
|
|
128
|
+
}
|
|
129
|
+
else if (m > ix) {
|
|
130
|
+
for (let i = ix + 1; i <= m; i++)
|
|
131
|
+
f /= g / i - r;
|
|
132
|
+
}
|
|
133
|
+
if (v <= f)
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
// Squeeze using upper and lower bounds on log f
|
|
138
|
+
const amaxp = (k / npq) * ((k * (k / 3 + 0.625) + 0.1666666666666) / npq + 0.5);
|
|
139
|
+
const ynorm = (-k * k) / (2 * npq);
|
|
140
|
+
const alv = Math.log(v);
|
|
141
|
+
if (alv < ynorm - amaxp)
|
|
142
|
+
break;
|
|
143
|
+
if (alv <= ynorm + amaxp) {
|
|
144
|
+
// Final test using Stirling's formula (M is the integer mode m)
|
|
145
|
+
const x1 = ix + 1;
|
|
146
|
+
const f1 = m + 1;
|
|
147
|
+
const z = n + 1 - m;
|
|
148
|
+
const w = n - ix + 1;
|
|
149
|
+
if (alv <=
|
|
150
|
+
xm * Math.log(f1 / x1) +
|
|
151
|
+
(n - m + 0.5) * Math.log(z / w) +
|
|
152
|
+
(ix - m) * Math.log((w * p) / (x1 * q)) +
|
|
153
|
+
stirlingTail(f1) +
|
|
154
|
+
stirlingTail(z) +
|
|
155
|
+
stirlingTail(x1) +
|
|
156
|
+
stirlingTail(w)) {
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (psave > 0.5)
|
|
164
|
+
ix = n - ix;
|
|
165
|
+
return ix;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Random numbers from the binomial distribution. Equivalent to R's `rbinom()`.
|
|
169
|
+
*
|
|
170
|
+
* @param n Number of values to generate (fractions are truncated, as in R).
|
|
171
|
+
* @param size Number of trials.
|
|
172
|
+
* @param prob Probability of success in each trial.
|
|
173
|
+
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
174
|
+
*/
|
|
175
|
+
export function rbinom(n, size, prob) {
|
|
176
|
+
return generate(n, () => rbinom1(size, prob));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Random vectors from the multinomial distribution. Equivalent to R's `rmultinom()`.
|
|
180
|
+
*
|
|
181
|
+
* Unlike R, which returns a K x n matrix with one draw per column, this returns `n` rows
|
|
182
|
+
* with one draw per row (the transpose of R's result). The random stream is the same as R's.
|
|
183
|
+
*
|
|
184
|
+
* @param n Number of values to generate (fractions are truncated, as in R).
|
|
185
|
+
* @param size Number of objects distributed into the K categories in each draw.
|
|
186
|
+
* @param prob Category weights of length K. They need not sum to 1 (they are divided by their sum).
|
|
187
|
+
* @returns An array of `n` arrays, each of length K and summing to `size`.
|
|
188
|
+
* @throws RangeError if `size` is not a non-negative integer, or `prob` has a negative or
|
|
189
|
+
* non-finite value, or no positive value.
|
|
190
|
+
*/
|
|
191
|
+
export function rmultinom(n, size, prob) {
|
|
192
|
+
if (!Number.isFinite(size) || size < 0 || Math.round(size) !== size)
|
|
193
|
+
throw new RangeError("size must be a non-negative integer");
|
|
194
|
+
let sum = 0;
|
|
195
|
+
for (const v of prob) {
|
|
196
|
+
if (!Number.isFinite(v) || v < 0)
|
|
197
|
+
throw new RangeError("prob must contain non-negative finite numbers");
|
|
198
|
+
sum += v;
|
|
199
|
+
}
|
|
200
|
+
if (sum === 0)
|
|
201
|
+
throw new RangeError("prob must contain at least one positive value");
|
|
202
|
+
const pr = prob.map((v) => v / sum);
|
|
203
|
+
const K = pr.length;
|
|
204
|
+
const draw = () => {
|
|
205
|
+
const out = new Array(K).fill(0);
|
|
206
|
+
let left = size;
|
|
207
|
+
let pTot = 1;
|
|
208
|
+
for (let k = 0; k < K - 1; k++) {
|
|
209
|
+
if (pr[k]) {
|
|
210
|
+
const pk = pr[k] / pTot;
|
|
211
|
+
out[k] = pk < 1 ? rbinom1(left, pk) : left;
|
|
212
|
+
left -= out[k];
|
|
213
|
+
}
|
|
214
|
+
if (left <= 0)
|
|
215
|
+
return out;
|
|
216
|
+
pTot -= pr[k];
|
|
217
|
+
}
|
|
218
|
+
out[K - 1] = left;
|
|
219
|
+
return out;
|
|
220
|
+
};
|
|
221
|
+
if (!Number.isFinite(n) || n < 0)
|
|
222
|
+
throw new RangeError("n must be a non-negative finite number");
|
|
223
|
+
return Array.from({ length: Math.trunc(n) }, draw);
|
|
224
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Generate n random values. As in R, n must be non-negative (fractions are truncated) */
|
|
2
|
+
export declare function generate(n: number, f: () => number): number[];
|
|
3
|
+
/** Integer power x^n (by repeated squaring; Math.pow is not used, to round the same way as R's generators) */
|
|
4
|
+
export declare function powInt(x: number, n: number): number;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Shared helpers for the r functions.
|
|
2
|
+
/** Generate n random values. As in R, n must be non-negative (fractions are truncated) */
|
|
3
|
+
export function generate(n, f) {
|
|
4
|
+
if (!Number.isFinite(n) || n < 0)
|
|
5
|
+
throw new RangeError("n must be a non-negative finite number");
|
|
6
|
+
const len = Math.trunc(n);
|
|
7
|
+
const out = new Array(len);
|
|
8
|
+
for (let i = 0; i < len; i++)
|
|
9
|
+
out[i] = f();
|
|
10
|
+
return out;
|
|
11
|
+
}
|
|
12
|
+
/** Integer power x^n (by repeated squaring; Math.pow is not used, to round the same way as R's generators) */
|
|
13
|
+
export function powInt(x, n) {
|
|
14
|
+
let pow = 1;
|
|
15
|
+
if (Number.isNaN(x))
|
|
16
|
+
return x;
|
|
17
|
+
if (n !== 0) {
|
|
18
|
+
if (!Number.isFinite(x))
|
|
19
|
+
return Math.pow(x, n);
|
|
20
|
+
if (n < 0) {
|
|
21
|
+
n = -n;
|
|
22
|
+
x = 1 / x;
|
|
23
|
+
}
|
|
24
|
+
for (;;) {
|
|
25
|
+
if (n & 1)
|
|
26
|
+
pow *= x;
|
|
27
|
+
n >>= 1;
|
|
28
|
+
if (n)
|
|
29
|
+
x *= x;
|
|
30
|
+
else
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return pow;
|
|
35
|
+
}
|