-
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathApproximateCounting.cs
More file actions
73 lines (62 loc) · 2.73 KB
/
ApproximateCounting.cs
File metadata and controls
73 lines (62 loc) · 2.73 KB
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
using System;
namespace ApproximateCounting
{
class ApproximateCounting
{
static readonly Random Rng = new();
/// <param name="v">value in register</param>
/// <param name="a">scaling value for the logarithm based on Morris's paper</param>
/// <returns>N(v,a) the approximate count for the given values</returns>
static double N(int v, double a)
{
return a * Math.Pow(1 + 1 / a, v - 1);
}
/// <param name="v">value in register</param>
/// <param name="a">scaling value for the logarithm based on Morris's paper</param>
/// <returns>Returns the new value for v</returns>
static int Increment(int v, double a)
{
var delta = 1 / (N(v + 1, a) - N(v, a));
if (Rng.NextDouble() <= delta)
return v + 1;
else
return v;
}
/// <summary>
/// This function simulates approximate counting
/// </summary>
/// <param name="noItems">number of items to count and loop over</param>
/// <param name="a">a scaling value for the logarithm based on Morris's paper</param>
/// <returns>It returns n(v,a), the approximate count</returns>
static double ApproximateCount(int noItems, double a)
{
var v = 0;
for (var i = 0; i < noItems; i++)
{
v = Increment(v, a);
}
return N(v, a);
}
/// <param name="noTrials">the number of counting trials</param>
/// <param name="noItems">the number of items to count to</param>
/// <param name="a">a scaling value for the logarithm based on Morris's paper</param>
/// <param name="threshold">the maximum percent error allowed</param>
/// <returns>"passed" or "failed" depending on the test result</returns>
static string TextApproximateCount(int noTrials, int noItems, double a, double threshold)
{
var sum = 0.0;
for (var i = 0; i < noTrials; i++)
sum += ApproximateCount(noItems, a);
var avg = sum / noTrials;
return Math.Abs((avg - noItems) / noItems) < threshold ? "passed" : "failed";
}
static void Main()
{
Console.WriteLine("[#]\nCounting Tests, 100 trials");
Console.WriteLine($"[#]\nTesting 1,000, a = 30, 1% error : {TextApproximateCount(100, 1_000, 30, 0.1)}");
Console.WriteLine($"[#]\nTesting 12,345, a = 10, 10% error : {TextApproximateCount(100, 12_345, 10, 0.1)}");
Console.WriteLine(
$"[#]\nTesting 222,222, a = 0.5, 20% error : {TextApproximateCount(100, 222_222, 0.5, 0.2)}");
}
}
}