-
-
Notifications
You must be signed in to change notification settings - Fork 359
Add Approximate counting in Java #898
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 4 commits
5ccad44
6a2dacd
75814d9
b21bfc9
b0b382d
aa6d0dc
073978a
1b361ba
083225e
3e98fb9
a16469f
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,82 @@ | ||
import java.lang.Math; | ||
import java.util.stream.DoubleStream; | ||
|
||
public class ApproximateCounting { | ||
|
||
/* | ||
* This function taks | ||
* - v: value in register | ||
* - a: a scaling value for the logarithm based on Morris's paper | ||
* It returns the approximate count | ||
*/ | ||
static double n(double v, double a) { | ||
return a * (Math.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 the new value for v | ||
*/ | ||
static double increment(double v, double a) { | ||
double delta = 1 / (n(v + 1, a) - n(v, a)); | ||
|
||
if (Math.random() <= delta) { | ||
return v + 1; | ||
} else { | ||
return v; | ||
} | ||
} | ||
|
||
|
||
|
||
/* | ||
* This function takes | ||
* - v: value in register | ||
* - a: a scaling value for the logarithm based on Morris's paper | ||
* It returns the new value for v | ||
*/ | ||
static double approximateCount(int nItems, double a) { | ||
double v = 0; | ||
|
||
for (int i = 1; i < nItems + 1; i++) { | ||
v = increment(v, a); | ||
} | ||
|
||
return n(v, a); | ||
} | ||
|
||
/* | ||
* This function takes | ||
* - nTrails: the number of counting trails | ||
* - nItems: the number of items to count | ||
* - a: a scaling value for th elogarithm based on Morris's paper | ||
Amaras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* - threshold: the maximum percent error allowed | ||
* It terminates the program on failure | ||
*/ | ||
static void testApproximateCount(int nTrails, int nItems, double a, double threshold) { | ||
Amaras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
double avg = DoubleStream.generate(() -> approximateCount(nItems, a)) | ||
.limit(nTrails) | ||
Amaras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.average() | ||
.getAsDouble(); | ||
|
||
if (Math.abs((avg - nItems) / nItems) < threshold) { | ||
System.out.println("passed"); | ||
} | ||
} | ||
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. Shouldn't it terminate the program on failure? You don't seem to check anywhere. 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'm not quite sure. I don't think this function should terminate the program on failure. |
||
|
||
|
||
public static void main(String args[]) { | ||
System.out.println("testing 1,000, a = 30, 1% error"); | ||
testApproximateCount(100, 1_000, 30, 0.1); | ||
|
||
System.out.println("testing 12,345, a = 10, 1% error"); | ||
testApproximateCount(100, 12_345, 10, 0.1); | ||
|
||
System.out.println("testing 222,222, a = 0.5, 10% error"); | ||
testApproximateCount(100, 222_222, 0.5, 0.2); | ||
Amaras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.