Macaw.Draw.Random (macaw_draw v0.1.0)

Copy Markdown View Source

Random variates over an explicit state (spec/07 §22), copied from visualize's Visualize.Random, which read and advanced the calling process's :rand state. Macaw.Draw holds no state, so every draw takes a state and answers the value with the next state, {value, state}.

iex> alias Macaw.Draw.Random
iex> {a, state} = Random.uniform(Random.new(42))
iex> {b, _state} = Random.uniform(state)
iex> {a == elem(Random.uniform(Random.new(42)), 0), a != b}
{true, true}

new(seed) is :rand.seed_s(:exsss, {s, 2s, 3s}), the state visualize's seed/1 installed in the process, so a seed draws the same values as visualize's did for every distribution that consumes uniforms in the same order. A parameter of the wrong type is {[name], {:type, :number}} and one out of range {[name], {:out_of_range, value}}, at the parameter's name; a state that is no :rand state is {[:state], {:type, :state}}.

Summary

Types

A draw: the value and the next state, or the faults of its parameters.

A :rand state, as :rand.seed_s/2 makes it.

Functions

1 with probability p, else 0; p = 0.5.

x / (x + y), x = gamma(alpha) and y = gamma(beta).

The successes in n trials of probability p; 0 for n = 0.

location + scale · tan(π (u − 0.5)); location = 0, scale = 1.

−ln(1 − u) / lambda; lambda = 1.

Marsaglia–Tsang; a shape below 1 boosted through shape + 1; scale = 1.

The failures before the first success, trunc(ln(1 − u) / ln(1 − p)), p in (0, 1].

A point uniform over the disc, r = radius · √u; cx = cy = 0, radius = 1.

A point uniform in the rectangle from (x0, y0) to (x1, y1).

exp(normal(mu, sigma)); mu = 0, sigma = 1.

The state of an integer seed: :rand.seed_s(:exsss, {s, 2s, 3s}).

A Box–Muller normal variate; mean = 0, stddev = 1.

A point uniform on the circumference; cx = cy = 0, radius = 1.

1 / (1 − u)^(1 / alpha), at least 1; alpha = 1.

One element of a list, or nil for [].

Knuth's multiplication method.

n distinct elements of a list (fewer when it is shorter).

n calls of fun, a 1-arity function of the state answering {value, state}, each given the state the last answered: {values, state}, [] for n ≤ 0.

A Fisher–Yates permutation of a list.

A float in [min, max); min = 0, max = 1.

An integer in [min, max].

scale · (−ln(1 − u))^(1 / shape); scale = 1.

Types

draw(value)

@type draw(value) :: {value, state()} | {:error, [Macaw.Base.Fault.t()]}

A draw: the value and the next state, or the faults of its parameters.

state()

@type state() :: :rand.state()

A :rand state, as :rand.seed_s/2 makes it.

Functions

bernoulli(p \\ 0.5, state)

@spec bernoulli(number(), state()) :: draw(0 | 1)

1 with probability p, else 0; p = 0.5.

beta(alpha, beta, state)

@spec beta(number(), number(), state()) :: draw(float())

x / (x + y), x = gamma(alpha) and y = gamma(beta).

binomial(n, p, state)

@spec binomial(non_neg_integer(), number(), state()) :: draw(non_neg_integer())

The successes in n trials of probability p; 0 for n = 0.

cauchy(location \\ 0, scale \\ 1, state)

@spec cauchy(number(), number(), state()) :: draw(float())

location + scale · tan(π (u − 0.5)); location = 0, scale = 1.

exponential(lambda \\ 1, state)

@spec exponential(number(), state()) :: draw(float())

−ln(1 − u) / lambda; lambda = 1.

gamma(shape, scale \\ 1, state)

@spec gamma(number(), number(), state()) :: draw(float())

Marsaglia–Tsang; a shape below 1 boosted through shape + 1; scale = 1.

geometric(p, state)

@spec geometric(number(), state()) :: draw(non_neg_integer())

The failures before the first success, trunc(ln(1 − u) / ln(1 − p)), p in (0, 1].

in_circle(cx \\ 0, cy \\ 0, radius \\ 1, state)

@spec in_circle(number(), number(), number(), state()) :: draw({float(), float()})

A point uniform over the disc, r = radius · √u; cx = cy = 0, radius = 1.

in_rect(x0, y0, x1, y1, state)

@spec in_rect(number(), number(), number(), number(), state()) ::
  draw({float(), float()})

A point uniform in the rectangle from (x0, y0) to (x1, y1).

log_normal(mu \\ 0, sigma \\ 1, state)

@spec log_normal(number(), number(), state()) :: draw(float())

exp(normal(mu, sigma)); mu = 0, sigma = 1.

new(seed)

@spec new(integer()) :: state() | {:error, [Macaw.Base.Fault.t()]}

The state of an integer seed: :rand.seed_s(:exsss, {s, 2s, 3s}).

normal(mean \\ 0, stddev \\ 1, state)

@spec normal(number(), number(), state()) :: draw(float())

A Box–Muller normal variate; mean = 0, stddev = 1.

on_circle(cx \\ 0, cy \\ 0, radius \\ 1, state)

@spec on_circle(number(), number(), number(), state()) :: draw({float(), float()})

A point uniform on the circumference; cx = cy = 0, radius = 1.

pareto(alpha \\ 1, state)

@spec pareto(number(), state()) :: draw(float())

1 / (1 − u)^(1 / alpha), at least 1; alpha = 1.

pick(list, state)

@spec pick([term()], state()) :: draw(term())

One element of a list, or nil for [].

poisson(lambda, state)

@spec poisson(number(), state()) :: draw(non_neg_integer())

Knuth's multiplication method.

sample(list, n, state)

@spec sample([term()], integer(), state()) :: draw([term()])

n distinct elements of a list (fewer when it is shorter).

samples(fun, n, state)

@spec samples((state() -> {term(), state()}), integer(), state()) :: draw([term()])

n calls of fun, a 1-arity function of the state answering {value, state}, each given the state the last answered: {values, state}, [] for n ≤ 0.

shuffle(list, state)

@spec shuffle([term()], state()) :: draw([term()])

A Fisher–Yates permutation of a list.

uniform(min \\ 0, max \\ 1, state)

@spec uniform(number(), number(), state()) :: draw(float())

A float in [min, max); min = 0, max = 1.

uniform_int(min, max, state)

@spec uniform_int(integer(), integer(), state()) :: draw(integer())

An integer in [min, max].

weibull(shape, scale \\ 1, state)

@spec weibull(number(), number(), state()) :: draw(float())

scale · (−ln(1 − u))^(1 / shape); scale = 1.