public static IndianBuffet<Integer> createIntegerDocumentSampler(double alpha) { return new IndianBuffet<>(alpha, new IdentityConverter()); }
/** * Apply the function to the argument and return the result * * @param ignored Ignored argument * @return A sample from this distribution. */ @Override public double apply(double ignored) { return sample(); }
@Test public void testSetZeroWhileIterating() { Multinomial<Integer> table = new Multinomial<>(); for (int i = 0; i < 10000; ++i) { table.add(i, i); } // Setting a sample's weight to 0 removes from the items map. // If that map is used when iterating (it used to be), it will // trigger a ConcurrentModificationException. for (Integer sample : table) { table.set(sample, 0); } }
@Test public void testInsert() { Random rand = RandomUtils.getRandom(); Multinomial<Integer> table = new Multinomial<>(); double[] p = new double[10]; for (int i = 0; i < 10; i++) { p[i] = rand.nextDouble(); table.add(i, p[i]); } checkSelfConsistent(table); for (int i = 0; i < 10; i++) { assertEquals(p[i], table.getWeight(i), 0); } }
@Test public void testSingleton() { Multiset<String> oneThing = HashMultiset.create(); oneThing.add("one"); Multinomial<String> s = new Multinomial<>(oneThing); assertEquals("one", s.sample(0)); assertEquals("one", s.sample(0.1)); assertEquals("one", s.sample(1)); }
@Test(expected=NullPointerException.class) public void testNoNullValuesAllowed() { Multinomial<Integer> table = new Multinomial<>(); // No null values should be allowed. table.add(null, 1); }
@Test public void testOffset() { OnlineSummarizer s = new OnlineSummarizer(); Sampler<Double> sampler = new Normal(2, 5); for (int i = 0; i < 10001; i++) { s.add(sampler.sample()); } assertEquals(String.format("m = %.3f, sd = %.3f", s.getMean(), s.getSD()), 2, s.getMean(), 0.04 * s.getSD()); assertEquals(5, s.getSD(), 0.12); }
public void delete(T value) { set(value, 0); }
@Test public void testZeros() { Empirical z = new Empirical(true, true, 3, 0, 1, 0.5, 2, 1, 3.0); assertEquals(-16.52, z.sample(0), 1.0e-2); assertEquals(20.47, z.sample(1), 1.0e-2); }
@Test public void testExtremeDiscount() { ChineseRestaurant x = new ChineseRestaurant(100, 1); Multiset<Integer> counts = HashMultiset.create(); for (int i = 0; i < 10000; i++) { counts.add(x.sample()); } assertEquals(10000, x.size()); for (int i = 0; i < 10000; i++) { assertEquals(1, x.count(i)); } }
public static IndianBuffet<String> createTextDocumentSampler(double alpha) { return new IndianBuffet<>(alpha, new WordConverter()); }
@Override public T sample() { if (gen.nextDouble() >= p) { return delegate.sample(); } else { return missingMarker; } } }
@Test public void testBasics() { for (double alpha : new double[]{0.1, 1, 10, 100}) { checkDistribution(new PoissonSampler(alpha), alpha); } }
@Override public Double sample() { return sample(gen.nextDouble()); }
@Override protected T computeNext() { while (valuesIterator.hasNext()) { T next = valuesIterator.next(); if (items.containsKey(next)) { return next; } } return endOfData(); } };
@Override public Double sample() { return sample(gen.nextDouble()); }
@Test public void testSample() throws Exception { double[] data = new double[10001]; Sampler<Double> sampler = new Normal(); for (int i = 0; i < data.length; i++) { data[i] = sampler.sample(); } Arrays.sort(data); NormalDistribution reference = new NormalDistribution(RandomUtils.getRandom().getRandomGenerator(), 0, 1, NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); assertEquals("Median", reference.inverseCumulativeProbability(0.5), data[5000], 0.04); } }
@Test public void testSimpleDist() { RandomUtils.useTestSeed(); Empirical z = new Empirical(true, true, 3, 0, 1, 0.5, 2, 1, 3.0); List<Double> r = Lists.newArrayList(); for (int i = 0; i < 10001; i++) { r.add(z.sample()); } Collections.sort(r); assertEquals(2.0, r.get(5000), 0.15); }