dtnorm: Truncated Normal Distribution

Description Usage Arguments Value Examples

Description

Random number generation, probability density and cumulative density functions for truncated normal distribution.

Usage

1
2
3
4
5
6
rtnorm(n, mean, sd, lower, upper)

dtnorm(x, mean = 0, sd = 1, lower = -Inf, upper = Inf, log = FALSE)

ptnorm(x, mean = 0, sd = 1, lower = -Inf, upper = Inf,
  lower.tail = TRUE, log.p = FALSE)

Arguments

n

number of observations. n must be a scalar.

mean

mean (must be scalar).

sd

standard deviation (must be scalar).

lower

lower truncation value (must be scalar).

upper

upper truncation value (must be scalar).

x, q

vector of quantiles;

lt

lower tail. If TRUE (default) probabilities are P[X <= x], otherwise, P[X > x].

lp

log probability. If TRUE (default is FALSE) probabilities p are given as log(p).

Value

a column vector.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
## rtn example
dat1 <- rtnorm(1e5, 0, 1, 0, Inf)
## dat2 <- msm::rtnorm(n, mean, sd, lower, upper)
## den2 <- density(dat2)
hist(dat1, breaks="fd", freq=F)
## lines(den2$x, den2$y, lwd=2.5)
## res <- microbenchmark(
##     rtn(n, mean, sd, lower, upper),
##     msm::rtnorm(n, mean, sd, lower, upper))
## print(res)

## dtn example
x <- seq(-5, 5, length.out=1e3)
dat1 <- dtnorm(x, 0, 1, -2, 2, 0)
## dat2 <- msm::dtnorm(x, mean, sd, lower, upper, 0)
plot(x, dat1, type="l", lwd=2)
## lines(x, dat2, lwd=2, lty="dashed", col="red")

## res <- microbenchmark(
##     dtnorm(x, mean, sd, lower, upper, 0),
##     msm::dtnorm(x, mean, sd, lower, upper, 0))
## print(res)

## ptn example
x <- seq(-50, 10, length.out=1e3)
mean <- 0
sd <- 1
lower <- 0
upper <- 5
dat1 <- ptnorm(x, 0, 1, 0, 5, lp=TRUE)
## dat2 <- msm::ptnorm(x, mean, sd, lower, upper, log.p=TRUE)
## all(dat1[,1] == dat2)

plot(x, log(dat1[,1]))
## lines(x, log(dat2), col="red", lwd=2)
## mtext("pnorm(x, log=TRUE)", adj = 0)
## mtext("log(pnorm(x))", col = "red", adj = 1)

TasCL/cpda documentation built on May 3, 2019, 11:48 p.m.