public static LongPrimitiveIterator maybeWrapIterator(LongPrimitiveIterator delegate, double samplingRate) { return samplingRate >= 1.0 ? delegate : new SamplingLongPrimitiveIterator(delegate, samplingRate); }
@Override public long nextLong() { if (hasNext) { long result = next; doNext(); return result; } throw new NoSuchElementException(); }
public static LoadStatistics runLoad(Recommender recommender, int howMany) throws TasteException { DataModel dataModel = recommender.getDataModel(); int numUsers = dataModel.getNumUsers(); double sampleRate = 1000.0 / numUsers; LongPrimitiveIterator userSampler = SamplingLongPrimitiveIterator.maybeWrapIterator(dataModel.getUserIDs(), sampleRate); recommender.recommend(userSampler.next(), howMany); // Warm up Collection<Callable<Void>> callables = new ArrayList<>(); while (userSampler.hasNext()) { callables.add(new LoadCallable(recommender, userSampler.next())); } AtomicInteger noEstimateCounter = new AtomicInteger(); RunningAverageAndStdDev timing = new FullRunningAverageAndStdDev(); AbstractDifferenceRecommenderEvaluator.execute(callables, noEstimateCounter, timing); return new LoadStatistics(timing); }
@Test public void testSmallInput() { SamplingLongPrimitiveIterator t = new SamplingLongPrimitiveIterator( countingIterator(1), 0.9999); assertTrue(t.hasNext()); assertEquals(0L, t.nextLong()); assertFalse(t.hasNext()); }
@Test public void testExactSizeMatch() { SamplingLongPrimitiveIterator t = new SamplingLongPrimitiveIterator( countingIterator(10), 1); for (int i = 0; i < 10; i++) { assertTrue(t.hasNext()); assertEquals(i, t.next().intValue()); } assertFalse(t.hasNext()); }
@Test public void testEmptyCase() { assertFalse(new SamplingLongPrimitiveIterator( countingIterator(0), 0.9999).hasNext()); assertFalse(new SamplingLongPrimitiveIterator( countingIterator(0), 1).hasNext()); }
@Override public long[] getUserNeighborhood(long userID) throws TasteException { DataModel dataModel = getDataModel(); UserSimilarity userSimilarityImpl = getUserSimilarity(); TopItems.Estimator<Long> estimator = new Estimator(userSimilarityImpl, userID, minSimilarity); LongPrimitiveIterator userIDs = SamplingLongPrimitiveIterator.maybeWrapIterator(dataModel.getUserIDs(), getSamplingRate()); return TopItems.getTopUsers(n, userIDs, null, estimator); }
@Test public void testSample() { double p = 0.1; int n = 1000; double sd = Math.sqrt(n * p * (1.0 - p)); for (int i = 0; i < 1000; i++) { SamplingLongPrimitiveIterator t = new SamplingLongPrimitiveIterator(countingIterator(n), p); int k = 0; while (t.hasNext()) { long v = t.nextLong(); k++; assertTrue(v >= 0L); assertTrue(v < 1000L); } // Should be +/- 5 standard deviations except in about 1 out of 1.7M cases assertTrue(k >= 100 - 5 * sd); assertTrue(k <= 100 + 5 * sd); } }
public static LongPrimitiveIterator maybeWrapIterator(LongPrimitiveIterator delegate, double samplingRate) { return samplingRate >= 1.0 ? delegate : new SamplingLongPrimitiveIterator(delegate, samplingRate); }
@Override public long[] getUserNeighborhood(long userID) throws TasteException { DataModel dataModel = getDataModel(); UserSimilarity userSimilarityImpl = getUserSimilarity(); TopItems.Estimator<Long> estimator = new Estimator(userSimilarityImpl, userID, minSimilarity); LongPrimitiveIterator userIDs = SamplingLongPrimitiveIterator.maybeWrapIterator(dataModel.getUserIDs(), getSamplingRate()); return TopItems.getTopUsers(n, userIDs, null, estimator); }
@Override public long nextLong() { if (hasNext) { long result = next; doNext(); return result; } throw new NoSuchElementException(); }
public static LongPrimitiveIterator maybeWrapIterator(LongPrimitiveIterator delegate, double samplingRate) { return samplingRate >= 1.0 ? delegate : new SamplingLongPrimitiveIterator(delegate, samplingRate); }
public static LoadStatistics runLoad(Recommender recommender, int howMany) throws TasteException { DataModel dataModel = recommender.getDataModel(); int numUsers = dataModel.getNumUsers(); double sampleRate = 1000.0 / numUsers; LongPrimitiveIterator userSampler = SamplingLongPrimitiveIterator.maybeWrapIterator(dataModel.getUserIDs(), sampleRate); recommender.recommend(userSampler.next(), howMany); // Warm up Collection<Callable<Void>> callables = Lists.newArrayList(); while (userSampler.hasNext()) { callables.add(new LoadCallable(recommender, userSampler.next())); } AtomicInteger noEstimateCounter = new AtomicInteger(); RunningAverageAndStdDev timing = new FullRunningAverageAndStdDev(); AbstractDifferenceRecommenderEvaluator.execute(callables, noEstimateCounter, timing); return new LoadStatistics(timing); }
@Override public long nextLong() { if (hasNext) { long result = next; doNext(); return result; } throw new NoSuchElementException(); }
@Test(expected = IllegalArgumentException.class) public void testBadRate2() { new SamplingLongPrimitiveIterator(countingIterator(1), 1.1); }
public static LoadStatistics runLoad(Recommender recommender, int howMany) throws TasteException { DataModel dataModel = recommender.getDataModel(); int numUsers = dataModel.getNumUsers(); double sampleRate = 1000.0 / numUsers; LongPrimitiveIterator userSampler = SamplingLongPrimitiveIterator.maybeWrapIterator(dataModel.getUserIDs(), sampleRate); recommender.recommend(userSampler.next(), howMany); // Warm up Collection<Callable<Void>> callables = Lists.newArrayList(); while (userSampler.hasNext()) { callables.add(new LoadCallable(recommender, userSampler.next())); } AtomicInteger noEstimateCounter = new AtomicInteger(); RunningAverageAndStdDev timing = new FullRunningAverageAndStdDev(); AbstractDifferenceRecommenderEvaluator.execute(callables, noEstimateCounter, timing); return new LoadStatistics(timing); }
public SamplingLongPrimitiveIterator(RandomWrapper random, LongPrimitiveIterator delegate, double samplingRate) { Preconditions.checkNotNull(delegate); Preconditions.checkArgument(samplingRate > 0.0 && samplingRate <= 1.0, "Must be: 0.0 < samplingRate <= 1.0"); // Geometric distribution is special case of negative binomial (aka Pascal) with r=1: geometricDistribution = new PascalDistribution(random.getRandomGenerator(), 1, samplingRate); this.delegate = delegate; this.hasNext = true; doNext(); }
@Test(expected = IllegalArgumentException.class) public void testBadRate1() { new SamplingLongPrimitiveIterator(countingIterator(1), 0.0); }
@Override public long[] getUserNeighborhood(long userID) throws TasteException { DataModel dataModel = getDataModel(); UserSimilarity userSimilarityImpl = getUserSimilarity(); TopItems.Estimator<Long> estimator = new Estimator(userSimilarityImpl, userID, minSimilarity); LongPrimitiveIterator userIDs = SamplingLongPrimitiveIterator.maybeWrapIterator(dataModel.getUserIDs(), getSamplingRate()); return TopItems.getTopUsers(n, userIDs, null, estimator); }
public SamplingLongPrimitiveIterator(RandomWrapper random, LongPrimitiveIterator delegate, double samplingRate) { Preconditions.checkNotNull(delegate); Preconditions.checkArgument(samplingRate > 0.0 && samplingRate <= 1.0, "Must be: 0.0 < samplingRate <= 1.0"); // Geometric distribution is special case of negative binomial (aka Pascal) with r=1: geometricDistribution = new PascalDistribution(random.getRandomGenerator(), 1, samplingRate); this.delegate = delegate; this.hasNext = true; doNext(); }