-
-
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
Merged
leios
merged 11 commits into
algorithm-archivists:main
from
PaddyKe:approximate_counting_java
Dec 2, 2021
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5ccad44
added approximative counting in java
PaddyKe 6a2dacd
added code examples to markdown files
PaddyKe 75814d9
fixed typo
PaddyKe b21bfc9
fixed indentation
PaddyKe b0b382d
fixed typos
PaddyKe aa6d0dc
fixed printed comments
PaddyKe 073978a
added failed message
PaddyKe 1b361ba
Merge branch 'master' into approximate_counting_java
leios 083225e
Merge branch 'master' into approximate_counting_java
leios 3e98fb9
Update contents/approximate_counting/code/java/ApproximateCounting.java
leios a16469f
Merge branch 'main' into approximate_counting_java
leios File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
contents/approximate_counting/code/java/ApproximateCounting.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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 = 0; i < nItems; i++) { | ||
v = increment(v, a); | ||
} | ||
|
||
return n(v, a); | ||
} | ||
|
||
/* | ||
* This function takes | ||
* - nTrials: the number of counting trails | ||
* - nItems: the number of items to count | ||
* - a: a scaling value for the logarithm based on Morris's paper | ||
* - threshold: the maximum percent error allowed | ||
* It terminates the program on failure | ||
*/ | ||
static void testApproximateCount(int nTrials, int nItems, double a, double threshold) { | ||
double avg = DoubleStream.generate(() -> approximateCount(nItems, a)) | ||
.limit(nTrials) | ||
.average() | ||
.getAsDouble(); | ||
|
||
if (Math.abs((avg - nItems) / nItems) < threshold) { | ||
System.out.println("passed"); | ||
} else { | ||
System.out.println("failed"); | ||
} | ||
} | ||
|
||
|
||
public static void main(String args[]) { | ||
System.out.println("testing 1,000, a = 30, 10% error"); | ||
testApproximateCount(100, 1_000, 30, 0.1); | ||
|
||
System.out.println("testing 12,345, a = 10, 10% error"); | ||
testApproximateCount(100, 12_345, 10, 0.1); | ||
|
||
System.out.println("testing 222,222, a = 0.5, 20% error"); | ||
testApproximateCount(100, 222_222, 0.5, 0.2); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.