This is a 100% Pure Javascript ( TypeScript ) re-write of Statistical R nmath "core" numerical library found here. This is a manual re-write, "emscripten" was not used.

Summary

libRmath.js port contains all functions implemented in R nmath counterpart:

probability and quantile functions related to 21 distributions.

functions to work with Special functions in mathematics ( Bessel , Gamma , Beta ).

( , , ). 7 uniform PRNG's. (same output pseudo-random sequence in R for the same initial seeding).

6 normally distributed PRNG's. (same output sequence in R for te same initial seeding).

Function vector (array) arguments follow the R recycling rule.

With this library it becomes trivial to implement hypothesis testing in javascript, calculating p-values and (1 - α) confidence intervals. ( ANOVA uses the F-distribution. Tukey HSD uses ptukey function, etc, etc).

All functions in libRmath.so has been re-written to Javascript ( Typescript ). Use the library with either vanilla Javascript or Typescript . Type definition files are included in the npm package.

Node and Web

The library is an UMD library, it can be used in a web client as in server side node environment.

Installation

serverside

npm install --save lib-r-math.js

web

The module directory contains a minimized bundle for use in html <script> tag. The library is attached to the window.libR object after loading.

< script src = " https://unpkg.com/lib-r-math.js@1.0.74/dist/lib/libR.min.js " > < script > const libR = window . libR ; const { Tukey , Normal , Beta , StudentT , Wilcoxon } = libR ; </ script >

Table of Contents

Differences with R

Some implementation differences exist with R nmath

PRNG's are not global singletons, but separate object instances and you can have as many as you want. The programmer has the choice of having different deviate generators sharing a common source PRNG.

Wilcoxon Sum Rank functions dwilcox, pwilcox, qwilcox use a fraction of the memory, (R will give memory allocation errors for samples ~1000). The JS solution allocates memory sparsely.

Helper functions for porting R programs

Summary

R language operators and function arguments can work with vectorized input . These helper functions are used to mimic this functionality and assist porting of scripts from the R ecosystem using libRmath.js .

div

Divides scalar or an array of values with element of the second array or scalar.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { div } = libR . R ; div ( 3 , 5 ) ; div ( [ 0 , 1 , 2 , 3 ] , 5 ) ; div ( [ 10 , 2 , 3 ] , [ 2 , 4 ] ) ;

mult

Multiplies scalar or an array of values with another scalar or array of values. Applies R recycling rules in case of arguments of unequal array length.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { mult } = libR . R ; mult ( 3 , 5 ) ; mult ( [ 0 , 1 , 2 , 3 ] , [ 5 , 2 ] ) ;

asArray

Creates a new function from an existing one for it to always return its result as an array.

Usage:

; ; ; r ( ) r ( ) r ( )

sum

Analog to R 's sum function. Calculates the sum of all elements of an array.

const libR = require ( ' lib-r-math.js ' ) ; const { sum } = libR . R ; sum ( 5 ) ; sum ( [ 1 , 2 , 3 , 4 ] ) ;

summary

Gives summary information of numeric data in an array.

typescript decl

declare function summary : ISummary ; interface ISummary { ; ; ; ; relX ; relX2 ; ; }

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { summary } = libR . R ; summary ( [ 360 , 352 , 294 , 160 , 146 , 142 , 318 , 200 , 142 , 116 ] )

numberPrecision

Truncates numbers to a specified significant digits. Takes single numeric value as argument or an array of numbers.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const digits4 = libR . R . numberPrecision ( 4 ) ; const pr4a = digits4 ( 1 . 12345678 ) ; const pr4b = digits4 ( [ 0 . 4553 , - 2 . 1243 ] ) ;

any

Test a Predicate for each element in an Array. Returns true or false depending on a test function.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const any = libR . R . any ; any ( [ 1 , 2 , 3 , 4 ] ) ( x => x < 2 ) ; any ( [ 1 , 2 , 3 , 4 ] ) ( x => x > 5 ) ;

arrayrify (DEPRICATED use multiplex )

Mimics R vectorized function arguments. Wraps an existing function changing the first first argument to accept both scalar (number) or an array( number[] ).

Note: Only the first argument is vectorized

typescript decl

declare function arrayrify < T , R > ;

R example

# raise each element by power of 2 c(1,2,3,4)^2 #[1] 1 4 9 16

Javascript equivalent

const libR = require ( ' lib-r-math.js ' ) ; const { arrayrify } = libR . R ; const pow = arrayrify ( Math . pow ) ; pow ( 3 , 4 ) ; pow ( [ 3 , 4 , 5 ] , 4 ) ;

each

Functional analog to Array.prototype.forEach , but can also loop over object properties. The return type can be either an new array or a scalar (see Example ).

Example:

const libR = require ( ' lib-r-math.js ' ) ; const { each } = libR . R ; each ( 11 ) ( v => console . log ( v * 2 ) ) ; each ( [ 3 ] ) ( v => console . log ( v * 2 ) ) ; each ( [ 11 , 12 ] ) ( ( v , idx ) => console . log ( { v , idx } ) ) ; each ( { p : 1 , name : ' myname ' } ) ( ( value , key ) => console . log ( ` ${ key } = ${ value } ` ) )

flatten or c (alias)

Analog to R's c function. Constructs a final array by (recursively) flattening and merging all of its arguments which can be a combination of scalars and arrays.

Example:

const libR = require ( ' lib-r-math.js ' ) ; const { c } = libR . R ; c ( - 1 , 0 , [ 1 ] , ' r ' , ' b ' , [ 2 , 3 , [ 4 , 5 ] ] ) ;

map

Functional analog to Array.prototype.map , but can also loop over object properties. The return type can be either an new array or a scalar (see Example ).

Example:

const libR = require ( ' lib-r-math.js ' ) ; const { map } = libR . R ; map ( 11 ) ( v => v * 2 ) ; map ( [ 3 ] ) ( v => v * 2 ) ; map ( [ 11 , 12 ] ) ( ( v , idx ) => idx ) ; map ( { p : 1 , name : ' myname ' } ) ( ( value , key ) => ` ${ key } = ${ value } ` )

selector

Filter function generator, to be used with Array.prototype.filter to pick elements based on their order (zero based index) in the array. Usually used together with seq to pick items from an array.

NOTE: Always returns an instance of Array.

Example:

const libR = require ( ' lib-r-math.js ' ) ; const { selector } = libR . R ; [ ' an ' , ' array ' , ' with ' , ' some ' , ' elements ' ] . filter ( selector ( [ 0 , 2 , 3 ] ) ) ; [ ' an ' , ' array ' , ' with ' , ' some ' , ' elements ' ] . filter ( selector ( 3 ) ) ; const seq = libR . R . seq ( ) ( ) ; [ 7 , 1 , 2 , 9 , 4 , 8 , 16 ] . filter ( selector ( seq ( 0 , 6 , 2 ) ) ) ;

seq

typescript decl

;

R analog to the seq function in R. Generates an array between start and end (inclusive) using step (defaults to 1 ). The JS implementation ignores the sign of the step argument and only looks at its absolute value.

Like in R, If (end-start)/step is not an exact integer, seq will not overstep the bounds while counting up (or down).

Arguments:

adjustUp : (default 0). If end >= start then adjust value is added to every element in the array.

: (default 0). If >= then value is added to every element in the array. adjustDown : (default 0). If start >= end then adjustMin value is added to every element in the array.

: (default 0). If >= then value is added to every element in the array. start : (inclusive) the sequence start value

: (inclusive) the sequence start value stop : defaults to 1 . (inclusive) the sequence stop value if possible with step

: defaults to . (inclusive) the sequence stop value if possible with step : defaults to 1 , sign is ignored. Sign is inferred from the values of start and stop .

First we look how seq works in R.

R

seq(1,3,0.5); #[1] 1.0 1.5 2.0 2.5 3.0 seq(7,-2, -1.3); #[1] 7.0 5.7 4.4 3.1 1.8 0.5 -0.8

Equivalent in Javascript

const libR = require ( ' lib-r-math.js ' ) ; let seqA = libR . R . seq ( ) ( ) ; seqA ( 1 , 5 ) ; seqA ( 5 , - 3 ) ; seqA ( 3 ) let seqB = libR . R . seq ( 1 ) ( - 2 ) ; seqB ( 0 , 4 ) ; seqB ( 6 , 5 , 0 . 3 ) ;

multiplex

Turns an existing javascript function into one that follows the R argument recycling rule.

Multiplexes the value of several array arguments into one array with the use of a mapping function.

The length of the result is the maximum of the lengths of the parameters. All parameters are recycled to that length.

const libR = require ( ' lib-r-math.js ' ) ; const { multiplex , c } = libR . R ; const pow = multiplex ( Math . pow ) ; pow ( [ 1 , 2 , 3 , 4 ] , 2 ) ; pow ( 2 , [ 2 , 3 , 4 ] ) pow ( [ 2 , 3 ] , [ 2 , 3 , 4 , 5 ] ) ;

timeseed

Generates a number based by on your system time clock. Intended use is with PRNG (re)initialization. Its a synchronized function that will wait for some milliseconds before sampling the system clock and returning a result.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { rng : { timeseed } } = libR ; timeseed ( ) ;

Uniform Pseudo Random Number Generators

Summary

In 'R', the functions that generate random deviates of distributions (Example: Poisson ( rpois ), Student-t ( rt ), Normal ( rnorm ), etc) use uniform PRNG's directly or indirectly (as wrapped in a normal distributed PRNG). This section discusses the uniform distributed PRNG's that have been ported from R to JS.

The 7 Uniform Random Number Generators

All 7 uniform random generators have been ported and tested to yield exactly the same as their R counterpart.

Improvements compared to R

In R it is impossible to use different types of uniform random generators at the same time because of a global shared seed buffer. In our port every random generator has its own buffer and can therefore be used concurrently.

General Usage

All uniform random generator classes have these public methods:

init : set the random generator seed. Same as R set.seed() seed (read/write property) : get/set the current seed values as an array. Same as R .Random.seed . unif_random : get a random value, same as runif(1) in R

From Matsumoto and Nishimura (1998). A twisted GFSR with period 2^19937 - 1 and equi-distribution in 623 consecutive dimensions (over the whole period). The seed is a 624-dimensional set of 32-bit integers plus a current position in that set.

usage example:

const libR = require ( ' lib-r-math.js ' ) ; const { R : { numberPrecision } , rng : { MersenneTwister , timeseed } } = libR ; const precision = numberPrecision ( 9 ) ; const mt = new MersenneTwister ( 12345 ) ; mt . init ( timeseed ( ) ) ; mt . init ( 0 ) ; mt . seed . slice ( 0 , 8 ) ; const rmt1 = mt . unif_rand ( 5 ) ; precision ( rmt1 ) ;

Equivalent in R

RNGkind("Mersenne-Twister") set.seed(0) #show first 8 values of the seed buffer .Random.seed[2:9] #[1] 624 1280795612 -169270483 -442010614 -603558397 -222347416 1489374793 #[8] 865871222 runif(5) #[1] 0.8966972 0.2655087 0.3721239 0.5728534 #[5] 0.9082078

The seed, is an integer vector of length 3, where each element is in 1:(p[i] - 1) , where p is the length 3 vector of primes, p = (30269, 30307, 30323) . The Wichmann–Hill generator has a cycle length of 6.9536e12 = ( 30269 * 30307 * 30323 ) , see Applied Statistics (1984) 33, 123 which corrects the original article).

usage example:

const libR = require ( ' lib-r-math.js ' ) ; const { rng : { WichmannHill , timeseed } , R : { numberPrecision } } = libR ; const precision = numberPrecision ( 9 ) ; const wh = new WichmannHill ( 1234 ) ; wh . init ( timeseed ( ) ) ; wh . init ( 0 ) ; wh . seed ; const rwh1 = wh . unif_rand ( 5 ) ; precision ( rwh1 ) ;

in R console:

> RNGkind("Wichmann-Hill") > set.seed(0) > runif(5) [1] 0.4625532 0.2658268 0.5772108 0.5107932 [5] 0.3375606

A multiply-with-carry RNG is used, as recommended by George Marsaglia in his post to the mailing list ‘sci.stat.math’. It has a period of more than 2^60 and has passed all tests (according to Marsaglia). The seed is two integers (all values allowed).

usage example:

const libR = require ( ' lib-r-math.js ' ) ; const { rng : { MarsagliaMultiCarry , timeseed } , R : { numberPrecision } } = libR ; const precision = numberPrecision ( 9 ) ; const mmc = new MarsagliaMultiCarry ( 1234 ) ; mmc . init ( timeseed ( ) ) ; mmc . init ( 0 ) ; mmc . seed ; const rmmc = mmc . unif_rand ( 5 ) ; precision ( rmmc ) ;

in R console:

> RNGkind("Marsaglia-Multicarry") > set.seed(0) # we cannot access the PRNG directly # we need to use runif wrapper. > runif(5) [1] 0.1691538 0.5315435 0.5946053 0.2333154 [5] 0.4576562

Marsaglia's famous Super-Duper from the 70's. This is the original version which does not pass the MTUPLE test of the Diehard battery. It has a period of about 4.6*10^18 for most initial seeds. The seed is two integers (all values allowed for the first seed: the second must be odd).

We use the implementation by Reeds et al (1982–84).

usage example:

const libR = require ( ' lib-r-math.js ' ) ; const { rng : { SuperDuper , timeseed } , R : { seq , numberPrecision } } = libR ; const precision = numberPrecision ( 9 ) ; const sd = new SuperDuper ( 1234 ) ; sd . init ( timeseed ( ) ) ; sd . init ( 0 ) ; sd . seed ; const rsd1 = sd . unif_rand ( 5 ) ; precision ( rsd1 ) ;

in R console:

> RNGkind("Super-Duper") > set.seed(0) > runif(5) [1] 0.6404036 0.5927313 0.4129687 0.1877294 [5] 0.2679058

An earlier version from Knuth (1997).

The 2002 version was not backwards compatible with the earlier version: the initialization of the GFSR from the seed was altered. R did not allow you to choose consecutive seeds, the reported ‘weakness’, and already scrambled the seeds.

usage example:

