Learn R Programming

extraDistr (version 1.8.1)

TruncNormal: Truncated normal distribution

Description

Density, distribution function, quantile function and random generation for the truncated normal distribution.

Usage

dtnorm(x, mu = 0, sigma = 1, a = -Inf, b = Inf, log = FALSE)
ptnorm(q, mu = 0, sigma = 1, a = -Inf, b = Inf, lower.tail = TRUE, log.p = FALSE)
qtnorm(p, mu = 0, sigma = 1, a = -Inf, b = Inf, lower.tail = TRUE, log.p = FALSE)
rtnorm(n, mu = 0, sigma = 1, a = -Inf, b = Inf)

Arguments

x, q
vector of quantiles.
mu, sigma
location and scale parameters. Scale must be positive.
a, b
minimal and maximal boundries for truncation (-Inf and Inf by default).
log, log.p
logical; if TRUE, probabilities p are given as log(p).
lower.tail
logical; if TRUE (default), probabilities are $P[X \le x]$ otherwise, $P[X > x]$.
p
vector of probabilities.
n
number of observations. If length(n) > 1, the length is taken to be the number required.

Details

Probability density function $$ f(x) = \frac{\phi(\frac{x-\mu}{\sigma})} {\Phi(\frac{b-\mu}{\sigma}) - \Phi(\frac{a-\mu}{\sigma})} $$

Cumulative distribution function $$ F(x) = \frac{\Phi(\frac{x-\mu}{\sigma}) - \Phi(\frac{a-\mu}{\sigma})} {\Phi(\frac{b-\mu}{\sigma}) - \Phi(\frac{a-\mu}{\sigma})} $$

Quantile function $$ F^{-1}(p) = \Phi^{-1}\left(\Phi\left(\frac{a-\mu}{\sigma}\right) + p \times \left[\Phi\left(\frac{b-\mu}{\sigma}\right) - \Phi\left(\frac{a-\mu}{\sigma}\right)\right]\right) $$

For random generation algorithm described by Robert (1995) is used.

References

Robert, C.P. (1995). Simulation of truncated normal variables. Statistics and Computing 5(2): 121-125. http://arxiv.org/abs/0907.4010

Burkardt, J. (17 October 2014). The Truncated Normal Distribution. Florida State University. http://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf

Examples

Run this code

x <- rtnorm(1e5, 5, 3, b = 7)
xx <- seq(-10, 10, by = 0.001)
hist(x, 100, freq = FALSE)
lines(xx, dtnorm(xx, 5, 3, b = 7), col = "red")
hist(ptnorm(x, 5, 3, b = 7))
plot(ecdf(x))
lines(xx, ptnorm(xx, 5, 3, b = 7), col = "red", lwd = 2)

Run the code above in your browser using DataLab