Learn R Programming

oeli (version 0.7.8)

dtnorm_cpp: Truncated normal distribution

Description

The function dtnorm() computes the density of a truncated normal distribution.

The function rtnorm() samples from a truncated normal distribution.

The function dttnorm() and rttnorm() compute the density and sample from a two-sided truncated normal distribution, respectively.

The functions with suffix _cpp perform no input checks, hence are faster.

Usage

dtnorm_cpp(x, mean, sd, point, above, log = FALSE)

dttnorm_cpp(x, mean, sd, lower, upper, log = FALSE)

rtnorm_cpp(mean, sd, point, above, log = FALSE)

rttnorm_cpp(mean, sd, lower, upper, log = FALSE)

dtnorm(x, mean, sd, point, above, log = FALSE)

dttnorm(x, mean, sd, lower, upper, log = FALSE)

rtnorm(n = 1, mean, sd, point, above, log = FALSE)

rttnorm(n = 1, mean, sd, lower, upper, log = FALSE)

Value

For dtnorm() and dttnorm(): The density value.

For rtnorm() and rttnorm(): A numeric of length n with the random draws.

Arguments

x

[numeric(1)]
A quantile.

mean

[numeric(1)]
The mean.

sd

[numeric(1)]
The non-negative standard deviation.

point, lower, upper

[numeric(1)]
The truncation point.

above

[logical(1)]
Truncate from above? Else, from below.

log

[logical(1)]
For dtnorm() and dttnorm(), return the logarithm of the density value?

For rtnorm() and rttnorm(), return the exponential of the draw, which is a draw from the truncated log-normal distribution?

n

[integer(1)]
The number of requested samples.

Details

rtnorm() draws by the rejection methods of Robert (1995), and rttnorm() inverts the distribution function of the truncated tail, so that both remain accurate when a truncation point lies far in the tail.

References

Robert, C. P. (1995). Simulation of truncated normal variables. Statistics and Computing, 5(2), 121-125.

See Also

Other simulation helpers: Simulator, correlated_regressors(), ddirichlet_cpp(), dmixnorm_cpp(), dmvnorm_cpp(), dwishart_cpp(), gaussian_tv(), simulate_markov_chain()

Examples

Run this code
# compute density
dtnorm(x = 1, mean = 0, sd = 1, point = 0, above = FALSE)
dttnorm(x = 0, mean = 0, sd = 1, lower = -1, upper = 1, log = TRUE)

# sample
rtnorm(n = 3, mean = 0, sd = 1, point = 0, above = FALSE)
rttnorm(mean = 0, sd = 1, lower = -1, upper = 1)

Run the code above in your browser using DataLab