These functions provide information about the triangular distribution on the interval from min to max with mode equal to mode. ctri gives the characteristic function, dtri gives the density function, estri gives the expected shortfall, mgtri gives the moment generating function, ptri gives the distribution function, qtri gives the quantile function, and rtri gives the random variate generator.

ctri(t, min = 0, max = 1, mode = 0.5)

dtri(x, min = 0, max = 1, mode = 0.5, log = FALSE)

estri(p, min = 0, max = 1, mode = 0.5, lower_tail = TRUE, log_p = FALSE)

mgtri(t, min = 0, max = 1, mode = 0.5)

ptri(q, min = 0, max = 1, mode = 0.5, lower_tail = TRUE, log_p = FALSE)

qtri(p, min = 0, max = 1, mode = 0.5, lower_tail = TRUE, log_p = FALSE)

rtri(n, min = 0, max = 1, mode = 0.5, dqrng = FALSE)

Arguments

t

Vector of dummy variables.

min

Lower limit of the distribution. Must have min \(<\) max.

max

Upper limit of the distribution. Must have max \(>\) min.

mode

The mode of the distribution. Must have mode \(\ge\) min and mode \(\le\) max.

x, q

Vector of quantiles.

log, log_p

Logical; if TRUE, probabilities p are given as log(p).

p

Vector of probabilities.

lower_tail

Logical; if TRUE (default), probabilities p are \(P[X \le x]\), otherwise, \(P[X > x]\).

n

Number of observations. Must have length of one.

dqrng

Logical; if FALSE (default), runif will be used to generate random numbers instead of dqrunif from the dqrng package.

Value

ctri gives the characteristic function, dtri gives the density function, estri gives the expected shortfall, mgtri gives the moment generating function, ptri gives the distribution function, qtri gives the quantile function, and rtri gives the random variate generator.

The numerical arguments other than n with values of size one are recycled to the length of t for ctri and mgtri, the length of x for dtri, the length of p for estri and qtri, the length of q for ptri, and n for rtri. This determines the length of the result.

The logical arguments log, lower_tail, and log_p must be of length one each.

Details

If min, max, or mode are not specified they assume the default values of 0, 1, and 0.5 respectively.

The triangular distribution has density $$0$$ for \(x < min\) or \(x > max\) $$f(x) = \frac{2(x - min)}{(max - min)(mode - min)}$$ for \(min \le x < mode\), and $$f(x) = \frac{2(max - x)}{(max - min)(max - mode)}$$ for \(mode < x \le max\).

rtri will not generate either of the extreme values unless max - min is small compared to min, and in particular not for the default arguments.

Note

The characteristics of output from pseudo-random number generators (such as precision and periodicity) vary widely. See .Random.seed for more information on R's random number generation algorithms.

See also

RNG about random number generation in R.

Distributions for other standard distributions.

Examples

# min, max, and mode with lengths equal to the length of x x <- c(0, 0.5, 1) d <- dtri(x, min = c(0, 0, 0), max = c(1, 1, 1), mode = c(0.5, 0.5, 0.5)) # min and max will be recycled to the length of x rec_d <- dtri(x, min = 0, max = 1, mode = c(0.5, 0.5, 0.5)) all.equal(d, rec_d)
#> [1] TRUE
# min, max, and mode with lengths equal to the length of x n <- 3 set.seed(1) r <- rtri(n, min = c(0, 0, 0), max = c(1, 1, 1), mode = c(0.5, 0.5, 0.5)) # min and max will be recycled to the length of n set.seed(1) rec_r <- rtri(n, min = 0, max = 1, mode = c(0.5, 0.5, 0.5)) all.equal(r, rec_r)
#> [1] TRUE
# Using dqrng::dqrunif() dqrng::dqset.seed(1) r <- rtri(n, min = 0, max = 1, mode = 0.5, dqrng = TRUE) # Log quantiles x <- c(0, 0.5, 1) log_d <- dtri(x, log = TRUE) d <- dtri(x, log = FALSE) all.equal(log(d), log_d)
#> [1] TRUE
# Upper tail probabilities q <- c(0, 0.5, 1) upper_p <- ptri(q, lower_tail = FALSE) p <- ptri(q, lower_tail = TRUE) all.equal(upper_p, 1 - p)
#> [1] TRUE
# Log probabilities q <- c(0, 0.5, 1) log_p <- ptri(q, log_p = TRUE) p <- ptri(q, log_p = FALSE) all.equal(upper_p, 1 - p)
#> [1] TRUE
# The quantile function p <- c(0, 0.5, 1) upper_q <- ptri(1 - p, lower_tail = FALSE) q <- ptri(p, lower_tail = TRUE) all.equal(upper_q, q)
#> [1] TRUE
p <- c(0, 0.5, 1) log_q <- qtri(log(p), log_p = TRUE) q <- qtri(p, log_p = FALSE) all.equal(log_q, q)
#> [1] TRUE
# Moment generating function t <- c(1, 2, 3) mgtri(t)
#> [1] 1.683357 2.952492 5.387626
# Characteristic function t <- c(1, 2, 3) ctri(t)
#> [1] 0.8594513+0.4695204i 0.4967514+0.7736445i 0.0584297+0.8239422i
# Expected Shortfall p <- c(0.1, 0.5, 1) estri(p)
#> [1] 0.1490712 0.3333333 0.5000000