Skip to content
Jeffrey Heer edited this page Dec 5, 2015 · 7 revisions

WikiAPI ReferenceGenerators

Data Generators and Distributions

# dl.zeros(length)

Generate an array of zeros, with the given integer length.

dl.zeros(5); // returns [0, 0, 0, 0, 0]

# dl.repeat(value, length)

Generate an array consisting of a single value repeated length times.

dl.repeat(1, 5);   // returns [1, 1, 1, 1, 1]
dl.repeat('a', 5); // returns ['a', 'a', 'a', 'a', 'a']

# dl.range([start, ]stop[, step])

Generates an array containing an arithmetic progression, similar to the Python built-in range. This method is often used to iterate over a sequence of numeric or integer values, such as the indexes into an array. Unlike the Python version, the arguments are not required to be integers, though the results are more predictable if they are due to floating point precision. If step is omitted, it defaults to 1. If start is omitted, it defaults to 0. The stop value is not included in the result. The full form returns an array of numbers [start, start + step, start + 2 * step, …]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. If the returned array would contain an infinite number of values, an error is thrown rather than causing an infinite loop.

# dl.random.bootstrap(domain[, smooth])

Creates a bootstrap sampler, which uniformly pulls samples with replacement from the input domain (an array of values). The optional smooth parameter specifies the standard deviation of normally-distributed random noise to add to each bootstrapped sample (default 0). The invalid values null, undefined and NaN will be removed from the domain prior to sampling.

The return value is a zero-argument function that returns random samples. In addition, the returned function has a samples functions available as a bound property:

  • bootstrap.samples(length) - accepts a single length argument and returns an array of samples.
var bs = dl.random.bootstrap([1,2,3]);
bs(); // samples with replacement from the set {1, 2, 3}
bs.samples(100); // returns an array of 100 bootstrapped samples

# dl.random.uniform([min, max])

Models a random variable with a uniform distribution. The min and max arguments determine the range of the distribution, specifically [min, max). If one argument is provided, it is treated as max, and min defaults to zero. If no arguments are provided, the range [0, 1) is used.

The return value is a zero-argument function that returns random numbers. In addition, the returned function has a number of functions available as bound properties:

var r = dl.random.uniform(1, 10);
r(); // returns a uniform sample between 1 (inclusive) and 10 (exclusive)
r.samples(100); // returns an array of 100 uniformly-distributed random numbers
r.icdf(0.5); // returns 5.5, the median of the distribution
r.cdf(5.5); // returns 0.5, indicating 5.5 is the median of the distribution

# dl.random.integer([min, ]max)

Generate samples from a discrete uniform distribution over integers. The min and max arguments determine the range over which to sample, specifically [min, max). If one argument is provided, it is treated as max, and min defaults to zero. At least one argument must be provided.

The return value is a zero-argument function that returns random integers. In addition, the returned function has a number of functions available as bound properties:

var r = dl.random.integer(1, 5);
r(); // returns a random integer between 1 (inclusive) and 5 (exclusive)
r.samples(100); // returns an array of 100 uniformly-distributed random integers
r.icdf(0.5); // returns 2, the median of the distribution
r.cdf(2); // returns 0.5, indicating 2 is the median of the distribution

# dl.random.normal([mean, stdev])

Generate samples from a normal (Gaussian) distribution. The mean and stdev arguments specify the mean and standard deviation of the distribution. If one argument is provided, it is treated as mean, and stdev defaults to one. If no arguments are provided, a mean of zero and a standard deviation of one are used.

The return value is a zero-argument function that returns random numbers. In addition, the returned function has a number of functions available as bound properties:

var r = dl.random.normal(0, 5);
r(); // returns a sample from normal distribution with mean 0 and stdev 5
r.samples(100); // returns an array of 100 normally-distributed random numbers
r.icdf(0.5); // returns 0, the median of the distribution
r.cdf(0); // returns 0.5, indicating 0 is the median of the distribution
Clone this wiki locally