Exemple #1
0
    def eval(self, fast=True, pts=1000):
        """
        Evaluate the true divergence by performing numeric integration on the n^d grid of points.
        """
        if not fast:
            print "using slow integration"
            if self.dim == 1:
                val = numeric_integration(lambda x: np.multiply(
                        np.power(self.p.eval(np.matrix(x)), self.alpha),
                        np.power(self.q.eval(np.matrix(x)), self.beta)),
                                          [lb], [ub])
            if self.dim == 2:
                val = numeric_integration(lambda x,y: np.multiply(
#                         np.power(self.p.eval(np.concatenate(np.matrix(x), np.matrix(y))), self.alpha),
#                         np.power(self.q.eval(np.concatenate(np.matrix(x), np.matrix(y))), self.beta)),
                        np.power(self.p.eval(np.concatenate(np.matrix(x), np.matrix(y))), self.alpha),
                        np.power(self.q.eval(np.concatenate(np.matrix(x), np.matrix(y))), self.beta)),
                                          [lb, lb], [ub, ub])

        if fast:
            print "using fast integration"
            if self.dim == 1:
                val = fast_integration(lambda x: np.multiply(
                        np.power(self.p.eval(np.matrix(x)), self.alpha),
                        np.power(self.q.eval(np.matrix(x)), self.beta)),
                                          [lb], [ub], pts=pts)
            if self.dim == 2:
                val = fast_integration(lambda x: np.multiply(
                        np.power(self.p.eval(np.matrix(x)), self.alpha),
                        np.power(self.q.eval(np.matrix(x)), self.beta)),
                                          [lb,lb], [ub,ub])
        return val
Exemple #2
0
def l2_rate(Dp, Dq, ns, iters=50, fast=True):
    """
    Collect data for L_2^2 divergence estimation of the distributions Dp, Dq over the set of n-values.
    Repeat trials iters times.
    """
    ms = []
    vs = []
    x = np.matrix(np.arange(0, 1, 0.001)).T
    truth = np.mean(np.array((Dp.eval(x)- Dq.eval(x)))**2)

    truth = numeric_integration(lambda x:
                                (Dp.eval(np.matrix(x))- Dq.eval(np.matrix(x)))**2,
                                [0], [1])
    for n in ns:
        sub_scores = []
        for i in range(iters):
            pdata = Dp.sample(n)
            qdata = Dq.sample(n)
            E = estimators.L2Estimator(pdata, qdata, Dp.s)
            val = E.eval(fast=fast)
            sub_scores.append(np.abs(val-truth))

        sub_scores.sort();
        sub_scores = sub_scores[int(0.2*iters): int(0.8*iters)];
        ms.append(np.mean(sub_scores))
        vs.append(np.std(sub_scores))
        print "n = %d, av_er = %0.2f, std_er = %0.4f" % (n, np.mean(sub_scores), np.std(sub_scores))
    return (ns, ms, vs)
Exemple #3
0
def test_linear_functional2(Dp,
                            Dq,
                            ns,
                            alpha=0.5,
                            beta=0.5,
                            iters=10,
                            fast=True):
    """
    Another test case for the linear functional
    """
    ms = []
    vs = []
    T = numeric_integration(
        lambda x: np.multiply(np.power(Dp.eval(x), alpha),
                              np.power(Dq.eval(x), beta)), [0], [1])
    print "True Expectation: %f\n" % (T)
    for n in ns:
        sub_scores = []
        for i in range(iters):
            pdata = Dp.sample(n)
            qdata = Dq.sample(n)
            fn1 = lambda x: np.multiply(np.power(Dp.eval(x), alpha - 1),
                                        np.power(Dq.eval(x), beta))
            val1 = alpha * np.mean(fn1(pdata))
            #fn2 = lambda x: np.multiply(np.power(Dp.eval(x), alpha), np.power(Dq.eval(x), beta-1))
            val2 = beta * np.mean(fn1(qdata))
            sub_scores.append(np.abs(val1 + val2 - T))
        sub_scores.sort()
        sub_scores = sub_scores[int(0.2 * iters):int(0.8 * iters)]
        ms.append(np.mean(sub_scores))
        vs.append(np.std(sub_scores))
        print "n = %d, av_er = %0.2f, std_er = %0.4f" % (
            n, np.mean(sub_scores), np.std(sub_scores))
    return (ns, ms, vs)
