def test_mvnormal(self):
        """Compare the results to the figure 2 in the paper."""
        from numpy.random import normal, multivariate_normal

        N = 30000
        p = normal(0, 1, size=(N, 2))
        np.random.seed(1)
        q = multivariate_normal([.5, -.5], [[.5, .1], [.1, .3]], size=N)

        aaeq(dd.kldiv(p, q), 1.39, 1)
        aaeq(dd.kldiv(q, p), 0.62, 1)
    def test_mvnormal(self):
        """Compare the results to the figure 2 in the paper."""
        from numpy.random import normal, multivariate_normal

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

        aaeq(dd.kldiv(p, q), 1.39, 1)
        aaeq(dd.kldiv(q, p), 0.62, 1)
    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 n in range(trials):
            out.append(dd.kldiv(p.rvs(N), q.rvs(N), 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)
    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 n in range(trials):
            out.append(dd.kldiv(p.rvs(n), q.rvs(n), 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)
    def check_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 = [dd.kldiv(p.rvs(N), q.rvs(N)) for i in range(30)]
        aaeq(np.mean(re), ra, 2)

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

        re = [dd.kldiv(p.rvs(N), q.rvs(N * 2)) for i in range(30)]
        aaeq(np.mean(re), ra, 2)
    def check_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 = [dd.kldiv(p.rvs(n), q.rvs(n)) for i in range(30)]
        aaeq(np.mean(re), ra, 2)

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

        re = [dd.kldiv(p.rvs(n), q.rvs(n * 2)) for i in range(30)]
        aaeq(np.mean(re), ra, 2)
    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 = dd.kldiv(p.rvs(N), q.rvs(N))

        aaeq(re, ra, 1)
    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 = dd.kldiv(p.rvs(N), q.rvs(N))

        aaeq(re, ra, 1)