const libR = require ( ' lib-r-math.js ' ) ; const { rng : { KnuthTAOCP , timeseed } , R : { seq , numberPrecision } } = libR ; const precision = numberPrecision ( 9 ) ; const kn97 = new KnuthTAOCP ( 1234 ) ; kn97 . init ( timeseed ( ) ) ; kn97 . init ( 0 ) ; kn97 . seed ; const rkn97 = kn97 . unif_rand ( 5 ) ; precision ( rkn97 ) ;

in R console:

> RNGkind("Knuth-TAOCP") > set.seed(0) > runif(5) [1] 0.6274008 0.3541867 0.9898934 0.8624081 0.6622992

"Knuth TAOCP 2002"

A 32-bit integer GFSR using lagged Fibonacci sequences with subtraction. That is, the recurrence used is

X[j] = (X[j-100] - X[j-37]) mod 2^30

and the ‘seed’ is the set of the 100 last numbers (actually recorded as 101 numbers, the last being a cyclic shift of the buffer). The period is around 2129.

usage example:

const libR = require ( ' lib-r-math.js ' ) ; const { rng : { KnuthTAOCP2002 , timeseed } , R : { numberPrecision } } = libR ; const precision = numberPrecision ( 9 ) ; const kt2002 = new KnuthTAOCP2002 ( 1234 ) ; kt2002 . init ( timeseed ( ) ) ; kt2002 . init ( 0 ) ; kt2002 . seed ; const kt02 = kt2002 . unif_rand ( 5 ) ; precision ( kt02 ) ;

in R console:

> RNGkind("Knuth-TAOCP-2002") > set.seed(0) > runif(5) [1] 0.1958190 0.7538669 0.4724112 0.1931604 [5] 0.1950184

A ‘combined multiple-recursive generator’ from L'Ecuyer (1999), each element of which is a feedback multiplicative generator with three integer elements: thus the seed is a (signed) integer vector of length 6. The period is around 2^191.

The 6 elements of the seed are internally regarded as 32-bit unsigned integers. Neither the first three nor the last three should be all zero, and they are limited to less than 4294967087 and 4294944443 respectively.

This is not particularly interesting of itself, but provides the basis for the multiple streams used in package parallel.

usage example:

const libR = require ( ' lib-r-math.js ' ) ; const { rng : { LecuyerCMRG , timeseed } , R : { numberPrecision } } = libR ; const precision = numberPrecision ( 9 ) ; const lc = new LecuyerCMRG ( 1234 ) ; lc . init ( timeseed ( ) ) ; lc . init ( 0 ) ; lc . seed ; const lc1 = lc . unif_rand ( 5 ) ; precision ( lc1 ) ;

in R console:

> RNGkind("L'Ecuyer-CMRG") > set.seed(0) > .Random.seed[2:7] #show the seeds [1] -835792825 1280795612 -169270483 -442010614 [5] -603558397 -222347416 #pick 6 random numbers #same numbers as generated in javascript > runif(5) [1] 0.3329275 0.8903526 0.1639634 0.2990508 [5] 0.3952391

Normal distributed Random Number Generators

Summary

This section discusses the normal distributed PRNG's that have been ported from R to JS.

All 6 normal random generators have been ported and tested to yield exactly the same as their R counterpart.

General Use

All normal random generator adhere to the same principles:

A constructor that takes an instance of a uniform PRNG as an argument A function norm_random : get a random value, same as rnorm(1) in R. A function unif_random : the underlying uniform PRNG. The default argument for the constructor for normal PRNG is : Mersenne-Twister . The class instance property rng contains the wrapped uniform PRNG instance. All PRNG producing normal variates are packaged under the JS library name space rng.normal .

Ahrens, J. H. and Dieter, U. (1973) Extensions of Forsythe's method for random sampling from the normal distribution. Mathematics of Computation 27, 927-937.

example usage:

const libR = require ( ' lib-r-math.js ' ) ; const { rng : { SuperDuper , normal : { AhrensDieter } } , R : { numberPrecision } } = libR ; const precision = numberPrecision ( 9 ) ; const sd = new SuperDuper ( 0 ) ; const ad1 = new AhrensDieter ( sd ) ; sd . init ( 9987 ) ; ad1 . rng . init ( 9987 ) ; const ad2 = new AhrensDieter ( ) ; ad2 . rng . init ( 0 ) ; const rngAd = ad2 . norm_rand ( 5 ) ; precision ( rngAd ) ; ad2 . unif_rand ( ) ; ad2 . rng . unif_rand ( ) ;

in R console

> RNGkind("Mersenne-Twister",normal.kind="Ahrens-Dieter") > set.seed(0) > rnorm(5) [1] -1.1761675 0.6741177 1.0641435 -0.1438973 [5] -1.2311498 > runif(2) [1] 0.2016819 0.8983897

Box Muller

Box, G. E. P. and Muller, M. E. (1958) A note on the generation of normal random deviates. Annals of Mathematical Statistics 29, 610–611.

Example usage:

const libR = require ( ' lib-r-math.js ' ) ; const { rng : { SuperDuper , normal : { BoxMuller } } , R : { numberPrecision } } = libR ; const precision = numberPrecision ( 9 ) ; const sd = new SuperDuper ( 0 ) ; const bm1 = new BoxMuller ( sd ) ; sd . init ( 0 ) ; const bm2 = new BoxMuller ( ) ; bm2 . rng . init ( 0 ) ; const bm = bm2 . norm_rand ( 5 ) ; precision ( bm ) ; bm2 . unif_rand ( ) ; bm2 . rng . unif_rand ( ) ;

R equivalent

> RNGkind("Mersenne-Twister",normal.kind="Box-Muller") > set.seed(0) > rnorm(5) [1] 1.2973876 -0.9843785 -0.7327989 0.7597742 [5] 1.4999888 > runif(2) [1] 0.8983897 0.9446753

Buggy Kinderman Ramage

Kinderman, A. J. and Ramage, J. G. (1976) Computer generation of normal random variables. Journal of the American Statistical Association 71, 893-896.

The Kinderman-Ramage generator used in versions prior to 1.7.0 (now called "Buggy") had several approximation errors and should only be used for reproduction of old results.

example usage:

const libR = require ( ' lib-r-math.js ' ) ; const { R : { numberPrecision } , rng : { SuperDuper , normal : { BuggyKindermanRamage } } } = libR ; const precision = numberPrecision ( 9 ) ; const sd = new SuperDuper ( 0 ) ; const bkm1 = new BuggyKindermanRamage ( sd ) ; sd . init ( 0 ) ; bkm1 . rng . init ( 0 ) ; const bkm2 = new BuggyKindermanRamage ( ) ; bkm2 . rng . init ( 0 ) ; const bk = bkm2 . norm_rand ( 5 ) ; precision ( bk ) ; bkm2 . unif_rand ( ) ; bkm2 . rng . unif_rand ( ) ;

in R Console

RNGkind("Mersenne-Twister",normal.kind="Buggy Kinderman-Ramage"); set.seed(0); rnorm(5); #[1] 0.3216151 1.2325156 0.2803695 -1.1751964 #[5] -1.6047136 runif(2); #[1] 0.1765568 0.6870228

Inversion

Inverse transform sampling wiki

example usage:

const libR = require ( ' lib-r-math.js ' ) ; const { rng : { SuperDuper , normal : { Inversion } } , R : { numberPrecision } } = libR ; const precision = numberPrecision ( 9 ) ; const sd = new SuperDuper ( 0 ) ; const inv1 = new Inversion ( sd ) ; sd . init ( 0 ) ; const inv2 = new Inversion ( ) ; inv2 . rng . init ( 0 ) ; const in2 = inv2 . norm_rand ( 5 ) ; precision ( in2 ) ; inv2 . unif_rand ( ) ; inv2 . rng . unif_rand ( ) ;

in R console

> RNGkind("Mersenne-Twister",normal.kind="Inversion") > set.seed(0) > rnorm(5) [1] 1.2629543 -0.3262334 1.3297993 1.2724293 [5] 0.4146414 > runif(2) [1] 0.06178627 0.20597457

Kinderman Ramage

Kinderman, A. J. and Ramage, J. G. (1976) Computer generation of normal random variables. Journal of the American Statistical Association 71, 893-896.

Non "buggy" version

Example usage:

const libR = require ( ' lib-r-math.js ' ) ; const { rng : { SuperDuper , normal : { KindermanRamage } } , R : { numberPrecision } } = libR ; const precision = numberPrecision ( 9 ) ; const sd = new SuperDuper ( 0 ) ; const km1 = new KindermanRamage ( sd ) ; sd . init ( 1234 ) ; km1 . rng . init ( 1234 ) ; const km2 = new KindermanRamage ( ) ; km2 . rng . init ( 0 ) ; const k2 = km2 . norm_rand ( 5 ) ; precision ( k2 ) ; km2 . unif_rand ( ) ; km2 . rng . unif_rand ( ) ;

in R console

> RNGkind("Mersenne-Twister",normal.kind="Kinder") > set.seed(0) > rnorm(5) [1] 0.3216151 1.2325156 0.2803695 -1.1751964 [5] -1.6047136 > runif(2) [1] 0.1765568 0.6870228 >

Normal and Uniform distributions

Summary

In the Section 1 and 2 we discussed uniform and normal PRNG classes. These classes can be used by themselves but mostly intended to be consumed to generate random numbers with a particular distribution (like Uniform , Normal , Gamma , Weibull , Chi-square etc).

Uniform distribution

dunif, qunif, punif, runif

See R doc

These functions are created with the factory method Uniform taking as argument a uniform PRNG. Defaults to Mersenne-Twister.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Uniform , rng : { SuperDuper } } = libR ; const uni1 = Uniform ( new SuperDuper ( 0 ) ) ; const uni2 = Uniform ( ) ; const { runif , dunif , punif , qunif } = uni2 ;

dunif

The density function. See R doc

typescript decl

declare function dunif : number | number [ ] ;

x : scalar or vector of quantiles

: scalar or vector of quantiles min, max lower and upper limits of the distribution. Must be finite.

lower and upper limits of the distribution. Must be finite. asLog if true , results are given as ln.

Example:

const libR = require ( ' lib-r-math.js ' ) ; const { Uniform , R : { numberPrecision , c } } = libR ; const precision = numberPrecision ( 9 ) ; const { runif , dunif , punif , qunif } = Uniform ( ) ; const x = [ - 1 , 0 , 0 . 4 , 1 , 2 ] ; const d0 = dunif ( 0 . 5 ) ; const d1 = dunif ( x ) ; const d2 = dunif ( x , 0 , 2 ) ; const d3 = dunif ( x , 0 , 2 , true ) ; precision ( d3 ) ;

punif

The probability function. See R doc

typescript decl

declare function punif : number | number [ ] ;

x : scalar or vector of quantiles

: scalar or vector of quantiles min, max : lower and upper limits of the distribution. Must be finite.

: lower and upper limits of the distribution. Must be finite. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : if true , probabilities p are given as ln(p).

Example:

const libR = require ( ' lib-r-math.js ' ) ; const { Uniform , R : { numberPrecision } } = libR ; const precision = numberPrecision ( 9 ) ; const { runif , dunif , punif , qunif } = Uniform ( ) ; const q = [ - 2 , 0 . 25 , 0 . 75 , 2 ] ; const p1 = punif ( 0 . 25 ) ; const p2 = punif ( q ) ; const p3 = punif ( q , 0 , 1 , false ) ; precision ( p3 ) ; const p4 = punif ( q , 0 , 2 , false , true ) ; precision ( p4 ) ; const p5 = punif ( q , 0 , 2 , true , true ) ; precision ( p5 ) ;

qunif

The quantile function. See R doc

typescript decl

declare function qunif : number | number [ ] ;

p : scalar or vector of quantiles

: scalar or vector of quantiles min, max lower and upper limits of the distribution. Must be finite.

lower and upper limits of the distribution. Must be finite. lowerTail if true (default), probabilities are P[X ≤ x], otherwise, P[X > x].

if (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP if true , probabilities p are given as ln(p).

Example:

const libR = require ( ' lib-r-math.js ' ) ; const { Uniform , R : { numberPrecision , multiplex } } = libR ; const precision = numberPrecision ( 9 ) ; const { runif , dunif , punif , qunif } = Uniform ( ) ; const p = [ 0 , 0 . 1 , 0 . 5 , 0 . 9 , 1 ] ; const q1 = qunif ( 0 ) ; const q2 = qunif ( p , - 1 , 1 , false ) ; const log = multiplex ( Math . log ) ; const q3 = qunif ( log ( p ) , - 1 , 1 , false , true ) ;

runif

Generates random deviates. See R doc

typescript decl

declare function runif : number | number [ ] ;

n : number of deviates. Defaults to 1.

: number of deviates. Defaults to 1. min, max lower and upper limits of the distribution. Must be finite.

Example:

const libR = require ( ' lib-r-math.js ' ) ; const { Uniform , rng : { LecuyerCMRG } , R : { numberPrecision } } = libR ; const _9 = numberPrecision ( 9 ) ; const lm = new LecuyerCMRG ( 1234 ) ; const { runif , dunif , punif , qunif } = Uniform ( lm ) ; const r1 = _9 ( runif ( 4 ) ) ; const r2 = _9 ( runif ( 5 , - 1 , 1 , true ) ) ;

Equivalent in R

RNGkind("L'Ecuyer-CMRG"); set.seed(1234); #1 runif(4); #[1] 0.4729098 0.7697837 0.2160154 0.4138440 runif(5,-1,1); #[1] 0.1220072 0.8654446 0.0295475 -0.1844924 0.6457497

Normal distribution

dnorm, qnorm, pnorm, rnorm

Density, distribution function, quantile function and random generation for the normal distribution with mean equal to mean and standard deviation equal to sd . See R doc. These functions are created with the factory method Normal taking as optional argument a normal PRNG (defaults to Inversion.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Normal , rng : { SuperDuper , normal : { AhrensDieter } } } = libR ; const norm1 = Normal ( new AhrensDieter ( new SuperDuper ( 1234 ) ) ) ; const norm2 = Normal ( ) ; const { rnorm , dnorm , pnorm , qnorm } = norm2 ;

dnorm

The density function of the Normal distribution. See R doc

typescript decl

declare function dnorm : number | number [ ] ;

x :scalar or array of quantiles

:scalar or array of quantiles mu : mean, default 0 .

: mean, default . sigma : standard deviation, default 1 .

: standard deviation, default . asLog : give result as ln(..) value

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Normal , R : { numberPrecision , seq : _seq , c } } = libR ; const seq = _seq ( ) ( ) ; const _9 = numberPrecision ( 9 ) ; const { rnorm , dnorm , pnorm , qnorm } = Normal ( ) ; const d1 = _9 ( dnorm ( 0 ) ) ; const d2 = _9 ( dnorm ( 3 , 4 , 2 ) ) ; const d3 = _9 ( dnorm ( - 10 ) ) ; const x = c ( - Infinity , Infinity , NaN , seq ( - 4 , 4 ) ) ; const d4 = _9 ( dnorm ( x ) ) ; const d5 = _9 ( dnorm ( x , 0 , 1 , true ) ) ;

