-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipftolint.py
1077 lines (875 loc) · 38 KB
/
zipftolint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
from statsmodels.formula.api import ols
import pandas as pd
import scipy.optimize as opt
import scipy.stats as st
from scipy.special import zeta
def length(x):
if type(x) == float or type(x) == int or type(x) == np.int32 or type(x) == np.float64 or type(x) == np.float32 or type(x) == np.int64:
return 1
return len(x)
def zetafun(x):
'''
Zeta Function
Description
Zeta function, internal
Usage
zetafun(x)
Parameters
----------
x:
For zetafun, a vector or matrix whose real values must be greater than
or equal to 1.
Details
-------
This functions are not intended to be called by the user. zetafun is a
condensed version of the Riemann's zeta function given in R's VGAM package.
Please use that reference if looking to directly implement Riemann's zeta
function. The function we have included is done so out of convenience.
References
----------
Derek S. Young (2010). tolerance: An R Package for Estimating Tolerance
Intervals. Journal of Statistical Software, 36(5), 1-39.
URL http://www.jstatsoft.org/v36/i05/.
Yee, T. (2010), The VGAM Package for Categorical Data Analysis, Journal of
Statistical Software, 32, 1–34.
Example
-------
zetafun([2,3,4,5,6])
'''
x = np.array(x)
if any(x < 1):
return "Invalid input for Riemann's zeta function."
a = 12
k = 8
B = [1/6, -1/30, 1/42, -1/30, 5/66, -691/2730, 7/6, -3617/510]
ans = []
for i in range(1,a):
ans.append(1/i**x[i%length(x)-1])
ans = np.array(ans)
ans = np.sum(ans)
ans = ans + 1/((x - 1) * a**(x - 1)) + 1/(2 * a**x)
term = (x/2)/a**(x + 1)
ans = ans + term * B[0]
for mm in range(1,k):
term = term * (x + 2 * mm - 2) * (x + 2 * mm - 3)/(a * a * 2 * mm * (2 * mm - 1))
ans = ans + term * B[mm]
return ans
def dzipfman(x, s, b = None, N = None, log = False):
'''
Zipf-Mandelbrot Distributions
Description
Density (mass) function for the Zipf, Zipf-Mandelbrot, and zeta
distributions.
Usage
dzipfman(x, s = None, b = None, N = None, log = False)
Parameters
----------
x: list
Vector of quantiles.
s, b: float
The shape parameters, both of which must be greater than 0. b must be
specified for Zipf-Mandelbrot distributions.
N: int
The number of categories, which must be integer-valued for Zipf and
Zipf-Mandelbrot distributions. For a zeta distribution, N = Inf must be used.
log: bool
Logical vectors. If TRUE, then the probabilities are given as log(p).
Details
-------
The Zipf-Mandelbrot distribution has mass
p(x) = (x + b)^-s/∑_{i=1}^{N}(i + b)^(-s),
where x=1,…,N, s,b>0 are shape parameters, and N is the number of distinct
categories. The Zipf distribution is just a special case of the
Zipf-Mandelbrot distribution where the second shape parameter b=0. The zeta
distribution has mass
p(x) = x^-λ/ζ(s),
where x=1,2,…, s>1 is the shape parameter, and ζ() is the Riemann zeta
function given by:
ζ(t) = ∑_{i=1}^∞ 1/i^t<∞.
Note that the zeta distribution is just a special case of the Zipf
distribution where s>1 and N goes to infinity.
Value
-----
dzipfman gives the density (mass), pzipfman gives the distribution function,
qzipfman gives the quantile function, and rzipfman generates random deviates
for the specified distribution.
Note
----
These functions may be updated in a future version of the package so as to
allow greater flexibility with the inputs.
References
----------
Derek S. Young (2010). tolerance: An R Package for Estimating Tolerance
Intervals. Journal of Statistical Software, 36(5), 1-39.
URL http://www.jstatsoft.org/v36/i05/.
Mandelbrot, B. B. (1965), Information Theory and Psycholinguistics. In B.
B. Wolman and E. Nagel, editors. Scientific Psychology, Basic Books.
Young, D. S. (2013), Approximate Tolerance Limits for Zipf-Mandelbrot
Distributions, Physica A: Statistical Mechanics and its Applications,
392, 1702–1711.
Zipf, G. K. (1949), Human Behavior and the Principle of Least Effort,
Hafner.
Zornig, P. and Altmann, G. (1995), Unified Representation of Zipf
Distributions, Computational Statistics and Data Analysis, 19, 461–473.
Examples
--------
## Randomly generated data from the Zipf distribution.
dzipfman(x = 1, s = 2, N = 100)
## Randomly generated data from the Zipf-Mandelbrot distribution.
dzipfman(x = [4,5,9], s = 2, b = 3, N = 100)
## Randomly generated data from the zeta distribution.
dzipfman(x = [4,8], s = 1.3, N = np.inf)
'''
if N == None:
return 'Must specify N.'
out = np.linspace(0,0,length(x))
if length(x) == 1:
x = [x]
x = np.array(x)
temp = [a if ((a != np.floor(a) and a <= N) or (a < 1) or (a == np.floor(a) and (a>N))) else 0 for a in x]
if sum(temp) > 0:
out = np.linspace(0,0,length(temp))
if sum(temp) != length(x):
if b == None and N < np.inf:
if s <= 0:
return "Invalid value for s!"
temp = np.array(temp)
out[np.where(out==temp)] = (np.float_power(x[np.where(x != temp)],-s))/sum(np.float_power(range(1,int(N+1)),-s))
elif b != None:
if s <= 0 or b < 0:
return "Invalid value for s and/or b!"
if N == np.inf:
return "N must be finite!"
out[np.where(out==temp)] = (np.float_power((x[np.where(x != temp)] + b),-s))/(sum(np.float_power((np.array(range(1,N+1)) + b),-s)))
else:
if s <= 1:
return "Invalid value for s!"
out[np.where(out==temp)] = (np.float_power(x[np.where(x != temp)],-s))/zeta(s)
if log:
out = np.log(out)
if any(x != np.floor(x)):
ind = np.where(x!=np.floor(x))[0]
for i in range(length(ind)):
if i == 0:
print("Warning messages:")
print(f'{i+1}: In the function dzipfman, non-integer x = {x[ind[i]]}')
if i == length(ind)-1:
print('\n')
return out
def pzipfman(q, s, b = None, N = None, lowertail = True, logp = False):
'''
Zipf-Mandelbrot Distributions
Description
distribution function for the Zipf, Zipf-Mandelbrot, and zeta
distributions.
Usage
pzipfman(q, s, b = None, N = None, lowertail = True, logp = False)
Parameters
----------
x: list
Vector of quantiles.
s, b: float
The shape parameters, both of which must be greater than 0. b must be
specified for Zipf-Mandelbrot distributions.
N: int
The number of categories, which must be integer-valued for Zipf and
Zipf-Mandelbrot distributions. For a zeta distribution, N = Inf must be used.
logp: bool
Logical vectors. If TRUE, then the probabilities are given as log(p).
lowertail: bool
Logical vector. If TRUE, then probabilities are P[X≤ x], else P[X>x].
Details
-------
The Zipf-Mandelbrot distribution has mass
p(x) = (x + b)^-s/∑_{i=1}^{N}(i + b)^(-s),
where x=1,…,N, s,b>0 are shape parameters, and N is the number of distinct
categories. The Zipf distribution is just a special case of the
Zipf-Mandelbrot distribution where the second shape parameter b=0. The zeta
distribution has mass
p(x) = x^-λ/ζ(s),
where x=1,2,…, s>1 is the shape parameter, and ζ() is the Riemann zeta
function given by:
ζ(t) = ∑_{i=1}^∞ 1/i^t<∞.
Note that the zeta distribution is just a special case of the Zipf
distribution where s>1 and N goes to infinity.
Value
-----
pzipfman gives the distribution function, for the specified distribution.
Note
----
These functions may be updated in a future version of the package so as to
allow greater flexibility with the inputs.
References
----------
Derek S. Young (2010). tolerance: An R Package for Estimating Tolerance
Intervals. Journal of Statistical Software, 36(5), 1-39.
URL http://www.jstatsoft.org/v36/i05/.
Mandelbrot, B. B. (1965), Information Theory and Psycholinguistics. In B.
B. Wolman and E. Nagel, editors. Scientific Psychology, Basic Books.
Young, D. S. (2013), Approximate Tolerance Limits for Zipf-Mandelbrot
Distributions, Physica A: Statistical Mechanics and its Applications,
392, 1702–1711.
Zipf, G. K. (1949), Human Behavior and the Principle of Least Effort,
Hafner.
Zornig, P. and Altmann, G. (1995), Unified Representation of Zipf
Distributions, Computational Statistics and Data Analysis, 19, 461–473.
Examples
## Randomly generated data from the Zipf distribution.
pzipfman(q = 5, s = 2, N = 100)
## Randomly generated data from the Zipf-Mandelbrot distribution.
pzipfman(q = [2,5,7], s = 2, b = 3, N = 100)
## Randomly generated data from the zeta distribution.
pzipfman(q = [6,8], s = 1.3, N = np.inf)
'''
if N == None:
return 'Must specify N.'
q = np.array(q)
q = np.floor(q)
if length(q) == 1:
q = np.array([q])
temp = []#np.array([None,]*length(q))
if b == None and N < np.inf:
if s <= 0:
return "Invalid value for s!"
if any(q <= 0):
temp[np.where(q<=0)] = 0
if any(q > N):
temp[np.where(q>N)] = 1
if any(q > 0) and any(q <= N):
ind = (np.where(q > 0) and np.where(q <= N))[0]
for i in range(length(ind)):
temp.append(dzipfman(x=range(1,int(q[ind[i]])+1), s = s, N = N))
elif b != None:
if s <= 0 or b < 0:
return 'Invalid value for s and/or b!'
if N == np.inf:
return "N must be finite!"
if any(q <= 0):
temp[np.where(q <= 0)] = 0
if any(q > N):
temp[np.where(q > N)] = 1
if any(q > 0) and any(q < N):
ind = (np.where(q > 0) and np.where(q <= N))[0]
for i in range(length(ind)):
temp.append(dzipfman(x=range(1,int(q[ind[i]])+1), s = s, b = b, N = N))
else:
if s <= 1:
return 'Invalid value for s!'
if any(q <= 0):
temp[np.where(q <= 0)] = 0
if any(q == np.inf):
temp[np.where(q == np.inf)] = 1
if any(q > 0) and any(q < np.inf):
ind = (np.where(q > 0) and np.where(q <= N))[0]
for i in range(length(ind)):
temp.append(dzipfman(x=range(1,int(q[ind[i]])+1), s = s, b = b, N = np.inf))
if lowertail == False:
for i in range(length(temp)):
temp[i] = np.round(1-sum(np.round(temp[i],12)),8)
if any(temp < 0):
temp[np.where(temp<0)] = 0
if any(temp > 1):
temp[np.where(temp>1)] = 0
if logp:
temp = np.log(temp)
elif logp:
for i in range(length(temp)):
temp[i] = np.round(np.log(temp[i][0]) + np.log(1+sum(temp[i][1:])/temp[i][0]),8)
else:
temp = np.array(list((map(sum,temp))))
if any(temp) < 0:
temp[np.where(temp<0)] = 0
if any(temp) > 1:
temp[np.where(temp>1)] = 0
return temp
def qzipfman(p, s = 1, b = None, N = None, lowertail = True, logp = False):
'''
Zipf-Mandelbrot Distributions
Description
Quantile function for the Zipf, Zipf-Mandelbrot, and zeta distributions.
Usage
qzipfman(p, s, b = None, N = None, lowertail = True,
logp = False)
Parameters
----------
p: list
Vector of probabilities.
s, b: float
The shape parameters, both of which must be greater than 0. b must be
specified for Zipf-Mandelbrot distributions.
N: int
The number of categories, which must be integer-valued for Zipf and
Zipf-Mandelbrot distributions. For a zeta distribution, N = Inf must
be used.
logp: bool
Logical vectors. If TRUE, then the probabilities are given as log(p).
lowertail: bool
Logical vector. If TRUE, then probabilities are P[X≤ x], else P[X>x].
Details
-------
The Zipf-Mandelbrot distribution has mass
p(x) = (x + b)^-s/∑_{i=1}^{N}(i + b)^(-s),
where x=1,…,N, s,b>0 are shape parameters, and N is the number of distinct
categories. The Zipf distribution is just a special case of the
Zipf-Mandelbrot distribution where the second shape parameter b=0. The zeta
distribution has mass
p(x) = x^-λ/ζ(s),
where x=1,2,…, s>1 is the shape parameter, and ζ() is the Riemann zeta
function given by:
ζ(t) = ∑_{i=1}^∞ 1/i^t<∞.
Note that the zeta distribution is just a special case of the Zipf
distribution where s>1 and N goes to infinity.
Value
-----
qzipfman gives the quantile function
Note
----
These functions may be updated in a future version of the package so as to
allow greater flexibility with the inputs.
References
----------
Derek S. Young (2010). tolerance: An R Package for Estimating Tolerance
Intervals. Journal of Statistical Software, 36(5), 1-39.
URL http://www.jstatsoft.org/v36/i05/.
Mandelbrot, B. B. (1965), Information Theory and Psycholinguistics. In B.
B. Wolman and E. Nagel, editors. Scientific Psychology, Basic Books.
Young, D. S. (2013), Approximate Tolerance Limits for Zipf-Mandelbrot
Distributions, Physica A: Statistical Mechanics and its Applications,
392, 1702–1711.
Zipf, G. K. (1949), Human Behavior and the Principle of Least Effort,
Hafner.
Zornig, P. and Altmann, G. (1995), Unified Representation of Zipf
Distributions, Computational Statistics and Data Analysis, 19, 461–473.
Examples
## Randomly generated data from the Zipf distribution.
qzipfman(p = 0.20, s = 2, N = 100, lowertail = False)
qzipfman(p = 0.80, s = 2, N = 100)
## Randomly generated data from the Zipf-Mandelbrot distribution.
qzipfman(p = 0.20, s = 2, b = 3, N = 100, lowertail = False)
qzipfman(p = 0.80, s = 2, b = 3, N = 100)
## Randomly generated data from the zeta distribution.
qzipfman(p = 0.20, s = 1.3, lowertail = False, N = np.inf)
qzipfman(p = 0.80, s = 1.3, N = np.inf)
'''
if N == None:
return 'Must specify N.'
if N != np.inf:
N = int(N)
if length(p) == 1:
p = [p]
p = np.array(p)
if logp:
p = np.exp(p)
if lowertail == False:
p = 1-p
if b == None and N < np.inf:
if s <= 0:
return "Invalid value for s!"
allp = np.linspace(1,1,length(p))
tempind = None
if any(p > 1) or any(p < 0):
tempind = np.where(p > 1)
allp[tempind] = np.nan
tempind = np.where(p < 0)
allp[tempind] = np.nan
temp = ([pzipfman(q=1,s=s,N=N) >= a or a>1 for a in p])
nottemp = [not t for t in temp]
if sum(nottemp) > 0 and N < 1e6:
ptemp = np.cumsum(dzipfman(x=np.array(range(1,N+1)), s = s, N = N))
outp = []
outp2 = []
for i in range(temp.count(not True)):
outp.append(min(np.where(ptemp >= p[np.where(nottemp)][i]))[0]+1) #not sure if +1 should be here, makes the same as R
try:
outp2.append(max(np.where(ptemp == p[np.where(nottemp)][i]))[0]+1) ##
except:
outp2.append(-np.inf)
allp[np.where(nottemp)] = max(outp,outp2)
elif sum(nottemp) > 0 and N >= 1e6:
x = np.array([1e6,1e300])
y = np.array([np.round(sum(dzipfman(x=np.array(range(1,int(1e6+1))),s=s,N=N)),8),1])
whichp = np.where([a >= y[0] and a < 1 for a in p])[0]
# #creating an lm object, 2 steps
# # 1.) make a dataframe (df)
# # 2.) lm_object: lm('y ~ x*', data = df) == ols('y ~ x*', data = df).fit()
xtmp = (-1/x)
df = pd.DataFrame({'xtmp':xtmp,'y':y})
outlm = ols('y~xtmp',data=df).fit()
be = outlm.params
newq = -1/((p[whichp]-be[0])/be[1])
if any(newq >= 1e300) or any(newq == np.inf):
allp[np.where(newq >= 1e300)] = np.inf
if any(newq < 1e300) and any(newq >= 1e6):
allp[whichp[np.where(newq < 1e300) and np.where(newq >= 1e6)]] = np.floor(newq[np.where(newq < 1e300) and np.where(newq >= 1e6)])
elif b != None:
if s <= 0 or b < 0:
return "Invalid value for s and/or b!"
if N == np.inf:
return "N must be finite"
allp = np.linspace(1,1,length(p))
tempind = None
if any(p > 1) or any(p < 0):
tempind = np.where(p > 1)
allp[tempind] = np.nan
tempind = np.where(p < 0)
allp[tempind] = np.nan
temp = ([pzipfman(q=1,s=s,N=N) >= a or a>1 for a in p])
nottemp = [not t for t in temp]
if sum(nottemp)>0 and N < 1e6:
ptemp = np.cumsum(dzipfman(x=np.array(range(1,N+1)), s = s, b = b, N = N))
outp = []
outp2 = []
for i in range(temp.count(not True)):
outp.append(min(np.where(ptemp >= p[np.where(nottemp)][i]))[0]+1) #not sure if +1 should be here, makes the same as R
try:
outp2.append(max(np.where(ptemp == p[np.where(nottemp)][i]))[0]+1) ##
except:
outp2.append(-np.inf)
allp[np.where(nottemp)] = max(outp,outp2)
elif sum(nottemp) > 0 and N >= 1e6:
x = np.array([1e6,1e300])
y = np.array([np.round(sum(dzipfman(x=np.array(range(1,int(1e6+1))),s=s,b=b,N=N)),8),1])
whichp = np.where([a >= y[0] and a < 1 for a in p])[0]
# #creating an lm object, 2 steps
# # 1.) make a dataframe (df)
# # 2.) lm_object: lm('y ~ x*', data = df) == ols('y ~ x*', data = df).fit()
xtmp = (-1/x)
df = pd.DataFrame({'xtmp':xtmp,'y':y})
outlm = ols('y~xtmp',data=df).fit()
be = outlm.params
newq = -1/((p[whichp]-be[0])/be[1])
if any(newq >= 1e300) or any(newq == np.inf):
allp[np.where(newq >= 1e300)] = np.inf
if any(newq < 1e300) and any(newq >= 1e6):
allp[whichp[np.where(newq < 1e300) and np.where(newq >= 1e6)]] = np.floor(newq[np.where(newq < 1e300) and np.where(newq >= 1e6)])
else:
if s <= 1:
return "Invalid value for s!"
allp = np.array([None,]*length(p))
if any(p>1) or any(p<0):
tempind = np.where(p > 1)
allp[tempind] = np.nan
tempind = np.where(p < 0)
allp[tempind] = np.nan
temp = (1/zeta(s)*np.cumsum(np.float_power(range(1,int(1e06+1)),-s)))
temp1 = 1/zeta(s)
tempmax = round(max(temp),7)
if any(p <= temp1):
allp[(np.where(p <= temp1) and np.where(p >= 0))][0] = 1
if any(p == 1):
allp[np.where(p == 1)] = np.inf
if any(p > tempmax):
x = np.array([1e6, 1e300])
y = np.array([tempmax,1])
xtmp = (-1/x)
df = pd.DataFrame({'xtmp':xtmp,'y':y})
outlm = ols('y~xtmp',data=df).fit()
be = outlm.params
whichp = np.where(p > tempmax) and np.where(p <= 1)
newq = -1/((p[whichp]-be[0])/be[1])
if any(newq >= 1e300) or any(newq == -np.inf):
allp[np.where(newq >= 1e300)] = np.inf
if any(newq < 1e300) and any(newq >= 1e6):
allp[whichp[np.where(newq < 1e300) and np.where(newq >= 1e6)]] = np.floor(newq[np.where(newq < 1e300) and np.where(newq >= 1e6)])
if any(allp == None):
NoneArr = np.where(allp == None)[0]
for i in range(list(allp).count(None)):
allp[NoneArr[i]] = np.min((np.where(temp >= p[NoneArr[i]]))[0]+1)
if any(allp == -np.inf):
allp[np.where(allp = -np.inf)] = np.nan
return allp
def rzipfman(n, s = 1, b = None, N = None):
'''
Zipf-Mandelbrot Distributions
Description
random generation for the Zipf, Zipf-Mandelbrot, and zeta distributions.
Usage
rzipfman(n, s, b = None, N = None)
Parameters
----------
n: int
The number of observations. If length>1, then the length is taken to
be the number required.
s, b: float
The shape parameters, both of which must be greater than 0. b must be
specified for Zipf-Mandelbrot distributions.
N: int
The number of categories, which must be integer-valued for Zipf and
Zipf-Mandelbrot distributions. For a zeta distribution, N = Inf must
be used.
Details
-------
The Zipf-Mandelbrot distribution has mass
p(x) = (x + b)^-s/∑_{i=1}^{N}(i + b)^(-s),
where x=1,…,N, s,b>0 are shape parameters, and N is the number of distinct
categories. The Zipf distribution is just a special case of the
Zipf-Mandelbrot distribution where the second shape parameter b=0. The zeta
distribution has mass
p(x) = x^-λ/ζ(s),
where x=1,2,…, s>1 is the shape parameter, and ζ() is the Riemann zeta
function given by:
ζ(t) = ∑_{i=1}^∞ 1/i^t<∞.
Note that the zeta distribution is just a special case of the Zipf
distribution where s>1 and N goes to infinity.
Value
-----
rzipfman generates random deviates for the specified distribution.
Note
----
These functions may be updated in a future version of the package so as to
allow greater flexibility with the inputs.
References
----------
Derek S. Young (2010). tolerance: An R Package for Estimating Tolerance
Intervals. Journal of Statistical Software, 36(5), 1-39.
URL http://www.jstatsoft.org/v36/i05/.
Mandelbrot, B. B. (1965), Information Theory and Psycholinguistics. In B.
B. Wolman and E. Nagel, editors. Scientific Psychology, Basic Books.
Young, D. S. (2013), Approximate Tolerance Limits for Zipf-Mandelbrot
Distributions, Physica A: Statistical Mechanics and its Applications,
392, 1702–1711.
Zipf, G. K. (1949), Human Behavior and the Principle of Least Effort,
Hafner.
Zornig, P. and Altmann, G. (1995), Unified Representation of Zipf
Distributions, Computational Statistics and Data Analysis, 19, 461–473.
Examples
## Randomly generated data from the Zipf distribution.
rzipfman(n = 150, s = 2, N = 100)
## Randomly generated data from the Zipf-Mandelbrot distribution.
rzipfman(n = 150, s = 2, b = 3, N = 100)
## Randomly generated data from the zeta distribution.
rzipfman(n = 100, s = 1.3, N = np.inf)
'''
if N == None:
return 'Must specify N.'
if b == None and N < np.inf:
if s <= 0:
return "Invalid value for s!"
out = qzipfman(p = st.uniform.rvs(size=n), s=s, N=N)
out = [int(a) for a in out]
elif b != None:
if s <= 0 or b <0:
return "Invalid value for s and/or b!"
if N == np.inf:
return "N must be finite!"
out = qzipfman(p = st.uniform.rvs(size=n), s=s, b=b, N=N)
out = [int(a) for a in out]
else:
if s <= 1:
return "Invalid value for s!"
out = qzipfman(p = st.uniform.rvs(size=n), s=s, N=np.inf)
out = [int(a) for a in out]
outlvl = pd.DataFrame(pd.Series(out).value_counts().sort_values(ascending=False)).T.columns
y = list(range(length(np.unique(out))))
out = np.array(out)
indexes = []
for i in range(length(outlvl)):
indexes.append(np.where(out == outlvl[i]))
for i in range(length(indexes)):
out[indexes[i]] = i
out = np.array([y[a]+1 for a in out])
return out
def zmll(x, N = None, s = 1, b = 1, dist = 'Zipf'):
'''
Maximum Likelihood Estimation for Zipf-Mandelbrot Models
Description
Performs maximum likelihood estimation for the parameters of the Zipf,
Zipf-Mandelbrot, and zeta distributions.
Usage
zmll(x, N = None, s = 1, b = 1, dist = ["Zipf", "Zipf-Man", "Zeta"])
Parameters
----------
x:
A vector of raw data or a table of counts which is distributed
according to a Zipf, Zipf-Mandelbrot, or zeta distribution. Do not
supply a vector of counts!
N:
The number of categories when dist = "Zipf" or dist = "Zipf-Man". This
is not used when dist = "Zeta". If N = None, then N is estimated based
on the number of categories observed in the data.
s:
The initial value to estimate the shape parameter, which is set to 1
by default. If a poor initial value is specified, then a warning
message is returned.
b:
The initial value to estimate the second shape parameter when
dist = "Zipf-Man", which is set to 1 by default. If a poor initial
value is specified, then a warning message is returned.
dist:
Options are dist = "Zipf", dist = "Zipf-Man", or dist = "Zeta" if the
data is distributed according to the Zipf, Zipf-Mandelbrot, or zeta
distribution, respectively.
Details
Zipf-Mandelbrot models are commonly used to model phenomena where the
frequencies of categorical data are approximately inversely proportional
to its rank in the frequency table.
Returns
-------
zmll returns a dataframe with coefficients
Note
This function may be updated in a future version of the package so as to
allow greater flexibility with the inputs.
References
Derek S. Young (2010). tolerance: An R Package for Estimating Tolerance
Intervals. Journal of Statistical Software, 36(5), 1-39.
URL http://www.jstatsoft.org/v36/i05/.
Mandelbrot, B. B. (1965), Information Theory and Psycholinguistics. In B.
B. Wolman and E. Nagel, editors. Scientific Psychology, Basic Books.
Zipf, G. K. (1949), Human Behavior and the Principle of Least Effort,
Hafner.
Zornig, P. and Altmann, G. (1995), Unified Representation of Zipf
Distributions, Computational Statistics and Data Analysis, 19, 461–473.
Examples
## Maximum likelihood estimation for randomly generated data
## from the Zipf, Zipf-Mandelbrot, and zeta distributions.
N = 30
s = 2
b = 5
Zdata = [6, 2, 1, 4, 8, 3, 3, 14, 2, 1, 21, 5, 18, 2, 30, 10, 8, 2,
11, 4, 16, 13, 17, 1, 7, 1, 1, 28, 19, 27, 2, 7, 7, 13, 1,
15, 1, 16, 9, 9, 7, 29, 3, 10, 3, 1, 20, 8, 12, 6, 11, 5, 1,
5, 23, 3, 3, 14, 6, 9, 1, 24, 5, 11, 15, 1, 5, 5, 4, 10, 1,
12, 1, 3, 4, 2, 9, 2, 1, 25, 6, 8, 2, 1, 1, 1, 4, 6, 7, 26,
10, 2, 1, 2, 17, 4, 3, 22, 8, 2]
## Zipf
zmll(x = Zdata, N = N, s = s, b = b, dist = 'Zipf')
## Zipf-Mandelbrot
zmll(x = Zdata, N = N, s = s, b = b, dist = 'Zipf-Man')
# Zeta
zmll(x = Zdata, N = np.inf, s = s, b = b, dist = 'Zeta')
'''
x = pd.DataFrame(x)
x = pd.DataFrame(x.value_counts()).T
x.columns = list(range(0,length(x.iloc[0])))
Ntemp = length(x.iloc[0])
x = x.reindex(np.argsort(x.columns),axis=1)
if dist == 'Zeta':
N = Ntemp
if N == None:
N = Ntemp
if N < Ntemp:
return "N cannot be smaller than the maximun number of categories in x!"
Nseq = np.array(list(range(1,N+1)))
zeros = np.zeros(N-length(x.iloc[0]))
zeros = [int(z) for z in zeros]
zeros = pd.DataFrame(zeros).T
zeros.columns = ['']*length(zeros.columns)
x.iloc[0] = x.iloc[0]
x = pd.concat([x,zeros],axis=1)
x = x.iloc[0].to_numpy()
if dist == 'Zipf':
def llzipf(s):
return sum(x*(s*np.log(Nseq)+np.log(sum(1/(Nseq)**s))))
s = opt.minimize(llzipf, x0=0, method = 'BFGS')['x']
vcov = opt.minimize(llzipf, x0=0, method = 'BFGS')['hess_inv'].ravel()
fit = pd.DataFrame({'s':s,'vcov':vcov})
if dist == "Zipf-Man":
def llzima(params = [s,b]):
return sum(x*(params[0]*np.log(Nseq+params[1])+np.log(sum(1/(Nseq+params[1])**params[0]))))
s = opt.minimize(llzima, x0=[0,0],method = 'L-BFGS-B')['x']
vcov = opt.minimize(llzima, x0=[0,0],method = 'L-BFGS-B')['hess_inv'].todense()
fit = pd.DataFrame([s,vcov]).T
fit.columns = ['Coefficients','vcov']
if dist == "Zeta":
def llzeta(s):
return sum(x*(s*np.log(Nseq)+np.log(zetafun(s))))
s = opt.minimize(llzeta, x0=1+1e-14, method = 'BFGS')['x']
vcov = opt.minimize(llzeta, x0=1+1e-14, method = 'BFGS')['hess_inv'].ravel()
fit = pd.DataFrame({'s':s,'vcov':vcov})
return fit
def zipftolint(x, m = None, N = None, alpha = 0.05, P = 0.99, side = 1, s = 1, b = 1, dist = 'Zipf', ties = False):
'''
Zipf-Mandelbrot Tolerance Intervals
Description
Provides 1-sided or 2-sided tolerance intervals for data distributed
according to Zipf, Zipf-Mandelbrot, and zeta distributions.
Usage
zipftolint(x, m = None, N = None, alpha = 0.05, P = 0.99, side = 1, s = 1,
b = 1, dist = ["Zipf", "Zipf-Man", "Zeta"], ties = False)
Parameters
----------
x: list
A vector of raw data or a table of counts which is distributed
according to a Zipf, Zipf-Mandelbrot, or zeta distribution. Do not
supply a vector of counts!
m: int, optional
The number of observations in a future sample for which the tolerance limits will be calculated. By default, m = NULL and, thus, m will be set equal to the original sample size.
N: int, optional
The number of categories when dist = "Zipf" or dist = "Zipf-Man". This
is not used when dist = "Zeta". If N = None, then N is estimated based
on the number of categories observed in the data.
alpha: float, optional
The level chosen such that 1-alpha is the confidence level.
P: float, optional
The proportion of the population to be covered by this tolerance
interval.
side: 1 or 2, optional
Whether a 1-sided or 2-sided tolerance interval is required
(determined by side = 1 or side = 2, respectively).
s: float, optional
The initial value to estimate the shape parameter in the zmll function.
b: float, optional
The initial value to estimate the second shape parameter in the zmll
function when dist = "Zipf-Man".
dist: string, optional
Options are dist = "Zipf", dist = "Zipf-Man", or dist = "Zeta" if the
data is distributed according to the Zipf, Zipf-Mandelbrot, or zeta
distribution, respectively.
ties: bool, optional
How to handle if there are other categories with the same frequency as
the category at the estimated tolerance limit. The default is False,
which does no correction. If TRUE, then the highest ranked
(i.e., lowest number) of the tied categories is selected for the lower
limit and the lowest ranked (i.e., highest number) of the tied
categories is selected for the upper limit.
Details
Zipf-Mandelbrot models are commonly used to model phenomena where the
frequencies of categorical data are approximately inversely proportional
to its rank in the frequency table. Zipf-Mandelbrot distributions are
heavily right-skewed distributions with a (relatively) large mass placed
on the first category. For most practical applications, one will typically
be interested in 1-sided upper bounds.
Returns
-------
zipftolint returns a data frame with the following items:
alpha
The specified significance level.
P
The proportion of the population covered by this tolerance
interval.
shat
MLE for the shape parameter s.
bhat
MLE for the shape parameter b when dist = "Zipf-Man".
1-sided.lower
The 1-sided lower tolerance bound. This is given only if side = 1.
1-sided.upper
The 1-sided upper tolerance bound. This is given only if side = 1.
2-sided.lower
The 2-sided lower tolerance bound. This is given only if side = 2.
2-sided.upper
The 2-sided upper tolerance bound. This is given only if side = 2.
Note
This function may be updated in a future version of the package so as to
allow greater flexibility with the inputs.
References
Derek S. Young (2010). tolerance: An R Package for Estimating Tolerance
Intervals. Journal of Statistical Software, 36(5), 1-39.
URL http://www.jstatsoft.org/v36/i05/.
Mandelbrot, B. B. (1965), Information Theory and Psycholinguistics.
In B. B. Wolman and E. Nagel, editors. Scientific Psychology,
Basic Books.
Young, D. S. (2013), Approximate Tolerance Limits for Zipf-Mandelbrot
Distributions, Physica A: Statistical Mechanics and its Applications,
392, 1702–1711.
Zipf, G. K. (1949), Human Behavior and the Principle of Least Effort,
Hafner.
Zornig, P. and Altmann, G. (1995), Unified Representation of Zipf
Distributions, Computational Statistics and Data Analysis, 19, 461–473.
Examples
## 95%/99% 1-sided tolerance intervals for the Zipf, Zipf-Mandelbrot, and
zeta distributions.
N = 30
s = 2
b = 5
# Zipf
Zdata = [6, 2, 1, 4, 8, 3, 3, 14, 2, 1, 21, 5, 18, 2, 30, 10, 8, 2,
11, 4, 16, 13, 17, 1, 7, 1, 1, 28, 19, 27, 2, 7, 7, 13, 1,
15, 1, 16, 9, 9, 7, 29, 3, 10, 3, 1, 20, 8, 12, 6, 11, 5, 1,
5, 23, 3, 3, 14, 6, 9, 1, 24, 5, 11, 15, 1, 5, 5, 4, 10, 1,
12, 1, 3, 4, 2, 9, 2, 1, 25, 6, 8, 2, 1, 1, 1, 4, 6, 7, 26,
10, 2, 1, 2, 17, 4, 3, 22, 8, 2]
zipftolint(x = Zdata, dist = 'Zipf', N = N, s=s, b=b)
# Zipf-Mandelbrot
Zdata = [2,2,2,2,2,2,2,2,3,3,2,2,2,2,2,3,2,3,2,3,2,3,2,2,3,3,3,4,5,6]
zipftolint(x = Zdata, dist = 'Zipf-Man',side = 1)
#Zeta
Zdata = [0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1]
zipftolint(x = Zdata, dist = 'Zeta', N = N, s=s, b=b,side = 2)
'''
if side != 1 and side != 2:
return 'Must specify a one-sided or two-sided procedure.'
if side == 2:
alpha = alpha/2
P = (P+1)/2
fit = zmll(x=x,N=N,s=s,b=b,dist=dist)
x = pd.DataFrame(x)
x = pd.DataFrame(x.value_counts()).T
x.columns = list(range(0,length(x.iloc[0])))