Debian Codegraph @

current/cardinality/LogLog8.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 LogLog8 extends CardinalityTracker {
13
14 /*--------------------------------------------------------------*/
15 /*---------------- Initialization ----------------*/
16 /*--------------------------------------------------------------*/
17
18 /** Create a LogLog with default parameters */
19 LogLog8(){
20 this(2048, 31, -1, 0);
21 }
22
23 /** Create a LogLog with parsed parameters */
24 LogLog8(Parser p){
25 super(p);
26 maxArray=new byte[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 LogLog8(int buckets_, int k_, long seed, float minProb_){
37 super(buckets_, k_, seed, minProb_);
38 maxArray=new byte[buckets];
39 }
40
41 /*--------------------------------------------------------------*/
42 /*---------------- Methods ----------------*/
43 /*--------------------------------------------------------------*/
44
45 // /** Restores floating point to integer */
46 // private long restore2(int score, int bucket){
47 // final int bucketBits=Integer.numberOfTrailingZeros(buckets);
48 // int leading=score;
49 // long mantissa=(1L<<bucketBits)|bucket;
50 // int shift=wordlen-leading-bucketBits-1;
51 // long original=mantissa<<shift;
52 // return original;
53 // }
54
55 /** Restores floating point to integer */
56 private long restore(int score){
57 int leading=score;
58 long mantissa=1;
59 int shift=64-leading-1;
60 long original=mantissa<<shift;
61 return original;
62 }
63
64 @Override
65 public final long cardinality(){
66 double difSum=0;
67 double estLogSum=0;
68 int count=0;
69 LongList list=new LongList(buckets);
70
71 for(int i=0; i<maxArray.length; i++){
72 int max=maxArray[i];
73 long val=restore(max);
74 if(max>0 && val>0){
75 final long dif=val;
76 difSum+=dif;
77 count++;
78 double est=2*(Long.MAX_VALUE/(double)dif)*SKIPMOD;
79 estLogSum+=Math.log(est);
80 list.add(dif);
81 }
82 }
83
84 final int div=count;//Could also be count be that causes problems
85 final double mean=difSum/Tools.max(div, 1);
86 list.sort();
87 final long median=list.median();
88 final double mwa=list.medianWeightedAverage();
89
90 //What to use as the value from the counters
91 final double proxy=mean;
92
93 // assert(false) : mean+", "+median+", "+difSum+", "+list;
94
95 final double estimatePerSet=2*(Long.MAX_VALUE/proxy)*SKIPMOD;
96
97 //12000000 16635460.58 //8k sims, 100k reads, 128k buckets
98 //16635789.16 //16k sims, 100k reads, 128k buckets
99 //16635901.26 //64k sims
100 //16635476.90 //128k
101 //16635631.18 //256k
102 //16635645.59 //512k
103
104 //0.72134379048167576520498945661274
105 //0.72134281774212774758647730006006
106 final double conversionFactor=0.7213428177;
107 final double total=conversionFactor*estimatePerSet*div*((count+buckets)/(float)(buckets+buckets));
108
109 // final double estSum=div*Math.exp(estLogSum/(Tools.max(div, 1)));
110 // double medianEst=2*(Long.MAX_VALUE/(double)median)*SKIPMOD*div;
111
112 // new Exception().printStackTrace();
113
114 // System.err.println(maxArray);
115 //// Overall, it looks like "total" is the best, then "estSum", then "medianEst" is the worst, in terms of variance.
116 // System.err.println("difSum="+difSum+", count="+count+", mean="+mean+", est="+estimatePerSet+", total="+(long)total);
117 // System.err.println("estSum="+(long)estSum+", median="+median+", medianEst="+(long)medianEst);
118
119 long cardinality=(long)(total);
120 lastCardinality=cardinality;
121 return cardinality;
122 }
123
124 @Override
125 public final void add(CardinalityTracker log){
126 assert(log.getClass()==this.getClass());
127 add((LogLog8)log);
128 }
129
130 public void add(LogLog8 log){
131 if(maxArray!=log.maxArray){
132 for(int i=0; i<buckets; i++){
133 maxArray[i]=Tools.max(maxArray[i], log.maxArray[i]);
134 }
135 }
136 }
137
138 @Override
139 public void hashAndStore(final long number){
140 // if(number%SKIPMOD!=0){return;} //Slows down moderately
141 long key=number;
142
143 // key=hash(key, tables[((int)number)&numTablesMask]);
144
145 key=Tools.hash64shift(key);
146 // if(key<0 || key>maxHashedValue){return;}//Slows things down by 50% lot, mysteriously
147 byte leading=(byte)(Long.numberOfLeadingZeros(key)&63);//mask is used to keep number in 6 bits
148
149 // counts[leading]++;
150
151 // if(leading<3){return;}//Slows things down slightly
152 // final int bucket=(int)((number&Integer.MAX_VALUE)%buckets);
153 final int bucket=(int)(key&bucketMask);
154
155 maxArray[bucket]=Tools.max(leading, maxArray[bucket]);
156 }
157
158 @Override
159 public final float[] compensationFactorLogBucketsArray(){
160 return null;
161 }
162
163 /*--------------------------------------------------------------*/
164 /*---------------- Fields ----------------*/
165 /*--------------------------------------------------------------*/
166
167 /** Maintains state. These are the actual buckets. */
168 private final byte[] maxArray;
169
170 }