Skip to contents

Density (nv_dunif), distribution function (nv_punif), quantile function (nv_qunif), and random generation (nv_runif) for the Uniform distribution on the interval from min to max.

Usage

nv_dunif(x, min = 0, max = 1, log = FALSE)

nv_punif(q, min = 0, max = 1, lower_tail = TRUE, log_p = FALSE)

nv_qunif(p, min = 0, max = 1, lower_tail = TRUE, log_p = FALSE)

nv_runif(shape, min = 0, max = 1, dtype = NULL)

Arguments

x, q

(arrayish)
Quantiles at which to evaluate the density (x) or the distribution function (q).

min, max

(arrayish)
Lower and upper limits of the distribution. Either scalars, or arrays of exactly the same shape as x/q/p (or the sample, for nv_runif), in which case the interval varies elementwise and each element of x/q/p is evaluated against, or each draw made from, its own min/max.

log, log_p

(logical(1))
If TRUE, the densities/probabilities are given as logarithms. For nv_qunif this describes the input p.

lower_tail

(logical(1))
If TRUE (default), probabilities are \(P(X \le x)\); otherwise, \(P(X > x)\).

p

(arrayish)
Probabilities at which to evaluate the quantile function. Values outside \([0, 1]\) give NaN.

shape

(integer())
Shape of the result.

dtype

(NULL | character(1) | DataType)
Floating point data type of the sample. The default (NULL) takes it from min and max, and uses the default float type where both are R values.

Value

(arrayish)
nv_dunif(), nv_punif(), and nv_qunif() return an arrayish with the same shape as x/q/p and the float data type described under Data Types.

nv_runif() returns the sample, an arrayish of shape shape and the data type described under dtype.

Details

The Uniform distribution has probability density function: $$f(x) = \frac{1}{b - a}, \quad a \le x \le b$$ and zero elsewhere, where \(a\) is min and \(b\) is max.

All four are univariate functions evaluated elementwise, returning one value per element of x/q/p (or of the sample). Non-scalar min/max therefore give a separate univariate Uniform per element, not a multivariate Uniform over the hyper-rectangle \(\prod_i [a_i, b_i]\). For that, reduce over the result: nv_prod(nv_dunif(x, min, max)), or nv_sum(nv_dunif(x, min, max, log = TRUE)) on the log scale.

Data Types

nv_dunif(), nv_punif() and nv_qunif() compute at a float data type and return it. If x/q/p is a float, they compute at its data type; an R double settles on the default float. An integer x/q/p (an array or an R integer) is converted to the widest float among the arrays given for min or max, or to the default float if there is none. Integers beyond \(2^{24}\) are not all exact at f32, so a large integer may change in that conversion.

An R value for min or max takes the computing data type. An array is promoted to it, so a wider one (e.g. f64 for an f32 x) is an error.

Random State

The draws come from the active random state: the global random state (see nv_set_seed()), or the random state that with_nv_rng_state() or local_nv_rng_state() make the default for a scope. They advance it in place.

Random generation

nv_runif draws uniforms on the open interval \((0, 1)\) and scales them to \((a, b)\). Rounding to the data type can still give \(a\) or \(b\) where the interval is narrow next to its magnitude, e.g. for nv_runif(n, 1e6, 1e6 + 1) at f32.

min and max are arrayish, so they may vary across the sample: they are applied to the draws after they have been reshaped to shape, and so may either be scalars or have exactly that shape. As in base R's runif(), an element whose min equals its max is that value, and one whose min or max is not finite, or whose max is less than its min, is NaN. The random state is advanced regardless.

Examples

x <- nv_array(c(-0.5, 0, 0.25, 1, 1.5))
nv_dunif(x)
#> AnvlArray
#>  0
#>  1
#>  1
#>  1
#>  0
#> [ CPUf32{5} ] 
nv_dunif(x, min = -1, max = 2)
#> AnvlArray
#>  0.3333
#>  0.3333
#>  0.3333
#>  0.3333
#>  0.3333
#> [ CPUf32{5} ] 
nv_dunif(x, log = TRUE)
#> AnvlArray
#>  -inf
#>     0
#>     0
#>     0
#>  -inf
#> [ CPUf32{5} ] 

# `min`/`max` may vary elementwise, giving one univariate Uniform per
# element rather than a single distribution over a hyper-rectangle
lower <- nv_array(c(-1, -1, 0, 0, 1))
upper <- nv_array(c(0, 1, 1, 2, 2))
nv_dunif(x, min = lower, max = upper)
#> AnvlArray
#>  1.0000
#>  0.5000
#>  1.0000
#>  0.5000
#>  1.0000
#> [ CPUf32{5} ] 

nv_punif(x)
#> AnvlArray
#>  0.0000
#>  0.0000
#>  0.2500
#>  1.0000
#>  1.0000
#> [ CPUf32{5} ] 
nv_punif(x, min = -1, max = 2)
#> AnvlArray
#>  0.1667
#>  0.3333
#>  0.4167
#>  0.6667
#>  0.8333
#> [ CPUf32{5} ] 
nv_punif(x, lower_tail = FALSE)
#> AnvlArray
#>  1.0000
#>  1.0000
#>  0.7500
#>  0.0000
#>  0.0000
#> [ CPUf32{5} ] 
nv_punif(x, log_p = TRUE)
#> AnvlArray
#>     -inf
#>     -inf
#>  -1.3863
#>  -0.0000
#>  -0.0000
#> [ CPUf32{5} ] 

p <- nv_array(c(0.025, 0.5, 0.975))
nv_qunif(p)
#> AnvlArray
#>  0.0250
#>  0.5000
#>  0.9750
#> [ CPUf32{3} ] 
nv_qunif(p, min = -1, max = 2)
#> AnvlArray
#>  -0.9250
#>   0.5000
#>   1.9250
#> [ CPUf32{3} ] 
nv_qunif(p, lower_tail = FALSE)
#> AnvlArray
#>  0.9750
#>  0.5000
#>  0.0250
#> [ CPUf32{3} ] 
nv_qunif(nv_array(c(-700, -2, -0.1), dtype = "f64"), log_p = TRUE)
#> AnvlArray
#>  9.8597e-305
#>   1.3534e-01
#>   9.0484e-01
#> [ CPUf64{3} ] 

with_nv_seed(42L, nv_runif(c(2, 3)))
#> AnvlArray
#>  0.5303 0.3134 0.9015
#>  0.6983 0.6181 0.6219
#> [ CPUf32{2,3} ] 

# `min`/`max` may also be arrays of the same shape as the sample
lower <- nv_array(matrix(c(0, 10, 20, 30, 40, 50), nrow = 2))
nv_runif(c(2, 3), min = lower, max = lower + 1)
#> AnvlArray
#>   0.6672 20.7215 40.1268
#>  10.3543 30.4211 50.0421
#> [ CPUf32{2,3} ]