Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 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
share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.