Obsidian | Level 7

## Quantiles for Observations from a Triangular Distribution

The QUANTILE function lists several distributions but the TRIANGULAR distribution is not one of them.  The RAND function can compute random variables from a triangular distribution.  Suppose I have observations from a triangular distribution, how do I compute their quantiles?  It's easy for distributions that the QUANTILE allows, but the triangle distribution isn't one of them.  Any code is appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: Quantiles for Observations from a Triangular Distribution

@PaulN wrote:

Here's what I came up with. It seems to work.

data triangle_percentile;
set triangle;

/*a=minimum, b=maximum, c=mode h =height*/

/*using the 1/2*base*height for a triangle         */

/*and slope = (y2-y1)/(x2-x1), which is either   */

/* h/(c-a) for x<=c or h/(b-c) for x > c                */

/*the negative sign for the slope for the latter   */

/is the minus sign after the 1.                           */
a=0;
b=1;
c=0.2;

h=2;

if x <= c then
q= 0.5*(x-a)*(h/(c-a))*x;
else
q=1 - 0.5*(b-x)*(h/(b-c))*(1-x);
run;

Hello @PaulN,

Your algorithm correctly computes the values of the cumulative distribution function (CDF) from given quantiles. (For general values of a and b the formulas should read

`q = 0.5*(x-a)*(h/(c-a))*(x-a)`

and

`q = 1 - 0.5*(b-x)*(h/(b-c))*(b-x)`

respectively, but since a=0 and b=1 in your example, you get the correct results.)

But you set out to compute quantiles (from cumulative probabilities) like SAS's QUANTILE function -- the inverse function of the CDF. For that purpose @mkeintz's algorithm is correct.

For example, to answer the typical question "What is the 95% quantile of that triangular distribution?", his formula yields the 0.8 in the left column of your table from the 0.95 (=95%) in the right column:

`1 - sqrt((1-0)*(1-0.2)*(1-0.95)) = 0.8`

(mathematically; in SAS under Windows the result deviates from 0.8 by a tiny rounding error due to numeric representation issues).

8 REPLIES 8
Super User

## Re: Quantiles for Observations from a Triangular Distribution

Simulation and PROC UNIVARIATE?

or Calling @Rick_SAS

PROC Star

## Re: Quantiles for Observations from a Triangular Distribution

@PaulN wrote:

The QUANTILE function lists several distributions but the TRIANGULAR distribution is not one of them.  The RAND function can compute random variables from a triangular distribution.  Suppose I have observations from a triangular distribution, how do I compute their quantiles?  It's easy for distributions that the QUANTILE allows, but the triangle distribution isn't one of them.  Any code is appreciated.

Do you know the parameters of the trangular distribution?  I.e. the minimum (a), maximum (b) and mode (c)?

If so, then according to https://github.com/distributions-io/triangular-quantile the formula for quantiles (q) of a triangular distribution is:

``````  if p < (c-a)/(b-a) then q= a + sqrt((b-a)*(c-a)*p) ;
else q= b - sqrt((b-a)*(b-c)*(1-p)) ; ``````

where

• p is the cumulative distribution level for which you want a quantile
and
• (c-a)/(b-a) is the cumulative distribution at the mode value c  (i.e.  F(c)  )
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Obsidian | Level 7

## Re: Quantiles for Observations from a Triangular Distribution

I do know the minimum (a), maximum (b), and mode (c).

minimum = 0

maximum = 1

mode = 0.2

Height of triangle = 2.

I'll use your code and see what I get.  I'll compare it to my hand computations.  Thank you for the help.

Obsidian | Level 7

## Re: Quantiles for Observations from a Triangular Distribution

Here's the triangle distribution that I'm attempting to get SAS to compute quantiles.  When I do it by hand I get the following but when I run the code it doesn't match.  Any suggestions?

data triangle;
input x;
datalines;
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
;
run;

data triangle_percentile;
set triangle;

/*a=minimum, b=maximum, c=mode*/
a=0;
b=1;
c=0.2;

if x <= (c-a)/(b-a) then
q=a + sqrt((b-a)*(c-a)*x);
else
q=b - sqrt((b-a)*(b-c)*(1-x));
run;

Obsidian | Level 7

## Re: Quantiles for Observations from a Triangular Distribution

Here's what I came up with. It seems to work.

data triangle_percentile;
set triangle;

/*a=minimum, b=maximum, c=mode h =height*/

/*using the 1/2*base*height for a triangle         */

/*and slope = (y2-y1)/(x2-x1), which is either   */

/* h/(c-a) for x<=c or h/(b-c) for x > c                */

/*the negative sign for the slope for the latter   */

/is the minus sign after the 1.                           */
a=0;
b=1;
c=0.2;

h=2;

if x <= c then
q= 0.5*(x-a)*(h/(c-a))*x;
else
q=1 - 0.5*(b-x)*(h/(b-c))*(1-x);
run;

## Re: Quantiles for Observations from a Triangular Distribution

@PaulN wrote:

Here's what I came up with. It seems to work.

data triangle_percentile;
set triangle;

/*a=minimum, b=maximum, c=mode h =height*/

/*using the 1/2*base*height for a triangle         */

/*and slope = (y2-y1)/(x2-x1), which is either   */

/* h/(c-a) for x<=c or h/(b-c) for x > c                */

/*the negative sign for the slope for the latter   */

/is the minus sign after the 1.                           */
a=0;
b=1;
c=0.2;

h=2;

if x <= c then
q= 0.5*(x-a)*(h/(c-a))*x;
else
q=1 - 0.5*(b-x)*(h/(b-c))*(1-x);
run;

Hello @PaulN,

Your algorithm correctly computes the values of the cumulative distribution function (CDF) from given quantiles. (For general values of a and b the formulas should read

`q = 0.5*(x-a)*(h/(c-a))*(x-a)`

and

`q = 1 - 0.5*(b-x)*(h/(b-c))*(b-x)`

respectively, but since a=0 and b=1 in your example, you get the correct results.)

But you set out to compute quantiles (from cumulative probabilities) like SAS's QUANTILE function -- the inverse function of the CDF. For that purpose @mkeintz's algorithm is correct.

For example, to answer the typical question "What is the 95% quantile of that triangular distribution?", his formula yields the 0.8 in the left column of your table from the 0.95 (=95%) in the right column:

`1 - sqrt((1-0)*(1-0.2)*(1-0.95)) = 0.8`

(mathematically; in SAS under Windows the result deviates from 0.8 by a tiny rounding error due to numeric representation issues).

Obsidian | Level 7

## Re: Quantiles for Observations from a Triangular Distribution

Thanks for the clarification.  I should have stated my problem differently.  Your help is appreciated.