- Notifications
You must be signed in to change notification settings - Fork31
Javascript Pure Implementation of Statistical R "core" numerical libRmath.so
License
R-js/libRmath.js
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This R statisticalnmath re-created in typescript/javascript and handwritten webassembly.
Distributions:
beta, binomial, binomial-negative, cauchy, ch-2, exponential, fisher,gamma, geometric, hypergeometric (web assembly), logistic, log normal, multinomial, normal/gaussian, poisson distribution, singnrank (web assembly), student-t, tukey, uniform, weibull, wilcoxon.
Special functions:
bessel, beta, gamma, choose (the binomial coefficient $\binom{n}{k}$).
Pseudo Random genererators:
knuth-taocp, lecuyer-cmrg, marsalglia-multicarry, mersenne-twister, super-duper, wichman-hill, ahrens-dieter, box-muller, buggy-kinderman-ramage, inversion, kinderman-ramage.
This library has zero external dependencies.
This documentation assumes you knowledgable about the R build-in libaries.
npm i lib-r-math.js@latest# most recent stable versionOlder documentation:
If you were not using a previous version to 2.0.0, you can skipbreaking changes and go to:
The normal and uniform implementation of the various RNG's are not exported publicly anymoreSelect normal and uniform RNG's via the functionRNGkind.
// this is NOT possible anymoreimport{AhrensDieter}from'lib-r-math.js';constad=newAhrensDieter();ad.random();// NEW way of doing thingsimport{RNGkind,rnorm}from'lib-r-math.js';RNGkind({normal:'AHRENS_DIETER'});// R analog to "RNGkind"rnorm(8);// get 8 samples, if you only want one sample consider rnormOne()
Functions removed from 2.0.0 onwards:any,arrayrify,multiplex,each,flatten,c,map,selector,seq,summary.
It is recommended you either use well established js tools likeRxjs orRamdajs to mangle arrays and data.
Functions removed from 2.0.0 onwards:numberPrecision
This function mimicked the R'soptions(digits=N).
Functions changed from 2.0.0 onwards:timeseed.
timeseed is now replaced by a cryptographic safe seedseed.
Functions changed from 2.0.0 onwards:
All these functions will return type ofFloat64Array:rbeta,rbinom,rcauchy,rchisq,rexp,rf,rgamma,rgeom,rhyper,rlogis,rlnorm,rmultinom,rnorm,rpois,rsignrank,rt,runif,rweibull,rwilcox.
For single scalar (number) return values, use the analogs:rbetaOne,rbinomOne,rcauchyOne,rchisqOne,rexpOne,rfOne,rgammaOne,rgeomOne,rhyperOne,rlogisOne,rlnormOne,rnormOne,rpoisOne,rsignrankOne,rtOne,runifOne,rweibullOne,rwilcoxOne.
Example:
import{rbinom,rbinomOne,setSeed}from'lib-r-math.js';rbinom(0);//// -> FloatArray(0)setSeed(123);// set.seed(123) in Rrbinom(2,8,0.5);// -> Float64Array(2) [ 3, 5 ] //same result as in RsetSeed(456);// set.seed(456) in RrbinomOne(350,0.5);// -> 174 ( a single scalar )
There is no UMD module from 2.0.0 onward.
Minimal version of node required is22.12.0.
npm i lib-r-math.js
lib-r-math.js supports the following module types:
ESM for use inobservablehq
ObservableHQ works nicely with lib-r-math.js visualizing data generated by lib-r-math.js functions
Example:observableHQ-bessel-demo
<scripttype="module">import{BesselJ}from'https://unpkg.dev/lib-r-math.js@latest/dist/web.esm.mjs';console.log(BesselJ(3,0.4));//-> -0.30192051329163955</script>
<scriptsrc="https://unpkg.dev/lib-r-math.js@latest/dist/web.iife.js"></script><script>constansw=window.R.BesselJ(3,0.4);console.log(answ);//-> -0.30192051329163955</script>
import{BesselJ}from'lib-r-math.js';constansw=BesselJ(3,0.4);//-> -0.30192051329163955
const{ BesselJ}=require('lib-r-math.js');constansw=BesselJ(3,0.4);//-> -0.30192051329163955
- libRmath.js
- BREAKING CHANGES For version 2.0
- Installation and usage
- Table of Contents
- Auxiliary functions
- Distributions
- The Beta distribution
- The Binomial distribution
- The Negative Binomial Distribution
- The Cauchy Distribution
- The Chi-Squared (non-central) Distribution
- The Exponential Distribution
- The F Distribution
- The Gamma Distribution
- The Geometric Distribution
- The Hypergeometric Distribution (Web Assembly accelerated)
- The Logistic Distribution
- The Log Normal Distribution
- The Multinomial Distribution
- The Normal Distribution
- The Poisson distribution
- Distribution of the Wilcoxon Signed Rank Statistic
- The Student t Distribution
- The Studentized Range Distribution
- The Uniform Distribution
- The Weibull Distribution
- Distribution of the Wilcoxon Rank Sum Statistic
- Special Functions of Mathematics
RNGkind is the analog to R's "RNGkind". This is how you select what RNG (normal and uniform) you use and thesamplingKind (Rounding or Rejection) property
Follows closely the R implementationhere
R console:
> RNGkind()[1]"Mersenne-Twister""Ahrens-Dieter"[3]"Rejection"
Just like inR, callingRNGkind with no argument returns the currently active RNG's (uniform and normal) and sample kind (Rounding or Rejection)
Like inR,RNGkind optionally takes an argument of typeRandomGenSet, after processing it will return the (adjusted)RandomGenSet indicating what RNG's and "kind of sampling" is being used.
Rjstypescript decl:
functionRNGkind(options?:RandomGenSet):RandomGenSet;
Arguments:
options: an object of typeRandomGenSetoptions.uniform: string, specify name of uniform RNG to use.options.normal: string, specify nam of normal RNG (shaper) to useoptions.sampleKind: string, specify sample strategy to use
Typescript definition:
typeRandomGenSet={uniform?:|'KNUTH_TAOCP'|'KNUTH_TAOCP2002'|'LECUYER_CMRG'|'MARSAGLIA_MULTICARRY'|'MERSENNE_TWISTER'|'SUPER_DUPER'|'WICHMANN_HILL';normal?:'AHRENS_DIETER'|'BOX_MULLER'|'BUGGY_KINDERMAN_RAMAGE'|'KINDERMAN_RAMAGE'|'INVERSION';sampleKind?:'ROUNDING'|'REJECTION';};
TheRNGkind function is decorated with the following extra properties:
| property | description | example |
|---|---|---|
RNGkind.uniform | list of constants of uniform RNG's | RNGkind.uniform.MARSAGLIA_MULTICARRY is equal to the string"MARSAGLIA_MULTICARRY" |
RNGkind.normal | list of constants of normal RNG's | RNGkind.normal.KINDERMAN_RAMAGE is equal to the string"KINDERMAN_RAMAGE" |
RNGkind.sampleKind | list of sampling strategies | RNGkind.sampleKind.ROUNDING is equal to the string"ROUNDING" |
Example: set uniform RNG toSUPER_DUPER and normal RNG toBOX_MULLER
import{RNGkind}from'lib-r-math.js';constuniform=RNGkind.uniform.SUPER_DUPER;constnormal=RNGkind.normal.BOX_MULLER;RNGkind({ uniform, normal});//-> "sampleKind" not specified so this will not be changedRNGkind();// no arguments, will return the current used RNG's and "sampleKind"// returns// {// uniform: 'SUPER_DUPER',// normal: 'BOX_MULLER',// sampleKind: 'ROUNDING' // was not changed from default setting// }
Uses a single value to initialize the internal state of the currently selected uniform RNG.
R console analog:set.seed
Rjstypescript decl
functionsetSeed(s:number):void;
Arguments:
sis coerced to an unsigned 32 bit integer
R console analog:.Random.seed
Rjstypescript decl
functionrandomSeed(internalState?:Uint32Array|Int32Array):Uint32Array|Int32Array|never;
Arguments:
- (optional)
internalState: the value of a previously saved RNG state, the current RNG state will be set to this. - return state of the current selected RNG
Exceptions:
- If the
internalStatevalue is not correct for the RNG selected an Error will be thrown.
All distribution functions follow a prefix pattern:
d(likedbeta,dgamma) are density functionsp(likepbeta,pgamma) are (cumulative) distribution functionq(likeqbeta,qgamma) are quantile functionsr(likerbeta/rbetaOne,rgamma/rgammaOne) generates random deviates
| type | function spec |
|---|---|
| density function | function dbeta(x: number, shape1: number, shape2: number, ncp?: number, log = false): number |
| distribution function | function pbeta(q: number, shape1: number, shape2: number, ncp?: number, lowerTail = true, logP = false): number |
| quantile function | function qbeta(p: number, shape1: number, shape2: number, ncp?: number, lowerTail = true, logP = false): number |
| random generation (bulk) | function rbeta(n: number, shape1: number, shape2: number, ncp?: number): Float32Array |
| random generation | function rbetaOne(shape1: number, shape2: number): number |
- Arguments:
x, q: quantile valuep: probabilityn: number of observationsshape1, shape2: Shape parameters of the Beta distributionlog, logP: iftrue, probabilities are given aslog(p).lowerTail: iftrue, probabilities areP[X ≤ x], otherwise,P[X > x].
Example:
import{dbeta}from'lib-r-math.js';dbeta(0.5,2,2);// -> 1.5
| type | function spec |
|---|---|
| density function | function dbinom(x: number, n: number, prob: number, log = false): number |
| distribution function | function pbinom(q: number, n: number, prob: number, lowerTail = true, logP = false): number |
| quantile function | function qbinom(p: number, size: number, prob: number, lower_tail = true, logP = false): number |
| random generation (bulk) | function rbinom(n: number, size: number, prob: number): Float64Array |
| random generation | function rbinomOne(size: number, prob: number): number |
- Arguments:
x, q: quantile valuep: probabilityn: number of observations.size: number of trials (zero or more).prob: probability of success on each trial.log, logP: iftrue, probabilities are given aslog(p).lowerTail: iftrue, probabilities areP[X ≤ x], otherwise,P[X > x].
Example:
import{dbinom}from'lib-r-math.js';dbinom(50,100,0.5);// -> 0.07958924
| type | function spec |
|---|---|
| density function | function dnbinom(x: number, size: number, prob?: number, mu?: number, log = false): number |
| distribution function | function pnbinom(q: number, size: number, prob?: number, mu?: number, lowerTail = true, logP = false): number |
| quantile function | function qnbinom(p: number, size: number, prob?: number, mu?: number, lowerTail = true, logP = false): number |
| random generation (bulk) | function rnbinom(n: number, size: number, prob?: number, mu?: number): Float64Array |
| random generation | function rnbinom(size: number, prob?: number, mu?: number): number |
Arguments:
x, q: quantile value.p: probability.n: number of observations.size: target for number of successful trials, (need not be integer) or dispersion parameter (the shape parameter of the gamma mixing distribution). Must be strictly positive.prob: probability of success in each trial.0 < prob <= 1.mu: alternative parametrization via mean: see ‘Details’.log, logP: iftrue, probabilities are given aslog(p).lowerTail: iftrue, probabilities areP[X ≤ x], otherwise,P[X > x].
Details:R doc
A negative binomial distribution can also arise as a mixture of Poisson distributions with mean distributed as a gamma distribution (see pgamma) with scale parameter (1 - prob)/prob and shape parameter size. (This definition allows non-integer values of size.)
Analternative parametrization (often used in ecology) is by the meanmu (see above), and size, the dispersion parameter, whereprob = size/(size+mu). The variance ismu + mu^2/size in this parametrization.
Example:
R console:
> options(digits=22)>126/ dnbinom(0:8,size=2,prob=1/2)[1]504.0000000000000000000503.9999999999998863132672.00000000000000000001008.0000000000001136868[5]1612.79999999999949977792688.00000000000136424214607.99999999999727151598064.0000000000000000000[9]14336.0000000000145519152
Equivalence in js (fidelity):
import{dnbinom}from'lib-r-math.js';console.log([0,1,2,3,4,5,6,7,8].map((x)=>126/dnbinom(x,2,0.5)));// ->[504,503.9999999999999,672,1008.0000000000001,1612.7999999999988,2688.0000000000014,4607.999999999997,8064,14336.000000000015];
| type | function spec |
|---|---|
| density function | function dcauchy(x: number, location = 0, scale = 1, log = false): number |
| distribution function | function pcauchy(x: number, location = 0, scale = 1, lowerTail = true, logP = false): number |
| quantile function | function qcauchy(p: number, location = 0, scale = 1, lowerTail = true, logP = false): number |
| random generation (bulk) | function rcauchy(n: number, location = 0, scale = 1): Float32Array |
| random generation | function rcauchyOne(location = 0, scale = 1): number |
Arguments:
x, q: quantile value.p: probability.n: number of observations.location, scale: location and scale parameters.log, logP: iftrue, probabilities are given aslog(p).lowerTail: iftrue, probabilities areP[X ≤ x], otherwise,P[X > x].
Examples
R console:
dcauchy(-1:4)[1]0.159154943091895345608220.318309886183790691216440.159154943091895345608220.06366197723675813546773[5]0.031830988618379067733870.01872411095198768526959
Equivalence in js (fidelity):
import{dcauchy}from'lib-r-math.js';console.log([-1,0,1,2,3,4].map((x)=>dcauchy(x)));// -> [ 0.15915494309189535, 0.3183098861837907, 0.15915494309189535, 0.06366197723675814, 0.03183098861837907, 0.018724110951987685 ]
| type | function spec |
|---|---|
| density function | function dchisq(x: number, df: number, ncp?: number, log = false ): number |
| distribution function | function pchisq(p: number, df: number, ncp?: number, lowerTail = true, logP = false ): number |
| quantile function | function qchisq(p: number, df: number, ncp?: number, lowerTail = true, logP = false ): number |
| random generation (bulk) | function rchisq(n: number, df: number, ncp?: number): Float64Array |
| random generation | function rchisqOne(df: number, ncp?: number): number |
Arguments:
x, q: quantile.p: probability.n: number of observations.df: degrees of freedom (non-negative, but can be non-integer).ncp: non-centrality parameter (non-negative).log, logP: iftrue, probabily p are given as log(p).lowerTail: if true`TRUE (default), probabilities are P[X \le x]P[X≤x], otherwise, P[X > x]P[X>x].
Examples
R console:
dchisq(1,df=1:3)[1]0.24197070.30326530.2419707
Equivalence in js (fidelity):
import{dchisq}from'lib-r-math.js';console.log([1,2,3].map((df)=>dchisq(1,df)));// -> [ 0.24197072451914337, 0.3032653298563167, 0.24197072451914337 ]
| type | function spec |
|---|---|
| density function | function dexp(x: number, rate = 1, log = false): number |
| distribution function | function pexp(q: number, rate = 1, lowerTail = true, logP = false): number |
| quantile function | function qexp(p: number, rate = 1, lowerTail = true, logP = false): number |
| random generation (bulk) | function rexp(n: number, rate = 1):Float64Array |
| random generation | function rexpOne(rate = 1): number |
Arguments:
x, q: quantile.p: probabily.n: number of observations.rate: the exp rate parameterlog, logP: iftrue, probabilitiespare given aslog(p).lower.tail: iftrue(default), probabilities are P[ X ≤ x ], otherwise, P[X > x].
Examples
R console:
dexp(1)- exp(-1)[1]0
Equivalence in js (fidelity):
import{dexp}from'lib-r-math.js';console.log(dexp(1)-Math.exp(-1));// -> 0
| type | function spec |
|---|---|
| density function | function df(x: number, df1: number, df2: number, ncp?: number, log = false): number |
| distribution function | function pf(q: number, df1: number, df2: number, ncp?: number, lowerTail = true, logP = false): number |
| quantile function | function qf(p: number, df1: number, df2: number, ncp?: number, lowerTail = true, logP = false): number |
| random generation (bulk) | function rf(n: number, df1: number, df2: number, ncp?: number): Float64Array |
| random generation | function rfOne(df1: number, df2: number, ncp?: number): number |
Arguments:
x, q: quantile.p: probabily.n: number of observations.df1, df1: degrees of freedom.Infinityis allowed.ncp: non-centrality parameter. If omitted the central F is assumed.log, logP: iftrue, probabilitiespare given aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x ], otherwise,P[X > x]S.
**NOTE: JS has no named arguments for functions, so specify ncp = undefined, if you want to change thelog, logP, lowerTail away from their defaults
Examples
R console:
## Identity (F <-> Beta <-> incompl.beta):n1<-7 ;n2<-12;qF<- c((0:4)/4,1.5,2:16)x<-n2/(n2+n1*qF)stopifnot(all.equal(pf(qF,n1,n2,lower.tail=FALSE), pbeta(x,n2/2,n1/2)))
Equivalence in js (fidelity):
import{pf,pbeta}from'lib-r-math.js';varqF=[0.0,0.25,0.5,0.75,1.0,1.5,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0];varn1=7;varn2=12;varxs=qF.map((qf)=>n2/(n2+n1*qf));varbetas=xs.map((x)=>pbeta(x,n2/2,n1/2));varfisher=qF.map((qf)=>pf(qf,n1,n2,undefined/*no ncp*/,false));// array "betas" and "fisher" should be equalconsole.log(fisher.map((f,i)=>f-betas[i]));//-> [ 0, 0, 0, 0, 0, ...., 0]
| type | function spec |
|---|---|
| density function | function dgamma(x: number, shape: number, rate?: number, scale?: number, log = false): number |
| distribution function | function pgamma(q: number, shape: number, rate?: number, scale?: number, lowerTail = true, logP = false): number |
| quantile function | function qgamma(p: number, shape: number, rate?: number, scale?: number, lowerTail = true, logP = false): number |
| random generation (bulk) | function rgamma(n: number, shape: number, rate?: number, scale?: number): Float64Array |
| random generation | function rgammaOne(shape: number, rate?: number, scale?: number): number |
Arguments:
x, q: quantilep: probabilityn: number of observations.rate: an alternative way to specify the scale.shape, scale: shape and scale parameters. Must be positive, scale strictly.log, logP: iftrue, probabilities/densitiespare returned aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x], otherwise,P[X > x].
Example:
R console:
-log(dgamma(1:4,shape=1))[1]1234
Equivalence in js (fidelity):
import{dgamma}from'lib-r-math.js';letdg=[1,2,3,4].map((x)=>Math.log(dgamma(x,1)));// -> [ -1, -2, -3, -4 ]//// this is Equivalence to to// [1,2,3,4].map (x => dgamma(x, 1, undefined, undefined, true) );
| type | function spec |
|---|---|
| density function | function dgeom(x: number, p: number, log = false): number |
| distribution function | function qgeom(p: number, prob: number, lowerTail = true, logP = false): number |
| quantile function | function qgeom(p: number, prob: number, lowerTail = true, logP = false): number |
| random generation (bulk) | function rgeom(n: number, prob: number): Float64Array |
| random generation | function rgeomOne(p: number): number |
Arguments:
x, q: quantilep: probabilityn: number of observations.prob: probability of success in each trial. 0 < prob <= 1.log, logP: iftrue, probabilities/densitiespare returned aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x], otherwise,P[X > x].
Example:
R console:
qgeom((1:9)/10,prob=.2)[1]0012345710
Equivalence in js (fidelity):
import{qgeom}from'lib-r-math.js';letdg=[1,2,3,4,5,6,7,8,9].map((p)=>p/10).map((p)=>qgeom(p,0.2));console.log(dg);// -> [ 0, 0, 1, 2, 3, 4, 5, 7, 10 ]
| type | function spec |
|---|---|
| density function | function dhyper(x: number, m: number, n: number, k: number, log = false): number |
| distribution function | function phyper(q: number, m: number, n: number, k: number, lowerTail = true, logP = false): number |
| quantile function | function qhyper(p: number, m: number, n: number, k: number, lowerTail = true, logP = false): number |
| random generation (bulk) | function rhyper(nn: number, m: number, n: number, k: number): Float64Array |
| random generation | function rhyperOne(m: number, n: number, k: number): number |
Arguments:
x, q: quantilep: probabilitym: the number of white balls in the urn.n: the number of black balls in the urn.k: the number of balls drawn from the urn, hence must be in0,1,…,m+n.p: probability, it must be between 0 and 1.nn: number of observations.log, logP: iftrue, probabilities/densitiespare returned aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x], otherwise,P[X > x].
Example:
R console:
m<-10;n<-7;k<-8x<-0:(k+1)rbind(phyper(x,m,n,k), dhyper(x,m,n,k)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10][1,]00.00041135340.013368980.1170300.41937470.78218840.96359520.998148911.000000001[2,]00.00041135340.012957630.1036610.30234470.36281370.18140680.034553680.001851090
Equivalence in js (fidelity):
import{phyper,dhyper}from"lib-r-math.js";varm=10;varn=7;vark=8;varxs=[0,1,2,3,4,5,6,7,8,9];console.log( ...xs.map(x=>phyper(x,m,n,k)));00.0004113533525298230.013368983957219260.117030028794734740.41937474290415460.78218839983545850.96359522830111070.998148909913615811console.log( ...xs.map(x=>dhyper(x,m,n,k)));00.0004113533525298230.0129576306046894370.103661044837515480.302344714109419930.36281365693130410.181406828465652060.034553681612505140.0018510900863842050
UseuseWasmBackendHyperGeom andclearBackendHyperGeom to enable/disable wasm backend.
import{useWasmBackendHyperGeom,clearBackendHyperGeom,//qhyper}from'lib-r-math.js';// the functions "qhyper" will be accelerated (on part with native C for node >=16)useWasmBackendHyperGeom();qhyper(0.5,2**31-1,2**31-1,2**31-1);// 28 sec (4.3 Ghz Pentium) wasm big numbers to make it do some work// -> 1073741806clearBackendHyperGeom();// revert to js backendqhyper(0.5,2**31-1,2**31-1,2**31-1);// this will take 428 sec (4.3 Ghz Pentium)// -> 1073741806
| type | function spec |
|---|---|
| density function | function dlogis(x: number, location = 0, scale = 1, log = false): number |
| distribution function | function plogis(x: number, location = 0, scale = 1, lowerTail = true, logP = false): number |
| quantile function | function qlogis(p: number, location = 0, scale = 1, lowerTail = true, logP = false): number |
| random generation (bulk) | function rlogis(n: number, location = 0, scale = 1): Float64Array |
| random generation | function rlogisOne(location = 0, scale = 1): number |
Arguments:
x, q: quantilep: probabilitylocation, scale: location and scale parameters.n: number of observations.log, logP: iftrue, probabilities/densitiespare returned aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x], otherwise,P[X > x].
Example:
R console:
> RNGkind()[1]"Mersenne-Twister""Inversion""Rejection"> set.seed(12345)> var(rlogis(4000,0,scale=5))[1]80.83207
Equivalence in js (fidelity):
import{setSeed,RNGkind,rlogis}from'lib-r-math.js';constuniform=RNGkind.uniform.MERSENNE_TWISTER;constnormal=RNGkind.normal.INVERSION;RNGkind({ uniform, normal});setSeed(12345);letsamples=rlogis(4000,0,5);// get 4000 samples// calculate sample varianceconstN=samples.length;constµ=samples.reduce((sum,x)=>sum+x,0)/N;constS=(1/(N-1))*samples.reduce((sum,x)=>sum+(x-µ)**2,0);// sample varianceconsole.log(S);// -> 80.83207248898108 (fidelity proven)
| type | function spec |
|---|---|
| density function | function dlnorm(x: number, meanlog = 0, sdlog = 1, log = false): number |
| distribution function | function plnorm(q: number, meanlog = 0, sdlog = 1, lowerTail = true, logP = false): number |
| quantile function | function qlnorm(p: number, meanlog = 0, sdlog = 1, lowerTail = true, logP = false): number |
| random generation (bulk) | function rlnorm(n: number, meanlog = 0, sdlog = 1): Float32Array |
| random generation | function rlnormOne(meanlog = 0, sdlog = 1): number |
Arguments:
x, q: quantilep: probabilitymeanlog, sdlog: mean and standard deviation of the distribution on the log scale with default values of 0 and 1 respectively.n: number of observations.log, logP: iftrue, probabilities/densitiespare returned aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x], otherwise,P[X > x].
Examples:
R console:
dlnorm(1)== dnorm(0)[1]TRUE
Equivalence in js (fidelity):
import{dlnorm,dnorm}from'lib-r-math.js';console.log(dlnorm(1)===dnorm(0));// -> true
| type | function spec |
|---|---|
| density function | function dmultinom(x: Float32Array, prob: Float32Array, log = false): number |
| density function (R like) | function dmultinomLikeR(x: Float32Array, prob: Float32Array, log = false): number |
| random generation (bulk) | function rmultinom(n: number, size: number, prob: Float64Array): Float64Array |
Arguments:
x: quantilen: number of random vectors to draw.size:- integer, say
N, specifying the total number of objects that are put intoKboxes in the typical multinomial experiment. dmultinomomit's thesizeparameter (used in R version), see "Details" below for motivation.
- integer, say
prob: numeric non-negative array of lengthK, specifying the probability for theKclasses; is internally normalized to sum 1. Infinite and missing values are not allowed.log: iftrue, log probabilities are computed.
Motivation for removingsize argument fromdmultinom:
The code snippet shows clarification
N<- sum(x)if (is.null(size))# if size is the default (null) then assign it the value N (number of )size<-Nelseif (size!=N)# if manually set AND not equal to sum(x) throw Error, stop("size != sum(x), i.e. one is wrong")
Because of the above R code allowing manual setting ofsize in dmultinom is omitted
Example:
R console:
> RNGkind()[1]"Mersenne-Twister""Inversion""Rejection"> set.seed(1234)> rmultinom(10,size=12,prob= c(0.1,0.2,0.8)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10][1,]0120110000[2,]3321224411[3,]9881199881111>
Equivalence in js (fidelity):
import{RNGkind,setSeed,rmultinom}from'lib-r-math.js';RNGkind({uniform:RNGkind.uniform.MERSENNE_TWISTER,normal:RNGkind.normal.INVERSION});setSeed(1234);// use same seed as in R exampleconstanswer=rmultinom(10,12,newFloat64Array([0.1,0.2,0.8]));// returns a (row-first) matrix as a single Float64Array with size (prob.length x size)console.log(...answer);// -> 0 3 9 1 3 8 2 2 8 0 1 11 1 2 9 1 2 9 0 4 8 0 4 8 0 1 11 0 1 11// first column 0 3 9// second column 1 3 8 etc etc
| type | function spec |
|---|---|
| density function | function dnorm(x: number, mean = 0, sd = 1, log = false): number |
| distribution function | function pnorm(q: number, mean = 0, sd = 1, lowerTail = true, logP = false): number |
| quantile function | function qnorm(p: number, mean = 0, sd = 1, lowerTail = true, logP = false): number |
| random generation (bulk) | function rnorm(n: number, mean = 0, sd = 1): Float64Array |
| random generation | function rnormOne(mean = 0, sd = 1): number |
Arguments:
x, q: quantilep: probabilitymean, sd: mean and standard deviation.n: number of observations.log, logP: iftrue, probabilities/densitiespare returned aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x], otherwise,P[X > x].
Example:
R console:
dnorm(0)==1/sqrt(2*pi)[1]TRUEdnorm(1)== exp(-1/2)/sqrt(2*pi)[1]TRUEdnorm(1)==1/sqrt(2*pi*exp(1))[1]TRUE
Equivalence in js:
import{dnorm}from'lib-r-math.js';const{ sqrt, exp,PI:pi}=Math;console.log(dnorm(1)===exp(-1/2)/sqrt(2*pi));// -> trueconsole.log(dnorm(1)===exp(-1/2)/sqrt(2*pi));// -> trueconsole.log(dnorm(1)===1/sqrt(2*pi*exp(1)));// -> true
| type | function spec |
|---|---|
| density function | function dpois(x: number, lambda: number, log = false): number |
| distribution function | function ppois(q: number,lambda: number, lowerTail = true, logP = false): number |
| quantile function | function qpois(p: number, lambda: number, lowerTail = true, logP = false): number |
| random generation (bulk) | function rpois(n: number, lamda: number): Float32Array |
| random generation | function rpoisOne(lambda: number): number |
Arguments:
x, q: quantile.p: probability.lambda: non-negative mean.n: number of observations.log, logP: iftrue, probabilities/densitiespare returned aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x], otherwise,P[X > x].
Example:
R console:
> options(digits=20)>-log(dpois(0:7,lambda=1)* gamma(1+0:7))# == 1[1]1.000000000000000000001.000000000000000000001.000000000000000000001.00000000000000000000[5]0.999999999999999777961.000000000000000222041.000000000000000222041.00000000000000000000
Equivalence in js:
import{dpois,gamma}from'lib-r-math.js';const{ log}=Math;letarr=[0,1,2,3,4,5,6,7];letresult=arr.map((x)=>-log(dpois(x,1)*gamma(x+1)));console.log(...result);//-> 1 1 1 1 0.9999999999999996 1 1.0000000000000009 0.9999999999999989
| type | function spec |
|---|---|
| density function | function dsignrank(x: number, n: number, log = false): number |
| distribution function | function psignrank(q: number, n: number, lowerTail = true, logP = false): number |
| quantile function | function qsignrank(p: number, n: number, lowerTail = true, logP = false): number |
| random generation (bulk) | function rsignrank(nn: number, n: number): Float64Array |
| random generation | function rsignrank(nn, n): number |
Arguments:
x, q: quantile.p: probability.nn: number of observations.n: number of observations in the sample. A positive integer.log, logP: iftrue, probabilities/densitiespare returned aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x], otherwise,P[X > x].
Examples:
R console:
> options(digits=20)>x=seq(0,5*6/2)>y=dsignrank(x,5)>data.frame(x,y)xy00.03125000000000000000010.03125000000000000000020.03125000000000000000030.06250000000000000000040.06250000000000000000050.09374999999999998612260.09374999999999998612270.09374999999999998612280.09374999999999998612290.093749999999999986122100.093749999999999986122110.062500000000000000000120.062500000000000000000130.031250000000000000000140.031250000000000000000150.031250000000000000000
Equivalence in js:
import{dsignrank}from'lib-r-math.js';constN=5;for(letx=0;x<=(N*(N+1))/2;x++){console.log(x,dsignrank(x,N));}/*0 0.031251 0.031252 0.031253 0.06254 0.06255 0.093749999999999996 0.093749999999999997 0.093749999999999998 0.093749999999999999 0.0937499999999999910 0.0937499999999999911 0.062512 0.062513 0.0312514 0.0312515 0.03125*/
dsignrank,psignrank andqsignrank have an optional Web Assembly backend, turn this backend on/off withuseWasmBackendSignRank andclearBackendSignRank respectivily.
Example
import{useWasmBackendSignRank,clearBackendSignRank,psignrank}from'lib-r-math.js';useWasmBackendSignRank();// all sign rank functions acceleratedconstp=psignrank(...);// so something usefullclearBackendSignRank();// use javascript backend for signrank distribution
| type | function spec |
|---|---|
| density function | function dt(x: number, df: number, ncp = 0, log = false): number |
| distribution function | function pt(q: number, df: number, ncp = 0, lowerTail = true, logP = false): number |
| quantile function | function qt(p: number, df: number, ncp?: number, lowerTail = true, logP = false): number |
| random generation (bulk) | function rt(n: number, df: number, ncp?: number): Float64Array |
| random generation | function rtOne(df: number): number |
Arguments:
x, q: quantile.p: probability.n: number of observations.df: degrees of freedom (>0, maybe non-integer).df = Infis allowed.ncp: non-centrality parameter$\delta$ ; currently except forrt(), only forabs(ncp) <= 37.62. If omitted, use the central t distribution.log, logP: iftrue, probabilities/densitiespare returned aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x], otherwise,P[X > x].
Example:
R console:
1- pt(1:5,df=1)[1]0.2499999999999998[2]0.1475836176504333[3]0.1024163823495667[4]0.0779791303773694[5]0.0628329581890011
Equivalence in js:
import{pt}from'lib-r-math.js';for(letq=1;q<=5;q++){console.log(1-pt(q,1));}// 0.24999998762491238// 0.14758361679076415// 0.10241638219629579// 0.07797913031910642// 0.06283295806783729
| type | function spec |
|---|---|
| distribution function | function ptukey(q: number, nmeans: number, df: number, nrnages = 1, lowerTail = true, logP = false): number |
| quantile function | function qt(p: number, df: number, ncp?: number, lowerTail = true, logP = false): number |
Arguments:
q: quantile.p: probability.nmeans: sample size for range (same for each group).df: degrees of freedom for ss (see below).nranges: number of groups whose maximum range is considered.log, logP: iftrue, probabilities/densitiespare returned aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x], otherwise,P[X > x].
Example:
R console:
ptukey(-1:8,nm=6,df=5) [1]0.00000000000000000.00000000000000000.0272115020859732 [4]0.27798450616094320.60079715694467330.8017143642776676 [7]0.90142570659577410.94894950692959810.9721701726664311[10]0.9840420193770625
Equivalence in js:
import{ptukey}from'lib-r-math.js';function*generatePTukeyData(){for(letq=-1;q<=8;q++){yieldptukey(q,6,5);}}console.log(...generatePTukeyData());// 0 0 0.02721150208597321// 0.2779845061609432 0.6007971569446733 0.8017143642776676// 0.9014257065957741 0.9489495069295981 0.9721701726664311// 0.9840420193770627
| type | function spec |
|---|---|
| density function | function dunif(x: number, min = 0, max = 1, log = false): number |
| distribution function | function punif(q: number, min = 0, max = 1, lowerTail = true, logP = false): number |
| quantile function | function qunif(p: number, min = 0, max = 1, lowerTail = true, logP = false): number |
| random generation (bulk) | function runif(n: number, min = 0, max = 1): Float64Array |
| random generation | function runifOne(min: number, max: number): number |
Arguments:
x,q: quantile.p: probability.min, max: lower and upper limits of the distribution. Must be finite.n: number of observations.log, logP: iftrue, probabilities/densitiespare returned aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x], otherwise,P[X > x].
Example:
R console:
> RNGkind()[1]"Mersenne-Twister""Inversion""Rejection"> set.seed(12345)> runif(5)[1]0.7209038962610070.8757731930818410.7609823283273730.886124566197395[5]0.456480960128829
Equivalence in js:
import{RNGkind,setSeed,runif}from'lib-r-math.js';// these are defaults so skip setting via "RNGkind" this set if not changedconstuniform=RNGkind.uniform.MERSENNE_TWISTER;constnormal=RNGkind.normal.INVERSION;RNGkind({ uniform, normal});// set seedsetSeed(12345);console.log(...runif(5));// -> 0.7209038962610066 0.8757731930818409 0.7609823283273727 0.8861245661973953 0.4564809601288289
| type | function spec |
|---|---|
| density function | function dweibull(x: number, shape: number, scale = 1, log = false): number |
| distribution function | function pweibull(q: number, shape: number, scale = 1, lowerTail = true, logP = false): number |
| quantile function | function qweibull(p: number, shape: number, scale = 1, lowerTail = true, logP = false): number |
| random generation (bulk) | function rweibull(n: number, shape: number, scale = 1): Float64Array |
| random generation | function rweibullOne(shape: number, scale = 1): number |
Arguments:
x,q: quantile.p: probability.n: number of observations.shape, scale: shape and scale parameters, the latter defaulting to 1.log, logP: iftrue, probabilities/densitiespare returned aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x], otherwise,P[X > x].
Example:
R console:
x<- c(0, rlnorm(50))all.equal(dweibull(x,shape=1), dexp(x))
Equivalence in js:
import{rlnorm,dweibull,dexp}from'lib-r-math.js';constsamples=rlnorm(50);constviolation=samples.find((x)=>dweibull(x,1)!==dexp(x));console.log(violation);// -> undefined, no violation!
| type | function spec |
|---|---|
| density function | function dwilcox(x: number, m: number, n: number, log = false): number |
| distribution function | function pwilcox(q: number, m: number, n: number, lowerTail = true, logP = false): number |
| quantile function | function qwilcox(x: number, m: number, n: number, lowerTail = true, logP = false): number |
| random generation (bulk) | function rwilcox(nn: number, m: number, n: number): Float32Array |
| random generation | function rwilcoxOne(m: number, n: number): number |
Arguments:
x, q: quantile.p: probability.nn: number of observations.m, n: numbers of observations in the first and second sample, respectively.log, logP: iftrue, probabilities/densitiespare returned aslog(p).lowerTail: iftrue(default), probabilities areP[ X ≤ x], otherwise,P[X > x].
Example:
R console:
>x<- seq(-1, (4*6+1),4);>fx= dwilcox(x,4,6)>fx[1]0.000000000000000000.014285714285714290.047619047619047620.076190476190476200.06666666666666667[6]0.028571428571428570.00476190476190476
Equivalence in js:
import{dwilcox}from'lib-r-math.js';for(letx=-1;x<=4*6+1;x+=4){console.log(dwilcox(x,4,6));}// ->// 0// 0.014285714285714285// 0.047619047619047616// 0.0761904761904762// 0.06666666666666667// 0.02857142857142857// 0.004761904761904762
Special functions are particular mathematical functions which have more or less established names and notations due to their importance in mathematical analysis, functional analysis, physics, or other applications.
There is no general formal definition, but the list of mathematical functions contains functions which are commonly accepted as special.
Bessel Functions of integer and fractional order, of first and second kind,
| type | function spec |
|---|---|
| Modified Bessel function of the first kind | function BesselI(x: number, nu: number, exponScaled = false): number |
| Modified Bessel function of the third kind | function BesselK(x: number, nu: number, exponScaled = false): number |
| Bessel function of the first kind | function BesselJ(x: number, nu: number): number |
| Bessel function of the second kind | function BesselY(x: number, nu: number): number |
Arguments:
x: must be ≥ 0.nu: The order (maybe fractional and negative) of the corresponding Bessel function.exponScaled: iftrue, the results are exponentially scaled in order to avoid overflow ($I_{\nu}$ ) or underflow ($K_{\nu}$ ), respectively.
Details:IfexponScaled = true,
ForbesselK which is symmetric innu.
The current algorithms will give warnings about accuracy loss for large arguments. In some cases, these warnings are exaggerated, and the precision is perfect. For largenu, say in the order of millions, the current algorithms are rarely useful.
Example:
R console:
>data.frame(besselI(0,0:4), besselI(1,0:4), besselI(2,0:4), besselI(3,0:4), besselI(4,0:4))besselI.0..0.4.besselI.1..0.4.besselI.2..0.4.besselI.3..0.4.besselI.4..0.4.111.266065877752008182.27958530233606734.88079258586502411.30192195213633200.565159103992485031.59063685463732913.9533702174026099.75946515370445300.135747669767038280.68894844769873822.2452124409299516.42218937528411400.022168424924331900.21273995923985260.9597536294960083.33727577842035500.002737120221046870.05072856997918020.3257051819379351.41627570765359
Equivalence in js:
import{BesselI}from'lib-r-math.js';for(letnu=0;nu<=4;nu++){constrow=[0,1,2,3,4].map((x)=>BesselI(x,nu)+'\t');console.log(...row);}/*1 1.2660658777520082 2.2795853023360673 4.880792585865024 11.3019219521363330 0.565159103992485 1.590636854637329 3.9533702174026093 9.759465153704450 0.13574766976703828 0.6889484476987382 2.245212440929951 6.4221893752841060 0.022168424924331902 0.21273995923985264 0.959753629496008 3.3372757784203450 0.002737120221046866 0.05072856997918024 0.32570518193793546 1.4162757076535895*/
The functionsbeta andlbeta return the beta function and the natural logarithm of the beta function,
| type | function spec |
|---|---|
| beta function | function beta(a: number, b: number): number |
| logarithem of the beta function | function lbeta(a: number, b: number): number |
Arguments:
a, b: non negative values quantile.
The formal definition is
(Abramowitz and Stegun section 6.2.1, page 258). Note that it is only defined in R for non-negative a and b, and is infinite if either is zero.
No examples provided, usage is straightforward
| type | function spec |
|---|---|
| gamma function | function gamma(x: number): number |
| natural logarithm of the | function lgamma(x: number, sgn?: Int32Array) |
Arguments:
x: number, can be negative.sgn: (optional) an array, if provided, will have its first element set to the sign ofx
| type | function spec |
|---|---|
| first derivative of the logarithm of the gamma function | function digamma(x: number): number |
| second derivative of the logarithm of the gamma function | function trigamma(x: number): number |
| third derivative of the logarithm of the gamma function | function tetragamma(x: number): number |
| forth derivative of the logarithm of the gamma function | function pentagamma(x: number): number |
| Nth derivative of the logarithm of the gamma function | function psigamma(x: number, deriv: number): number |
Arguments:
x: number, can be negative.deriv:>= 0,deriv = 0computes the first derivative$\Psi_{0}(x) = \frac{\Gamma^{\prime}(x)}{\Gamma(x)}$ deriv = 1computes the second derivative of the logarithm of the gamma function$\Psi_{1}(x) = \frac{d^2}{dx^2}ln\Gamma(x)$ deriv = Ncomputes the(N+1)th derivative of the logarithm of the gamma function$\Psi_{n}(x) = \frac{d^n}{dx^n}ln\Gamma(x)$
| type | function spec |
|---|---|
| binomial coefficient | function choose(n: number, k: number): number |
| natural log of | function lchoose(n: number, k: number): number |
Arguments:
n,k: "n over k".- For $ k \ge 1 $ it is defined as $ \frac{n(n-1)\cdots(n-k+1)}{k!} $.
- For
$k=0$ it is defined as 1. - For
$k \lt 0$ , it is defined as 0.
About
Javascript Pure Implementation of Statistical R "core" numerical libRmath.so
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.