Equivalent in R

dnorm(0); #[1] 0.3989423 dnorm(3, 4, 2); #[1] 0.1760327 dnorm(-10) #[1] 7.694599e-23 x = c(-Inf, Inf, NaN, seq(-4, 4)); dnorm(x) # [1] 0.0000000000 0.0000000000 NaN 0.0001338302 0.0044318484 # [6] 0.0539909665 0.2419707245 0.3989422804 0.2419707245 0.0539909665 #[11] 0.0044318484 0.0001338302 dnorm(x, 0,1, TRUE); # [1] -Inf -Inf NaN -8.9189385 -5.4189385 -2.9189385 # [7] -1.4189385 -0.9189385 -1.4189385 -2.9189385 -5.4189385 -8.9189385

pnorm

The distribution function of the Normal distribution. See R doc

typescript decl

declare function pnorm : number | number [ ] ;

q :scalar or array of quantiles

:scalar or array of quantiles mu : mean (default 0)

: mean (default 0) sigma : standard deviation (default 1)

: standard deviation (default 1) lowerTail : if true (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : give result as log value

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Normal , R : { numberPrecision , multiplex , seq : _seq } } = libR ; const { rnorm , dnorm , pnorm , qnorm } = Normal ( ) ; const seq = _seq ( ) ( ) ; const _9 = numberPrecision ( 9 ) ; const q = seq ( - 1 , 1 ) ; const p1 = _9 ( pnorm ( q ) ) ; const p2 = _9 ( pnorm ( q , 0 , 1 , false ) ) ; const p3 = _9 ( pnorm ( q , 0 , 1 , false , true ) ) ;

Equivalent in R

pnorm(-1:1); #[1] 0.1586553 0.5000000 0.8413447 pnorm(-1:1, lower.tail=FALSE); #[1] 0.8413447 0.5000000 0.1586553 pnorm(-1:1, log.p= TRUE); #[1] -0.1727538 -0.6931472 -1.8410216

qnorm

The quantile function of the Normal distribution. See R doc

typescript decl

declare function qnorm : number | number [ ] ;

p : probabilities (scalar or array).

: probabilities (scalar or array). mu : normal mean (default 0).

: normal mean (default 0). sigma : standard deviation (default 1).

: standard deviation (default 1). logP : probabilities are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Normal , R : { multiplex , seq : _seq , numberPrecision } } = libR ; const log = multiplex ( Math . log ) ; const _9 = numberPrecision ( 9 ) ; const seq = _seq ( ) ( ) ; const { rnorm , dnorm , pnorm , qnorm } = Normal ( ) ; const p = seq ( 0 , 1 , 0 . 25 ) ; const q1 = _9 ( qnorm ( 0 ) ) ; const q2 = _9 ( qnorm ( p , 0 , 2 ) ) ; const q3 = _9 ( qnorm ( p , 0 , 2 , false ) ) ; const q4 = _9 ( qnorm ( log ( p ) , 0 , 2 , false , true ) ) ;

Equivalent in R

p = seq(0, 1, 0.25); qnorm(0); #[1] -Inf qnorm(p, 0, 2); #[1] -Inf -1.34898 0.00000 1.34898 Inf qnorm(p, 0, 2, FALSE); #[1] Inf 1.34898 0.00000 -1.34898 -Inf qnorm(log(p), 0, 2, FALSE, TRUE); #[1] Inf 1.34898 0.00000 -1.34898 -Inf

rnorm

Generates random normal deviates. See R doc

typescript decl

declare function rnorm : number | number [ ] ;

n : number of deviates

: number of deviates mu : mean of the distribution. Defaults to 0.

: mean of the distribution. Defaults to 0. sigma : standard deviation. Defaults to 1.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Normal , R : { numberPrecision } } = libR ; const _9 = numberPrecision ( 9 ) ; const { rnorm , dnorm , pnorm , qnorm } = Normal ( ) ; const r1 = _9 ( rnorm ( 5 ) ) ; const r2 = _9 ( rnorm ( 5 , 2 , 3 ) ) ;

Equivalent in R

RNGkind("Mersenne-Twister",normal.kind="Inversion") set.seed(0) rnorm(5) #[1] 1.2629543 -0.3262334 #[3] 1.3297993 1.2724293 #[5] 0.4146414 rnorm(5,2,3) #[1] -2.6198501 -0.7857011 #[3] 1.1158387 1.9826985 #[5] 9.2139602

Other Probability Distributions

summary

libRmath.so contains 19 probability distributions (other then Normal and Uniform ) with their specific density, quantile and random generators, all are ported and have been verified to yield the same output.

Beta distribution

dbeta, qbeta, pbeta, rbeta

See R doc. See wiki.

These functions are members of an object created by the Beta factory method. The factory method needs an instance of an optional normal PRNG. Various instantiation methods are given below.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Beta , rng : { SuperDuper , normal : { BoxMuller } } } = libR ; const explicitB = Beta ( new BoxMuller ( new SuperDuper ( 0 ) ) ) ; const defaultB = Beta ( ) ; const { dbeta , pbeta , qbeta , rbeta } = defaultB ;

dbeta

The density function of the Beta distribution. See R doc.

$$ \frac{\Gamma(a+b)}{Γ(a) Γ(b)} x^{(a-1)}(1-x)^{(b-1)} $$

typescript decl

declare function dbeta : number | number [ ] ;

x : scalar or array of quantiles. 0 <= x <= 1

: scalar or array of quantiles. 0 <= x <= 1 shape1 : non-negative a parameter of the Beta distribution.

: non-negative parameter of the Beta distribution. shape2 : non-negative b parameter of the Beta distribution.

: non-negative parameter of the Beta distribution. ncp : non centrality parameter. Note: undefined is different then 0

: non centrality parameter. Note: is different then asLog : return result as ln(p)

const libR = require ( ' lib-r-math.js ' ) ; const { Beta , R : { numberPrecision } } = libR ; const _9 = numberPrecision ( 9 ) ; const { dbeta , pbeta , qbeta , rbeta } = Beta ( ) ; const d1 = _9 ( dbeta ( 0 . 4 , 2 , 2 , 1 ) ) ; const d2 = _9 ( dbeta ( 0 . 4 , 2 , 2 , undefined , true ) ) ; const d3 = _9 ( dbeta ( 0 . 4 , 2 , 2 , 1 , true ) ) ; const d4 = _9 ( dbeta ( [ 0 , 0 . 2 , 0 . 4 , 0 . 8 , 1 , 1 . 2 ] , 2 , 2 ) ) ;

Equivalent in R

#1 dbeta(0.4,2,2, ncp=1) #[1] 1.287246 #2 dbeta(0.4,2,2, log = TRUE) #[1] 0.3646431 #3 dbeta(0.4,2,2, ncp=1, TRUE) #[1] 0.2525049 #4 dbeta( c(0, 0.2, 0.4, 0.8, 1, 1.2), 2, 2) #[1] 0.00 0.96 1.44 0.96 0.00 0.00

pbeta

The cumulative probability function of the Beta distribution. See R doc.

declare function pbeta : number | number [ ] ;

p : quantiles. 0 <= x <= 1

: quantiles. 0 <= x <= 1 shape1 : non-negative a parameter of the Beta distribution.

: non-negative parameter of the Beta distribution. shape2 : non-negative b parameter of the Beta distribution.

: non-negative parameter of the Beta distribution. ncp : non centrality parameter. Note: undefined is different then 0

: non centrality parameter. Note: is different then lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : return probabilities as ln(p)

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Beta , R : { multiplex , numberPrecision , seq : _seq } } = libR ; const _9 = numberPrecision ( 9 ) ; const log = multiplex ( Math . log ) ; const seq = _seq ( ) ( ) ; const { dbeta , pbeta , qbeta , rbeta } = Beta ( ) ; const q = seq ( 0 , 1 , 0 . 2 ) ; const p1 = _9 ( pbeta ( 0 . 5 , 2 , 5 ) ) ; const p2 = _9 ( pbeta ( 0 . 5 , 2 , 5 , 4 ) ) ; const p3 = _9 ( pbeta ( q , 2 , 5 , 4 ) ) ; const p4 = _9 ( pbeta ( q , 2 , 5 , undefined ) ) ; const p5 = _9 ( pbeta ( q , 2 , 5 , undefined , true , true ) ) ;

Equivalent in R

q = c(0, 0.2, 0.4, 0.6, 0.8, 1); #1 pbeta(0.5, 2, 5); #[1] 0.890625 #2 pbeta(0.5, 2, 5, 4) #[1] 0.6392384 #3 pbeta(q, 2, 5, 4); #[1] 0.0000000 0.1061302 0.4381503 0.8135394 #[5] 0.9860245 1.0000000 #4 pbeta(q, 2, 5); #[1] 0.00000 0.34464 0.76672 0.95904 0.99840 1.00000 #5 pbeta(q, 2, 5, log.p=TRUE) #[1] -Inf -1.065254885 -0.265633603 #[4] -0.041822495 -0.001601281 0.000000000

qbeta

The quantile function of the Beta distribution. See R doc.

typescript decl

declare function qbeta : number | number [ ] ;

p : quantiles (scalar or array).

: quantiles (scalar or array). shape1 : non-negative a parameter of the Beta distribution.

: non-negative parameter of the Beta distribution. shape2 : non-negative b parameter of the Beta distribution.

: non-negative parameter of the Beta distribution. ncp : non centrality parameter. Note: undefined is different then 0

: non centrality parameter. Note: is different then lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : return probabilities as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Beta , R : { multiplex , numberPrecision } } = libR ; const ln = multiplex ( Math . log ) ; const _9 = numberPrecision ( 9 ) ; const { dbeta , pbeta , qbeta , rbeta } = Beta ( ) ; const p = [ 0 , 0 . 25 , 0 . 5 , 0 . 75 , 1 ] ; const q1 = _9 ( qbeta ( 0 , 99 , 66 ) ) ; const q2 = _9 ( qbeta ( p , 4 , 5 ) ) ; const q3 = _9 ( qbeta ( p , 4 , 5 , 3 ) ) ; const q4 = _9 ( qbeta ( p , 4 , 5 , undefined , false ) ) ; const q5 = _9 ( qbeta ( ln ( p ) , 4 , 5 , undefined , false , true ) ) ;

Equivalent in R

p = c(0,.25,.5,.75,1); #1 qbeta(0,99,66); #[1] 0 #2 qbeta(p, 4,5); #[1] 0.0000000 0.3290834 0.4401552 0.5554863 1.0000000 #3 qbeta(p, 4,5,3); #[1] 0.0000000 0.4068615 0.5213446 0.6318813 1.0000000 #4 qbeta(p, 4,5, lower.tail = FALSE); #[1] 1.0000000 0.5554863 0.4401552 0.3290834 #[5] 0.0000000 #5 qbeta( log(p) ,4,5, lower.tail = FALSE, log.p=TRUE); #[1] 1.0000000 0.5554863 0.4401552 0.3290834 0.0000000

rbeta

Generates random deviates for the Beta distribution. See R doc.

typescript decl

declare function rbeta : number | number [ ] ;

n : number of deviates

: number of deviates shape1 : non-negative a parameter of the Beta distribution.

: non-negative parameter of the Beta distribution. shape2 : non-negative b parameter of the Beta distribution.

: non-negative parameter of the Beta distribution. ncp : non centrality parameter.

const libR = require ( ' lib-r-math.js ' ) ; const { Beta , rng : { LecuyerCMRG , normal : { Inversion } } , R : { multiplex , numberPrecision } } = libR ; const ln = multiplex ( Math . log ) ; const _9 = numberPrecision ( 9 ) ; const lc = new LecuyerCMRG ( 0 ) ; const { dbeta , pbeta , qbeta , rbeta } = Beta ( new Inversion ( lc ) ) ; const r1 = _9 ( rbeta ( 5 , 0 . 5 , 0 . 5 ) ) ; const r2 = _9 ( rbeta ( 5 , 2 , 2 , 4 ) ) ; lc . init ( 0 ) ; const r3 = _9 ( rbeta ( 5 , 2 , 2 ) ) ; const r4 = _9 ( rbeta ( 5 , 2 , 2 , 5 ) ) ;

Same values as in R

Equivalent in R

RNGkind("L'Ecuyer-CMRG", normal.kind ="Inversion") set.seed(0) #1 rbeta(5, 0.5, 0.5) #[1] 0.80058395 0.96296158 0.70071074 0.16974266 0.01698456 #2 rbeta(5, 2, 2, 4) #[1] 0.94097721 0.80393801 0.76206615 0.77531523 0.03958948 set.seed(0) #3 rbeta(5, 2, 2); #[1] 0.3795589 0.2401427 0.4253711 0.9352803 0.6367415 #4 rbeta(5, 2, 2, 5); #[1] 0.5320349 0.9850429 0.7248192 0.6764536 0.8373724

Binomial distribution

dbinom, qbinom, pbinom, rbinom

See R doc and wiki.

These functions are members of an object created by the Binomial factory method. The factory method needs an instance of a normal PRNG. Various instantiation methods are given below.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Binomial , rng : { LecuyerCMRG } } = libR ; const lc = new LecuyerCMRG ( 0 ) ; const explicitB = Binomial ( lc ) ; const defaultB = Binomial ( ) ; const { dbinom , pbinom , qbinom , rbinom } = defaultB ;

dbinom

The density function of the Binomial distribution. See R doc

$$p(x) = \frac{n!}{x!(n-x)!} p^{x} (1-p)^{n-x}$$

typescript decl

declare function dbinom : number | number [ ] ;

x : scalar or array of quantiles.

: scalar or array of quantiles. size : number of trails

: number of trails p : probability of success.

