#! /usr/bin/env python3 # def zipf_probability ( n, p ): #****************************************************************************80 # # Purpose: # # ZIPF_PROBABILITY sets up a Zipf probability vector. # # Modified: # # 19 February 2016 # # Author: # # Original C version by Warren Smith. # Python version by John Burkardt. # # Reference: # # George Zipf, # The Psychobiology of Language, # 1935. # # Parameters: # # Input, unsigned int N, indicates the size of X. # # Input, double P, the Zipf parameter. # 1.0 < P. # # Output, double X[N+2], contains in X[1] through X[N] the # probabilities of outcomes 1 through N. # import numpy as np from normalize import normalize x = np.zeros ( n + 2 ) for i in range ( 1, n + 1 ): x[i] = i ** ( - p ) x = normalize ( n, x ) return x def zipf_probability_test ( ): #*****************************************************************************80 # ## ZIPF_PROBABILITY_TEST tests ZIPF_PROBABILITY. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 19 February 2016 # # Author: # # John Burkardt # import platform from r8vec_print import r8vec_print print ( '' ) print ( 'ZIPF_PROBABILITY_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' ZIPF_PROBABILITY sets up a probablity vector X of N+2 elements' ) print ( ' containing in X[1:N] the probabilities of outcomes 1 through N' ) print ( ' in a Zipf distribution with parameter P.' ) n = 5 p = 1.0 x = zipf_probability ( n, p ) r8vec_print ( n + 2, x, ' X for N = 5, P = 1.0' ) n = 5 p = 2.0 x = zipf_probability ( n, p ) r8vec_print ( n + 2, x, ' X for N = 5, P = 2.0' ) n = 10 p = 2.0 x = zipf_probability ( n, p ) r8vec_print ( n + 2, x, ' X for N = 10, P = 2.0' ) # # Terminate. # print ( '' ) print ( 'ZIPF_PROBABILITY_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) zipf_probability_test ( ) timestamp ( )