Esempio n. 1
0
def compErrors2(x, y, samples, ks, trueMI, p, z=None):
    nsamples = len(samples)
    nks = len(ks)
    res1 = np.zeros((nks, nsamples))
    ni = 0

    for n in samples:
        ki = 0
        for k in ks:
            aa = KnnEstimator(k=k, p=p)

            if z is not None:
                res1[ki, ni] = trueMI - aa._cmi1(x[:n, :], y[:n, :], z[:n, :])

            else:
                res1[ki, ni] = trueMI - aa._cmi1(x[:n, :], y[:n, :], z)

            ki += +1
        ni += 1

    return res1
Esempio n. 2
0
def testCMI2(seed, k=3, tests=10):
    np.random.seed(seed)
    samples = [100, 25000]

    ic = np.array([[1.0, -0.2, 0], [-0.2, 1.0, 0.6], [0, 0.6, 1.0]])
    c = np.linalg.inv(ic)
    c[0, 0] = 10 * c[0, 0]

    n = np.max(samples)
    nsamples = len(samples)
    res1 = np.zeros((2, nsamples))
    res2 = np.zeros((2, nsamples))
    res3 = np.zeros((2, nsamples))

    knn = KnnEstimator(k=k)
    for tt in range(0, tests):

        data = np.random.multivariate_normal([0, 0, 0], c, n)
        jj = 0
        for ss in samples:
            x, y, z = 0, 2, 1
            cmixy_zT = mvnCMI(c, [x], [y], [z])
            res1[0, jj] += (cmixy_zT - knn._cmi1(
                data[:ss, [x]], data[:ss, [y]], data[:ss, [z]])) / tests
            res1[1, jj] += (cmixy_zT - knn._cmi2(
                data[:ss, [x]], data[:ss, [y]], data[:ss, [z]])) / tests

            x, y, z = 1, 2, 0
            cmixy_zT = mvnCMI(c, [x], [y], [z])
            res2[0, jj] += (cmixy_zT - knn._cmi1(
                data[:ss, [x]], data[:ss, [y]], data[:ss, [z]])) / tests
            res2[1, jj] += (cmixy_zT - knn._cmi2(
                data[:ss, [x]], data[:ss, [y]], data[:ss, [z]])) / tests

            x, y, z = 0, 1, 2
            cmixy_zT = mvnCMI(c, [x], [y], [z])
            res3[0, jj] += (cmixy_zT - knn._cmi1(
                data[:ss, [x]], data[:ss, [y]], data[:ss, [z]])) / tests
            res3[1, jj] += (cmixy_zT - knn._cmi2(
                data[:ss, [x]], data[:ss, [y]], data[:ss, [z]])) / tests
            jj += 1

    plt.figure()
    plt.xlim(0.9, len(samples) + 0.1)
    x = list(range(1, len(samples) + 1, 1))
    for ii in range(0, 2):
        lab = "MI_" + str(ii)
        plt.plot(x, res1[ii, :], label=lab, marker='o')
        plt.xticks(x, samples)

    #plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
    plt.legend(loc='best')
    plt.show()

    plt.figure()
    plt.xlim(0.9, len(samples) + 0.1)
    x = list(range(1, len(samples) + 1, 1))
    for ii in range(0, 2):
        lab = "MI_" + str(ii)
        plt.plot(x, res2[ii, :], label=lab, marker='o')
        plt.xticks(x, samples)

    #plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
    plt.legend(loc='best')
    plt.show()

    plt.figure()
    plt.xlim(0.9, len(samples) + 0.1)
    x = list(range(1, len(samples) + 1, 1))
    for ii in range(0, 2):
        lab = "MI_" + str(ii)
        plt.plot(x, res3[ii, :], label=lab, marker='o')
        plt.xticks(x, samples)

    #plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
    plt.legend(loc='best')
    plt.show()