Probability distributions

Sample and evaluate probability distributions
]help •distribution

•distribution — Distributions

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 stream, as ¿ does. g sample shape draws them from 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. quantile⁻¹ is cdf.
d←•distribution "normal"
d.cdf 0                         ⍝ 0.5
b←10 0.5 •distribution "binomial"
b.quantile 0.5                  ⍝ 5ₓ

Parameters are finite real units or vectors. Scale, shape, rate and degrees of freedom are positive, except where stated. Distributions shows them 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, non-real inputs or p ∉ [0,1]; 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 or sampler ranges.

Construct a distribution with params •distribution name, then sample or evaluate it. Leave out the parameters to get the standard ones. For the normal distribution they’re mean 0 and standard deviation 1:

n←•distribution "normal"
⍴n.sample 2 3
n.cdf 0
n.quantile 0.5
n.density 0
[2 3]ₓ
0.5
0
0.3989422804

sample 100 returns a vector. sample ⍬ returns a rank-0 array. Zero dimensions give empty arrays. Continuous samples are floats. Discrete samples are exact integers:

b←2 0.5 •distribution "binomial"
b.density 0 1 2
b.cdf 0.5 1.5
b.quantile 0.25 0.5 1
0.25 0.5 0.25
0.25 0.75
[0 1 2]ₓ

Evaluation pervades arrays, preserving shape, nesting, keys and axis names. Parameters belong to the constructed distribution.

u←•distribution "uniform"
u.cdf "low" "high":0.2 0.8
["low":0.2 "high":0.8]

Reproducible draws

•rand seed makes a generator with its own stream. Put it on the left of sample for reproducible draws. The same seed gives the same draws. A generator also has roll and deal, which work like ¿. Copies of a generator share one stream.

Without a generator, sample and ¿ use the session’s stream, initially seeded by the operating system. •prefs ["seed":42] restarts that stream with seed 42.

]help •rand

•rand — Generator

•rand seed returns a generator: a keyed vector of two functions that draw from one stream of random numbers. The seed is a nonnegative integer. 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. Copies of a generator share its stream. Drawing from one copy moves every copy on.

In a map, the cells take the generator’s numbers in turn. A batched call takes every cell’s numbers for one draw before its next draw. A map whose cells each draw once gives the same numbers either way. A guard that splits the cells is the exception: each side of the guard draws for its own cells in turn. Here the two cells take the stream’s next four numbers:

g←•rand 3 ⋄ a←{g.roll ⍵⍴6}¨1 3 ⋄ h←•rand 3 ⋄ ((0⊃a),1⊃a)≡h.roll 4⍴6    ⍝ $t

¿ draws from the session’s own stream, which •prefs ["seed":n] seeds.

Errors: DOMAIN for a seed that is not a nonnegative integer, 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←•rand 42
g n.sample 3
g.roll 6 6 6
3 g.deal 10
0.8327121583 ¯0.2689723931 ¯0.908052671
1 0 1
1 7 6

Parameters and support

Gamma takes scale, whereas exponential takes rate. These describe the same distribution:

g←1 2 •distribution "gamma"
e←0.5 •distribution "exponential"
(g.cdf 2) = e.cdf 2
$t

Quantile endpoints give the support bounds, including infinity. Discrete quantiles return the smallest supported integer whose CDF reaches p. p=0 gives the lower support bound. Certain events stay constant at both endpoints:

n←•distribution "normal"
n.quantile 0 1
p←0 •distribution "poisson"
p.sample 3
¯∞ ∞
[0 0 0]ₓ

Numerics

Numerical routines come from rustymath. An array of numbers goes to it as one slice, so density, cdf and quantile on a large array run without per-item overhead. Binomial trials and Poisson rate are limited to 2⁵³ by the floating-point samplers. Gamma scale must have a finite reciprocal. Uniform intervals must have a finite width.