Monday, April 7, 2014

Generate zipf distribution in C++

Standard library and Gnu scientific library GSL, do not provide a way to get the probability mass function of the zipf distribution directly. By looking around from wiki and stackoverflow post, implemented a couple lines of code for zipf pmf:


#include
 
// when one doesn't know the population size
// assume the population is infinity
// then the generalized harmonic number
// can be approximated as zeta function
double get_pmf_zipf(int rank, double skew) {
// use zeta function, one can approximate the zipf pmf
return pow(rank, -1*skew)/gsl_sf_zeta(skew);
}
 
// when one knows the population
// we can calculate the generalized harmonic number
double get_pmf_zipf(int rank, int N, double skew) {
double base = 0;
for (int k = 1; k <= N; k++) base += pow(k, -1*skew);
return pow(rank, -1*skew)/base;
}

No comments:

Post a Comment