: probability of success. asLog : return result as ln(p)

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Binomial , R : { numberPrecision , seq : _seq } } = libR ; const _9 = numberPrecision ( 9 ) ; const seq = _seq ( ) ( ) ; const x = seq ( 1 , 4 ) ; const { dbinom , pbinom , qbinom , rbinom } = Binomial ( ) ; const d1 = _9 ( dbinom ( 2 , 4 , 0 . 3 ) ) ; const d2 = _9 ( dbinom ( 2 , 4 , 0 . 3 , true ) ) ; const d3 = _9 ( dbinom ( x , 4 , 0 . 3 ) ) ; const d4 = _9 ( dbinom ( x , 4 , 0 . 3 , true ) ) ;

Equivalent in R

#1 dbinom(2,4,0.3) #[1] 0.2646 #2 dbinom(2,4,0.3, TRUE) #[1] -1.329536 #3 dbinom(c(1,2,3,4),4,0.3) #[1] 0.4116 0.2646 0.0756 0.0081 #4 dbinom(c(1,2,3,4),4,0.3, TRUE) #[1] -0.8877033 -1.3295360 -2.5822990 -4.8158912

pbinom

The cumulative probability function of the Binomial distribution. See R doc

declare function pbinom : number | number [ ] ;

q : scalar or array of quantiles.

: scalar or array of quantiles. size : number of trails

: number of trails prob : probability of success.

: probability of success. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : return result as ln(p)

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Binomial , R : { numberPrecision , seq : _seq } } = libR ; const _9 = numberPrecision ( 9 ) ; const seq = _seq ( ) ( ) ; const { dbinom , pbinom , qbinom , rbinom } = Binomial ( ) ; const q = seq ( 0 , 4 ) ; const p1 = pbinom ( 4 , 4 , 0 . 5 ) ; const p2 = _9 ( pbinom ( q , 4 , 0 . 5 ) ) ; const p3 = _9 ( pbinom ( q , 4 , 0 . 5 , false , true ) ) ;

Equivalent in R

q = c(0, 1, 2, 3, 4); #1 pbinom(4, 4, 0.5) #[1] 1 #2 pbinom(q, 4, 0.5) #[1] 0.0625 0.3125 0.6875 0.9375 1.0000 #3 pbinom(q, 4, 0.5, FALSE, TRUE) #[1] -0.06453852 -0.37469345 -1.16315081 #[4] -2.77258872 -Inf

qbinom

The quantile function of the Binomial distribution. See R doc

typescript decl

declare function qbinom : number | number [ ] ;

p : scalar or array of quantiles.

: scalar or array of quantiles. size : number of trails

: number of trails prob : probability of success.

: probability of success. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. LogP : return result as ln(p)

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Binomial , R : { multiplex , numberPrecision , seq : _seq } } = libR ; const _9 = numberPrecision ( 9 ) ; const log = multiplex ( Math . log ) ; const seq = _seq ( ) ( ) ; const { dbinom , pbinom , qbinom , rbinom } = Binomial ( ) ; const p = seq ( 0 , 1 , 0 . 25 ) ; const q1 = _9 ( qbinom ( 0 . 25 , 4 , 0 . 3 ) ) ; const q2 = _9 ( qbinom ( p , 40 , 0 . 3 ) ) ; const q3 = _9 ( qbinom ( p , 40 , 0 . 3 , false ) ) ; const q4 = _9 ( qbinom ( log ( p ) , 40 , 0 . 3 , false , true ) ) ;

Equivalent in R

p = seq(0,1,0.25); #c(0, 0.25, 0.5, 0.75, 1); #1 qbinom(.25,4,.3) #[1] 1 #2 qbinom(p, 40,.3) #[1] 0 10 12 14 40 #3 qbinom(p, 40,.3, FALSE) #[1] 40 14 12 10 0 #4 qbinom(log(p), 40,.3, FALSE, TRUE) #[1] 40 14 12 10 0

rbinom

Generates random beta deviates for the Binomial distribution. See R doc.

typescript decl

declare function rbinom : number | number [ ] ;

n : number of deviates

: number of deviates size : number of trails

: number of trails prob : probability of success.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Binomial , rng : { KnuthTAOCP2002 } } = libR ; const kn = new KnuthTAOCP2002 ( 1234 ) ; const { dbinom , pbinom , qbinom , rbinom } = Binomial ( kn ) ; const r1 = rbinom ( 2 , 40 , 0 . 5 ) ; const r2 = rbinom ( 3 , 20 , 0 . 5 ) ; const r3 = rbinom ( 2 , 10 , 0 . 25 ) ;

Equivalent in R

RNGkind("Knuth-TAOCP-2002") set.seed(1234) #1 rbinom(2, 40, 0.5); #[1] 24 18 #2 rbinom(3, 20, 0.5); #[1] 11 13 13 #3 rbinom(2, 10, 0.25); #[1] 2 2

Negative Binomial distribution

dnbinom, pnbinom, qnbinom, rnbinom.

See R doc See wiki

These functions are members of an object created by the NegativeBinomial factory method. This factory method needs an instance of a normal PRNG. Various instantiation methods are given below.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { NegativeBinomial , rng : { SuperDuper , normal : { BoxMuller } } } = libR ; const bm = new BoxMuller ( new SuperDuper ( 0 ) ) ; const explicitNB = NegativeBinomial ( bm ) ; const defaultNB = NegativeBinomial ( ) ; const { dnbinom , pnbinom , qnbinom , rnbinom } = defaultNB ;

dnbinom

The density function of the Negative Binomial distribution.

$$ \frac{Γ(x+n)}{Γ(n) x!} p^{n} (1-p)^{x} $$

See R doc.

typescript decl

declare function dnbinom : number | number [ ] ;

x : non-negative integer quantiles. Number of failures before reaching size successes.

: non-negative integer quantiles. Number of failures before reaching successes. size : target for number of successful trials, or dispersion parameter (the shape parameter of the gamma mixing distribution). Must be strictly positive, need not be integer.

: target for number of successful trials, or dispersion parameter (the shape parameter of the gamma mixing distribution). Must be strictly positive, need not be integer. prob : probability of success in each trial. 0 < prob <= 1

: probability of success in each trial. 0 < prob <= 1 mu : alternative parametrization via mean: see ‘Details’ section.

: alternative parametrization via mean: see ‘Details’ section. asLog : if true , probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { NegativeBinomial , R : { seq : _seq , numberPrecision } } = libR ; const seq = _seq ( ) ( ) ; const _9 = numberPrecision ( 9 ) ; const { dnbinom , pnbinom , qnbinom , rnbinom } = NegativeBinomial ( ) ; const x = seq ( 0 , 10 , 2 ) ; const d1 = _9 ( dnbinom ( x , 3 , 0 . 5 ) ) ; const d2 = _9 ( dnbinom ( x , 3 , undefined , 3 * ( 1 - 0 . 5 ) / 0 . 5 ) ) ; const d3 = _9 ( dnbinom ( x , 3 , undefined , 3 * ( 1 - 0 . 5 ) / 0 . 5 , true ) ) ;

Equivalent in R

#1 dnbinom(0:10, size = 3, prob = 0.5); # [1] 0.125000000 0.187500000 0.187500000 0.156250000 0.117187500 0.082031250 # [7] 0.054687500 0.035156250 0.021972656 0.013427734 0.008056641 #2 dnbinom(0:10, size = 3, mu = 3*(1-0.5)/0.5); # [1] 0.125000000 0.187500000 0.187500000 0.156250000 0.117187500 0.082031250 # [7] 0.054687500 0.035156250 0.021972656 0.013427734 0.008056641 dnbinom(0:10, size = 3, mu = 3*(1-0.5)/0.5, log=T); # [1] -2.079442 -1.673976 -1.673976 -1.856298 -2.143980 -2.500655 -2.906120 # [8] -3.347953 -3.817956 -4.310433 -4.821259

pnbinom

The gives the cumulative probability function of the Negative Binomial distribution. See R doc.

typescript decl

declare function pnbinom : number | number [ ]

q : non-negative integer quantiles.

: non-negative integer quantiles. size : target for number of successful trials, or dispersion parameter (the shape parameter of the gamma mixing distribution). Must be strictly positive, need not be integer.

: target for number of successful trials, or dispersion parameter (the shape parameter of the gamma mixing distribution). Must be strictly positive, need not be integer. prob : probability of success in each trial. 0 < prob <= 1

: probability of success in each trial. 0 < prob <= 1 mu : alternative parametrization via mean: see ‘Details’ section.

: alternative parametrization via mean: see ‘Details’ section. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : if true , probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { NegativeBinomial , R : { numberPrecision , seq : _seq , c } } = libR ; const seq = _seq ( ) ( ) ; const _9 = numberPrecision ( 9 ) ; const { dnbinom , pnbinom , qnbinom , rnbinom } = NegativeBinomial ( ) ; const x = c ( seq ( 0 , 6 ) , Infinity ) ; const p1 = _9 ( pnbinom ( x , 3 , 0 . 5 ) ) ; const p2 = _9 ( pnbinom ( x , 3 , undefined , 3 * ( 1 - 0 . 5 ) / 0 . 5 ) ) ; const p3 = _9 ( pnbinom ( x , 3 , 0 . 5 , undefined , false ) ) ; const p4 = _9 ( pnbinom ( x , 3 , 0 . 5 , undefined , false , true ) ) ;

Equivalent in R

x = c(seq(0, 6), Inf); #1 pnbinom(x, 3, 0.5) #[1] 0.1250000 0.3125000 0.5000000 0.6562500 0.7734375 0.8554688 0.9101562 #[8] 1.0000000 #2 pnbinom(x, size=3, mu=3*(1-0.5)/0.5) #[1] 0.87500000 0.68750000 0.50000000 0.34375000 0.22656250 0.14453125 0.08984375 #[8] 0.00000000 #3 pnbinom(x, size=3, prob=0.5, lower.tail=FALSE); #[1] 0.87500000 0.68750000 0.50000000 0.34375000 0.22656250 0.14453125 0.08984375 #[8] 0.00000000 #4 pnbinom(x, size=3, prob=0.5, lower.tail=FALSE, log.p=TRUE); #[1] -0.1335314 -0.3746934 -0.6931472 -1.0678406 -1.4847344 -1.9342595 -2.4096832 #[8] -Inf

qnbinom

The quantile function of the Negative Binomial distribution. See R doc.

typescript decl

declare function qnbinom : number | number [ ] ;

p : probabilities (scalar or array).

: probabilities (scalar or array). size : target for number of successful trials, or dispersion parameter (the shape parameter of the gamma mixing distribution). Must be strictly positive, need not be integer.

: target for number of successful trials, or dispersion parameter (the shape parameter of the gamma mixing distribution). Must be strictly positive, need not be integer. prob : probability of success in each trial. 0 < prob <= 1

: probability of success in each trial. 0 < prob <= 1 mu : alternative parametrization via mean: see ‘Details’ section.

: alternative parametrization via mean: see ‘Details’ section. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : if true , probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { NegativeBinomial , R : { numberPrecision , seq : _seq , multiplex } } = libR ; const _9 = numberPrecision ( 9 ) ; const log = multiplex ( Math . log ) ; const seq = _seq ( ) ( ) ; const { dnbinom , pnbinom , qnbinom , rnbinom } = NegativeBinomial ( ) ; const p = seq ( 0 , 1 , 0 . 2 ) ; const q1 = _9 ( qnbinom ( p , 3 , 0 . 5 ) ) ; const q2 = _9 ( qnbinom ( p , 3 , 0 . 5 , undefined , false ) ) ; const q3 = _9 ( qnbinom ( log ( p ) , 3 , 0 . 5 , undefined , false , true ) ) ;

Equivalent in R

p = seq(0, 1, 0.2); #1 qnbinom(p, 3, 0.5); #[1] 0 1 2 3 5 Inf #2 qnbinom(p, 3, 0.5, lower.tail = FALSE); #[1] Inf 5 3 2 1 0 #3 qnbinom(log(p),3,0.5, lower.tail = FALSE, log.p = TRUE); #[1] Inf 5 3 2 1 0

rnbinom

Generates random deviates for the Negative binomial distribution. See R doc.

typescript decl

declare function rnbinom : number | number [ ] ;

n : ensemble size.

: ensemble size. size : target of successful trials.

: target of successful trials. prob : probability of success in each trial. 0 < prob <= 1

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { NegativeBinomial , rng : { SuperDuper , normal : { BoxMuller } } } = libR ; const bm = new BoxMuller ( new SuperDuper ( 12345 ) ) ; const { dnbinom , pnbinom , qnbinom , rnbinom } = NegativeBinomial ( bm ) ; const r1 = rnbinom ( 7 , 100 , 0 . 5 ) ; const r2 = rnbinom ( 7 , 100 , 0 . 1 ) ; const r3 = rnbinom ( 7 , 100 , 0 . 9 ) ; bm . rng . init ( 98765 ) ; const r4 = rnbinom ( 7 , 100 , undefined , 100 * ( 1 - 0 . 5 ) / 0 . 5 ) ;

Equivalent in R

RNGkind("Super-Duper", normal.kind="Box-Muller") set.seed(12345); #1 rnbinom(7, 100, 0.5); #[1] 109 95 89 112 88 90 90 #2 rnbinom(7, 100, 0.1); #[1] 989 1004 842 974 820 871 798 #3 rnbinom(7, 100, 0.9); #[1] 10 14 9 7 12 11 10 #4 set.seed(98765) rnbinom(7,100, mu= 100*(1-0.5)/0.5) #[1] 87 120 113 107 87 95 88

Cauchy distribution

dcauchy, qcauchy, pcauchy, rcauchy

See R doc. See wiki

These functions are members of an object created by the Cauchy factory method. The factory method needs as optional argument an instance of one of the uniform PRNG generators.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Cauchy , rng : { WichmannHill } } = libR ; const wh = new WichmannHill ( 1234 ) ; const explicitC = Cauchy ( wh ) ; const defaultC = Cauchy ( ) ; const { dcauchy , pcauchy , qcauchy , rcauchy } = defaultC ;

dcauchy

The density function of the The Cauchy density. See R doc.

Lemma formula: s is the "scale" parameter and l is the "location" parameter.

$$ f(x) = \frac{1}{ π s (1 + ( \frac{x-l}{s} )^{2}) } $$

typescript decl

declare function dcauchy : number | number [ ] ;

