|
| 1 | +package com.clearspring.analytics.stream.frequency; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.ByteArrayOutputStream; |
| 5 | +import java.io.DataInputStream; |
| 6 | +import java.io.DataOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.Random; |
| 9 | + |
| 10 | +/** |
| 11 | + * Count-Min Sketch datastructure. |
| 12 | + * An Improved Data Stream Summary: The Count-Min Sketch and its Applications |
| 13 | + * http://www.eecs.harvard.edu/~michaelm/CS222/countmin.pdf |
| 14 | + */ |
| 15 | +public class CountMinSketch implements IFrequency |
| 16 | +{ |
| 17 | + public static final long PRIME_MODULUS = (1L << 31) - 1; |
| 18 | + private int depth; |
| 19 | + private int width; |
| 20 | + private long[][] table; |
| 21 | + private long[] hashA; |
| 22 | + private long size; |
| 23 | + private double eps; |
| 24 | + private double confidence; |
| 25 | + |
| 26 | + private CountMinSketch() |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + public CountMinSketch(int depth, int width, int seed) |
| 31 | + { |
| 32 | + this.depth = depth; |
| 33 | + this.width = width; |
| 34 | + this.eps = 2.0 / width; |
| 35 | + this.confidence = 1 - 1 / Math.pow(2, depth); |
| 36 | + initTablesWith(depth, width, seed); |
| 37 | + } |
| 38 | + |
| 39 | + public CountMinSketch(double epsOfTotalCount, double confidence, int seed) |
| 40 | + { |
| 41 | + // 2/w = eps ; w = 2/eps |
| 42 | + // 1/2^depth <= 1-confidence ; depth >= -log2 (1-confidence) |
| 43 | + this.eps = epsOfTotalCount; |
| 44 | + this.confidence = confidence; |
| 45 | + this.width = (int) Math.ceil(2 / epsOfTotalCount); |
| 46 | + this.depth = (int) Math.ceil(-Math.log(1 - confidence) / Math.log(2)); |
| 47 | + initTablesWith(depth, width, seed); |
| 48 | + } |
| 49 | + |
| 50 | + private void initTablesWith(int depth, int width, int seed) |
| 51 | + { |
| 52 | + this.table = new long[depth][width]; |
| 53 | + this.hashA = new long[depth]; |
| 54 | + Random r = new Random(seed); |
| 55 | + // We're using a linear hash functions |
| 56 | + // of the form (a*x+b) mod p. |
| 57 | + // a,b are chosen independently for each hash function. |
| 58 | + // However we can set b = 0 as all it does is shift the results |
| 59 | + // without compromising their uniformity or independence with |
| 60 | + // the other hashes. |
| 61 | + for (int i = 0; i < depth; ++i) |
| 62 | + { |
| 63 | + hashA[i] = r.nextInt(Integer.MAX_VALUE); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public double getRelativeError() |
| 68 | + { |
| 69 | + return eps; |
| 70 | + } |
| 71 | + |
| 72 | + public double getConfidence() |
| 73 | + { |
| 74 | + return confidence; |
| 75 | + } |
| 76 | + |
| 77 | + private int hash(long item, int i) |
| 78 | + { |
| 79 | + long hash = hashA[i] * item; |
| 80 | + // A super fast way of computing x mod 2^p-1 |
| 81 | + // See http://www.cs.princeton.edu/courses/archive/fall09/cos521/Handouts/universalclasses.pdf |
| 82 | + // page 149, right after Proposition 7. |
| 83 | + hash += hash >> 32; |
| 84 | + hash &= PRIME_MODULUS; |
| 85 | + // Doing "%" after (int) conversion is ~2x faster than %'ing longs. |
| 86 | + return ((int) hash) % width; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void add(long item, long count) |
| 91 | + { |
| 92 | + if (count < 0) |
| 93 | + { |
| 94 | + // Actually for negative increments we'll need to use the median |
| 95 | + // instead of minimum, and accuracy will suffer somewhat. |
| 96 | + // Probably makes sense to add an "allow negative increments" |
| 97 | + // parameter to constructor. |
| 98 | + throw new IllegalArgumentException("Negative increments not implemented"); |
| 99 | + } |
| 100 | + for (int i = 0; i < depth; ++i) |
| 101 | + { |
| 102 | + table[i][hash(item, i)] += count; |
| 103 | + } |
| 104 | + size += count; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public long size() |
| 109 | + { |
| 110 | + return size; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * The estimate is correct within 'epsilon' * (total item count), |
| 115 | + * with probability 'confidence'. |
| 116 | + */ |
| 117 | + @Override |
| 118 | + public long estimateCount(long item) |
| 119 | + { |
| 120 | + long res = Long.MAX_VALUE; |
| 121 | + for (int i = 0; i < depth; ++i) |
| 122 | + { |
| 123 | + res = Math.min(res, table[i][hash(item, i)]); |
| 124 | + } |
| 125 | + return res; |
| 126 | + } |
| 127 | + |
| 128 | + public static byte[] serialize(CountMinSketch sketch) |
| 129 | + { |
| 130 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 131 | + DataOutputStream s = new DataOutputStream(bos); |
| 132 | + try |
| 133 | + { |
| 134 | + s.writeLong(sketch.size); |
| 135 | + s.writeInt(sketch.depth); |
| 136 | + s.writeInt(sketch.width); |
| 137 | + for (int i = 0; i < sketch.depth; ++i) |
| 138 | + { |
| 139 | + s.writeLong(sketch.hashA[i]); |
| 140 | + for (int j = 0; j < sketch.width; ++j) |
| 141 | + { |
| 142 | + s.writeLong(sketch.table[i][j]); |
| 143 | + } |
| 144 | + } |
| 145 | + return bos.toByteArray(); |
| 146 | + } catch (IOException e) |
| 147 | + { |
| 148 | + // Shouldn't happen |
| 149 | + throw new RuntimeException(e); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public static CountMinSketch deserialize(byte[] data) |
| 154 | + { |
| 155 | + ByteArrayInputStream bis = new ByteArrayInputStream(data); |
| 156 | + DataInputStream s = new DataInputStream(bis); |
| 157 | + try |
| 158 | + { |
| 159 | + CountMinSketch sketch = new CountMinSketch(); |
| 160 | + sketch.size = s.readLong(); |
| 161 | + sketch.depth = s.readInt(); |
| 162 | + sketch.width = s.readInt(); |
| 163 | + sketch.eps = 2.0 / sketch.width; |
| 164 | + sketch.confidence = 1 - 1 / Math.pow(2, sketch.depth); |
| 165 | + sketch.hashA = new long[sketch.depth]; |
| 166 | + sketch.table = new long[sketch.depth][sketch.width]; |
| 167 | + for (int i = 0; i < sketch.depth; ++i) |
| 168 | + { |
| 169 | + sketch.hashA[i] = s.readLong(); |
| 170 | + for (int j = 0; j < sketch.width; ++j) |
| 171 | + { |
| 172 | + sketch.table[i][j] = s.readLong(); |
| 173 | + } |
| 174 | + } |
| 175 | + return sketch; |
| 176 | + } |
| 177 | + catch (IOException e) |
| 178 | + { |
| 179 | + // Shouldn't happen |
| 180 | + throw new RuntimeException(e); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments