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, state, 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 asx/q/p(or the sample, fornv_runif), in which case the interval varies elementwise and each element ofx/q/pis evaluated against, or each draw made from, its ownmin/max.- log, log_p
(
logical(1))
IfTRUE, the densities/probabilities are given as logarithms. Fornv_qunifthis describes the inputp.- lower_tail
(
logical(1))
IfTRUE(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]\) giveNaN.- shape
(
integer())
Shape of the result.- state
(
arrayish)
RNG state: a 1-D array of twoui64elements, asnv_rng_state()returns. The data type and length are fixed by the generator, not by the default data types, and the returnedstatehas them too.- dtype
(
NULL|character(1)|DataType)
Floating point data type of the sample. The default (NULL) takes it fromminandmax, and uses the default float type where both are R values.
Value
(arrayish | named list of two arrayish)nv_dunif(), nv_punif(), and nv_qunif() return an arrayish with the
same shape and data type as x/q/p.
nv_runif() returns a named list of two arrayish: state, the updated
RNG state with the input state's data type and shape, and values, the
sample 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
the data type of x/q/p, which must be float; an R value settles on the
default float. An R value for min or max takes this data
type. An array is promoted to it, so a wider one (e.g. f64 for an f32
x) is an error.
Random generation
nv_runif samples from the open interval \((a, b)\).
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 RNG
state is advanced regardless.
See also
Other rng:
nv_normal,
nv_rbinom(),
nv_rng_state(),
nv_sample(),
nv_sample_int()
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} ]
# `state` is the updated RNG state, `values` the sample
state <- nv_rng_state(42L)
result <- nv_runif(c(2, 3), state)
result$values
#> AnvlArray
#> 0.8690 0.1506 0.5203
#> 0.3103 0.9928 0.1065
#> [ 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), state, min = lower, max = lower + 1)$values
#> AnvlArray
#> 0.8690 20.1506 40.5203
#> 10.3103 30.9928 50.1065
#> [ CPUf32{2,3} ]