x : scalar or array of quantile(s).

: scalar or array of quantile(s). location : the location parameter, default 0.

: the location parameter, default 0. scale : the scale parameter, default 1.

: the scale parameter, default 1. asLog : return values as ln(p)

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Cauchy , R : { numberPrecision , seq : _seq } } = libR ; const seq = _seq ( ) ( ) ; const _9 = numberPrecision ( 9 ) ; const { dcauchy , pcauchy , qcauchy , rcauchy } = Cauchy ( ) ; const x = seq ( - 4 , 4 , 2 ) ; const d1 = _9 ( dcauchy ( x , - 2 , 0 . 5 ) ) ; const d2 = _9 ( dcauchy ( x , - 2 , 0 . 5 , true ) ) ; const d3 = _9 ( dcauchy ( x , 0 , 2 ) ) ;

Equivalent in R

x=seq(-4,4,2); #1 dcauchy(seq(-4,4,2), location=-2, scale=0.5); #[1] 0.037448222 0.636619772 0.037448222 0.009794150 0.004390481 #2 dcauchy(seq(-4,4,2), location=-2, scale=0.5, log=TRUE); #[1] -3.2847960 -0.4515827 -3.2847960 -4.6259700 -5.4283164 #3 dcauchy(seq(-4,4,2), location=0, scale=2); #[1] 0.03183099 0.07957747 0.15915494 0.07957747 0.03183099

pcauchy

The cumulative probability function of the Cauchy distribution. See R doc.

typescript decl

declare function pcauchy : T ;

q : Scalar or array of quantile(s).

: Scalar or array of quantile(s). location : The location parameter, default 0.

: The location parameter, default 0. scale : The scale parameter, default 1.

: The scale parameter, default 1. lowerTail : If TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: If TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : If TRUE, probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Cauchy , R : { numberPrecision } } = libR ; const seq = libR . R . seq ( ) ( ) ; const _9 = numberPrecision ( 9 ) ; const { dcauchy , pcauchy , qcauchy , rcauchy } = Cauchy ( ) ; const x = seq ( - 4 , 4 , 2 ) ; const p1 = _9 ( pcauchy ( x , - 2 , 0 . 5 ) ) ; const p2 = _9 ( pcauchy ( x , - 2 , 0 . 5 , true , true ) ) ; const p3 = _9 ( pcauchy ( x , 0 , 2 ) ) ;

Equivalent in R

x=seq(-4,4,2) #1 pcauchy(x, location=-2, scale=0.5); #[1] 0.07797913 0.50000000 0.92202087 0.96041658 0.97353532 #2 pcauchy(x, location=-2, scale=0.5, log=TRUE); #[1] -2.55131405 -0.69314718 -0.08118742 -0.04038816 -0.02682117 #3 pcauchy(x, location=0, scale=2); #[1] 0.1475836 0.2500000 0.5000000 0.7500000 0.8524164

qcauchy

The quantile function of the Cauchy distribution. See R doc.

typescript decl

declare function qcauchy : number | number [ ] ;

p : Scalar or array of probabilities(s).

: Scalar or array of probabilities(s). location : The location parameter, default 0.

: The location parameter, default 0. scale : The scale parameter, default 1.

: The scale parameter, default 1. lowerTail : If TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: If TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : If TRUE, probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Cauchy , R : { numberPrecision , seq : _seq } } = libR ; const seq = _seq ( ) ( ) ; const _9 = numberPrecision ( 9 ) ; const { dcauchy , pcauchy , qcauchy , rcauchy } = Cauchy ( ) ; const x = seq ( 0 , 1 , 0 . 2 ) ; const q1 = _9 ( qcauchy ( x , - 2 , 0 . 5 ) ) ; const q2 = _9 ( qcauchy ( x , - 2 , 0 . 5 , false ) ) ; const q3 = _9 ( qcauchy ( x , 0 , 2 ) ) ;

Equivalent in R

x = seq(0, 1, 0.2); #[1] 0.0 0.2 0.4 0.6 0.8 1.0 #1 qcauchy(x, -2, 0.5); #[1] -Inf -2.688191 -2.162460 -1.837540 -1.311809 Inf #2 qcauchy(x, -2, 0.5, lower.tail=FALSE) #[1] Inf -1.311809 -1.837540 -2.162460 -2.688191 -Inf #3 qcauchy(x, 0, 2); #[1] -Inf -2.7527638 -0.6498394 0.6498394 2.7527638 Inf

rcauchy

Generates random deviates from the Cauchy distribution. See R doc.

typescript decl

declare function rcauchy : number | number [ ] ;

n : number of deviates to generate.

: number of deviates to generate. location : The location parameter, default 0.

: The location parameter, default 0. scale : The scale parameter, default 1.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Cauchy , rng : { SuperDuper } , R : { numberPrecision } } = libR ; const _9 = numberPrecision ( 9 ) ; const sd = new SuperDuper ( ) ; const { dcauchy , pcauchy , qcauchy , rcauchy } = Cauchy ( sd ) ; sd . init ( 43210 ) ; const r1 = _9 ( rcauchy ( 5 , 0 , 0 . 5 ) ) ; const r2 = _9 ( rcauchy ( 5 , 2 , 2 ) ) ; sd . init ( 9876 ) ; const r3 = _9 ( rcauchy ( 5 , - 2 , 0 . 25 ) ) ;

Equivalent in R

RNGkind("Super-Duper"); set.seed(43210) #1 rcauchy(5, 0, 0.5); #[1] 0.04726147 0.57770401 6.76536712 -0.03609975 0.71904252 #2 rcauchy(5, 2, 2); #[1] 3.198441 3.281472 1.245431 2.045993 3.539233 #3 set.seed(9876) rcauchy(5, -2, 0.25); #[1] -9.8223614 3.2588417 -0.9187242 -1.7870667 -1.7621220

Chi-Squared (non-central) Distribution

dchisq, qchisq, pchisq, rchisq

These functions are members of an object created by the ChiSquared factory method. The factory method needs as optional argument an instance of a normal PRNG. See wiki. See R doc

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { ChiSquared , rng : { WichmannHill , normal : { AhrensDieter } } } = libR ; const defaultChi = ChiSquared ( ) ; const wh = new WichmannHill ( ) ; const explicitChi = ChiSquared ( new AhrensDieter ( wh ) ) ; const { dchisq , pchisq , qchisq , rchisq } = explicitChi ;

dchisq

The X2 density function, see R doc.

$$ f_{n}(x) = \frac{1}{2^{\frac{n}{2}} Γ(\frac{n}{2})} x^{\frac{n}{2}-1} e^{\frac{-x}{2}} $$

typescript decl

declare function dchisq : number | number [ ] ;

x : quantiles (array or scalar).

: quantiles (array or scalar). df : degrees of freedom.

: degrees of freedom. ncp : non centrality parameter, default undefined.

: non centrality parameter, default undefined. asLog : return probabilities as ln(p), default false.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { ChiSquared , R : { numberPrecision , seq : _seq } } = libR ; const { dchisq , pchisq , qchisq , rchisq } = ChiSquared ( ) ; const seq = _seq ( ) ( ) ; const _9 = numberPrecision ( 9 ) ; const x = seq ( 0 , 10 , 2 ) ; const d1 = _9 ( dchisq ( x , 5 ) ) ; const d2 = _9 ( dchisq ( x , 3 , 4 ) ) ; const d3 = _9 ( dchisq ( x , 3 , 4 , true ) ) ;

Equivalent in R

x=seq(0, 10, 2); #1 dchisq(x, 5); #[1] 0.00000000 0.13836917 0.14397591 0.09730435 0.05511196 0.02833456 #2 dchisq(x, 3, 4); #[1] 0.00000000 0.08371766 0.09970211 0.09014742 0.07076499 0.05075827 #3 dchisq(x, 3, 4, TRUE); #[1] -Inf -2.480305 -2.305568 -2.406309 -2.648391 -2.980681

pchisq

The X2 cumulative probability function, see R doc.

typescript decl

declare function pchisq : number | number [ ] ;

q : quantiles (array or scalar).

: quantiles (array or scalar). df : degrees of freedom.

: degrees of freedom. ncp : non centrality parameter.

: non centrality parameter. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : return probabilities as ln(p)

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { ChiSquared , R : { numberPrecision , seq : _seq , c } } = libR ; const _9 = numberPrecision ( 9 ) ; const seq = _seq ( ) ( ) ; const { dchisq , pchisq , qchisq , rchisq } = ChiSquared ( ) ; const q = c ( seq ( 0 , 10 , 2 ) , Infinity ) ; const p1 = _9 ( pchisq ( q , 3 ) ) ; const p2 = _9 ( pchisq ( q , 8 , 4 , false ) ) ; const p3 = _9 ( pchisq ( q , 8 , 4 , true , true ) ) ;

Equivalent in R

q = c(seq(0, 10, 2), Inf); #1 pchisq(q, 3); #[1] 0.0000000 0.4275933 0.7385359 0.8883898 0.9814339 1.0000000 #2 pchisq(q, 8, 4, lower.tail=FALSE); #[1] 1.0000000 0.9962628 0.9610026 0.8722689 0.5873029 0.0000000 #3 pchisq(q, 8, 4, lower.tail=TRUE, log.p=TRUE); #[1] -Inf -5.5894197 -3.2442613 -2.0578284 -1.3441665 -0.8850413 0.0000000

qchisq

The X2 quantile function, see R doc.

typescript decl

declare function qchisq : number | number [ ] ;

p : probabilities (array or scalar).

: probabilities (array or scalar). df : degrees of freedom.

: degrees of freedom. ncp : non centrality parameter.

: non centrality parameter. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : probabilities are as ln(p)

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { ChiSquared , R : { multiplex , numberPrecision , seq : _seq } } = libR ; const seq = _seq ( ) ( ) ; const log = multiplex ( Math . log ) ; const _9 = numberPrecision ( 9 ) ; const { dchisq , pchisq , qchisq , rchisq } = ChiSquared ( ) ; const p = seq ( 0 , 1 , 0 . 2 ) ; const q1 = _9 ( qchisq ( p , 3 ) ) ; const q2 = _9 ( qchisq ( p , 50 , undefined , false ) ) ; const q3 = _9 ( qchisq ( log ( p ) , 50 , 0 , false , true ) ) ;

Equivalence in R

#R-script #data p=seq(0, 1, 0.2); #1 qchisq(p, 3); #[1] 0.000000 1.005174 1.869168 2.946166 4.641628 Inf #2 qchisq(p, 50, lower.tail=FALSE); #[1] Inf 58.16380 51.89158 46.86378 41.44921 0.00000 #3 qchisq(log(p), 50, 0, lower.tail=FALSE, log.p=TRUE); #[1] Inf 58.16380 51.89158 46.86378 41.44921 0.00000

rchisq

Creates random deviates for the X2 distribution, see R doc.

typescript decl

declare function rchisq : number | number [ ] ;

p : probabilities (array or scalar).

: probabilities (array or scalar). df : degrees of freedom.

: degrees of freedom. ncp : non centrality parameter.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { ChiSquared , rng : { LecuyerCMRG , normal : { AhrensDieter } } , R : { numberPrecision } } = libR ; const _9 = numberPrecision ( 9 ) ; const lc = new LecuyerCMRG ( 1234 ) ; const { dchisq , pchisq , qchisq , rchisq } = ChiSquared ( new AhrensDieter ( lc ) ) ; const r1 = _9 ( rchisq ( 5 , 6 ) ) ; const r2 = _9 ( rchisq ( 5 , 40 , 3 ) ) ; const r3 = _9 ( rchisq ( 5 , 20 ) ) ;

Equivalent in R

RNGkind("L'Ecuyer-CMRG", normal.kind="Ahrens-Dieter") set.seed(1234) #1 rchisq(5, 6); #[1] 12.4101973 6.7995418 9.8091188 4.6460409 0.3519855 #2 rchisq(5, 40, 3); #[1] 22.20106 44.03361 36.32012 44.62124 40.11426 #3 rchisq(5, 20); #[1] 24.43397 19.03792 26.69653 18.12886 26.72433

Exponential Distribution

dexp, qexp, pexp, rexp

See R doc

These functions are members of an object created by the Exponential factory method. The factory method needs as optional argument an instance of an uniform PRNG class.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Exponential , rng : { MarsagliaMultiCarry } } = libR ; const defaultExponential = Exponential ( ) ; const mmc = new MarsagliaMultiCarry ( 123456 ) ; const customExponential = Exponential ( mmc ) ; const { dexp , pexp , qexp , rexp } = defaultExponential ;

dexp

The Exponential density function, see R doc.

$$ f(x) = λ {e}^{- λ x} $$

typescript decl

declare function dexp : number | number [ ] ;

x : quantiles (array or scalar).

: quantiles (array or scalar). rate : the λ parameter.

: the λ parameter. asLog : return probabilities as ln(p)

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Exponential , R : { numberPrecision } } = libR ; const seq = libR . R . seq ( ) ( ) ; const precision = numberPrecision ( 9 ) ; const { dexp , pexp , qexp , rexp } = Exponential ( ) ; const x = seq ( 0 , 0 . 3 , 0 . 05 ) ; const d1 = dexp ( x , 3 ) ; precision ( d1 ) ; const d2 = dexp ( x , 3 , true ) ; precision ( d2 ) ; const d3 = dexp ( x , 0 . 2 ) ; precision ( d3 ) ;

Equivalent in R

x = seq(0, 0.3, 0.05); #1 dexp(x, 3) #[1] 3.000000 2.582124 2.222455 1.912884 1.646435 1.417100 1.219709 #2 dexp(x, 3, TRUE) #[1] 1.0986123 0.9486123 0.7986123 0.6486123 0.4986123 0.3486123 0.1986123 #3 dexp(x, 0.2) #[1] 0.2000000 0.1980100 0.1960397 0.1940891 0.1921579 0.1902459 0.1883529

pexp

The cumulative probability of the Exponential distribution, see R doc.

typescript decl

declare function pexp : number | number [ ] ;

q : quantiles (array or scalar).

: quantiles (array or scalar). rate : the λ parameter.

: the λ parameter. lowerTail : if TRUE (default), probabilities are P[X ≤ q], otherwise, P[X > q].

