Skip to content
Prev 63069 / 398498 Next

Zipf random number generation

On 25-Jan-05 Weiguang Shi wrote:
Understood! Though I think you mean

  1/c = sum(1.0 / pow((double) j, alpha)), j=1,2,...,N

Anyway, provided alpha > 1 the series converges for the
infinite sum, but if you limit the range to 1...N then
you could have any value of alpha (even 0 or negative).

Be that as it may, I don't know of any R function for
sampling such a distribution directly, but you could
certainly try the suggestion I gave at the end of my
previous response:

  N <- 10000 ##For instance. You need N large enough
  i0 <- (1:N) 
  p <- 1/(i0^alpha) ; p <- p/sum(p)
  X <- sample (i0, n, replace=TRUE, prob=p)

Then X will be a random sample of size n from the
distribution you describe on the range 1:N.

When I tried the above with alpha=1.1, N=10000, n=1000
the largest value of X was 9928, i.e. very nearly
equal to N, so this suggested that N=10000 may not be
large enough! So I then tried it with N=50000 and got
max(X) = 48303; then with N=100000 and max(X) = 94914.
And so on (at this point I was hitting swap-space on
this little machine). In short: with alpha so near 1,
the sample will tend to extend over the whole range
you give it when you specify N.

This is an indication that the simple-minded method
of my suggestion may fail to provide sufficiently large
sampled values when alpha is near 1.

However, since the "common practices" in your area,
as you describe them below, do set a value of N, then
presumably there is also a "common view" of what limit
for N is acceptable -- i.e. in your area it does not
matter if you fail to sample values larger than N.

In that case the above method should be satisfactory.

If not, then some other approach would be needed.
You certainly don't want to use the above method with N
approaching the largest machine-representable integer!

One possibility might be to split the sampling.
Let alpha > 1. Choose N fairly large. You would need
a method for evaluating sum(i^alpha) from 1 to infinity.
Let P = sum[1:N](i^alpha)/sum[1:inf](i^alpha).

With probability P, sample on (1:N) with the above method.
With probability (1-P), use a continuous approximation
on [(N+1):inf] with density proportional to 1/(x^alpha).
For the latter, the CDF is easily evaluated analytically;
likewise its inverse function. Hence by sampling from a
uniform distribution you can sample X from the distribution
with density proportional to 1/(x^alpha) on [(N+1):inf].
Then get the sampled value of i as floor(X).

This is in interesting question! I hope other readers can
suggest good ideas.

Hoping this helps,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861  [NB: New number!]
Date: 26-Jan-05                                       Time: 10:46:08
------------------------------ XFMail ------------------------------