Exemplo n.º 1
0
def l2_rate_experiment(ns, ss, d=1, iters=50, fast=True):
    """
    Experiment for plotting rate of convergence of divergence estimator on L_2^2 divergence.
    Input:
    est_type -- which estimator should we use. "linear" "plugin" and "quadratic" are supported.
    ns -- a list of sample sizes to evaluate on. Try np.logspace(1, 4, 20)
    ss -- a list which smoothness parameters should we evaluate on?
    d -- the dimension
    iters -- how many iterations should we use.
    fast -- should we use fast numeric integration or slow numeric integration

    Returns nothing but writes log files with the results of the experiments to ./data/
    """
    for s in ss:
        print "s = %s" % (str(s))
        if d == 1:
            Dp = density.UniTrigDensity(s, 1)
            Dq = density.UniTrigDensity(s, 1)
        else:
            Dp = density.TrigDensity(s, 1, d)
            Dq = density.TrigDensity(s, 1, d)
        (new_ns, ms, vs) = rates.l2_rate(Dp, Dq, ns, iters=iters, fast=fast)
        f = open("./data/l2_error_d=%d_s=%s.out" % (d, str(s)), "w")
        f.write("ns " + " ".join([str(n) for n in ns]) + "\n")
        f.write("ms " + " ".join([str(m) for m in ms]) + "\n")
        f.write("vs " + " ".join([str(v) for v in vs]))
        f.close()
    return
Exemplo n.º 2
0
def divergences_rate(est_type, ns, ss, d=1, iters=50, fast=True):
    """
    Experiment for plotting rate of convergence of divergence estimator on Renyi and Tsallis divergence.
    Input:
    est_type -- which estimator should we use. "linear" "plugin" and "quadratic" are supported.
    ns -- a list of sample sizes to evaluate on. Try np.logspace(1, 4, 20)
    ss -- a list which smoothness parameters should we evaluate on?
    d -- the dimension
    iters -- how many iterations should we use.
    fast -- should we use fast numeric integration or slow numeric integration

    Returns nothing but writes log files with the results of the experiments to ./data/
    """
    E = None
    if est_type == "plugin":
        E = estimators.PluginEstimator
    elif est_type == "linear":
        E = estimators.LinearEstimator
    elif est_type == "quadratic":
        E = estimators.QuadraticEstimator
    else:
        print "Estimator %s not supported" % (est_type)
        return

    for s in ss:
        print "s = %s" % (str(s))
        if d == 1:
            Dp = density.UniTrigDensity(s, 1)
            Dq = density.UniTrigDensity(s, 1)
        else:
            Dp = density.TrigDensity(s, 1, d)
            Dq = density.TrigDensity(s, 1, d)
        (new_ns, ms, vs) = rates.renyi_rate(E,
                                            Dp,
                                            Dq,
                                            ns,
                                            alpha=0.5,
                                            iters=iters,
                                            fast=fast)
        f = open("./data/%s_renyi_error_d=%d_s=%s.out" % (est_type, d, str(s)),
                 "w")
        f.write("ns " + " ".join([str(n) for n in ns]) + "\n")
        f.write("ms " + " ".join([str(m) for m in ms]) + "\n")
        f.write("vs " + " ".join([str(v) for v in vs]))
        f.close()
        (new_ns, ms, vs) = rates.tsallis_rate(E,
                                              Dp,
                                              Dq,
                                              ns,
                                              alpha=0.5,
                                              iters=iters,
                                              fast=fast)
        f = open(
            "./data/%s_tsallis_error_d=%d_s=%s.out" % (est_type, d, str(s)),
            "w")
        f.write("ns " + " ".join([str(n) for n in ns]) + "\n")
        f.write("ms " + " ".join([str(m) for m in ms]) + "\n")
        f.write("vs " + " ".join([str(v) for v in vs]))
        f.close()
    return
Exemplo n.º 3
0
def kde_rate(ns, ss, d=1, iters=50, fast=True):
    """
    Experiment for the KDE rate of convergence.
    ns -- which ns should we run the experiment on. Try np.logspace(1, 4, 20)
    ss -- which smoothnesses should we run the experiment on
    d -- the dimension
    iters -- how many iterations should we use

    Returns nothing but writes log files with the results of the experiments to ./data/
    """
    ps = [1, 2, 3]
    for s in ss:
        print "s = %s" % (str(s))
        if d == 1:
            D = density.UniTrigDensity(s, 1)
        else:
            D = density.TrigDensity(s, 1, d)

        (new_ns, ms, vs) = rates.kde_rate(D, ns, ps, iters=iters)
        for i in range(len(ps)):
            f = open(
                "./data/kde_error_d=%d_p=%d_s=%s.out" % (d, ps[i], str(s)),
                "w")
            f.write("ns " + " ".join([str(n) for n in ns]) + "\n")
            f.write("ms " + " ".join([str(m) for m in ms[i]]) + "\n")
            f.write("vs " + " ".join([str(v) for v in vs[i]]))
            f.close()
    return