: if TRUE (default), probabilities are P[X ≤ q], otherwise, P[X > q]. logP : return probabilities as ln(p)

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Exponential , R : { numberPrecision } } = libR ; const seq = libR . R . seq ( ) ( ) ; const precision = numberPrecision ( 9 ) ; const { dexp , pexp , qexp , rexp } = Exponential ( ) ; const q = seq ( 0 , 0 . 3 , 0 . 05 ) ; const p1 = pexp ( q , 3 ) ; precision ( p1 ) ; const p2 = pexp ( q , 7 , false , true ) ; precision ( p2 ) ; const p3 = pexp ( seq ( 0 , 10 , 2 ) , 0 . 2 ) ; precision ( p3 ) ;

Equivalent in R

#data q = seq(0, 0.3, 0.05); #1 pexp(q, 3); #[1] 0.0000000 0.1392920 0.2591818 0.3623718 0.4511884 0.5276334 0.5934303 #2 pexp(q, 7, FALSE, TRUE); #[1] 0.00 -0.35 -0.70 -1.05 -1.40 -1.75 -2.10 #3 pexp(seq(0,10,2),0.2) #[1] 0.0000000 0.3296800 0.5506710 0.6988058 0.7981035 0.8646647

qexp

The quantile function of the Exponential distribution, see R doc.

typescript decl

declare function qexp : number | number [ ] ;

p : probabilities (array or scalar).

: probabilities (array or scalar). rate : the λ parameter.

: the λ parameter. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : return probabilities as ln(p)

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Exponential , R : { arrayrify , numberPrecision } } = libR ; const log = arrayrify ( Math . log ) ; const seq = libR . R . seq ( ) ( ) ; const precision = numberPrecision ( 9 ) ; const { dexp , pexp , qexp , rexp } = Exponential ( ) ; const q = seq ( 0 , 10 , 2 ) ; const pp1 = pexp ( q , 0 . 2 ) ; const q1 = qexp ( log ( pp1 ) , 0 . 2 , true , true ) ; precision ( q1 ) ; const pp2 = pexp ( seq ( 0 , 10 , 2 ) , 0 . 2 ) ; const q2 = qexp ( pp1 , 0 . 2 ) ; precision ( q2 ) ; const pp3 = pexp ( seq ( 0 , 0 . 3 , 0 . 05 ) , 3 ) ; const q3 = qexp ( pp3 , 3 ) ; precision ( q3 ) ;

Equivalent in R

q = seq(0,10,2); #1 pp1 = pexp(q,0.2); qexp(log(pp1),0.2, TRUE, TRUE) #[1] 0 2 4 6 8 10 #2 pp2 = pexp(q ,0.2); qexp(pp2,0.2) #[1] 0 2 4 6 8 10 #3 pp3 = pexp(seq(0, 0.3, 0.05), 3); q3 = qexp(pp3,3) #[1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30

rexp

Creates random deviates for the Exponential distribution, see R doc.

typescript decl

declare function rexp : number | number [ ] ;

n : number of deviates to generate (array or scalar).

: number of deviates to generate (array or scalar). rate : the λ parameter.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Exponential , rng : { WichmannHill } , R : { numberPrecision } } = libR ; const precision = numberPrecision ( 9 ) ; const wh = new WichmannHill ( 1234 ) ; const { dexp , pexp , qexp , rexp } = Exponential ( wh ) ; wh . init ( 12345 ) ; const r1 = rexp ( 5 ) ; precision ( r1 ) ; const r2 = rexp ( 5 , 0 . 1 ) ; precision ( r2 ) ; const r3 = rexp ( 5 , 3 ) ; precision ( r3 ) ;

Equivalent in R

RNGkind("Wichmann-Hill") set.seed(12345) #1 rexp(5) #[1] 0.1891411 1.5673139 3.8044234 3.1539412 2.6618655 #2 rexp(5,0.1) #[1] 6.236918 3.690251 4.061700 9.356170 17.948649 #3 rexp(5,3) #[1] 0.1038344 0.1897598 0.3293326 0.4623079 0.4263606

F (non-central) Distribution

df, qf, pf, rf

See R doc

These functions are members of an object created by the FDist factory method. The factory method needs as optional argument an instance of one of the normal PRNG's.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { FDist , rng : { MersenneTwister , normal : { KindermanRamage } } } = libR ; const defaultF = FDist ( ) ; const mt = new MersenneTwister ( 1234 ) ; const customF = FDist ( new KindermanRamage ( mt ) ) ; const { df , pf , qf , rf } = customF ;

df

The density function of the F distribution. See R doc

With df1 and df2 degrees of freedom:

$$ \large f(x) = \frac{ Γ(\frac{df1 + df2}{2}) } { Γ(\frac{df1}{2}) Γ(\frac{df2}{2}) } {(\frac{n1}{n2})}^{(\frac{df1}{2})} x^{(\frac{df1}{2} - 1)} (1 + \frac{df1}{df2} x)^{-(n1 + n2)/2} $$

typescript decl

declare function df : number | number [ ] ;

x : quantiles (array or scalar).

: quantiles (array or scalar). df1 : degrees of freedom. Infinity is allowed.

: degrees of freedom. is allowed. df2 : degrees of freedom. Infinity is allowed.

: degrees of freedom. is allowed. ncp : non-centrality parameter. If omitted the central F is assumed.

: non-centrality parameter. If omitted the central F is assumed. asLog : if TRUE, probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { FDist , R : { numberPrecision } } = libR ; const seq = libR . R . seq ( ) ( ) ; const precision = numberPrecision ( 9 ) ; const { df , pf , qf , rf } = FDist ( ) ; const x = seq ( 0 , 4 , 0 . 5 ) ; const d1 = df ( x , 5 , 10 , 8 ) ; precision ( d1 ) ; const d2 = df ( x , 50 , 10 , undefined , true ) ; precision ( d2 ) ; const d3 = df ( x , 6 , 25 ) ; precision ( d3 ) ; const d4 = df ( x , 6 , 25 , 8 , true ) ; precision ( d4 ) ;

Equivalence in R

x = seq(0, 4, 0.5); #1. df(x, df1=5,df2=10, ncp=8) #[1] 0.0000000 0.0972907 0.2195236 0.2702561 0.2629984 0.2290042 0.1884130 #[8] 0.1505385 0.1185561 #2. df(x, df1=50,df2=10, log = TRUE) #[1] -Inf -0.6882178 -0.2225805 -0.9406188 -1.7711223 -2.5595094 -3.2807632 #[8] -3.9366072 -4.5344049 #3 df(x, 6, 25) #[1] 0.000000000 0.729921524 0.602808536 0.323999956 0.155316972 0.072482940 #[7] 0.034022568 0.016280785 0.007986682 #4 df(x, 6, 25, 8, log=TRUE) #[1] -Inf -2.432737 -1.382074 -1.081234 -1.094089 -1.270433 -1.540262 #[8] -1.865816 -2.224900

pf

The cumulative probability function of the F distribution. See R doc.

typescript decl

declare function pf : number [ ] | number ;

q : quantiles (array or scalar).

: quantiles (array or scalar). df1 : degrees of freedom. Infinity is allowed.

: degrees of freedom. is allowed. df2 : degrees of freedom. Infinity is allowed.

: degrees of freedom. is allowed. ncp : non-centrality parameter. If omitted the central F is assumed.

: non-centrality parameter. If omitted the central F is assumed. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. asLog : if TRUE, probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { FDist , R : { numberPrecision } } = libR ; const seq = libR . R . seq ( ) ( ) ; const precision = numberPrecision ( 9 ) ; const { df , pf , qf , rf } = FDist ( ) ; const x = seq ( 0 , 4 , 0 . 5 ) ; const p1 = pf ( x , 5 , 10 , 8 ) ; precision ( p1 ) ; const p2 = pf ( x , 50 , 10 , undefined , false ) ; pecision ( p2 ) ; const p3 = pf ( x , 50 , 10 , undefined , false , true ) ; precision ( p3 ) ; const p4 = pf ( x , 6 , 25 , 8 , undefined , true ) ; precision ( p4 ) ;

Equivalent in R

x = seq(0, 4, 0.5); #1 pf(x, 5, 10, 8); #[1] 0.00000000 0.01899614 0.10046841 0.22599052 0.36101519 0.48460988 0.58898121 #[8] 0.67350846 0.74051632 #2 pf(x, 50, 10, lower.tail=FALSE); #[1] 1.00000000 0.94681231 0.54364309 0.25065625 0.11813541 0.05958673 0.03219014 #[8] 0.01847304 0.01116140 #3 pf(x, 50, 10, lower.tail=FALSE, log.p=TRUE); #[1] 0.0000000 -0.0546544 -0.6094623 -1.3836728 -2.1359238 -2.8203224 -3.4360951 #[8] -3.9914432 -4.4952937 #4 pf(x, 6, 25, 8, log.p=TRUE); #[1] -Inf -4.2023511 -2.2961822 -1.3761450 -0.8577369 -0.5461776 -0.3525386 #[8] -0.2297973 -0.1509996

qf

The quantile function of the F distribution. See R doc.

typescript decl

declare function qf : number | number [ ] ;

p : probabilities (array or scalar).

: probabilities (array or scalar). df1 : degrees of freedom. Infinity is allowed.

: degrees of freedom. is allowed. df2 : degrees of freedom. Infinity is allowed.

: degrees of freedom. is allowed. ncp : non-centrality parameter. If omitted the central F is assumed.

: non-centrality parameter. If omitted the central F is assumed. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. asLog : if TRUE, probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { FDist } = libR ; const seq = libR . R . seq ( ) ( ) ; const precision = libR . R . numberPrecision ( 9 ) ; const { df , pf , qf , rf } = FDist ( ) ; const q = [ ... seq ( 0 , 4 ) , Infinity ] ; const pp1 = pf ( q , 50 , 10 , undefined , false ) ; const q1 = qf ( pp1 , 50 , 10 , undefined , false ) ; precision ( q1 ) ; const pp2 = pf ( q , 50 , 10 , 9 , false , true ) ; const q2 = qf ( pp2 , 50 , 10 , 9 , false , true ) ; precision ( q2 ) ; const pp3 = pf ( q , 6 , 25 , 8 ) ; const q3 = qf ( pp3 , 6 , 25 , 8 ) ; precision ( q3 ) ; const pp4 = pf ( q , 3 , 9000 , undefined , false ) ; const q4 = qf ( pp4 , 3 , 9000 , undefined , false ) ;

Equivlent in R

q = c( seq(0,4), Inf); #1. pp1=pf(q, 50, 10, lower.tail=FALSE); qf(pp1, 50, 10, lower.tail=FALSE); #[1] 0 1 2 3 4 Inf #2 pp2 = pf(q, 50, 10, 9, lower.tail=FALSE, log.p=TRUE); qf(pp2, 50, 10, 9, lower.tail=FALSE, log.p=TRUE); #[1] 0 1 2 3 4 Inf #3 pp3 = pf(q, 6, 25, 8); qf(pp3, 6, 25, 8); #[1] 0 1 2 3 4 Inf #4 pp4 = pf(q, 3, 9000, lower.tail=FALSE); qf(pp4, 3, 9000, lower.tail=FALSE); #[1] 0 1 2 3 4 Inf

rf

Generates deviates for the F distribution. See R doc.

typescript decl

declare function rf : number | number [ ] ;

n : number of deviates to generate.

: number of deviates to generate. df1 : degrees of freedom. Infinity is allowed.

: degrees of freedom. is allowed. df2 : degrees of freedom. Infinity is allowed.

: degrees of freedom. is allowed. ncp : non-centrality parameter. If omitted the central F is assumed.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { FDist , rng : { MersenneTwister , normal : { KindermanRamage } } , R : { numberPrecision } } = libR ; const seq = libR . R . seq ( ) ( ) ; const precision = numberPrecision ( 9 ) ; const mt = new MersenneTwister ( 1234 ) ; const { df , pf , qf , rf } = FDist ( new KindermanRamage ( mt ) ) ; precision ( rf ( 5 , 8 , 6 ) ) ; precision ( rf ( 5 , Infinity , Infinity ) ) ; precision ( rf ( 5 , 40 , Infinity , 0 ) ) ; precision ( rf ( 5 , 400 , Infinity ) ) ;

in R Console:

RNGkind("Mersenne-Twister", normal.kind="Kinderman-Ramage"); set.seed(1234); #1. rf(5,8,6) #[1] 0.3986174 2.1329082 2.0211488 2.5957924 4.0114025 #2. rf(5, Inf, Inf) #[1] 1 1 1 1 1 #3. rf(5, 40, Inf, 0) #[1] NaN NaN NaN NaN NaN #4. rf(5, 400, Inf) #[1] 1.0042401 1.0026916 1.0361985 0.9652930 0.9047864

Gamma distribution

dgamma, qgamma, pgamma, rgamma

See R doc. See wiki.

These functions are members of an object created by the Gamma factory method. The factory method needs as optional argument an instance of one of the normal PRNG's.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Gamma , rng : { KnuthTAOCP2002 , normal : { AhrensDieter } } } = libR ; const defaultGamma = Gamma ( ) ; const mt = new KnuthTAOCP2002 ( 123456 ) ; const customG = Gamma ( new AhrensDieter ( mt ) ) ; const { dgamma , pgamma , qgamma , rgamma } = customG ;

dgamma

The density function of the Gamma distribution. See R doc

$$ f(x)= \frac{1}{s^{a} \Gamma(a)} x^{a-1} e^{-x/s} $$

a : shape parameter

: shape parameter s : scale parameter

: scale parameter x : x >= 0

Alternative represention using shape parameter a and rate parameter β = $1/s$:

$$ f(x)= \frac{β^{a}}{\Gamma(a)} x^{a-1} e^{-xβ} $$

You must either specify scale or rate parameters but not both (unless rate = 1/scale).

typescript decl

declare function dgamma : number | number [ ] ;

x : quantiles (scalar or array).

: quantiles (scalar or array). shape : shape parameter, must be positive.

: shape parameter, must be positive. rate : The rate parameter, when specified, leave scale undefined (or set rate = 1/scale ). Must be strictly positive.

: The rate parameter, when specified, leave undefined (or set ). Must be strictly positive. scale : The scale parameter, when specified, leave rate undefined (or set scale = 1/rate ). Must be strictly positive.

