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
.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIs 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
.
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
Wouldn't a Python expovariate distribution, with the correct value of lambda, work?