def test_log_pdf_1d_1n_change(self):
     mu = asarray([0], dtype=numpy.bool8)
     spread = rand()
     dist = DiscreteRandomWalkProposal(mu, spread)
     X = asarray([[1]], dtype=numpy.bool8)
     expected = log(1.)
     self.assertAlmostEqual(dist.log_pdf(X), expected)
 def test_log_pdf_returned_array_dimension_multiple_X_2d(self):
     n = 2
     mu = asarray([0, 1], dtype=numpy.bool8)
     spread = .5
     dist = DiscreteRandomWalkProposal(mu, spread)
     X = asarray([[1, 0], [0, 0]], dtype=numpy.bool8)
     self.assertEqual(dist.log_pdf(X).shape, (n,))
 def test_log_pdf_returned_array_dimension_1d_X(self):
     n = 1
     mu = asarray([0], dtype=numpy.bool8)
     spread = .5
     dist = DiscreteRandomWalkProposal(mu, spread)
     X = asarray([[0]], dtype=numpy.bool8)
     self.assertEqual(dist.log_pdf(X).shape, (n,))
 def test_sample_dim(self):
     n = 3
     d = 2
     mu = randint(0, 2, d).astype(numpy.bool8)
     spread = .5
     dist = DiscreteRandomWalkProposal(mu, spread)
     s = dist.sample(n)
     self.assertEqual(s.samples.shape, (n, d))
 def test_sample_many_no_checks(self):
     num_runs = 1000
     for _ in range(num_runs):
         n = randint(1, 10)
         d = randint(1, 10)
         mu = randint(0, 2, d).astype(numpy.bool8)
         spread = rand()
         dist = DiscreteRandomWalkProposal(mu, spread)
         dist.sample(n)
 def test_log_pdf_1d_2n(self):
     mu = asarray([0], dtype=numpy.bool8)
     spread = rand()
     dist = DiscreteRandomWalkProposal(mu, spread)
     X = asarray([[1], [0]], dtype=numpy.bool8)
     log_liks = dist.log_pdf(X)
     expected = asarray([log(1.), -inf])
     self.assertAlmostEqual(norm(log_liks[0] - expected[0]), 0)
     self.assertEqual(log_liks[1], expected[1])
 def test_sample_many_full_spread(self):
     n = 3
     d = 5
     mu = randint(0, 2, d).astype(numpy.bool8)
     spread = 0.99999999999
     dist = DiscreteRandomWalkProposal(mu, spread)
     s = dist.sample(n).samples
     
     for i in range(n):
         self.assertTrue(all(s[i] != mu))
 def test_sample_many_no_spread(self):
     n = 3
     d = 5
     mu = randint(0, 2, d).astype(numpy.bool8)
     spread = 0.00000000001
     dist = DiscreteRandomWalkProposal(mu, spread)
     s = dist.sample(n).samples
     
     for i in range(n):
         # exactly one change
         self.assertTrue(sum(abs(s[i] - mu)) == 1)
Esempio n. 9
0
    def construct_proposal(self, y):
        k = self.kernel.kernel(y.reshape(1, len(y)), self.Z)

        # take care about bool8 overflows preventing larger values
        diff = y.astype(numpy.int64)
        diff = diff - self.Z
        beta = randn(len(self.Z))
        weighted_sum = sum((k * beta).T * diff, 0)
        thresholded_sum = weighted_sum > self.threshold
        xored = logical_xor(thresholded_sum, y)

        # return distribution object that adds noise to the xor point
        return DiscreteRandomWalkProposal(xored, self.spread)
 def test_sample_wrong_n_sameller_zero(self):
     mu = randint(0, 2, 10).astype(numpy.bool8)
     spread = .5
     dist = DiscreteRandomWalkProposal(mu, spread)
     self.assertRaises(ValueError, dist.sample, -1)
 def test_sample_wrong_n_type_none(self):
     mu = randint(0, 2, 10).astype(numpy.bool8)
     spread = .5
     dist = DiscreteRandomWalkProposal(mu, spread)
     self.assertRaises(TypeError, dist.sample, None)
 def test_contructor_correct_mu(self):
     mu = ones(2, dtype=numpy.bool8)
     spread = .5
     dist = DiscreteRandomWalkProposal(mu, spread)
     self.assertTrue(mu is dist.mu)
 def test_contructor_correct_spread(self):
     mu = ones(2, dtype=numpy.bool8)
     spread = .5
     dist = DiscreteRandomWalkProposal(mu, spread)
     self.assertEqual(spread, dist.spread)
 def test_log_pdf_2d_1n_change2(self):
     mu = asarray([0, 0], dtype=numpy.bool8)
     spread = rand()
     dist = DiscreteRandomWalkProposal(mu, spread)
     X = asarray([[1, 1]], dtype=numpy.bool8)
     self.assertTrue(all(dist.log_pdf(X) == log(spread)))
 def test_sample_type(self):
     mu = randint(0, 2, 10).astype(numpy.bool8)
     spread = .5
     dist = DiscreteRandomWalkProposal(mu, spread)
     s = dist.sample(1)
     self.assertTrue(isinstance(s, Sample))
 def test_sample_samples_dtype(self):
     mu = randint(0, 2, 10).astype(numpy.bool8)
     spread = .5
     dist = DiscreteRandomWalkProposal(mu, spread)
     s = dist.sample(1)
     self.assertEqual(s.samples.dtype, numpy.bool8)
 def test_log_pdf_2d_1n_change1(self):
     mu = asarray([0, 0], dtype=numpy.bool8)
     spread = rand()
     dist = DiscreteRandomWalkProposal(mu, spread)
     X = asarray([[1, 0]], dtype=numpy.bool8)
     self.assertAlmostEqual(dist.log_pdf(X)[0], log(1. - spread))
 def test_log_pdf_no_change(self):
     mu = asarray([0], dtype=numpy.bool8)
     spread = rand()
     dist = DiscreteRandomWalkProposal(mu, spread)
     X = asarray([[0]], dtype=numpy.bool8)
     self.assertAlmostEqual(dist.log_pdf(X), -inf)
 def test_log_pdf_wrong_type_float(self):
     d = 2
     mu = randint(0, 2, d).astype(numpy.bool8)
     spread = .5
     dist = DiscreteRandomWalkProposal(mu, spread)
     self.assertRaises(TypeError, dist.log_pdf, float(1.))
Esempio n. 20
0
 def construct_proposal(self, y):
     return DiscreteRandomWalkProposal(y, self.spread,
                                       self.flip_at_least_one)
 def test_log_pdf_type(self):
     mu = asarray([0], dtype=numpy.bool8)
     spread = .5
     dist = DiscreteRandomWalkProposal(mu, spread)
     X = asarray([[0]], dtype=numpy.bool8)
     self.assertEqual(type(dist.log_pdf(X)), numpy.ndarray)
 def test_log_pdf_wrong_dimension(self):
     d = 2
     mu = randint(0, 2, d).astype(numpy.bool8)
     spread = .5
     dist = DiscreteRandomWalkProposal(mu, spread)
     self.assertRaises(ValueError, dist.log_pdf, zeros((1, 3)))