Ejemplo n.º 1
0
    def test_sample(self, tpe):
        """Test GMMSampler sample function"""
        mus = numpy.linspace(-3, 3, num=12, endpoint=False)
        sigmas = [0.5] * 12

        gmm_sampler = GMMSampler(tpe, mus, sigmas, -3, 3)
        points = gmm_sampler.sample(25)
        points = numpy.array(points)

        assert len(points) <= 25
        assert numpy.all(points >= -3)
        assert numpy.all(points < 3)

        mus = numpy.linspace(-10, 10, num=10, endpoint=False)
        sigmas = [0.00001] * 10
        weights = numpy.linspace(1, 10, num=10)**3
        numpy.random.shuffle(weights)
        weights = weights / weights.sum()

        gmm_sampler = GMMSampler(tpe, mus, sigmas, -11, 9, weights)
        points = gmm_sampler.sample(10000)
        points = numpy.array(points)
        hist = numpy.histogram(points,
                               bins=[-11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9])

        assert numpy.all(hist[0].argsort() == numpy.array(weights).argsort())
        assert numpy.all(points >= -11)
        assert numpy.all(points < 9)
Ejemplo n.º 2
0
    def test_sample_narrow_space(self, tpe):
        """Test that sampling in a narrow space does not fail to fast"""
        mus = numpy.ones(12) * 0.5
        sigmas = [0.5] * 12

        times = []
        for bounds in [(0.4, 0.6), (0.49, 0.51), (0.499, 0.501), (0.49999, 0.50001)]:
            gmm_sampler = GMMSampler(tpe, mus, sigmas, *bounds)
            times.append(timeit.timeit(lambda: gmm_sampler.sample(2), number=100))

        # Test that easy sampling takes less time.
        assert sorted(times) == times

        gmm_sampler = GMMSampler(
            tpe, mus, sigmas, 0.05, 0.04, attempts_factor=1, max_attempts=10
        )
        with pytest.raises(RuntimeError) as exc:
            gmm_sampler.sample(1, attempts=10)

        assert exc.match("Failed to sample in interval")
Ejemplo n.º 3
0
    def test_get_loglikelis(self):
        """Test to get log likelis of points"""
        mus = numpy.linspace(-10, 10, num=10, endpoint=False)
        weights = numpy.linspace(1, 10, num=10)**3
        numpy.random.shuffle(weights)
        weights = weights / weights.sum()

        sigmas = [0.00001] * 10
        gmm_sampler = GMMSampler(tpe, mus, sigmas, -11, 9, weights)

        points = [mus[7]]
        pdf = norm(mus[7], sigmas[7])
        point_likeli = numpy.log(pdf.pdf(mus[7]) * weights[7])
        likelis = gmm_sampler.get_loglikelis(points)

        assert list(likelis) == point_likeli
        assert likelis[0] == point_likeli

        sigmas = [2] * 10
        gmm_sampler = GMMSampler(tpe, mus, sigmas, -11, 9, weights)

        log_pdf = []
        pdfs = []
        for i in range(10):
            pdfs.append(norm(mus[i], sigmas[i]))
        for pdf, weight in zip(pdfs, weights):
            log_pdf.append(numpy.log(pdf.pdf(0) * weight))
        point_likeli = numpy.log(numpy.sum(numpy.exp(log_pdf)))

        points = numpy.random.uniform(-11, 9, 30)
        points = numpy.insert(points, 10, 0)
        likelis = gmm_sampler.get_loglikelis(points)

        point_likeli = numpy.format_float_scientific(point_likeli,
                                                     precision=10)
        gmm_likeli = numpy.format_float_scientific(likelis[10], precision=10)
        assert point_likeli == gmm_likeli
        assert len(likelis) == len(points)