views:

35

answers:

2

I am working on some statistical code and exploring different ways of creating samples from random distributions - starting from a random number generator that generates uniform floating point values from 0 to 1

I know it is possible to generate approximate samples from a normal distribution by adding together a sufficiently large number of independent, identically distributed uniform random variables (by the central limit theorem).

Is it possible to do something similar to create samples from the logistic distribution? I'm assuming the samples to be added would need to be weighted or correlated somehow in order to avoid ending up with a normal.

P.S. I'm also aware there may be more efficient ways of generating random samples, I'm asking the question because I'm more interested in understanding how such a generator would work rather than efficiency....

+6  A: 

There's one very common way to create random numbers for most distributions of interest, which is the inverse cdf method.

First, generate an inverse Cumulative distribution function for the distribution in question -- since the cdf is a function that takes values in the distribution's domain and maps them onto [0,1], the inverse cdf is a function that takes values in [0,1] and maps them onto values in the distribution's domain, with the appropriate probabilities. A lot of common distributions have inverse cdfs derived analytically, but if your distribution is intractible or approximate, you can create an approximate numeric inverse cdf.

Second, take any good random number generator that generates numbers uniformly distributed on [0,1], and run its output through the inverse cdf. Now the outputs follow the distribution you started with.

The inverse cdf of the logistic distribution can be found here.

hobbs
+4  A: 

The inverse of the logistic distribution isn't hard to find, so you can use Inverse transform sampling. The basic algorithm is:

for each random variate x ~ logistic
  generate a random variate y ~ Uniform(0, 1)
  x := F^-1 (y)

where F^-1 is the inverse CDF for the logistic, or the desired, distribution. Most programming languages will let you generate a Uniform variate between 0 and 1 through some kind of rand function.

Here's some python code which generates 1000 random variates from a logistic distribution:

from random import random
import math
import pylab

loc, scale = 0, 1

randvars = []
for i in range(1000):
    x = random()
    y = loc + scale * math.log(x / (1-x))
    randvars.append(y)

pylab.hist(randvars)
ars
thanks this is a great answer. you don't happen to know if there is a way of getting a logistic distribution *without* using the c.d.f., rather by adding together a sum of different uniform values?
mikera
Sorry, I'm not aware of other methods.
ars