private static void checkSelfConsistent(Multinomial<Integer> table) { List<Double> weights = table.getWeights(); double totalWeight = table.getWeight(); double p = 0; int[] k = new int[weights.size()]; for (double weight : weights) { if (weight > 0) { if (p > 0) { k[table.sample(p - 1.0e-9)]++; } k[table.sample(p + 1.0e-9)]++; } p += weight / totalWeight; } k[table.sample(p - 1.0e-9)]++; assertEquals(1, p, 1.0e-9); for (int i = 0; i < weights.size(); i++) { if (table.getWeight(i) > 0) { assertEquals(2, k[i]); } else { assertEquals(0, k[i]); } } } }
@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); } }
Random rand = RandomUtils.getRandom(); Multinomial<Integer> table = new Multinomial<>(); assertEquals(0, table.getWeight(), 1.0e-9); table.add(i, p[i]); total += p[i]; assertEquals(total, table.getWeight(), 1.0e-9); assertEquals(total, table.getWeight(), 1.0e-9); assertEquals(total, table.getWeight(), 1.0e-9); for (int i = 0; i < 10; i++) { assertEquals(p[i], table.getWeight(i), 0); assertEquals(p[i] / total, table.getProbability(i), 1.0e-10); total += 5.1; assertEquals(total , table.getWeight(), 1.0e-9); for (int i = 0; i < 10; i++) { assertEquals(p[i], table.getWeight(i), 0); assertEquals(p[i] / total, table.getProbability(i), 1.0e-10); assertEquals(p[i], table.getWeight(i), 0);
WeightedVector curr = datapoints.get(currSeedIndex); double newWeight = nextSeed.getWeight() * distanceMeasure.distance(nextSeed, curr); if (newWeight < seedSelector.getWeight(currSeedIndex)) { seedSelector.set(currSeedIndex, newWeight);
WeightedVector curr = datapoints.get(currSeedIndex); double newWeight = nextSeed.getWeight() * distanceMeasure.distance(nextSeed, curr); if (newWeight < seedSelector.getWeight(currSeedIndex)) { seedSelector.set(currSeedIndex, newWeight);
WeightedVector curr = datapoints.get(currSeedIndex); double newWeight = nextSeed.getWeight() * distanceMeasure.distance(nextSeed, curr); if (newWeight < seedSelector.getWeight(currSeedIndex)) { seedSelector.set(currSeedIndex, newWeight);