params •distribution name returns the distribution called name, with parameters params. •distribution name uses the distribution’s standard parameters, listed in the table below. A distribution is a keyed vector of four functions:
sample shape draws random values from the session’s generator, as ¿ does. g sample shape draws them from g, a generator made by •rand.
density x gives the probability density, or the probability mass for a discrete distribution.
cdf x gives P(X ≤ x).
quantile p inverts the CDF. cdf⁻ is quantile, and quantile⁻ is cdf.
Parameters are finite real units or vectors. Scale, shape, rate and degrees of freedom are positive, except where the table says otherwise. Distributions shows distributions in use.
Name
Parameters
Standard
"normal"
μ σ: mean, standard deviation
0 1
"uniform"
a b: lower and upper bounds, a < b
0 1
"bernoulli"
p ∈ [0,1]
"binomial"
n p: integer trials n ≥ 0, p ∈ [0,1]
"poisson"
λ ≥ 0: mean
"beta"
α β: shapes
"gamma"
k θ: shape, scale, with mean kθ
"inversegamma"
α β: shape, scale, with density ∝ x⁻⁽ᵅ⁺¹⁾ exp(−β/x)
"exponential"
λ: rate, with mean 1/λ
1
"chisquared"
ν: degrees of freedom
"student"
ν: degrees of freedom, with location 0 and scale 1
"fisher"
ν₁ ν₂: degrees of freedom
"cauchy", "laplace", "logistic"
location, scale
0 1
"lognormal"
μ σ: mean and standard deviation of log(X)
0 1
"weibull"
k λ: shape, scale
Errors: DOMAIN for an unknown name, a name that isn’t a string, a monadic call for a distribution with no standard parameters, invalid parameters (binomial trials or a Poisson rate past 2⁵³ among them) or non-real inputs; LENGTH for the wrong number of parameters; RANK for matrix parameters or shapes; SYNTAX for a dyadic call to density, cdf or quantile; LIMIT for oversized shapes. A quantile of a probability outside [0,1] gives NaN.
Construct a distribution with params •distribution name, then sample or evaluate it. Leave out params to use the distribution’s standard parameters. For the normal distribution they’re mean 0 and standard deviation 1:
sample 100 returns a 100-element vector. sample ⍬ returns a rank-0 array. For a shape that contains 0, sample returns an empty array. Samples from a continuous distribution are floats. Samples from a discrete distribution are exact integers:
density, cdf and quantile pervade arrays, preserving shape, nesting, keys and axis names. Pass parameters when constructing the distribution, not when calling its functions.
u←•distribution"uniform"u.cdf"low""high":0.20.8
["low":0.2 "high":0.8]
Seeded draws
•rand seed makes a generator. Put it on the left of sample to use its seeded stream. A generator also has roll and deal, which work like ¿. Copies share the generator’s state. Draws can differ with batching, thread count or scheduling, execution route, platform or build. See •rand.
Without a generator, sample and ¿ use the session’s generator, which starts from a seed that the operating system supplies. •prefs ["seed":42] restarts it with seed 42.
]help•rand
•rand — Generator
•rand seed returns a generator: a keyed vector of two functions that draw random numbers. The seed is a nonnegative whole number. The same seed gives the same draws.
roll Y works like ¿Y.
X deal Y works like X¿Y.
A distribution’s sample takes a generator on its left, as in g d.sample 3. Each draw call uses a new sequence of numbers. A draw’s numbers depend on the seed and on how many draws came before it, not on how many numbers those draws produced. Copies of a generator share its count of draws.
A map gives the same numbers whether or not it runs as a batched call, because it draws for each cell separately, in cell order. A whole-array draw is one draw call and a map of draws is one for each cell, so ¿v and ¿¨v give different numbers.
Errors: DOMAIN for a seed that is not a nonnegative whole number, or a left argument to sample that is not a generator; LENGTH or RANK for more than one seed; SYNTAX for a dyadic call to •rand.
g←•rand42gn.sample3g.roll6663g.deal10
0.8327121583 ¯0.2689723931 ¯0.908052671
4 1 2
0 1 3
Parameters and support
"gamma" takes a scale parameter, whereas "exponential" takes a rate parameter. These describe the same distribution:
quantile 0 and quantile 1 return the lower and upper bounds of the support, which may be infinite. For a discrete distribution, quantile p returns the smallest integer in the support whose CDF is at least p. A result outside the 64-bit integer range is clamped to the largest or smallest 64-bit integer, as dynamic ⌊ does. The exception is quantile 0, which returns the lower bound of the support. When a distribution’s outcome is certain, as with a Poisson mean of 0, quantile 0, quantile 1 and every sample give that outcome:
•distribution uses the numerical routines from rustymath. An array of numbers is passed to rustymath as one slice, so density, cdf and quantile run on a large array without per-item overhead. "binomial" trials and "poisson" mean are at most 2⁵³, because their samplers use floating-point numbers. "gamma" scale must have a finite reciprocal. "uniform" bounds must have a finite difference b - a.