Пример #1
0
    def test_mvnormal(self):
        """Compare the results to the figure 2 in the paper."""
        from numpy.random import multivariate_normal, normal

        n = 30000
        p = normal(0, 1, size=(n, 2))
        np.random.seed(1)
        q = multivariate_normal([0.5, -0.5], [[0.5, 0.1], [0.1, 0.3]], size=n)

        assert_almost_equal(xca.kldiv(p, q), 1.39, 1)
        assert_almost_equal(xca.kldiv(q, p), 0.62, 1)
Пример #2
0
    def accuracy_vs_kth(self, n=100, trials=100):
        """Evalute the accuracy of the algorithm as a function of k.

        Parameters
        ----------
        N : int
          Number of random samples.
        trials : int
          Number of independent drawing experiments.

        Returns
        -------
        (err, stddev) The mean error and standard deviation around the
        analytical value for different values of k from 1 to 15.
        """
        p = stats.norm(0, 1)
        q = stats.norm(0.2, 0.9)

        k = np.arange(1, 16)

        out = []
        for i in range(trials):
            out.append(xca.kldiv(p.rvs(n), q.rvs(n), k=k))
        out = np.array(out)

        # Compare with analytical value
        err = out - analytical_KLDiv(p, q)

        # Return mean and standard deviation
        return err.mean(0), err.std(0)
Пример #3
0
    def test_different_sample_size(self):
        p = stats.norm(2, 1)
        q = stats.norm(2.6, 1.4)

        ra = analytical_KLDiv(p, q)

        n = 6000
        # Same sample size for x and y
        re = [xca.kldiv(p.rvs(n), q.rvs(n)) for i in range(30)]
        assert_almost_equal(np.mean(re), ra, 2)

        # Different sample sizes
        re = [xca.kldiv(p.rvs(n * 2), q.rvs(n)) for i in range(30)]
        assert_almost_equal(np.mean(re), ra, 2)

        re = [xca.kldiv(p.rvs(n), q.rvs(n * 2)) for i in range(30)]
        assert_almost_equal(np.mean(re), ra, 2)
Пример #4
0
    def test_against_analytic(self):
        p = stats.norm(2, 1)
        q = stats.norm(2.6, 1.4)

        ra = analytical_KLDiv(p, q)

        N = 10000
        np.random.seed(2)
        # x, y = p.rvs(N), q.rvs(N)

        re = xca.kldiv(p.rvs(N), q.rvs(N))

        assert_almost_equal(re, ra, 1)