1

Is there any scripting language function (like in python or bash) that samples from a zipf-like distribution, for exponent 0<a<=1.

PS: I am aware of existence of a numpy function that can generate zipf samples, but it's only for a >1.

2 Answers 2

2

Based on the basic equations, how about:

#!/usr/bin/python
import sys
k = float(sys.argv[1])
s = float(sys.argv[2])
N = int(sys.argv[3])
def zipf(k, s, N):
    return (1/k**s) / sum(1/n**s for n in range(1, N+1))
print zipf(k, s, N)

Uses only builtin, basic routines in the language. Could be converted to just about any other language, e.g. perl, ruby. Here is a version in awk:

#!/bin/awk -f
BEGIN {
    for (n=1; n<=N; n++) {
        den=den+1/n^s;
    }
    print (1/(k^s))/den;
}

The awk script requires no input file, but does require variable assignments on the command line:

$ /tmp/zipf.awk -vk=10 -vs=4 -vN=10
9.24183e-05
0

Wouldn't a Python expovariate distribution, with the correct value of lambda, work?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .