---
title: Probability distributions
description: Sample and evaluate probability distributions
---
```bpl
]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 generator, as [`¿`](glyphs/inverted-question.md) 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`.

```bpl
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 the table says otherwise. [Distributions](distributions.md) 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.

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

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:

```bpl
n←•distribution "normal"
ρn.sample 2 3
n.cdf 0
n.quantile 0.5
n.density 0
```

```output
[2 3]ₓ
0.5
0
0.3989422804
```

`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:

```bpl
b←2 0.5 •distribution "binomial"
b.density 0 1 2
b.cdf 0.5 1.5
b.quantile 0.25 0.5 1
```

```output
0.25 0.5 0.25
0.25 0.75
[0 1 2]ₓ
```

`density`, `cdf` and `quantile` pervade arrays, preserving shape, nesting, keys and axis names. Pass parameters when constructing the distribution, not when calling its functions.

```bpl
u←•distribution "uniform"
u.cdf "low" "high":0.2 0.8
```

```output
["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 [`¿`](https://bpl.fast.ai//glyphs/inverted-question.html). Copies share the generator's state. Draws can differ with batching, thread count or scheduling, execution route, platform or build. See [`•rand`](system/rand.md).

Without a generator, `sample` and [`¿`](https://bpl.fast.ai//glyphs/inverted-question.html) use the session's generator, which starts from a seed that the operating system supplies. `•prefs ["seed":42]` restarts it with seed 42.

```bpl
]help •rand
```

# `•rand` — Generator

`•rand seed` returns a generator: a keyed vector of two functions that draw random numbers. The nonnegative whole number `seed` initializes its state.

- `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 state.

A [batched map](terms.md#term-batched-call) can draw a whole array at once. Draws can differ with batching, thread count or scheduling, execution route, platform or build.

```bpl
g←•rand 3 ◇ h←•rand 3 ◇ (g.roll 4ρ6)≡h.roll 4ρ6    ⍝ $t
```

[`¿`](https://bpl.fast.ai//glyphs/inverted-question.html) draws from the session's generator, which [`•prefs ["seed":n]`](system/prefs.md) seeds.

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`.

```bpl
g←•rand 42
g n.sample 3
g.roll 6 6 6
3 g.deal 10
```

```output
0.8327121583 ¯0.2689723931 ¯0.908052671
1 0 1
1 7 6
```

## Parameters and support

`"gamma"` takes a scale parameter, whereas `"exponential"` takes a rate parameter. These describe the same distribution:

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

```output
$t
```

`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 [`⌊`](https://bpl.fast.ai//glyphs/downstile.html) 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:

```bpl
n←•distribution "normal"
n.quantile 0 1
p←0 •distribution "poisson"
p.sample 3
```

```output
¯∞ ∞
[0 0 0]ₓ
```

## Numerics

`•distribution` uses the numerical routines from [rustymath](https://github.com/AnswerDotAI/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`.
