-
-
Notifications
You must be signed in to change notification settings - Fork 360
Add approximate counting C++ sample code #834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <cmath> | ||
#include <iostream> | ||
#include <numeric> | ||
#include <random> | ||
|
||
// Returns a pseudo-random number generator | ||
std::default_random_engine& rng() { | ||
// Initialize static pseudo-random engine with non-deterministic random seed | ||
static std::default_random_engine randEngine(std::random_device{}()); | ||
return randEngine; | ||
} | ||
|
||
// Returns a random double in [0, 1) | ||
double drand() { | ||
return std::uniform_real_distribution<double>(0.0, 1.0)(rng()); | ||
} | ||
|
||
// This function takes | ||
// - v: value in register | ||
// - a: a scaling value for the logarithm based on Morris's paper | ||
// It returns n(v,a), the approximate count | ||
auto n(double v, double a) { return a * (pow((1 + 1 / a), v) - 1); } | ||
|
||
// This function takes | ||
// - v: value in register | ||
// - a: a scaling value for the logarithm based on Morris's paper | ||
// It returns a new value for v | ||
auto increment(int v, double a) { | ||
// delta is the probability of incrementing our counter | ||
const auto delta = 1 / (n(v + 1, a) - n(v, a)); | ||
return (drand() <= delta) ? v + 1 : v; | ||
} | ||
|
||
// This simulates counting and takes | ||
// - n_items: number of items to count and loop over | ||
// - a: a scaling value for the logarithm based on Morris's paper | ||
// It returns n(v,a), the approximate count | ||
auto approximate_count(int n_items, double a) { | ||
auto v = 0; | ||
for (auto i = 0; i < n_items; ++i) | ||
v = increment(v, a); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a warning for I'm guessing it should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to reproduce the issue, what compiler are you using, and compilation flags? |
||
|
||
return n(v, a); | ||
} | ||
|
||
// This function takes | ||
// - n_trials: the number of counting trials | ||
// - n_items: the number of items to count to | ||
// - a: a scaling value for the logarithm based on Morris's paper | ||
// - threshold: the maximum percent error allowed | ||
// It returns a "pass" / "fail" test value | ||
auto test_approximate_count( | ||
int n_trials, int n_items, double a, double threshold) { | ||
auto sum = 0.0; | ||
for (auto i = 0; i < n_trials; ++i) | ||
sum += approximate_count(n_items, a); | ||
const auto avg = sum / n_trials; | ||
return std::abs((avg - n_items) / n_items) < threshold ? "pass" : "fail"; | ||
} | ||
|
||
int main() { | ||
std::cout << "Counting Tests, 100 trials\n"; | ||
|
||
std::cout << "testing 1,000, a = 30, 1% error " | ||
<< test_approximate_count(100, 1000, 30, 0.1) << "\n"; | ||
std::cout << "testing 12,345, a = 10, 1% error " | ||
<< test_approximate_count(100, 12345, 10, 0.1) << "\n"; | ||
// Note : with a lower a, we need more trials, so a higher % error here. | ||
std::cout << "testing 222,222, a = 0.5, 10% error " | ||
<< test_approximate_count(100, 222222, 0.5, 0.2) << "\n"; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.