Package 'triangulr'

Title: High-Performance Triangular Distribution Functions
Description: A collection of high-performance functions for the triangular distribution that consists of the probability density function, cumulative distribution function, quantile function, random variate generator, moment generating function, characteristic function, and expected shortfall function. References: Samuel Kotz, Johan Ren Van Dorp (2004) <doi:10.1142/5720> and Acerbi, Carlo and Tasche, Dirk. (2002) <doi:10.1111/1468-0300.00091>.
Authors: Alvin Nursalim [aut, cre]
Maintainer: Alvin Nursalim <[email protected]>
License: MIT + file LICENSE
Version: 1.2.1
Built: 2024-11-07 04:11:53 UTC
Source: https://github.com/alvindev0/triangulr

Help Index


The Triangular Distribution

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

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 \ge min and mode \le max.

log, log_p

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

lower_tail

Logical; if TRUE (default), probabilities p are P[Xx]P[X \le x], otherwise, P[X>x]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

00

for x<minx < min or x>maxx > max

f(x)=2(xmin)(maxmin)(modemin)f(x) = \frac{2(x - min)}{(max - min)(mode - min)}

for minx<modemin \le x < mode, and

f(x)=2(maxx)(maxmin)(maxmode)f(x) = \frac{2(max - x)}{(max - min)(max - mode)}

for mode<xmaxmode < 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.

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.

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)

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