Re: equiprobable intervals with triangular pdf
- To: mathgroup at smc.vnet.net
 - Subject: [mg41475] Re: equiprobable intervals with triangular pdf
 - From: bobhanlon at aol.com (Bob Hanlon)
 - Date: Wed, 21 May 2003 08:03:51 -0400 (EDT)
 - References: <baclfp$r4h$1@smc.vnet.net>
 - Sender: owner-wri-mathgroup at wolfram.com
 
It is generally better to define piecewise continuous functions with UnitStep. 
It is also recommended that you follow the conventions used in 
Statistics`ContinuousDistributions` for defining distributions so that use of 
your distribution is consistent with use of the standard distributions.
Clear[TriangularDistribution, a, b, x];
TriangularDistribution::usage = "TriangularDistribution[a, b] represents the
triangular distribution defined on the interval [a, b].";
TriangularDistribution /: PDF[
      TriangularDistribution[a_, b_], x_] := 
    (UnitStep[x-a]*4*(x-a) + 
          UnitStep[x-(a+b)/2]*4*(a+b-2*x) +
          UnitStep[x-b]*4*(x-b))/(a-b)^2;
TriangularDistribution /: CDF[
      TriangularDistribution[a_, b_], x_] := 
    Evaluate[FullSimplify[Integrate[
          PDF[TriangularDistribution[a,b],x],
          {x,-Infinity,x}]]];
TriangularDistribution /: Mean[
      TriangularDistribution[a_, b_], x_] := 
    (a+b)/2;
TriangularDistribution /: Mode[
      TriangularDistribution[a_, b_], x_] := 
    (a+b)/2;
TriangularDistribution /: Variance[
      TriangularDistribution[a_, b_], x_] := 
    (a-b)^2/24;
TriangularDistribution /: StandardDeviation[
      TriangularDistribution[a_, b_], x_] := 
    Sqrt[(a-b)^2/24];
TriangularDistribution /: Moment[
  TriangularDistribution[a_, b_], n_Integer?NonNegative] := 
  Simplify[-((2*((a + b)^(n + 2)/2^n - 
  2*(a^(n + 2) + b^(n + 2))))/
  ((a - b)^2*(n + 1)*(n + 2)))];
TriangularDistribution /: Quantile[
      TriangularDistribution[a_,b_], 
      q_] :=
    (1/4)*(4*a-2*Sqrt[2]*(a-b)*Sqrt[q]) + 
        UnitStep[q-1/2]*(1/2)*(a-b)*(Sqrt[2-2*q]+Sqrt[2]*Sqrt[q]-2) /;
      0 <=q<=1 && b>a;
TriangularDistribution /: equiProb[
      TriangularDistribution[a_,b_], n_Integer?Positive] :=
    Partition[Table[Quantile[dist,k/n],{k,0,n}],2,1];
As an example of deriving the functions defined above, the n-th Moment is
defined by
FullSimplify[Integrate[
    x^n*PDF[TriangularDistribution[a,b],x], 
    {x,a,b}], b>a]
-((2*((a + b)^(n + 2)/2^n - 2*(a^(n + 2) + b^(n + 2))))/
   ((a - b)^2*(n + 1)*(n + 2)))
a=2; b=5; dist=TriangularDistribution[a,b];
Plot[{PDF[dist,x], CDF[dist,x]},
    {x,a-2,b+2}, PlotRange->All];
And @@ Table[xr = Random[Real, {a,b}];
    Quantile[dist, CDF[dist, xr]]==xr, {100}]
True
And @@ Table[qr = Random[];
    CDF[dist, Quantile[dist, qr]]==qr, {100}]
True
Plot[Quantile[dist, q], {q,0,1}];
ep=Simplify[equiProb[dist,6]];
Integrate[PDF[dist, x], {x,#[[1]],#[[2]]}]& /@ 
    ep // Simplify
{1/6, 1/6, 1/6, 1/6, 1/6, 1/6}
Bob Hanlon
In article <baclfp$r4h$1 at smc.vnet.net>, S White <susanlcw at aol.com> wrote:
<< Subject:	equiprobable intervals with triangular pdf
From:		S White <susanlcw at aol.com>
To: mathgroup at smc.vnet.net
Date:		Tue, 20 May 2003 07:28:57 +0000 (UTC)
Hello all,
I posted a couple of weeks ago about dividing a normal distribution
into n equiprobable intervals and received some great responses.  I am
now working with a triangular pdf and need to do the same thing.
I am defining the triangular pdf on the interval [a,b] with mean at
(a+b)/2 as follows:
triPdf[x_,a_,b_]:=(2/(b-a))^2*(x-a)/;a<=x<(b+a)/2;
triPdf[x_,a_,b_]:=(2/(b-a))^2*(b-x)/;(b+a)/2<=x<=b;
triPdf[x_,a_,b_]:=0/;a>x||x>b;
triCdf[x_,a_,b_]:=N[Integrate[triPdf[y,a,b],{y,a,x}]]
When working with the normal command, the following function gave me
output in the form I need:
equiprob[dist_,n_]:=Partition[Table[Quantile[dist,k/n],{k,0,n}],2,1]
However, the Quantile function doesn't work on this triangular pdf.
I have worked on defining some function that would do the same thing
as the Quantile function does  but I keep running into numerous error
messages and it only works for certain a and b.
Does anyone have a suggestion of a function that will give me
equiprobable intervals in the output form
  {{a,x1},{x1,x2},...,{xn,b}}, 
where a,x1,...,xn,b are the endpoints of the equiprobable intervals?
I really appreciate any help...
Susan