: The scale parameter, when specified, leave undefined (or set ). Must be strictly positive. asLog : if true, probabilities/densities p are returned as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Gamma , R : { numberPrecision , arrayrify } } = libR ; const log = arrayrify ( Math . log ) ; const seq = libR . R . seq ( ) ( ) ; const precision = numberPrecision ( 9 ) ; const { dgamma , pgamma , qgamma , rgamma } = Gamma ( ) ; const x = seq ( 0 , 10 , 2 ) ; const d1 = dgamma ( x , 1 , 0 . 5 ) ; precision ( d1 ) ; const d2 = dgamma ( x , 2 , 1 / 2 ) ; precision ( d2 ) ; const d3 = dgamma ( x , 5 , 1 ) ; precision ( d3 ) ; const d4 = dgamma ( x , 7 . 5 , 1 , undefined , true ) ; precision ( d4 ) ;

in R Console

#1. these 2 give the same output dgamma( seq(0, 10, 2), 1, scale = 2); dgamma( seq(0, 10, 2), 1, rate = 1/2); #[1] 0.500000000 0.183939721 0.067667642 0.024893534 0.009157819 0.003368973 #2. dgamma( seq(0, 10, 2), 2, scale = 2); dgamma( seq(0, 10, 2), 2, rate = 1/2); #[1] 0.00000000 0.18393972 0.13533528 0.07468060 0.03663128 0.01684487 #3. dgamma( seq(0, 10, 2), 5, scale = 1); dgamma( seq(0, 10, 2), 5, rate = 1); #[1] 0.00000000 0.09022352 0.19536681 0.13385262 0.05725229 0.01891664 #4. dgamma( seq(0, 10, 2), 7.5, scale = 1, log = TRUE) dgamma( seq(0, 10, 2), 7.5, rate = 1, log = TRUE) #[1] -Inf -5.028908 -2.523451 -1.887928 -2.017994 -2.567561

pgamma

The cumulative probability function of the Gamma distribution. See R doc.

typescript decl

declare function pgamma : number | number [ ] ;

x : quantiles (scalar or array).

: quantiles (scalar or array). shape : shape parameter, must be positive.

: shape parameter, must be positive. rate : The rate parameter, when specified, leave scale undefined (or set rate = 1/scale ). Must be strictly positive.

: The rate parameter, when specified, leave undefined (or set ). Must be strictly positive. scale : The scale parameter, when specified, leave rate undefined (or set scale = 1/rate ). Must be strictly positive.

: The scale parameter, when specified, leave undefined (or set ). Must be strictly positive. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : if true, probabilities/densities p are as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Gamma , R : { arrayrify , numberPrecision } } = libR ; const log = arrayrify ( Math . log ) ; const seq = libR . R . seq ( ) ( ) ; const precision = numberPrecision ( 9 ) ; const { dgamma , pgamma , qgamma , rgamma } = Gamma ( ) ; const x = seq ( 0 , 10 , 2 ) ; const p1 = pgamma ( x , 1 , 0 . 5 ) ; const p1Equavalent = pgamma ( x , 1 , undefined , 2 ) ; precision ( p1 ) ; const p2 = pgamma ( x , 2 , 0 . 5 ) ; const p2Equivalent = pgamma ( x , 2 , undefined , 2 ) ; precision ( p2 ) ; const p3 = pgamma ( x , 5 , 1 , undefined , false , true ) ; const p3Equivalent = pgamma ( x , 5 , undefined , 1 , false , true ) ; precision ( p3 ) ; const p4 = pgamma ( x , 7 . 5 , 1 , undefined , false , true ) ; const p4Equivalent = pgamma ( x , 7 . 5 , undefined , 1 , false , true ) ; precision ( p4 ) ;

Equivalent in R

x=seq(0,10,2); #1 pgamma(x, 1, rate = 0.5); #[1] 0.0000000 0.6321206 0.8646647 0.9502129 0.9816844 0.9932621 #2 pgamma(x, 2, rate = 0.5); #[1] 0.0000000 0.2642411 0.5939942 0.8008517 0.9084218 0.9595723 #3 pgamma(x, 5, rate=1, lower.tail = FALSE, log.p = TRUE); #[1] 0.00000000 -0.05408985 -0.46388330 -1.25506787 -2.30626786 -3.53178381 #4 pgamma(x, 7.5, rate = 7.5, lower.tail = FALSE , log.p = TRUE ); #[1] 0.00000000 -0.00226522 -0.07927840 -0.38709136 -0.96219944 -1.76065222

qgamma

The quantile function of the Gamma distribution. See R doc.

typescript decl

declare function pgamma : number | number [ ] ;

x : quantiles (scalar or array).

: quantiles (scalar or array). shape : shape parameter, must be positive.

: shape parameter, must be positive. rate : The rate parameter, when specified, leave scale undefined (or set rate = 1/scale ). Must be strictly positive.

: The rate parameter, when specified, leave undefined (or set ). Must be strictly positive. scale : The scale parameter, when specified, leave rate undefined (or set scale = 1/rate ). Must be strictly positive.

: The scale parameter, when specified, leave undefined (or set ). Must be strictly positive. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : if true, probabilities/densities p are as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Gamma , R : { numberPrecision , arrayrify } } = libR ; const log = arrayrify ( Math . log ) ; const seq = libR . R . seq ( ) ( ) ; const precision = numberPrecision ( 9 ) ; const { dgamma , pgamma , qgamma , rgamma } = Gamma ( ) ; const x = seq ( 0 , 10 , 2 ) ; const pp1 = pgamma ( x , 1 , 0 . 5 ) ; const q1 = qgamma ( pp1 , 1 , 0 . 5 ) ; precision ( q1 ) ; const pp2 = pgamma ( x , 2 , 0 . 5 ) ; const q2 = qgamma ( pp2 , 2 , 0 . 5 ) ; precision ( q2 ) ; const pp3 = pgamma ( x , 5 , 1 , undefined , false , true ) ; const q3 = qgamma ( pp3 , 5 , undefined , 1 , false , true ) ; precision ( q3 ) ; const pp4 = pgamma ( x , 7 . 5 , 1 , undefined , false ) ; const q4 = qgamma ( log ( pp4 ) , 7 . 5 , 1 , undefined , false , true ) ; precision ( q4 ) ;

Equivalent in R

x = seq(0, 10, 2); #1. pp1 = pgamma(x, 1, 0.5) qgamma(pp1, 1, 0.5) #[1] 0 2 4 6 8 10 #2. pp2 = pgamma(x, 2, 0.5); qgamma(pp2, 2, 0.5); #[1] 0 2 4 6 8 10 #3. pp3 = pgamma(x, 5, 1, lower.tail= FALSE, log.p=TRUE); qgamma(pp3, 5, scale= 1, lower.tail=FALSE, log.p=TRUE); #[1] 0 2 4 6 8 10 #4 pp4 = pgamma(x, 7.5, 1, lower.tail=FALSE); qgamma(log(pp4), 7.5, 1, lower.tail=FALSE , log.p=TRUE); #[1] 0 2 4 6 8 10

rgamma

Generates random deviates for the Gamma distribution. See R doc.

declare function rgamma : number | number [ ] ;

n : number of deviates generated.

: number of deviates generated. shape : shape parameter, must be positive.

: shape parameter, must be positive. rate : The rate parameter, when specified, leave scale undefined (or set rate = 1/scale ). Must be strictly positive.

: The rate parameter, when specified, leave undefined (or set ). Must be strictly positive. scale : The scale parameter, when specified, leave rate undefined (or set scale = 1/rate ). Must be strictly positive.

Usage:

; ; ; ; ; ; ; ; precision ( r1 ) ; ; precision ( r2 ) ; ; precision ( r3 ) ;

Equivalent in R

RNGkind("L'Ecuyer-CMRG", normal.kind="Box-Muller") set.seed(1234); #1 rgamma(5, 1, 0.5); #[1] 0.24589578 1.18079997 0.12139797 1.93698980 0.00324085 #2 rgamma(5, 2, 0.5); #[1] 2.703580 2.138497 3.202168 2.997765 1.783942 #3 rgamma(5, 7.5, 1); #[1] 8.871102 5.348633 10.805079 9.077132 9.393374

Geometric distribution

dgeom, qgeom, pgeom, rgeom

See R doc and wiki.

These functions are properties of an object created by the Geometric factory method. The factory method needs as optional argument an instance of one of the normal PRNG's.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Geometric , rng : { SuperDuper , normal : { BoxMuller } } } = libR ; const defaultG = Geometric ( ) ; const sd = new SuperDuper ( 3456 ) ; const explicitG = Geometric ( new BoxMuller ( mt ) ) ; const { dgeom , pgeom , qgeom , rgeom } = explicitG ;

dgeom

The density function of the Geometric distribution. See R doc.

$$ \large p(x) = p (1-p)^{x} $$

typescript decl

declare function dgeom : number | number [ ] ;

x : quantiles (array or scalar).

: quantiles (array or scalar). prob : probability of success in each trial. 0 < prob <= 1.

: probability of success in each trial. 0 < prob <= 1. asLog : if TRUE, probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Geometric , R : { numberPrecision } } = libR ; const seq = libR . R . seq ( ) ( ) ; const precision = numberPrecision ( 9 ) ; const { dgeom , pgeom , qgeom , rgeom } = Geometric ( ) ; const x = seq ( 0 , 4 ) ; const d1 = dgeom ( x , 0 . 5 ) ; precision ( d1 ) ; const d2 = dgeom ( x , 0 . 2 , true ) ; precision ( d2 ) ;

Equivalent in R

x = seq(0,4); #1 > dgeom(x, 0.5) [1] 0.50000 0.25000 0.12500 0.06250 0.03125 #2 > dgeom(x, 0.2, TRUE) [1] -1.609438 -1.832581 -2.055725 -2.278869 -2.502012

pgeom

The distribution function of the Geometric distribution. See R doc.

typescript decl

declare function pgeom : number | number [ ] ;

q : the number of failures before success.

: the number of failures before success. prob : probability of success.

: probability of success. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : if TRUE, probabilities p are given as ln(p).

const libR = require ( ' lib-r-math.js ' ) ; const { Geometric , R : { numberPrecision } } = libR ; const seq = libR . R . seq ( ) ( ) ; const precision = numberPrecision ( 9 ) ; const { dgeom , pgeom , qgeom , rgeom } = Geometric ( ) ; const q = seq ( 5 , 9 ) ; const p1 = pgeom ( q , 0 . 1 ) ; precision ( p1 ) ; const p2 = pgeom ( q , 0 . 1 , false ) ; precision ( p2 ) ; const p3 = pgeom ( q , 0 . 2 , false , true ) ; precision ( p3 ) ;

Equivalent in R

q=seq(5, 9); #1 pgeom(q, 0.1); #[1] 0.4685590 0.5217031 0.5695328 0.6125795 0.6513216 #2 pgeom(q, 0.1, FALSE) #[1] 0.5314410 0.4782969 0.4304672 0.3874205 0.3486784 #3 pgeom(q, 0.2, FALSE, TRUE) #[1] -1.338861 -1.562005 -1.785148 -2.008292 -2.231436

qgeom

The quantile function of the Geometric distribution. See R doc.

typescript decl

declare function qgeom : number | number [ ] ;

p : probabilities (scalar or array).

: probabilities (scalar or array). prob : probability of success.

: probability of success. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : if TRUE, probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Geometric , R : { numberPrecision } } = libR ; const seq = libR . R . seq ( ) ( ) ; const precision = numberPrecision ( 9 ) ; const { dgeom , pgeom , qgeom , rgeom } = Geometric ( ) ; const q = seq ( 5 , 9 ) ; const pp1 = pgeom ( q , 0 . 2 , false , true ) ; const q1 = qgeom ( pp1 , 0 . 2 , false , true ) ; precision ( q1 ) ; const pp2 = pgeom ( q , 0 . 9 , true , true ) ; const q2 = qgeom ( pp2 , 0 . 9 , true , true ) ; precision ( q2 ) ; const pp3 = pgeom ( [ ... q , Infinity ] , 0 . 5 ) ; const q3 = qgeom ( pp3 , 0 . 5 ) ; precision ( q3 ) ;

Equivalent in R

q = seq(5, 9); #1 pp1 = pgeom(q, 0.2, FALSE, TRUE) qgeom(pp1, 0.2, FALSE, TRUE) #[1] 5 6 7 8 9 #2 pp2 = pgeom(q, 0.9, TRUE, TRUE); qgeom(pp2, 0.9, TRUE, TRUE); #[1] 5 6 7 8 9 #3 pp3 = pgeom(c(q, Inf), 0.5); qgeom(pp3, 0.5); #[1] 5 6 7 8 9, Inf

rgeom

Generates random deviates for the Geometric distribution. See R doc.

typescript decl

declare function rgeom : number | number [ ] ;

n : number of deviates to generate.

: number of deviates to generate. prob : probability of success.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Geometric , rng : { KnuthTAOCP , normal : { KindermanRamage } } , R : { arrayrify , numberPrecision , seq : seqCR } } = libR ; const log = arrayrify ( Math . log ) ; const seq = seqCR ( ) ( ) ; const precision = numberPrecision ( 9 ) ; const k97 = new KnuthTAOCP ( 1234 ) ; const { dgeom , pgeom , qgeom , rgeom } = Geometric ( new KindermanRamage ( mt ) ) ; k97 . init ( 3456 ) ; rgeom ( 5 , 0 . 001 ) ; k97 . init ( 9876 ) ; rgeom ( 5 , 0 . 999 ) ; k97 . init ( 934 ) ; rgeom ( 10 , 0 . 4 ) ;

in R Console

RNGkind("Mersenne-Twister", normal.kind = "Inversion"); #1. > set.seed(3456) > rgeom(5, 0.001) [1] 573 1153 75 82 392 #2 > set.seed(9876) > rgeom(5, 0.999); [1] 0 0 0 0 0 #3 > set.seed(934) > rgeom(10, 0.4); [1] 1 2 6 1 0 1 0 0 1 2

Hypergeometric distribution

dhyper, qhyper, phyper, rhyper

See R doc and wiki.

These functions are properties of an object created by the HyperGeometric factory method. The factory method needs as optional argument an instance of one of the uniform random PRNG's classes.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { HyperGeometric , rng : { MersenneTwister , SuperDuper } } = libR ; const log = libR . R . arrayrify ( Math . log ) ; const seq = libR . R . seq ( ) ( ) ; const precision = libR . R . numberPrecision ( 9 ) ; const sd = new SuperDuper ( 1234 ) ; const hyperG = HyperGeometric ( sd ) ; const default = HyperGeometric ( ) ; const { dhyper , phyper , qhyper , rhyper } = default ;

