50% off: Unlimited data and AI learning.
State of Data and AI Literacy Report 2025

triangle (version 1.0)

triangle: The Triangle Distribution

Description

These functions provide information about the triangle distribution on the interval from a to b with a maximum at c. dtriangle gives the density, ptriangle gives the distribution function, qtriangle gives the quantile function, and rtriangle generates n random deviates.

Usage

dtriangle(x, a = 0, b = 1, c = (a + b)/2)

ptriangle(q, a = 0, b = 1, c = (a + b)/2)

qtriangle(p, a = 0, b = 1, c = (a + b)/2)

rtriangle(n = 1, a = 0, b = 1, c = (a + b)/2)

Value

dtriangle gives the density, ptriangle gives the distribution function, qtriangle gives the quantile function, and rtriangle generates random deviates. Invalid arguments will result in return value NaN or NA.

Arguments

x, q

vector of quantiles.

a

lower limit of the distribution.

b

upper limit of the distribution.

c

mode of the distribution.

p

vector of probabilities.

n

number of observations. If length(n) > 1, the length is taken to be the number required.

Details

All probabilities are lower tailed probabilities. a, b, and c may be appropriate length vectors except in the case of rtriangle. rtriangle is derived from a draw from runif. The triangle distribution has density: f(x)=2(xa)(ba)(ca) for ax<c. f(x)=2(bx)(ba)(bc) for cxb. f(x)=0 elsewhere. The mean and variance are: E(x)=(a+b+c)3 V(x)=118(a2+b2+c2abacbc)

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

See Also

.Random.seed about random number generation, runif, etc for other distributions.

Examples

Run this code
## view the distribution
tri <- rtriangle(100000, 1, 5, 3)
hist(tri, breaks=100, main="Triangle Distribution", xlab="x")
mean(tri) # 1/3*(1 + 5 + 3) = 3
var(tri)  # 1/18*(1^2 + 3^2 + 5^2 - 1*5 - 1*3 - 5*3) = 0.666667
dtriangle(0.5, 0, 1, 0.5) # 2/(b-a) = 2
qtriangle(ptriangle(0.7)) # 0.7

Run the code above in your browser using DataLab