Exemple #4
0
def l2_rate(Dp, Dq, ns, iters=50, fast=True):
    """
    Collect data for L_2^2 divergence estimation of the distributions Dp, Dq over the set of n-values.
    Repeat trials iters times.
    """
    ms = []
    vs = []
    x = np.matrix(np.arange(0, 1, 0.001)).T
    truth = np.mean(np.array((Dp.eval(x) - Dq.eval(x)))**2)

    truth = numeric_integration(
        lambda x: (Dp.eval(np.matrix(x)) - Dq.eval(np.matrix(x)))**2, [0], [1])
    for n in ns:
        sub_scores = []
        for i in range(iters):
            pdata = Dp.sample(n)
            qdata = Dq.sample(n)
            E = estimators.L2Estimator(pdata, qdata, Dp.s)
            val = E.eval(fast=fast)
            sub_scores.append(np.abs(val - truth))

        sub_scores.sort()
        sub_scores = sub_scores[int(0.2 * iters):int(0.8 * iters)]
        ms.append(np.mean(sub_scores))
        vs.append(np.std(sub_scores))
        print "n = %d, av_er = %0.2f, std_er = %0.4f" % (
            n, np.mean(sub_scores), np.std(sub_scores))
    return (ns, ms, vs)
Exemple #5
0
    def eval(self, fast=True, pts=1000):
        """
        Evaluate the true divergence by performing numeric integration on the n^d grid of points.
        """
        if not fast:
            print "using slow integration"
            if self.dim == 1:
                val = numeric_integration(
                    lambda x: np.multiply(
                        np.power(self.p.eval(np.matrix(x)), self.alpha),
                        np.power(self.q.eval(np.matrix(x)), self.beta)), [lb],
                    [ub])
            if self.dim == 2:
                val = numeric_integration(
                    lambda x, y: np.multiply(
                        #                         np.power(self.p.eval(np.concatenate(np.matrix(x), np.matrix(y))), self.alpha),
                        #                         np.power(self.q.eval(np.concatenate(np.matrix(x), np.matrix(y))), self.beta)),
                        np.power(
                            self.p.eval(
                                np.concatenate(np.matrix(x), np.matrix(y))),
                            self.alpha),
                        np.power(
                            self.q.eval(
                                np.concatenate(np.matrix(x), np.matrix(y))),
                            self.beta)),
                    [lb, lb],
                    [ub, ub])

        if fast:
            print "using fast integration"
            if self.dim == 1:
                val = fast_integration(lambda x: np.multiply(
                    np.power(self.p.eval(np.matrix(x)), self.alpha),
                    np.power(self.q.eval(np.matrix(x)), self.beta)), [lb],
                                       [ub],
                                       pts=pts)
            if self.dim == 2:
                val = fast_integration(
                    lambda x: np.multiply(
                        np.power(self.p.eval(np.matrix(x)), self.alpha),
                        np.power(self.q.eval(np.matrix(x)), self.beta)),
                    [lb, lb], [ub, ub])
        return val
Exemple #6
0
 def kde_error(self, true_p, p_norm, fast=True):
     """
     Compute the error of this estimator in ell_p^p norm. 
     """
     if fast:
         integrator = lambda x,y,z: helper.fast_integration(x,y,z)
     else:
         integrator = lambda x,y,z: helper.numeric_integration(x,y,z)
     fn_handle = lambda x: np.power(np.array(np.abs(self.eval(np.matrix(x)) - true_p.eval(np.matrix(x)).reshape(x.shape[0],)))[0,:], p_norm)
     return integrator(fn_handle, [0.1 for i in range(self.d)], [0.9 for i in range(self.d)])