dhyper

The density function of the Hypergeometric distribution. See R doc and wiki.

$$ \large p(X = x) = \frac{choose(m, x) choose(n, k-x)}{choose(m+n, k)} $$

typescript decl

declare function dhyper : number | number [ ] ;

Where:

x : is the number of observed successes.

: is the number of observed successes. m : is the number of success states in the population

: is the number of success states in the population n : is the number of failure states in the population

: is the number of failure states in the population k : is the number of draws from the population (n+m) sample.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { HyperGeometric } = libR ; const log = libR . R . arrayrify ( Math . log ) ; const seq = libR . R . seq ( ) ( ) ; const precision = libR . R . numberPrecision ( 9 ) ; const { dhyper , phyper , qhyper , rhyper } = HyperGeometric ( ) ; const d1 = dhyper ( seq ( 0 , 4 ) , 5 , 3 , 5 , false ) ; precision ( d1 ) ; const d2 = dhyper ( seq ( 0 , 4 ) , 3 , 4 , 7 ) ; precision ( d2 ) ; const d3 = dhyper ( seq ( 0 , 3 ) , 3 , 4 , 5 ) ; precision ( d3 ) ; const d4 = dhyper ( seq ( 0 , 3 ) , 3 , 9 , 5 ) ; precision ( d4 ) ;

in R Console

#1 > dhyper( seq(0, 4), 5, 3, 5, FALSE ); [1] 0.0000000 0.0000000 0.1785714 0.5357143 0.2678571 #2 > dhyper( seq(0, 4), 3, 4, 7 ); [1] 0 0 0 1 0 #3 > dhyper( seq(0, 3), 3, 4, 5); [1] 0.0000000 0.1428571 0.5714286 0.2857143 #4 > dhyper( seq(0, 3), # success count, number of white balls drawn, must be ≤ 3 3, #population white balls 9, #population red balls 5 #total balls drawn, must be < (4+3) ); [1] 0.15909091 0.47727273 0.31818182 0.04545455

phyper

The distribution function of the Hypergeometric distribution. See R doc and wiki.

typescript decl

declare function phyper : number | number [ ] ;

q : is the number of observed successes.

: is the number of observed successes. m : is the number of success states in the population

: is the number of success states in the population n : is the number of failure states in the population

: is the number of failure states in the population k : is the number of draws from the population (n+m) sample.

: is the number of draws from the population (n+m) sample. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : if TRUE, probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { HyperGeometric } = libR ; const log = libR . R . arrayrify ( Math . log ) ; const seq = libR . R . seq ( ) ( ) ; const precision = libR . R . numberPrecision ( 9 ) ; const { dhyper , phyper , qhyper , rhyper } = HyperGeometric ( ) ; const p1 = phyper ( seq ( 2 , 5 ) , 5 , 3 , 5 ) ; precision ( p1 ) ; const p2 = phyper ( seq ( 2 , 6 ) , 9 , 18 , 9 , false ) ; precision ( p2 ) ; const p3 = phyper ( seq ( 2 , 6 ) , 9 , 18 , 6 , false , true ) ; precision ( p3 ) ;

Equivalent in R Console

#1 > phyper( seq(2, 5), 5, 3, 5 ); [1] 0.1785714 0.7142857 0.9821429 1.0000000 #2 > phyper( seq(2, 6), 9, 18, 9, FALSE); [1] 0.661155260 0.328440469 0.098099460 0.015834814 0.001209988 #3 > phyper( seq(2, 6), 9, 18, 6, FALSE, TRUE); [1] -1.188652 -2.616312 -4.835127 -8.167332 -Inf

qhyper

The quantile function of the Hypergeometric distribution. See R doc and wiki.

typescript decl

declare function qhyper : number | number [ ] ;

p : is probability of observed successes.

: is probability of observed successes. m : is the number of success states in the population

: is the number of success states in the population n : is the number of failure states in the population

: is the number of failure states in the population k : is the number of draws from the population (n+m) sample.

: is the number of draws from the population (n+m) sample. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : if TRUE, probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { HyperGeometric } = libR ; const log = libR . R . arrayrify ( Math . log ) ; const seq = libR . R . seq ( ) ( ) ; const precision = libR . R . numberPrecision ( 9 ) ; const { dhyper , phyper , qhyper , rhyper } = HyperGeometric ( ) ; const q1 = qhyper ( seq ( 0 , 1 , 0 . 2 ) , 5 , 2 , 3 ) ; precision ( q1 ) ; const q2 = qhyper ( log ( seq ( 0 , 1 , 0 . 2 ) ) , 5 , 2 , 3 , false , true ) ; precision ( q2 ) ; const q3 = qhyper ( seq ( 0 , 1 , 0 . 2 ) , 50 , 20 , 6 ) ; precision ( q3 ) ;

Equivalent in R Console

#1. >qhyper( seq(0, 1, 0.2), 5,2,3 ); [1] 1 2 2 2 3 3 #2. There is a bug in R: 'NaN' should be '3' >qhyper( log(seq(0, 1, 0.2)), 5, 2, 3, FALSE, TRUE); [1] NaN 3 2 2 2 1 #3 >qhyper( seq(0, 1, 0.2),50,20,6 ); [1] 0 3 4 5 5 6

rhyper

Generates random deviates for the Hypergeometric distribution. See R doc and wiki.

typescript decl

declare function rhyper : number | number [ ] ;

N : number of deviates to generate.

: number of deviates to generate. m : is the number of success states in the population

: is the number of success states in the population n : is the number of failure states in the population

: is the number of failure states in the population k : is the number of draws from the total population (n+m) sample.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { HyperGeometric , rng : { MersenneTwister } } = libR ; const log = libR . R . arrayrify ( Math . log ) ; const seq = libR . R . seq ( ) ( ) ; const precision = libR . R . numberPrecision ( 9 ) ; const mt = new MersenneTwister ( 1234 ) ; const hyperG = HyperGeometric ( mt ) ; const { dhyper , phyper , qhyper , rhyper } = hyperG ; mt . init ( 1234 ) ; rhyper ( 5 , 4 , 3 , 5 ) ; mt . init ( 9876 ) ; rhyper ( 5 , 40 , 19 , 13 ) ; mt . init ( 5688 ) ; rhyper ( 5 , 40 , 99 , 33 ) ;

Equivalent in R Console

RNGkind("Mersenne-Twister", normal.kind="Inversion") #1 >set.seed(1234); >rhyper(5, 4, 3, 5); [1] 2 3 3 3 4 #2 >set.seed(9876); >rhyper(5, 40, 19, 13); [1] 7 9 11 9 9 #3 > set.seed(5688); > rhyper(5, 40, 99, 33); [1] 12 10 10 7 12

Logistic distribution

dlogis, qlogis, plogis, rlogis

See R doc and wiki.

These functions are properties of an object created by the Logistic factory method. The factory method needs as optional argument an instance of one of the uniform random PRNG's classes.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Logistic , rng : { MersenneTwister , SuperDuper } } = libR ; const log = libR . R . arrayrify ( Math . log ) ; const seq = libR . R . seq ( ) ( ) ; const precision = libR . R . numberPrecision ( 9 ) ; const sd = new SuperDuper ( 1234 ) ; const customL = Logistic ( sd ) ; const defaultL = Logistic ( ) ; const { dlogis , plogis , qlogis , rlogis } = defaultL ;

dlogis

The density function of the Logistic distribution with location = m and scale = s has density function.

$$ f(x) = \large \frac{e^{-\frac{x-m}{s}}}{s \left( 1 + e^{-\frac{x-m}{s}} \right)^{2}} $$

See R doc.

typescript decl

declare function dlogis : number | number [ ] ;

x : quantiles (scalar or array).

: quantiles (scalar or array). location : location parameter of the Logistic distribution

: location parameter of the Logistic distribution scale : the scale parameter of the Logistic distribution. Strictly positive.

: the scale parameter of the Logistic distribution. Strictly positive. asLog : if TRUE, probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Logistic } = libR ; const log = libR . R . arrayrify ( Math . log ) ; const seq = libR . R . seq ( ) ( ) ; const precision = libR . R . numberPrecision ( 9 ) ; const { dlogis , plogis , qlogis , rlogis } = Logistic ( ) ; const x = [ - Infinity , ... seq ( - 10 , 10 , 2 . 5 ) , Infinity ] ; const d1 = dlogis ( x , 5 , 2 ) ; precision ( d1 ) ; const d2 = dlogis ( x , 0 , 2 , true ) ; precision ( d2 ) ; const d3 = dlogis ( x , - 9 , 2 ) ; precision ( d3 ) ;

Equivalent in R Console

> x = c(-Inf, seq(-10,10,2.5), Inf); #1 > dlogis(x, 5, 2); [1] 0.0000000000 0.0002762365 0.0009615112 0.0033240283 0.0112247052 [6] 0.0350518583 0.0865523935 0.1250000000 0.0865523935 0.0350518583 [11] 0.0000000000 #2 > dlogis(x, 0, 2, TRUE); [1] -Inf -5.706578 -4.489638 -3.350927 -2.447005 -2.079442 -2.447005 [8] -3.350927 -4.489638 -5.706578 -Inf #3 > dlogis(x, -5, 2); [1] 0.0000000000 0.0350518583 0.0865523935 0.1250000000 0.0865523935 [6] 0.0350518583 0.0112247052 0.0033240283 0.0009615112 0.0002762365 [11] 0.0000000000

plogis

The distribution function of the Logistic distribution. See R doc.

typescript decl

declare function plogis : number | number [ ] ;

q : quantiles (scalar or array).

: quantiles (scalar or array). location : location parameter of the Logistic distribution

: location parameter of the Logistic distribution scale : the scale parameter of the Logistic distribution. Strictly positive.

: the scale parameter of the Logistic distribution. Strictly positive. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : if TRUE, probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Logistic } = libR ; const log = libR . R . arrayrify ( Math . log ) ; const seq = libR . R . seq ( ) ( ) ; const precision = libR . R . numberPrecision ( 9 ) ; const { dlogis , plogis , qlogis , rlogis } = Logistic ( ) ; const x = [ - Infinity , ... seq ( - 10 , 10 , 2 . 5 ) , Infinity ] ; const p1 = plogis ( x , 5 , 2 ) ; precision ( p1 ) ; const p2 = plogis ( x , 0 , 2 , true , true ) ; precision ( p2 ) ; const p3 = plogis ( x , - 9 , 2 , false ) ; precision ( p3 ) ;

Equivalent in R Console

> x = c(-Inf, seq(-10,10,2.5), Inf); #1 > plogis(x, 5, 2); [1] 0.0000000000 0.0005527786 0.0019267347 0.0066928509 0.0229773699 [6] 0.0758581800 0.2227001388 0.5000000000 0.7772998612 0.9241418200 [11] 1.0000000000 #2 > plogis(x, 0, 2, TRUE, TRUE); [1] -Inf -5.006715348 -3.773245464 -2.578889734 -1.501929081 [6] -0.693147181 -0.251929081 -0.078889734 -0.023245464 -0.006715348 [11] 0.000000000 #3 plogis(x, -9, 2, FALSE); [1] 1.000000e+00 6.224593e-01 3.208213e-01 1.192029e-01 3.732689e-02 [6] 1.098694e-02 3.172683e-03 9.110512e-04 2.611903e-04 7.484623e-05 [11] 0.000000e+00

qlogis

The quantile function of the Logistic distribution. See R doc.

typescript decl

declare function qlogis : number | number [ ] ;

p : probabilities (scalar or array). 0 ≤ p ≤ 1.

: probabilities (scalar or array). 0 ≤ p ≤ 1. location : location parameter of the Logistic distribution

: location parameter of the Logistic distribution scale : the scale parameter of the Logistic distribution. Strictly positive.

: the scale parameter of the Logistic distribution. Strictly positive. lowerTail : if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

: if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x]. logP : if TRUE, probabilities p are given as ln(p).

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Logistic } = libR ; const log = libR . R . arrayrify ( Math . log ) ; const seq = libR . R . seq ( ) ( ) ; const precision = libR . R . numberPrecision ( 9 ) ; const { dlogis , plogis , qlogis , rlogis } = Logistic ( ) ; const x = [ - Infinity , ... seq ( - 10 , 10 , 2 . 5 ) , Infinity ] ; const pp1 = plogis ( x , 5 , 2 ) ; const q1 = qlogis ( pp1 , 5 , 2 ) ; precision ( q1 ) ; const pp2 = plogis ( x , 0 , 2 ) ; const q2 = qlogis ( log ( pp2 ) , 0 , 2 , true , true ) ; precision ( q2 ) ; const pp3 = plogis ( x , - 9 , 2 , false ) ; const q3 = qlogis ( pp3 , - 9 , 2 , false ) ; precision ( q3 ) ;

Equivalent in R

> x = c(-Inf, seq(-10,10,2.5), Inf); #1 > pp1 = plogis(x, 5, 2); > qlogis(pp1, 5, 2); [1] -Inf -10.0 -7.5 -5.0 -2.5 0.0 2.5 5.0 7.5 10.0 Inf #2 > pp2 = plogis(x, 0, 2); > qlogis(log(pp2), 0, 2, TRUE, TRUE); [1] -Inf -10.0 -7.5 -5.0 -2.5 0.0 2.5 5.0 7.5 10.0 Inf #3 > pp3 = plogis(x, -9, 2, FALSE); > qlogis(pp3, -9, 2, FALSE); [1] -Inf -10.0 -7.5 -5.0 -2.5 0.0 2.5 5.0 7.5 10.0 Inf

rlogis

Generates random deviates for the Logistic distribution. See R doc.

typescript decl

declare function rlogis : number | number [ ] ;

N : number of random deviates to generate.

: number of random deviates to generate. location : location parameter of the Logistic distribution

: location parameter of the Logistic distribution scale : the scale parameter of the Logistic distribution. Strictly positive.

Usage:

const libR = require ( ' lib-r-math.js ' ) ; const { Logistic , rng : { MersenneTwister } } = libR ; const log = libR . R . arrayrify ( Math . log ) ; const seq = libR . R . s