Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

TruncNormal

Truncated normal distribution


Description

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

Usage

dtnorm(x, mean = 0, sd = 1, a = -Inf, b = Inf, log = FALSE)

ptnorm(
  q,
  mean = 0,
  sd = 1,
  a = -Inf,
  b = Inf,
  lower.tail = TRUE,
  log.p = FALSE
)

qtnorm(
  p,
  mean = 0,
  sd = 1,
  a = -Inf,
  b = Inf,
  lower.tail = TRUE,
  log.p = FALSE
)

rtnorm(n, mean = 0, sd = 1, a = -Inf, b = Inf)

Arguments

x, q

vector of quantiles.

mean, sd

location and scale parameters. Scale must be positive.

a, b

lower and upper truncation points (a < x <= b, with a = -Inf and b = 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 ≤ 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) = φ((x-μ)/σ) / (Φ((b-μ)/σ) - Φ((a-μ)/σ))

Cumulative distribution function

F(x) = (Φ((x-μ)/σ) - Φ((a-μ)/σ)) / (Φ((b-μ)/σ) - Φ((a-μ)/σ))

Quantile function

F^-1(p) = Φ^-1(Φ((a-μ)/σ) + p * (Φ((b-μ)/σ) - Φ((a-μ)/σ)))

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. https://arxiv.org/abs/0907.4010

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

Examples

x <- rtnorm(1e5, 5, 3, b = 7)
hist(x, 100, freq = FALSE)
curve(dtnorm(x, 5, 3, b = 7), -8, 8, col = "red", add = TRUE)
hist(ptnorm(x, 5, 3, b = 7))
plot(ecdf(x))
curve(ptnorm(x, 5, 3, b = 7), -8, 8, col = "red", lwd = 2, add = TRUE)

R <- 1e5
partmp <- par(mfrow = c(2,4), mar = c(2,2,2,2))

hist(rtnorm(R), freq= FALSE, main = "", xlab = "", ylab = "")
curve(dtnorm(x), -5, 5, col = "red", add = TRUE)

hist(rtnorm(R, a = 0), freq= FALSE, main = "", xlab = "", ylab = "")
curve(dtnorm(x, a = 0), -1, 5, col = "red", add = TRUE)

hist(rtnorm(R, b = 0), freq= FALSE, main = "", xlab = "", ylab = "")
curve(dtnorm(x, b = 0), -5, 5, col = "red", add = TRUE)

hist(rtnorm(R, a = 0, b = 1), freq= FALSE, main = "", xlab = "", ylab = "")
curve(dtnorm(x, a = 0, b = 1), -1, 2, col = "red", add = TRUE)

hist(rtnorm(R, a = -1, b = 0), freq= FALSE, main = "", xlab = "", ylab = "")
curve(dtnorm(x, a = -1, b = 0), -2, 2, col = "red", add = TRUE)

hist(rtnorm(R, mean = -6, a = 0), freq= FALSE, main = "", xlab = "", ylab = "")
curve(dtnorm(x, mean = -6, a = 0), -2, 1, col = "red", add = TRUE)

hist(rtnorm(R, mean = 8, b = 0), freq= FALSE, main = "", xlab = "", ylab = "")
curve(dtnorm(x, mean = 8, b = 0), -2, 1, col = "red", add = TRUE)

hist(rtnorm(R, a = 3, b = 5), freq= FALSE, main = "", xlab = "", ylab = "")
curve(dtnorm(x, a = 3, b = 5), 2, 5, col = "red", add = TRUE)

par(partmp)

extraDistr

Additional Univariate and Multivariate Distributions

v1.9.1
GPL-2
Authors
Tymoteusz Wolodzko
Initial release
2020-08-20

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.