Exemple #7
0
 def kde_error(self, true_p, p_norm, fast=True):
     """
     Compute the error of this estimator in ell_p^p norm.
     """
     if fast:
         integrator = lambda x, y, z: helper.fast_integration(x, y, z)
     else:
         integrator = lambda x, y, z: helper.numeric_integration(x, y, z)
     fn_handle = lambda x: np.power(
         np.array(
             np.abs(
                 self.eval(np.matrix(x)) - true_p.eval(np.matrix(x)).
                 reshape(x.shape[0], )))[0, :], p_norm)
     return integrator(fn_handle, [0.1 for i in range(self.d)],
                       [0.9 for i in range(self.d)])
Exemple #8
0
def test_linear_functional(Dp, ns, iters=10, fast=True):
    """
    Test the linear functional estimator
    """
    ms = [];
    vs = [];
    T = numeric_integration(lambda x: np.array(Dp.eval(x)) * np.array(x), [0], [1]);
    print "True Expectation: %f\n" % (T)
    for n in ns:
        sub_scores = [];
        for i in range(iters):
            pdata = Dp.sample(n);
            val = np.mean(pdata, axis=0)

            sub_scores.append(np.abs(val-T))
        sub_scores.sort();
        sub_scores = sub_scores[int(0.2*iters): int(0.8*iters)];
        ms.append(np.mean(sub_scores))
        vs.append(np.std(sub_scores))
        print "n = %d, av_er = %0.2f, std_er = %0.4f" % (n, np.mean(sub_scores), np.std(sub_scores))
    return (ns, ms, vs)
Exemple #9
0
def test_linear_functional(Dp, ns, iters=10, fast=True):
    """
    Test the linear functional estimator
    """
    ms = []
    vs = []
    T = numeric_integration(lambda x: np.array(Dp.eval(x)) * np.array(x), [0],
                            [1])
    print "True Expectation: %f\n" % (T)
    for n in ns:
        sub_scores = []
        for i in range(iters):
            pdata = Dp.sample(n)
            val = np.mean(pdata, axis=0)

            sub_scores.append(np.abs(val - T))
        sub_scores.sort()
        sub_scores = sub_scores[int(0.2 * iters):int(0.8 * iters)]
        ms.append(np.mean(sub_scores))
        vs.append(np.std(sub_scores))
        print "n = %d, av_er = %0.2f, std_er = %0.4f" % (
            n, np.mean(sub_scores), np.std(sub_scores))
    return (ns, ms, vs)
Exemple #10
0
def test_linear_functional2(Dp, Dq, ns, alpha=0.5, beta=0.5, iters=10, fast=True):
    """
    Another test case for the linear functional
    """
    ms = []
    vs = []
    T = numeric_integration(lambda x: np.multiply(np.power(Dp.eval(x), alpha), np.power(Dq.eval(x), beta)), [0], [1])
    print "True Expectation: %f\n" % (T)
    for n in ns:
        sub_scores = [];
        for i in range(iters):
            pdata = Dp.sample(n);
            qdata = Dq.sample(n)
            fn1 = lambda x: np.multiply(np.power(Dp.eval(x), alpha-1), np.power(Dq.eval(x), beta))
            val1 = alpha* np.mean(fn1(pdata))
            #fn2 = lambda x: np.multiply(np.power(Dp.eval(x), alpha), np.power(Dq.eval(x), beta-1))
            val2 = beta* np.mean(fn1(qdata))
            sub_scores.append(np.abs(val1+val2-T))
        sub_scores.sort();
        sub_scores = sub_scores[int(0.2*iters): int(0.8*iters)];
        ms.append(np.mean(sub_scores))
        vs.append(np.std(sub_scores))
        print "n = %d, av_er = %0.2f, std_er = %0.4f" % (n, np.mean(sub_scores), np.std(sub_scores))
    return (ns, ms, vs)