Debian Codegraph @

current/cardinality/LogLog16.java

raw symbols SCIP
1 package cardinality;
2
3 import shared.Parser;
4 import shared.Tools;
5 import structures.LongList;
6
7 /**
8 * @author Brian Bushnell
9 * @date Mar 6, 2020
10 *
11 */
12 public final class LogLog16 extends CardinalityTracker {
13
14 /*--------------------------------------------------------------*/
15 /*---------------- Initialization ----------------*/
16 /*--------------------------------------------------------------*/
17
18 /** Create a LogLog with default parameters */
19 LogLog16(){
20 this(2048, 31, -1, 0);
21 }
22
23 /** Create a LogLog with parsed parameters */
24 LogLog16(Parser p){
25 super(p);
26 maxArray=new char[buckets];
27 }
28
29 /**
30 * Create a LogLog with specified parameters
31 * @param buckets_ Number of buckets (counters)
32 * @param k_ Kmer length
33 * @param seed Random number generator seed; -1 for a random seed
34 * @param minProb_ Ignore kmers with under this probability of being correct
35 */
36 LogLog16(int buckets_, int k_, long seed, float minProb_){
37 super(buckets_, k_, seed, minProb_);
38 maxArray=new char[buckets];
39 }
40
41 /*--------------------------------------------------------------*/
42 /*---------------- Methods ----------------*/
43 /*--------------------------------------------------------------*/
44
45 //Restores floating point to integer
46 private long restore(int score){
47 long lowbits=(~score)&mask;
48 int leading=(int)(score>>>mantissabits);
49 long mantissa=(1L<<mantissabits)|lowbits;
50 int shift=wordlen-leading-mantissabits-1;
51 long original=mantissa<<shift;
52 return original;
53 }
54
55 @Override
56 public final long cardinality(){
57 double difSum=0;
58 double hSum=0;
59 double gSum=0;
60 double rSum=0;
61 double estLogSum=0;
62 int count=0;
63 LongList list=new LongList(buckets);
64
65 for(int i=0; i<maxArray.length; i++){
66 int max=maxArray[i];
67 long val=restore(max);
68 if(max>0 && val>0){
69 long dif=val;
70 difSum+=dif;
71 hSum+=1.0/Tools.max(1, dif);
72 gSum+=Math.log(Tools.max(1, dif));
73 rSum+=Math.sqrt(dif);
74 count++;
75 double est=2*(Long.MAX_VALUE/(double)dif)*SKIPMOD;
76 estLogSum+=Math.log(est);
77 list.add(dif);
78 }
79 }
80 final int div=Tools.max(count, 1);//Could be count or buckets but one causes problems
81 final double mean=difSum/div;
82 double hmean=hSum/div;
83 double gmean=gSum/div;
84 double rmean=rSum/div;
85 hmean=1.0/hmean;
86 gmean=Math.exp(gmean);
87 rmean=rmean*rmean;
88 list.sort();
89 final long median=list.median();
90 final double mwa=list.medianWeightedAverage();
91
92 //What to use as the value from the counters
93 final double proxy=(USE_MEAN ? mean : USE_MEDIAN ? median : USE_MWA ? mwa : USE_HMEAN ? hmean : USE_GMEAN ? gmean : mean);
94
95 final double estimatePerSet=2*(Long.MAX_VALUE/proxy)*SKIPMOD;
96 final double total=estimatePerSet*div*((count+buckets)/(float)(buckets+buckets));
97
98 final double estSum=div*Math.exp(estLogSum/(Tools.max(div, 1)));
99 double medianEst=2*(Long.MAX_VALUE/(double)median)*SKIPMOD*div;
100
101 // new Exception().printStackTrace();
102
103 // System.err.println(maxArray);
104 //// Overall, it looks like "total" is the best, then "estSum", then "medianEst" is the worst, in terms of variance.
105 // System.err.println("difSum="+difSum+", count="+count+", mean="+mean+", est="+estimatePerSet+", total="+(long)total);
106 // System.err.println("estSum="+(long)estSum+", median="+median+", medianEst="+(long)medianEst);
107
108 long cardinality=(long)(total);
109 lastCardinality=cardinality;
110 return cardinality;
111 }
112
113 @Override
114 public final void add(CardinalityTracker log){
115 assert(log.getClass()==this.getClass());
116 add((LogLog16)log);
117 }
118
119 public void add(LogLog16 log){
120 if(maxArray!=log.maxArray){
121 for(int i=0; i<buckets; i++){
122 maxArray[i]=Tools.max(maxArray[i], log.maxArray[i]);
123 }
124 }
125 }
126
127 @Override
128 public void hashAndStore(final long number){
129 // if(number%SKIPMOD!=0){return;} //Slows down moderately
130 long key=number;
131
132 // key=hash(key, tables[((int)number)&numTablesMask]);
133
134 key=Tools.hash64shift(key);
135 // if(key<0 || key>maxHashedValue){return;}//Slows things down by 50% lot, mysteriously
136 int leading=Long.numberOfLeadingZeros(key)&63;//mask is used to keep number in 6 bits
137
138 // counts[leading]++;
139
140 // if(leading<3){return;}//Speeds up by 20%, even more at 4. Slows at 2.
141
142 int shift=wordlen-leading-mantissabits-1;
143
144 int score=(leading<<mantissabits)+(int)((~(key>>>shift))&mask);
145 // assert(false) : "\n"+Long.toBinaryString(key)+", leading="+leading+", shift="+shift+"\n"+Long.toBinaryString(score);
146
147 //+"\n"+score+"\n"+restore(score);
148
149 // final int bucket=(int)((number&Integer.MAX_VALUE)%buckets);
150 final int bucket=(int)(key&bucketMask);
151
152 int newValue=Tools.max(score, maxArray[bucket]);
153 assert(newValue>=0 && newValue<=Character.MAX_VALUE) : newValue;
154 maxArray[bucket]=(char)newValue;
155 }
156
157 @Override
158 public final float[] compensationFactorLogBucketsArray(){
159 return null;
160 }
161
162 /*--------------------------------------------------------------*/
163 /*---------------- Fields ----------------*/
164 /*--------------------------------------------------------------*/
165
166 private final char[] maxArray;
167
168 /*--------------------------------------------------------------*/
169 /*---------------- Statics ----------------*/
170 /*--------------------------------------------------------------*/
171
172 public static void setMantissaBits(int x){
173 assert(x>=0 && x<25);
174 assert(x+6<32);
175 mantissabits=x;
176 mask=(1<<mantissabits)-1;
177 }
178
179 private static final int wordlen=64;
180
181 /** Precision or mantissa bits.
182 * This should not be changed. As long as it is >10 the result will be accurate.
183 * At low values like 2 the cardinality estimate becomes too high due to a loss of precision,
184 * and would need a fixed multiplier.
185 */
186 private static int mantissabits=10;//10 is the max possible
187 private static int mask=(1<<mantissabits)-1;
188
189 }