The triangulr package provides high-performance triangular distribution functions which includes density function, distribution function, quantile function, random variate generator, moment generating function, characteristic function, and expected shortfall function for the triangular distribution.
You can install the released version of triangulr from CRAN with:
install.packages("triangulr")
And the development version from GitHub with:
# install.packages("devtools") devtools::install_github("irkaal/triangulr")
These are basic examples of using the included functions:
Using the density function, dtri().
x <- c(0.1, 0.5, 0.9) dtri(x, min = 0, max = 1, mode = 0.5) #> [1] 0.4 2.0 0.4 dtri(x, min = 0, max = rep.int(1, 3), mode = 0.5) #> [1] 0.4 2.0 0.4
Using the distribution function, ptri().
q <- c(0.1, 0.5, 0.9) 1 - ptri(q, lower_tail = FALSE) #> [1] 0.02 0.50 0.98 ptri(q, lower_tail = TRUE) #> [1] 0.02 0.50 0.98 ptri(q, log_p = TRUE) #> [1] -3.91202301 -0.69314718 -0.02020271 log(ptri(q, log_p = FALSE)) #> [1] -3.91202301 -0.69314718 -0.02020271
Using the quantile function, qtri().
p <- c(0.1, 0.5, 0.9) qtri(1 - p, lower_tail = FALSE) #> [1] 0.2236068 0.5000000 0.7763932 qtri(p, lower_tail = TRUE) #> [1] 0.2236068 0.5000000 0.7763932 qtri(log(p), log_p = TRUE) #> [1] 0.2236068 0.5000000 0.7763932 qtri(p, log_p = FALSE) #> [1] 0.2236068 0.5000000 0.7763932
Using the random variate generator, rtri().
set.seed(1) n <- 3 rtri(n, min = 0, max = 1, mode = 0.5) #> [1] 0.3643547 0.4313490 0.5378601 rtri(n, min = 0, max = rep.int(1, 3), 0.5) #> [1] 0.7857662 0.3175547 0.7746000 # Using dqrng::dqrunif() dqrng::dqset.seed(1) rtri(n, min = 0, max = rep.int(1, 3), 0.5, dqrng = TRUE) #> [1] 0.3951856 0.8516496 0.4494472
Using the moment generating function, mgtri().
t <- c(1, 2, 3) mgtri(t, min = 0, max = 1, mode = 0.5) #> [1] 1.683357 2.952492 5.387626 mgtri(t, min = rep.int(0, 3), max = 1, mode = 0.5) #> [1] 1.683357 2.952492 5.387626
Using the characteristic function, ctri().
t <- c(1, 2, 3) ctri(t, min = 0, max = 1, mode = 0.5) #> [1] 0.8594513+0.4695204i 0.4967514+0.7736445i 0.0584297+0.8239422i ctri(t, min = rep.int(0, 3), max = 1, mode = 0.5) #> [1] 0.8594513+0.4695204i 0.4967514+0.7736445i 0.0584297+0.8239422i
Using the expected shortfall function, estri().