1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.stat.descriptive.rank;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.NoSuchElementException;
28 import java.util.UUID;
29
30 import org.hipparchus.exception.LocalizedCoreFormats;
31 import org.hipparchus.exception.MathIllegalArgumentException;
32 import org.hipparchus.exception.MathIllegalStateException;
33 import org.hipparchus.exception.NullArgumentException;
34 import org.hipparchus.random.RandomGenerator;
35 import org.hipparchus.random.Well19937c;
36 import org.hipparchus.stat.StatUtils;
37 import org.hipparchus.stat.descriptive.AbstractStorelessUnivariateStatistic;
38 import org.hipparchus.stat.descriptive.AggregatableStatistic;
39 import org.hipparchus.stat.descriptive.StorelessUnivariateStatistic;
40 import org.hipparchus.util.FastMath;
41 import org.hipparchus.util.MathArrays;
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 public class RandomPercentile
89 extends AbstractStorelessUnivariateStatistic implements StorelessUnivariateStatistic,
90 AggregatableStatistic<RandomPercentile>, Serializable {
91
92
93 public static final double DEFAULT_EPSILON = 1e-4;
94
95 private static final long serialVersionUID = 1L;
96
97 private final int s;
98
99 private final int h;
100
101 private final BufferMap bufferMap;
102
103 private final double epsilon;
104
105 private final RandomGenerator randomGenerator;
106
107 private long n;
108
109 private Buffer currentBuffer;
110
111
112
113
114
115
116
117
118
119 public RandomPercentile(double epsilon, RandomGenerator randomGenerator) {
120 if (epsilon <= 0) {
121 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
122 epsilon, 0);
123 }
124 this.h = (int) FastMath.ceil(log2(1/epsilon));
125 this.s = (int) FastMath.ceil(FastMath.sqrt(log2(1/epsilon)) / epsilon);
126 this.randomGenerator = randomGenerator;
127 bufferMap = new BufferMap(h + 1, s, randomGenerator);
128 currentBuffer = bufferMap.create(0);
129 this.epsilon = epsilon;
130 }
131
132
133
134
135
136
137
138
139 public RandomPercentile(RandomGenerator randomGenerator) {
140 this(DEFAULT_EPSILON, randomGenerator);
141 }
142
143
144
145
146
147
148
149
150 public RandomPercentile(double epsilon) {
151 this(epsilon, new Well19937c());
152 }
153
154
155
156
157
158
159 public RandomPercentile() {
160 this(DEFAULT_EPSILON, new Well19937c());
161 }
162
163
164
165
166
167
168
169
170
171 public RandomPercentile(RandomPercentile original) {
172 super();
173 this.h = original.h;
174 this.n = original.n;
175 this.s = original.s;
176 this.epsilon = original.epsilon;
177 this.bufferMap = new BufferMap(original.bufferMap);
178 this.randomGenerator = original.randomGenerator;
179 Iterator<Buffer> iterator = bufferMap.iterator();
180 Buffer current = null;
181 Buffer curr = null;
182
183 while (current == null && iterator.hasNext()) {
184 curr = iterator.next();
185 if (curr.hasCapacity()) {
186 current = curr;
187 }
188 }
189
190
191
192 this.currentBuffer = current == null ? curr : current;
193 }
194
195 @Override
196 public long getN() {
197 return n;
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
212 public double evaluate(final double percentile, final double[] values, final int begin, final int length)
213 throws MathIllegalArgumentException {
214 if (MathArrays.verifyValues(values, begin, length)) {
215 RandomPercentile randomPercentile = new RandomPercentile(this.epsilon,
216 this.randomGenerator);
217 randomPercentile.incrementAll(values, begin, length);
218 return randomPercentile.getResult(percentile);
219 }
220 return Double.NaN;
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234 @Override
235 public double evaluate(final double[] values, final int begin, final int length) {
236 return evaluate(50d, values, begin, length);
237 }
238
239
240
241
242
243
244
245
246
247
248 public double evaluate(final double percentile, final double[] values) {
249 return evaluate(percentile, values, 0, values.length);
250 }
251
252 @Override
253 public RandomPercentile copy() {
254 return new RandomPercentile(this);
255 }
256
257 @Override
258 public void clear() {
259 n = 0;
260 bufferMap.clear();
261 currentBuffer = bufferMap.create(0);
262 }
263
264
265
266
267 @Override
268 public double getResult() {
269 return getResult(50d);
270
271 }
272
273
274
275
276
277
278
279
280 public double getResult(double percentile) {
281 if (percentile > 100 || percentile < 0) {
282 throw new MathIllegalArgumentException(LocalizedCoreFormats.OUT_OF_RANGE,
283 percentile, 0, 100);
284 }
285
286 final double q = percentile / 100;
287
288 double min = Double.POSITIVE_INFINITY;
289 double max = Double.NEGATIVE_INFINITY;
290 double bMin;
291 double bMax;
292 for (Buffer buffer : bufferMap) {
293 bMin = buffer.min();
294 if (bMin < min) {
295 min = bMin;
296 }
297 bMax = buffer.max();
298 if (bMax > max) {
299 max = bMax;
300 }
301 }
302
303
304 if (Double.compare(q, 0d) == 0 || n == 1) {
305 return min;
306 }
307 if (Double.compare(q, 1) == 0) {
308 return max;
309 }
310 if (n == 0) {
311 return Double.NaN;
312 }
313
314
315
316 if (bufferMap.halfEmpty()) {
317 return new Percentile(percentile).evaluate(bufferMap.levelZeroData());
318 }
319
320
321 final double targetRank = q * n;
322
323
324 double estimate = min + q * (max - min);
325 double estimateRank = getRank(estimate);
326 double lower;
327 double upper;
328 if (estimateRank > targetRank) {
329 upper = estimate;
330 lower = min;
331 } else if (estimateRank < targetRank) {
332 lower = estimate;
333 upper = max;
334 } else {
335 return estimate;
336 }
337 final double eps = epsilon / 2;
338 final double rankTolerance = eps * n;
339 final double minWidth = eps / n;
340 double intervalWidth = FastMath.abs(upper - lower);
341 while (FastMath.abs(estimateRank - targetRank) > rankTolerance && intervalWidth > minWidth) {
342 if (estimateRank > targetRank) {
343 upper = estimate;
344 } else {
345 lower = estimate;
346 }
347 intervalWidth = upper - lower;
348 estimate = lower + intervalWidth / 2;
349 estimateRank = getRank(estimate);
350 }
351 return estimate;
352 }
353
354
355
356
357
358
359
360
361 public double getRank(double value) {
362 double rankSum = 0;
363 for (Buffer buffer : bufferMap) {
364 rankSum += buffer.rankOf(value) * FastMath.pow(2, buffer.level);
365 }
366 return rankSum;
367 }
368
369
370
371
372
373
374
375
376
377 public double getQuantileRank(double value) {
378 return getRank(value) / getN();
379 }
380
381 @Override
382 public void increment(double d) {
383 n++;
384 if (!currentBuffer.hasCapacity()) {
385
386 if (bufferMap.canCreate()) {
387 final int level = (int) Math.ceil(Math.max(0, log2(n/(s * FastMath.pow(2, h - 1)))));
388 currentBuffer = bufferMap.create(level);
389 } else {
390 currentBuffer = bufferMap.merge();
391 }
392 }
393 currentBuffer.consume(d);
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 private static class Buffer implements Serializable {
419
420 private static final long serialVersionUID = 1L;
421
422 private final int size;
423
424 private final double[] data;
425
426 private final RandomGenerator randomGenerator;
427
428 private int level;
429
430 private long blockSize;
431
432 private int next;
433
434 private long consumed;
435
436 private long nextToTake;
437
438 private final UUID id;
439
440
441
442
443
444
445
446
447
448 Buffer(int size, int level, RandomGenerator randomGenerator) {
449 this.size = size;
450 data = new double[size];
451 this.level = level;
452 this.randomGenerator = randomGenerator;
453 this.id = UUID.randomUUID();
454 computeBlockSize();
455 }
456
457
458
459
460 private void computeBlockSize() {
461 if (level == 0) {
462 blockSize = 1;
463 } else {
464 long product = 1;
465 for (int i = 0; i < level; i++) {
466 product *= 2;
467 }
468 blockSize = product;
469 }
470 if (blockSize > 1) {
471 nextToTake = randomGenerator.nextLong(blockSize);
472 }
473 }
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491 public void consume(double value) {
492 if (consumed == nextToTake) {
493 data[next] = value;
494 next++;
495 }
496 consumed++;
497 if (consumed == blockSize) {
498 if (next == size) {
499 Arrays.sort(data);
500 } else {
501 consumed = 0;
502 if (blockSize > 1) {
503 nextToTake = randomGenerator.nextLong(blockSize);
504 }
505 }
506 }
507 }
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526 public void mergeWith(Buffer other) {
527
528 if (this.hasCapacity() || other.hasCapacity() || other.level != this.level) {
529 throw new MathIllegalArgumentException(LocalizedCoreFormats.INTERNAL_ERROR);
530 }
531
532 for (int i = 0; i < size; i++) {
533 if (randomGenerator.nextBoolean()) {
534 data[i] = other.data[i];
535 }
536 }
537
538 Arrays.sort(data);
539
540 other.setLevel(level + 1);
541 this.setLevel(level + 1);
542
543 other.clear();
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 public void mergeInto(Buffer higher) {
570
571 if (this.size != higher.size || this.hasCapacity() || higher.hasCapacity() ||
572 this.level >= higher.level) {
573 throw new MathIllegalArgumentException(LocalizedCoreFormats.INTERNAL_ERROR);
574 }
575 final int levelDifference = higher.level - this.level;
576 int m = 1;
577 for (int i = 0; i < levelDifference; i++) {
578 m *= 2;
579 }
580
581
582 for (int i = 0; i < size; i++) {
583
584 if (randomGenerator.nextInt(m + 1) == 0) {
585 higher.data[i] = data[i];
586 }
587 }
588
589 Arrays.sort(higher.data);
590 }
591
592
593
594
595 public boolean hasCapacity() {
596
597
598 return next < size || consumed < blockSize;
599 }
600
601
602
603
604
605
606 public void setLevel(int level) {
607 this.level = level;
608 }
609
610
611
612
613 public void clear() {
614 consumed = 0;
615 next = 0;
616 computeBlockSize();
617 }
618
619
620
621
622
623
624 public double[] getData() {
625 final double[] out = new double[next];
626 System.arraycopy(data, 0, out, 0, next);
627 return out;
628 }
629
630
631
632
633
634
635
636 public int rankOf(double value) {
637 int ret = 0;
638 if (!hasCapacity()) {
639 ret = Arrays.binarySearch(data, value);
640 if (ret < 0) {
641 return -ret - 1;
642 } else {
643 return ret;
644 }
645 } else {
646 for (int i = 0; i < next; i++) {
647 if (data[i] < value) {
648 ret++;
649 }
650 }
651 return ret;
652 }
653 }
654
655
656
657
658 public double min() {
659 if (!hasCapacity()) {
660 return data[0];
661 } else {
662 return StatUtils.min(getData());
663 }
664 }
665
666
667
668
669 public double max() {
670 if (!hasCapacity()) {
671 return data[data.length - 1];
672 } else {
673 return StatUtils.max(getData());
674 }
675 }
676
677
678
679
680 public int getLevel() {
681 return level;
682 }
683
684
685
686
687 public UUID getId() {
688 return id;
689 }
690 }
691
692
693
694
695
696
697
698 private static double log2(double x) {
699 return Math.log(x) / Math.log(2);
700 }
701
702
703
704
705
706
707 private static class BufferMap implements Iterable<Buffer>, Serializable {
708
709 private static final long serialVersionUID = 1L;
710
711 private final int capacity;
712
713 private final RandomGenerator randomGenerator;
714
715 private int count;
716
717 private final int bufferSize;
718
719 private final Map<Integer,List<Buffer>> registry;
720
721 private int maxLevel;
722
723
724
725
726
727
728
729
730
731 BufferMap(int capacity, int bufferSize, RandomGenerator randomGenerator) {
732 this.bufferSize = bufferSize;
733 this.capacity = capacity;
734 this.randomGenerator = randomGenerator;
735 this.registry = new HashMap<>();
736 }
737
738
739
740
741
742
743 BufferMap(BufferMap original) {
744 super();
745 this.bufferSize = original.bufferSize;
746 this.capacity = original.capacity;
747 this.count = 0;
748 this.randomGenerator = original.randomGenerator;
749 this.registry = new HashMap<>();
750 for (Buffer current : original) {
751
752 final Buffer newCopy = create(current.getLevel());
753
754 final double[] data = current.getData();
755 for (double value : data) {
756 newCopy.consume(value);
757 }
758 }
759 }
760
761
762
763
764
765
766
767
768
769
770
771
772 public Buffer create(int level) {
773 if (!canCreate()) {
774 return null;
775 }
776 count++;
777 Buffer buffer = new Buffer(bufferSize, level, randomGenerator);
778 List<Buffer> bufferList = registry.computeIfAbsent(level, k -> new ArrayList<>());
779 bufferList.add(buffer);
780 if (level > maxLevel) {
781 maxLevel = level;
782 }
783 return buffer;
784 }
785
786
787
788
789
790
791 public boolean canCreate() {
792 return count < capacity;
793 }
794
795
796
797
798
799
800
801
802
803
804
805
806
807 public boolean halfEmpty() {
808 return count * 2 < capacity &&
809 registry.size() == 1 &&
810 registry.containsKey(0);
811 }
812
813
814
815
816
817
818 public double[] levelZeroData() {
819 List<Buffer> levelZeroBuffers = registry.get(0);
820
821 int length = 0;
822 for (Buffer buffer : levelZeroBuffers) {
823 if (!buffer.hasCapacity()) {
824 length += buffer.size;
825 } else {
826 length += buffer.next;
827 }
828 }
829
830 int pos = 0;
831 int currLen;
832 final double[] out = new double[length];
833 for (Buffer buffer : levelZeroBuffers) {
834 if (!buffer.hasCapacity()) {
835 currLen = buffer.size;
836 } else {
837 currLen = buffer.next;
838 }
839 System.arraycopy(buffer.data, 0, out, pos, currLen);
840 pos += currLen;
841 }
842 return out;
843 }
844
845
846
847
848
849
850
851
852 public Buffer merge() {
853 int l = 0;
854 List<Buffer> mergeCandidates = null;
855
856 while (mergeCandidates == null && l <= maxLevel) {
857 final List<Buffer> bufferList = registry.get(l);
858 if (bufferList != null && bufferList.size() > 1) {
859 mergeCandidates = bufferList;
860 } else {
861 l++;
862 }
863 }
864 if (mergeCandidates == null) {
865
866 throw new MathIllegalStateException(LocalizedCoreFormats.INTERNAL_ERROR);
867 }
868 Buffer buffer1 = mergeCandidates.get(0);
869 Buffer buffer2 = mergeCandidates.get(1);
870
871 mergeCandidates.remove(0);
872 mergeCandidates.remove(0);
873
874 if (registry.get(l).isEmpty()) {
875 registry.remove(l);
876 }
877
878 buffer1.mergeWith(buffer2);
879
880
881 register(buffer1);
882 register(buffer2);
883
884
885 return buffer2;
886 }
887
888
889
890
891 public void clear() {
892 for (List<Buffer> bufferList : registry.values()) {
893 bufferList.clear();
894 }
895 registry.clear();
896 count = 0;
897 }
898
899
900
901
902
903
904 public void register(Buffer buffer) {
905 final int level = buffer.getLevel();
906 List<Buffer> list = registry.get(level);
907 if (list == null) {
908 list = new ArrayList<>();
909 registry.put(level, list);
910 if (level > maxLevel) {
911 maxLevel = level;
912 }
913 }
914 list.add(buffer);
915 }
916
917
918
919
920
921
922
923 public void deRegister(Buffer buffer) {
924 final Iterator<Buffer> iterator = registry.get(buffer.getLevel()).iterator();
925 while (iterator.hasNext()) {
926 if (iterator.next().getId().equals(buffer.getId())) {
927 iterator.remove();
928 return;
929 }
930 }
931 throw new MathIllegalStateException(LocalizedCoreFormats.INTERNAL_ERROR);
932 }
933
934
935
936
937
938 @Override
939 public Iterator<Buffer> iterator() {
940 return new Iterator<Buffer>() {
941
942
943 private final Iterator<Integer> levelIterator = registry.keySet().iterator();
944
945
946 private List<Buffer> currentList = registry.get(levelIterator.next());
947
948
949 private Iterator<Buffer> bufferIterator =
950 currentList == null ? null : currentList.iterator();
951
952 @Override
953 public boolean hasNext() {
954 if (bufferIterator == null) {
955 return false;
956 }
957 if (bufferIterator.hasNext()) {
958 return true;
959 }
960
961 if (levelIterator.hasNext()) {
962 List<Buffer> currentList = registry.get(levelIterator.next());
963 bufferIterator = currentList.iterator();
964 return true;
965 } else {
966
967 bufferIterator = null;
968 return false;
969 }
970 }
971
972 @Override
973 public Buffer next() {
974 if (hasNext()) {
975 return bufferIterator.next();
976 }
977 throw new NoSuchElementException();
978 }
979
980 @Override
981 public void remove() {
982 throw new UnsupportedOperationException();
983 }
984 };
985 }
986
987
988
989
990
991
992
993
994 public void absorb(BufferMap other) {
995
996 boolean full = true;
997 for (Buffer buffer : other) {
998 if (buffer.hasCapacity()) {
999 full = false;
1000 }
1001 register(buffer);
1002 count++;
1003 }
1004
1005 while (count > capacity || (count == capacity && full)) {
1006 mergeUp();
1007 count--;
1008 }
1009 }
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021 public void mergeUp() {
1022
1023
1024
1025
1026 Iterator<Buffer> bufferIterator = iterator();
1027 Buffer first = null;
1028 Buffer second = null;
1029 while ((first == null || second == null) && bufferIterator.hasNext()) {
1030 Buffer buffer = bufferIterator.next();
1031 if (!buffer.hasCapacity()) {
1032 if (first == null) {
1033 first = buffer;
1034 } else {
1035 second = buffer;
1036 }
1037 }
1038 }
1039 if (first == null || second == null || first.level > second.level) {
1040 throw new MathIllegalStateException(LocalizedCoreFormats.INTERNAL_ERROR);
1041 }
1042
1043
1044 if (first.getLevel() == second.getLevel()) {
1045 deRegister(first);
1046 deRegister(second);
1047 second.mergeWith(first);
1048 register(second);
1049 } else {
1050 deRegister(first);
1051 first.mergeInto(second);
1052 }
1053 }
1054 }
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066 public double reduce(double percentile, Collection<RandomPercentile> aggregates) {
1067 if (percentile > 100 || percentile < 0) {
1068 throw new MathIllegalArgumentException(LocalizedCoreFormats.OUT_OF_RANGE,
1069 percentile, 0, 100);
1070 }
1071
1072
1073
1074
1075
1076 Iterator<RandomPercentile> iterator = aggregates.iterator();
1077 boolean small = true;
1078 while (small && iterator.hasNext()) {
1079 small = iterator.next().bufferMap.halfEmpty();
1080 }
1081 if (small) {
1082 iterator = aggregates.iterator();
1083 double[] combined = {};
1084 while (iterator.hasNext()) {
1085 combined = MathArrays.concatenate(combined, iterator.next().bufferMap.levelZeroData());
1086 }
1087 final Percentile exactP = new Percentile(percentile);
1088 return exactP.evaluate(combined);
1089 }
1090
1091
1092
1093
1094
1095 double min = Double.POSITIVE_INFINITY;
1096 double max = Double.NEGATIVE_INFINITY;
1097 double combinedN = 0;
1098 iterator = aggregates.iterator();
1099 while (iterator.hasNext()) {
1100 final RandomPercentile curr = iterator.next();
1101 final double curMin = curr.getResult(0);
1102 final double curMax = curr.getResult(100);
1103 if (curMin < min) {
1104 min = curMin;
1105 }
1106 if (curMax > max) {
1107 max = curMax;
1108 }
1109 combinedN += curr.getN();
1110 }
1111
1112 final double q = percentile / 100;
1113
1114 if (Double.compare(q, 0d) == 0) {
1115 return min;
1116 }
1117 if (Double.compare(q, 1) == 0) {
1118 return max;
1119 }
1120
1121
1122 final double targetRank = q * combinedN;
1123
1124
1125
1126 double estimate = min + q * (max - min);
1127 double estimateRank = getAggregateRank(estimate, aggregates);
1128 double lower;
1129 double upper;
1130 if (estimateRank > targetRank) {
1131 upper = estimate;
1132 lower = min;
1133 } else if (estimateRank < targetRank) {
1134 lower = estimate;
1135 upper = max;
1136 } else {
1137 return estimate;
1138 }
1139 final double eps = epsilon / 2;
1140 double intervalWidth = FastMath.abs(upper - lower);
1141 while (FastMath.abs(estimateRank / combinedN - q) > eps && intervalWidth > eps / combinedN) {
1142 if (estimateRank == targetRank) {
1143 return estimate;
1144 }
1145 if (estimateRank > targetRank) {
1146 upper = estimate;
1147 } else {
1148 lower = estimate;
1149 }
1150 intervalWidth = FastMath.abs(upper - lower);
1151 estimate = lower + intervalWidth / 2;
1152 estimateRank = getAggregateRank(estimate, aggregates);
1153 }
1154 return estimate;
1155 }
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165 public double getAggregateRank(double value, Collection<RandomPercentile> aggregates) {
1166 double result = 0;
1167 for (RandomPercentile aggregate : aggregates) {
1168 result += aggregate.getRank(value);
1169 }
1170 return result;
1171 }
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183 public double getAggregateQuantileRank(double value, Collection<RandomPercentile> aggregates) {
1184 return getAggregateRank(value, aggregates) / getAggregateN(aggregates);
1185 }
1186
1187
1188
1189
1190
1191
1192
1193 public double getAggregateN(Collection<RandomPercentile> aggregates) {
1194 double result = 0;
1195 for (RandomPercentile aggregate : aggregates) {
1196 result += aggregate.getN();
1197 }
1198 return result;
1199 }
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214 @Override
1215 public void aggregate(RandomPercentile other)
1216 throws NullArgumentException {
1217 if (other == null) {
1218 throw new NullArgumentException();
1219 }
1220 if (other.s != s) {
1221 throw new MathIllegalArgumentException(LocalizedCoreFormats.INTERNAL_ERROR);
1222 }
1223 bufferMap.absorb(other.bufferMap);
1224 n += other.n;
1225 }
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238 public static long maxValuesRetained(double epsilon) {
1239 if (epsilon >= 1) {
1240 throw new MathIllegalArgumentException(
1241 LocalizedCoreFormats.NUMBER_TOO_LARGE_BOUND_EXCLUDED, epsilon, 1);
1242 }
1243 if (epsilon <= 0) {
1244 throw new MathIllegalArgumentException(
1245 LocalizedCoreFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED, epsilon, 0);
1246 }
1247 final long h = (long) FastMath.ceil(log2(1/epsilon));
1248 final long s = (long) FastMath.ceil(FastMath.sqrt(log2(1/epsilon)) / epsilon);
1249 return (h+1) * s;
1250 }
1251 }