R/triangular-distribution.R

Defines functions rtriang qtriang ptriang dtriang

Documented in dtriang ptriang qtriang rtriang

#' Triangular distribution
#'
#' Density, distribution function, quantile function and random generation
#' for the triangular distribution.
#'
#' @param x,q	            vector of quantiles.
#' @param p	              vector of probabilities.
#' @param n	              number of observations. If \code{length(n) > 1},
#'                        the length is taken to be the number required.
#' @param a,b,c           minimum, maximum and mode of the distribution.
#' @param log,log.p	      logical; if TRUE, probabilities p are given as log(p).
#' @param lower.tail	    logical; if TRUE (default), probabilities are \eqn{P[X \le x]}
#'                        otherwise, \eqn{P[X > x]}.
#'
#' @details
#'
#' Probability density function
#' \deqn{
#' f(x) = \left\{\begin{array}{ll}
#' \frac{2(x-a)}{(b-a)(c-a)} & x < c \\
#' \frac{2}{b-a}             & x = c \\
#' \frac{2(b-x)}{(b-a)(b-c)} & x > c
#' \end{array}\right.
#' }{
#' f(x) = [if x < c:] (2*(x-a)) / ((b-a)*(c-a))
#'        [if x = c:] 2/(b-a)
#'        [if x >= c:] (2*(b-x)) / ((b-a)*(b-c))
#' }
#'
#' Cumulative distribution function
#' \deqn{
#' F(x) = \left\{\begin{array}{ll}
#' \frac{(x-a)^2}{(b-a)(c-a)}     & x \leq c \\
#' 1 - \frac{(b-x)^2}{(b-a)(b-c)} & x > c
#' \end{array}\right.
#' }{
#' F(x) = [if x <= c:] (x-a)^2 / ((b-a)*(c-a))
#'        [if x > c:] 1 - ((b-x)^2 / ((b-a)*(b-c)))
#' }
#'
#' Quantile function
#' \deqn{
#' F^{-1}(p) = \left\{\begin{array}{ll}
#' a + \sqrt{p \times (b-a)(c-a)} & p \leq \frac{c-a}{b-a} \\
#' b - \sqrt{(1-p)(b-a)(b-c)}     & p > \frac{c-a}{b-a}
#' \end{array}\right.
#' }{
#' F^-1(p) = [if p < (c-a)/(b-a):] a + sqrt(p*(b-a)*(c-a))
#'           [else:] b - sqrt((1-p)*(b-a)*(b-c))
#' }
#' 
#' For random generation MINMAX method described by
#' Stein and Keblis (2009) is used.
#'
#' @references
#' Forbes, C., Evans, M. Hastings, N., & Peacock, B. (2011).
#' Statistical Distributions. John Wiley & Sons.
#' 
#' @references
#' Stein, W. E., & Keblis, M. F. (2009).
#' A new method to simulate the triangular distribution.
#' Mathematical and computer modelling, 49(5), 1143-1147.
#' 
#' @examples 
#' 
#' x <- rtriang(1e5, 5, 7, 6)
#' hist(x, 100, freq = FALSE)
#' curve(dtriang(x, 5, 7, 6), 3, 10, n = 500, col = "red", add = TRUE)
#' hist(ptriang(x, 5, 7, 6))
#' plot(ecdf(x))
#' curve(ptriang(x, 5, 7, 6), 3, 10, n = 500, col = "red", lwd = 2, add = TRUE)
#'
#' @name Triangular
#' @aliases Triangular
#' @aliases dtriang
#' 
#' @keywords distribution
#' @concept Univariate
#' @concept Continuous
#'
#' @export

dtriang <- function(x, a = -1, b = 1, c = (a+b)/2, log = FALSE) {
  cpp_dtriang(x, a, b, c, log[1L])
}


#' @rdname Triangular
#' @export

ptriang <- function(q, a = -1, b = 1, c = (a+b)/2, lower.tail = TRUE, log.p = FALSE) {
  cpp_ptriang(q, a, b, c, lower.tail[1L], log.p[1L])
}


#' @rdname Triangular
#' @export

qtriang <- function(p, a = -1, b = 1, c = (a+b)/2, lower.tail = TRUE, log.p = FALSE) {
  cpp_qtriang(p, a, b, c, lower.tail[1L], log.p[1L])
}


#' @rdname Triangular
#' @export

rtriang <- function(n, a = -1, b = 1, c = (a+b)/2) {
  if (length(n) > 1) n <- length(n)
  cpp_rtriang(n, a, b, c)
}

Try the extraDistr package in your browser

Any scripts or data that you put into this service are public.

extraDistr documentation built on Sept. 7, 2020, 5:09 p.m.