-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCountMinSketch.cs
179 lines (147 loc) · 5.84 KB
/
CountMinSketch.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
// Count Min Sketch
//
// TODO: The estimates are off more than I'd like. Probably need better hash functions.
//
// Compiled : C# Visual Studio 2013
namespace CountMinSketch
{
/// <summary>
/// Count Min Sketch (CM Sketch) is a probabilistic streaming algorithm that can be used to estimate the number of times
/// (frequency) that a given item has been seen. By accepting a some error in our measurements
/// we can save storage space when dealing with large amounts of data. They are similar to bloom filters in that they can only give
/// a lower bound on the number of times a value could have seen.
/// </summary>
public class CountMinSketch
{
#region FIELDS / PROPERTIES
/// <summary>
/// Helper class that keeps track of hash functions and their associated counts.
/// </summary>
private class HashCounter
{
private Func<int, int> HashFunction;
public int[] Counts {get; private set; }
public HashCounter(Func<int, int> function, int size)
{
this.HashFunction = function;
this.Counts = new int[size];
}
/// <summary>
/// Increment the counter associated with a given hash key.
/// </summary>
public void IncrementCount(int value)
{
var hashKey = this.HashFunction(value);
Debug.Assert(hashKey >= 0 && hashKey < this.Counts.Count());
this.Counts[hashKey]++;
}
/// <summary>
/// Increment the counter associated with a given hash key.
/// </summary>
public int GetCount(int value)
{
var hashKey = this.HashFunction(value);
Debug.Assert(hashKey >= 0 && hashKey < this.Counts.Count());
return this.Counts[hashKey];
}
}
public int Size { get; private set; }
public int NumberOfHashes
{
get {return hashes.Count; }
}
private List<HashCounter> hashes;
#endregion
#region CONSTRUCTORS
public CountMinSketch(int size)
{
this.Size = size;
Clear();
}
#endregion
public void Clear()
{
// TODO: The hashes should probably be passed into the min sketch class & use a strategy pattern.
this.hashes = new List<HashCounter>();
this.hashes.Add(new HashCounter((int value) => { return value.GetHashCode() % Size; }, Size));
this.hashes.Add(new HashCounter((int value) => { return (value ^ (value + 31) + value) % Size; }, Size));
this.hashes.Add(new HashCounter((int value) => { return ((Size - 1) + value + 7) % Size; }, Size));
}
public void Insert(int value)
{
foreach (var hash in this.hashes)
hash.IncrementCount(value);
}
public int GetSketchCount(int value)
{
var min = int.MaxValue;
foreach (var hash in hashes)
min = Math.Min(min, hash.GetCount(value));
return min;
}
public IEnumerable<int> GetCountArray(int index)
{
if (index >= this.NumberOfHashes) { throw new IndexOutOfRangeException(); }
var hash = this.hashes[index];
foreach (var count in this.hashes[index].Counts)
yield return count;
}
}
public static class Program
{
/// <summary>
/// Number of values to insert.
/// </summary>
private const int NUMBER_OF_VALUES_TO_INSERT = 1000;
/// <summary>
/// Number of counts to store for each hash function.
/// </summary>
private const int SKETCH_HASH_ARRAY_SIZE = 100;
/// <summary>
/// The maximum value to randomly generate.
/// </summary>
private const int MAX_VALUE_SIZE = 200;
public static void Main()
{
var count = new CountMinSketch(SKETCH_HASH_ARRAY_SIZE);
var actualCounts = new Dictionary<int, int>();
var rnd = new Random(DateTime.Now.Second * DateTime.Now.Millisecond); // Could be better.
foreach(var i in Enumerable.Range(0, NUMBER_OF_VALUES_TO_INSERT))
{
var value = rnd.Next(MAX_VALUE_SIZE);
if (actualCounts.ContainsKey(value))
actualCounts[value]++;
else
actualCounts.Add(value, 1);
count.Insert(value);
}
DisplayCounts(count);
Console.WriteLine("Val \t Act \t Est \t Error");
foreach (var key in actualCounts.Keys)
{
var actual = actualCounts[key];
var estimate = count.GetSketchCount(key);
var error = actual - estimate;
Debug.Assert(estimate >= actual, "The estimate should always greater than or equal to the actual (Count Min ...).");
double percent = (actual > 0 ) ? Math.Abs((double)error / (double)actual) : 0D;
Console.WriteLine("({0}) \t {1} \t {2} \t {3} ({4}%)", key, actual, estimate, error, Math.Round(percent * 100, 1));
}
Console.WriteLine("Press [Enter Key] to exit.");
Console.ReadLine();
}
private static void DisplayCounts(CountMinSketch count)
{
for (var i = 0; i < count.NumberOfHashes; i++)
{
Console.Write("\n(" + i + ")=>");
foreach (var value in count.GetCountArray(i))
Console.Write("," + value);
Console.WriteLine();
}
}
}
}