# Triangular: The Triangular Distribution In triangulr: High-Performance Triangular Distribution Functions

## Description

These functions provide information about the triangular distribution on the interval from `min` to `max` with mode equal to `mode`. `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.

## Usage

 ``` 1 2 3 4 5 6 7 8 9 10 11``` ```dtri(x, min = 0, max = 1, mode = 0.5, log = FALSE) 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) mgtri(t, min = 0, max = 1, mode = 0.5) estri(p, min = 0, max = 1, mode = 0.5, lower_tail = TRUE, log_p = FALSE) ```

## Arguments

 `x, q` Vector of quantiles. `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` ≥ `min` and `mode` ≤ `max`. `log, log_p` Logical; if `TRUE`, probabilities `p` are given as `log(p)`. `lower_tail` Logical; if `TRUE` (default), probabilities `p` are P[X ≤ x], otherwise, P[X > x]. `p` Vector of probabilities. `n` Number of observations. Must have length of one. `t` Vector of dummy variables.

## 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) = 2(x - min) / (max - min)(mode - min)

for min ≤ x < mode, and

E(x) = 2(max - x) / (max - min)(max - mode)

for mode < x ≤ 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.

## Value

`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 `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.

## 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.

`RNG` about random number generation in R.
 ``` 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63``` ```# 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) # 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) # 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) # 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) # 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) # 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) 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) # Moment generating function t <- c(1, 2, 3) mgtri(t) # Expected Shortfall p <- c(0.1, 0.5, 1) estri(p) ```