forked from stanford-futuredata/macrobase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow overweight items in AChao sampler (stanford-futuredata#189)
- Loading branch information
Showing
3 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
86 changes: 86 additions & 0 deletions
86
core/src/test/java/macrobase/analysis/sample/AChaoTest.java
This file contains 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,86 @@ | ||
package macrobase.analysis.sample; | ||
|
||
import com.google.common.collect.Lists; | ||
import macrobase.datamodel.Datum; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
|
||
public class AChaoTest { | ||
@Test | ||
public void simpleTest() throws Exception { | ||
int[] sample = {1, 2, 3, 4, 5, 6, 7}; | ||
|
||
Random r = new Random(0); | ||
AChao<Integer> ac = new AChao<>(2, r); | ||
|
||
for(int i : sample) { | ||
ac.insert(i, 1); | ||
} | ||
|
||
assertEquals((Integer) 2, (Integer) ac.getReservoir().size()); | ||
assertEquals((Integer) 5, ac.getReservoir().get(0)); | ||
assertEquals((Integer) 4, ac.getReservoir().get(1)); | ||
} | ||
|
||
@Test | ||
public void testOverweightItems() throws Exception { | ||
int[] sample = {1, 2, 3, 4, 5, 6, 7}; | ||
|
||
Random r = new Random(0); | ||
AChao<Integer> ac = new AChao<>(2, r); | ||
|
||
for(int i : sample) { | ||
ac.insert(i, 1); | ||
} | ||
|
||
assertEquals((Integer) 2, (Integer) ac.getReservoir().size()); | ||
assertEquals((Integer) 5, ac.getReservoir().get(0)); | ||
assertEquals((Integer) 4, ac.getReservoir().get(1)); | ||
|
||
ac.decayWeights(.1); | ||
ac.insert(100, 1000); | ||
|
||
assertEquals((Integer) 2, (Integer) ac.getReservoir().size()); | ||
assertTrue(ac.getReservoir().contains(100)); | ||
|
||
ac.decayWeights(.00001); | ||
|
||
ac.insert(200, 1000); | ||
assertTrue(ac.getReservoir().contains(200)); | ||
} | ||
|
||
@Test | ||
public void testOverweightItemSequential() throws Exception { | ||
int[] sample = {1, 2, 3, 4, 5, 6, 7}; | ||
|
||
Random r = new Random(0); | ||
AChao<Integer> ac = new AChao<>(100, r); | ||
|
||
for(int j = 0; j < 100; ++j) { | ||
for (int i : sample) { | ||
ac.insert(i, 1); | ||
} | ||
} | ||
|
||
ac.decayWeights(.00001); | ||
ac.insert(100, 1); | ||
ac.insert(200, 1); | ||
ac.insert(300, 1); | ||
|
||
assertEquals((Integer) 100, (Integer) ac.getReservoir().size()); | ||
assertTrue(ac.getReservoir().contains(100)); | ||
|
||
ac.decayWeights(.0000001); | ||
ac.insert(400, 1); | ||
|
||
assertTrue(ac.getReservoir().contains(400)); | ||
} | ||
} |