예제 #1
0
    def test_choice_consistency(self):
        N = 1000
        K = 100
        samples = []
        random_state = np.random.RandomState(1)
        for _ in range(N):
            # unnormalized probability distribution
            dist = random_state.randint(10, 1000, size=K).astype(float)
            samples.append(_utils.choice(dist, random_state.rand()))

        samples2 = []
        random_state = np.random.RandomState(1)
        for _ in range(N):
            # unnormalized probability distribution
            dist = random_state.randint(10, 1000, size=K).astype(float)
            samples2.append(_utils.choice(dist, random_state.rand()))
        self.assertEqual(samples, samples2)
예제 #2
0
    def test_choice(self):
        r = 0.9851367976958045
        probz = np.array([0.14800396, 0.29600989, 0.46001528])
        z = _utils.choice(probz, r)
        self.assertEqual(z, len(probz) - 1)

        N = 10000
        K = 100
        samples = []
        random_state = np.random.RandomState(1)
        for _ in range(N):
            # unnormalized probability distribution
            dist = random_state.randint(10, 1000, size=K).astype(float)
            samples.append(_utils.choice(dist, random_state.rand()))
        self.assertGreaterEqual(max(samples), 0)
        self.assertLessEqual(max(samples), K-1)

        p = 1/K
        bound = 3*np.sqrt(N*p*(1-p))
        counts = np.bincount(samples)
        for k in range(K):
            self.assertLess(abs(counts[k] - N*p), bound)