For Programmers: Free Programming Magazines  


Home > Archive > Matlab > September 2006 > What is a quick way to sample from a triangle distribution









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author What is a quick way to sample from a triangle distribution
abc34567ta@gmail.com

2006-09-28, 10:03 pm

Hi,

I want to sample from the triangle distribtuion with pdf given by

f(x) = 2(x-a)/(b-a)/(c-a) for a<=x<=c
2(b-x)/(b-a)/(b-c) for c<x<=b

(cdf given on http://en.wikipedia.org/wiki/Triangular_distribution.).

What is a quick way to sample from this distribution in Matlab.


Many thanks.

t

Roger Stafford

2006-09-28, 10:03 pm

In article <1159498158.041010.325860@e3g2000cwe.googlegroups.com>,
abc34567ta@gmail.com wrote:

> Hi,
>
> I want to sample from the triangle distribtuion with pdf given by
>
> f(x) = 2(x-a)/(b-a)/(c-a) for a<=x<=c
> 2(b-x)/(b-a)/(b-c) for c<x<=b
>
> (cdf given on http://en.wikipedia.org/wiki/Triangular_distribution.).
>
> What is a quick way to sample from this distribution in Matlab.
>
> Many thanks.
> t

-------------------
If you want n samples, do this:

h = sqrt(rand(1,n));
x = (b-a)*h.*rand(1,n)+c-(c-a)*h;

To demonstrate that this distribution is correct, do the above for a
large value of n, say, n = 10000, and follow it with this:

plot(x,1-h,'y.')

The filled triangle should be uniformly distributed area-wise with dots,
which is what your triangular distribution amounts to.

Roger Stafford
abc34567ta@gmail.com

2006-09-29, 4:13 am

Roger Stafford wrote:
> In article <1159498158.041010.325860@e3g2000cwe.googlegroups.com>,
> If you want n samples, do this:
>
> h = sqrt(rand(1,n));
> x = (b-a)*h.*rand(1,n)+c-(c-a)*h;


That is great, that will run really fast. Is there a book or paper
that I can cite for this formula?

Many thanks

t

Roger Stafford

2006-09-29, 4:13 am

In article <1159510775.580847.321330@c28g2000cwb.googlegroups.com>,
abc34567ta@gmail.com wrote:

> Roger Stafford wrote:
>
> That is great, that will run really fast. Is there a book or paper
> that I can cite for this formula?
>
> Many thanks
>
> t

--------------------
Undoubtedly there are references somewhere in the literature but I am
unable to quote them offhand. It's a little like giving citations for
integration methods in elementary calculus. It's just something that one
works out for oneself.

In this case, giving samples with your triangular distribution is
clearly equivalent to finding the x-coordinates of points uniformly
filling the triangle given by the plot I mentioned. And that in turn is
accomplished by ensuring that the probability of a point falling in the
upper portion of the triangle with 1 >= y >= 1-h be proportional to h^2,
which is to say that h should be proportional to the square root of that
probability. The expression for x generates a uniform random distribution
along the base of this upper triangle for given h.

Roger Stafford
Derek O'Connor

2006-09-29, 4:13 am

abc34567ta wrote:
>
>
> Roger Stafford wrote:
> <1159498158.041010.325860@e3g2000cwe.googlegroups.com>,
>
> That is great, that will run really fast. Is there a book or paper
> that I can cite for this formula?
>
> Many thanks
>
> t
>
>

Luc Devroye's book on Non-Uniform Random Number Generation,
Springer, 198?, is the bible on this topic.

It is out of print but he has made a PDF(scanned?) copy available at

<http://cg.scs.carleton.ca/~luc/rnbookindex.html>

Derek O'Connor
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com