Computes the Gaussian error for a given value . It is useful to calculate the probability of a normally distributed, random variable with mean 0 and variance of falling into the range .
Formula:
Parameters:
(integer or float) x (required)
Usage:
var gaussianError = stats.gaussianError(0.45);
Returns:
0.475481…
Computes the inverse Gaussian error for a given value within the range of , such as (see above). This method uses Mike Giles’ approximation in its single precision implementation.
Formulas:
with
Parameters:
(integer or float) x (required)
Usage:
var inverseError = stats.inverseGaussianError(0.475481);
Returns:
0.4499992
Returns the probit for a given
quantile
, which is defined as the inverse of the cumulative normal distribution function. It describes the deviation from the mean for a given cumulative probability of a normal distributed variable, e.g. over how many standard deviations from the mean a given cumulative probability is spread.Formula:
Parameters:
(integer or float) quantile (required)
Usage:
var probit = stats.probit(0.975); // two-sided value for 95%
Returns:
1.95996…
// 95% of the values of a normally distributed variable lie within
// the interval [-1.95996, 1.95996] of the standard normal distribution
statistics.js offers implementations of several simple pseudo-random number generators that can be used for shuffling or sampling data. Some can be seeded in order to generate deterministic numbers in case a replicable behaviour is needed. None of these generators are truly random or cryptographically secure. It is strongly advised to not use them for any security related applications and instead refer to more suitable algorithms outside of the scope of this library.
Generates pseudo-random numbers from a normal distribution with
mean
and standardDeviation
. Optionally, it can be seeded with two random number generators randomSourceA()
and randomSourceB()
. Both should return a random number from a uniform distribution in the interval on each call. As a fallback, if these functions fail to produce such a number in less than 50 tries Math.random()
will be used instead.Parameters:
(integer or float) mean (optional, default: 0)
(integer or float) standardDeviation (optional, default: 1)
Usage:
var xorshift = new stats.xorshift([100934093, 482920221, 592807725, 993051833]),
seeds = { randomSourceA: xorshift.next, randomSourceB: xorshift.next },
boxMuller = stats.boxMuller(12, 4, seeds);
Returns:
10.62038…
Shuffles the values in a dataset
data
that can be either the name of a column (string) or an array of values according to Durstenfeld’s version. It returns a random permutation of the values as an array
based on a random number generator randomSource
that returns a float in the interval on each call.Parameters:
(string or array) data (required)
(function) randomSource (optional, default: Math.random)
Usage:
var fisherYatesShuffle = stats.fisherYatesShuffle([1, 3, 7, 4, 12, 4, 4, 7, 3, 6, 7, 1, 2]);
Returns:
[6, 2, 1, 3, 4, 7, 4, 3, 7, 4, 12, 7, 1]
Generates pseudo-random numbers from a uniform distribution based on an array
seed
of exactly four numeric seeds. None of these numbers should be zero to ensure a good randomisation. You may supply a startIndex
of n
to skip the first n
generated numbers and allow the generator to reach a better performance before any numbers are used. Numbers can be generated with the next(normalise = true)
method invoked on the new xorshift object, choosing between raw values and normalised values, i.e. mapped to the interval of . This method is a variation of /u/uupaa’s implementation.Parameters:
(array) seed (required)
(integer) startIndex (optional, default: 0)
Usage:
var xorshift = new stats.xorshift([100934093, 482920221, 592807725, 993051833]);
var random = xorshift.next(false);
var randomNormalised = xorshift.next(true);
Returns:
random: 471695451
randomNormalised: 0.27623…
Generates pseudo-random numbers from a normal distribution with
mean
and standardDeviation
. It is similar to the Box-Muller generator, however, it can not be initialised with a seeding source. Numbers can be generated with the next()
method invoked on the new ziggurat object. This implementation is a variation of Filip Zembowicz’ translation of the original C code described in the original paper by Georg Marsaglia and Wai Wan Tsang (see link above).Parameters:
(integer or float) mean (optional, default: 0)
(integer or float) standardDeviation (optional, default: 1)
Usage:
var ziggurat = new stats.ziggurat(5, 1);
var random = ziggurat.next();
Returns:
4.72130…