Exemplo n.º 4
0
def estimator_rate(est_type, ns, ss, alpha, beta, d=1, iters=50, fast=True):
    """
    Experiment for the rate of convergence of the estimator for T(p,q) = \int p^\alpha q^\beta.
    est_type -- which estimator, "linear" "plugin" or "quadratic"
    ns -- which ns should we run the experiment on. Try np.logspace(1, 4, 20)
    ss -- which smoothnesses should we run the experiment on
    alpha -- the exponent for p
    beta -- the exponent for q
    d -- the dimension
    iters -- how many iterations should we use

    Returns nothing but writes log files with the results of the experiments to ./data/
    """
    E = None
    if est_type == "plugin":
        E = estimators.PluginEstimator
    elif est_type == "linear":
        E = estimators.LinearEstimator
    elif est_type == "quadratic":
        E = estimators.QuadraticEstimator
    else:
        print "Estimator %s not supported" % (est_type)
        return

    for s in ss:
        print "s = %s" % (str(s))
        if d == 1:
            Dp = density.UniTrigDensity(s, 1)
            Dq = density.UniTrigDensity(s, 1)
        else:
            Dp = density.TrigDensity(s, 1, d)
            Dq = density.TrigDensity(s, 1, d)
        (new_ns, ms, vs) = rates.estimator_rate(E,
                                                Dp,
                                                Dq,
                                                ns,
                                                alpha=alpha,
                                                beta=beta,
                                                iters=iters,
                                                fast=fast)
        f = open("./data/%s_error_d=%d_s=%s.out" % (est_type, d, str(s)), "w")
        f.write("ns " + " ".join([str(n) for n in ns]) + "\n")
        f.write("ms " + " ".join([str(m) for m in ms]) + "\n")
        f.write("vs " + " ".join([str(v) for v in vs]))
        f.close()
    return
Exemplo n.º 5
0
                    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


if __name__ == '__main__':
    print "Generating two uni-trig-densities"
    Dp = density.UniTrigDensity(2, 2)
    Dq = density.UniTrigDensity(2, 2)
    alpha = 1
    beta = 1

    T = Truth(Dp, Dq, alpha, beta)
    print "Numeric integration of true functional"
    tr = T.eval(100)
    print "T(p,q) approx %0.2f" % (tr)

    pl_scores = []
    lin_scores = []
    for n in range(100, 1000, 100):
        print "n = %d" % (n)
        pl_sub_scores = []
        lin_sub_scores = []
Exemplo n.º 6
0
        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)


if __name__ == '__main__':
    D = density.UniTrigDensity(2, 2)
    (ns, ms, vs) = test_linear_functional(D, np.logspace(1, 4, 20), iters=50)
    plot_estimator_rate(ns, ms, vs, 0.5)
    (m, b) = plot_log_log(ns, ms)
    print m, b

# To rescale a rate of n^{-\gamma}, plot ns versus
# [np.exp(np.log(ms[i]) + gamma*np.log(ns[i])) for i in range(len(ns))]
# And you should see the constant \gamma come out.

# To empirically verify the parameter \gamma, plot ns versus
# [- np.log(ms[i])/np.log(ns[i]) for i in range(len(ns))]
# And you should see the line approach \gamma.
Exemplo n.º 7
0
        """
        assert False, "Deprecated"
        coords = np.matrix(np.arange(0, 1, 0.01)).T
        vals = self.eval(coords)
        truth = true_p.eval(coords).reshape(coords.shape[0],)
        return 1.0/coords.shape[0]* np.linalg.norm(np.array(vals-truth)[0,:], ord=p_norm)**p_norm

if __name__=='__main__':
    do_one_d = True
    do_two_d = False #True
    n = 10000
    s = 2

    if do_one_d:
        print "One dimensional example"
        D = density.UniTrigDensity(s, 10)

        print "Sampling %d samples from univariate density" % (n)
        data = D.sample(n)

        K = KDE(data, s)

        pts = np.matrix(np.arange(0, 1.0, 0.001)).T

        print "Evaluating KDE on %d points" % (pts.shape[0])
        vals = K.eval(pts)

        fig = plt.figure(1, figsize=(10, 5))
        ax1 = fig.add_subplot(131)
        D.plot_fn(ax=ax1)
        plt.axis((0,1,0, 1.25));