Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: random number generator from logistic distribution

Subject: random number generator from logistic distribution

From: Jessica

Date: 29 Jan, 2008 23:33:09

Message: 1 of 3

Hi,
I'm trying to create a random number generator from the
logistic distribution. Does the following function look
right or am I completely off track?!

r=lrnd(m,n,mu,sigma)
p=rand(m,n)
r=log(p./(1-p)).*sigma+mu

Thanks for your help

Jess

Subject: Re: random number generator from logistic distribution

From: Roger Stafford

Date: 30 Jan, 2008 00:59:02

Message: 2 of 3

"Jessica " <jandre@mathworks.com> wrote in message <fnod3l$po
$1@fred.mathworks.com>...
> Hi,
> I'm trying to create a random number generator from the
> logistic distribution. Does the following function look
> right or am I completely off track?!
>
> r=lrnd(m,n,mu,sigma)
> p=rand(m,n)
> r=log(p./(1-p)).*sigma+mu
>
> Thanks for your help
>
> Jess
--------
  That looks good to me, except that of course functions are written this way:

 function r=lrnd(m,n,mu,sigma);
 etc.

You have correctly taken the inverse of the cumulative distribution function:

 p = F(x;mu,sigma) = 1/(1+exp(-(x-mu)/sigma))

  Note: Since sigma is a scalar, you don't need the dot when multiplying by it:

  r=log(p./(1-p))*sigma+mu;

Roger Stafford

Subject: Re: random number generator from logistic distribution

From: Roger Stafford

Date: 30 Jan, 2008 10:40:06

Message: 3 of 3

"Jessica " <jandre@mathworks.com> wrote in message <fnod3l$po
$1@fred.mathworks.com>...
> Hi,
> I'm trying to create a random number generator from the
> logistic distribution. Does the following function look
> right or am I completely off track?!
>
> r=lrnd(m,n,mu,sigma)
> p=rand(m,n)
> r=log(p./(1-p)).*sigma+mu
>
> Thanks for your help
>
> Jess
--------
  Because you expressed doubt about the validity of the method in your
logistic distribution generator, Jessica, I have decided that it would be
appropriate to show details of the reasoning behind it.

  You defined the random variable p in terms of matlab's 'rand' function:

 p = rand

(forget for a moment that your p is actually an m by n array) and the random
variable r in terms of p as:

 r = f(p) = log(p/(1-p))*s+m

This is easily shown to be mathematically equivalent to saying

 p = f_inverse(r) = 1/(1+exp(-(r-m)/s))

Then we can write the cumulative distribution of r as:

 P{r <= c} = P{f(p) <= c} =
 P{p <= f_inverse(c)} = P{p <= 1/(1+exp(-(c-m)/s)

This holds because f is a monotonically increasing function so that

 f(p) <= c

is equivalent to

 p <= f_inverse(c)

and so have equal probabilities. Next notice that random variable p = rand
has the important special property that P{p <= t} is always equal to t itself for
any t in 0 <= t <= 1, because MathWorks has gone to great lengths to ensure
that rand is uniformly distributed from 0 to 1. Thus, applying this to t = 1/(1
+exp(-(c-m)/s)), we have

 P{r <= c} = P{p <= 1/(1+exp(-(c-m)/s))} = 1/(1+exp(-(c-m)/s))

Therefore r has the required cumulative logistic distribution.

  The same kind of reasoning applies to any random variable which is to be
defined in terms of the special random variable p = rand. One simply applies
the inverse of the desired cumulative distribution function to p.

Roger Stafford

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics