Пример #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
Пример #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
Пример #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
Пример #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
Пример #5
0
        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));
        ax2 = fig.add_subplot(132)
        D.plot_data_histogram(data, ax=ax2)
        ax3 = fig.add_subplot(133)
        ax3.plot(pts, vals)
        plt.axis((0,1,0, 1.25));

    if do_two_d:
        print "Two dimensional example"
        D = density.TrigDensity(s, 5, 2)
        print "Sampling %d samples from 2-d density" % (n)
        data = D.sample(n)

        K = KDE(data, s)
        t = 100
        x = np.arange(0, 1, 1.0/t)
        y = np.arange(0, 1, 1.0/t)
        X,Y = np.meshgrid(x,y)
        v = np.matrix(zip(X.reshape(t**2,), Y.reshape(t**2,)))

        print "Evaluating KDE on %d points" % (len(x)*len(y))
        z = K.eval(v)
        z = np.array(z)
        Z = z.reshape(len(x), len(y))