Esempio n. 1
0
def kl_median(pdata):
    """
    Get two Gaussian kernels constructed with the median heuristic.
    Randomize V, W from the standard Gaussian distribution.
    """
    xtr, ytr = pdata.xy()
    medx2 = util.median_distance(xtr) ** 2
    medy2 = util.median_distance(ytr) ** 2
    k = kernel.KGauss(medx2)
    l = kernel.KGauss(medy2)
    return k, l
Esempio n. 2
0
def kl_kgauss_median(pdata):
    """
    Get two Gaussian kernels constructed with the median heuristic.
    """
    xtr, ytr = pdata.xy()
    dx = xtr.shape[1]
    dy = ytr.shape[1]
    medx2 = util.meddistance(xtr, subsample=1000)**2
    medy2 = util.meddistance(ytr, subsample=1000)**2
    k = kernel.KGauss(medx2)
    l = kernel.KGauss(medy2)
    return k, l
Esempio n. 3
0
    def setUp(self):
        n = 300
        dx = 2
        pdata_mean = get_pdata_mean(n, dx)
        X, Y = pdata_mean.xy()
        gwx2 = util.meddistance(X)**2
        gwy2 = util.meddistance(Y)**2
        k = kernel.KGauss(gwx2)
        l = kernel.KGauss(gwy2)
        J = 2
        V = np.random.randn(J, dx)
        W = np.random.randn(J, 1)

        self.nfsic = it.NFSIC(k, l, V, W, alpha=0.01)
        self.pdata_mean = pdata_mean
Esempio n. 4
0
    def test_list_permute(self):
        # Check that the relative frequency in the simulated histogram is
        # accurate enough.
        ps = data.PS2DSinFreq(freq=2)
        n_permute = 1000
        J = 4
        for s in [284, 77]:
            with util.NumpySeedContext(seed=s):
                pdata = ps.sample(n=200, seed=s + 1)
                dx = pdata.dx()
                dy = pdata.dy()
                X, Y = pdata.xy()

                k = kernel.KGauss(2)
                l = kernel.KGauss(3)
                V = np.random.randn(J, dx)
                W = np.random.randn(J, dy)
                #nfsic = it.NFSIC(k, l, V, W, alpha=0.01, reg=0, n_permute=n_permute,
                #        seed=s+3):

                #nfsic_result = nfsic.perform_test(pdata)
                arr = it.NFSIC.list_permute(X,
                                            Y,
                                            k,
                                            l,
                                            V,
                                            W,
                                            n_permute=n_permute,
                                            seed=s + 34,
                                            reg=0)
                arr_naive = it.NFSIC._list_permute_naive(X,
                                                         Y,
                                                         k,
                                                         l,
                                                         V,
                                                         W,
                                                         n_permute=n_permute,
                                                         seed=s + 389,
                                                         reg=0)

                # make sure that the relative frequency of the histogram does
                # not differ much.
                freq_a, edge_a = np.histogram(arr)
                freq_n, edge_n = np.histogram(arr_naive)
                nfreq_a = freq_a / float(np.sum(freq_a))
                nfreq_n = freq_n / float(np.sum(freq_n))
                arr_diff = np.abs(nfreq_a - nfreq_n)
                self.assertTrue(np.all(arr_diff <= 0.2))
Esempio n. 5
0
    def test_nfsic(self):
        n = 50
        dx = 3
        dy = 1
        X = np.random.randn(n, dx)
        Y = np.random.randn(n, dy) + 1
        medx2 = util.meddistance(X)**2
        medy2 = util.meddistance(Y)**2
        k = kernel.KGauss(medx2)
        l = kernel.KGauss(medy2)
        J = 3
        V = np.random.randn(J, dx)
        W = np.random.randn(J, dy)

        nfsic, mean, cov = it.nfsic(X, Y, k, l, V, W, reg=0)

        self.assertAlmostEqual(np.imag(nfsic), 0)
        self.assertGreater(nfsic, 0)
Esempio n. 6
0
    def test_approximation(self):
        n = 100
        d = 3
        X = np.random.rand(n, d) * 2 - 4

        sigma2 = 2.7
        feature_pairs = 50
        rff = feature.RFFKGauss(sigma2, feature_pairs, seed=2)
        Z = rff.gen_features(X)
        Krff = Z.dot(Z.T)

        # check approximation quality
        k = kernel.KGauss(sigma2)
        K = k.eval(X, X)
        diff = np.linalg.norm((Krff - K), "fro")
        self.assertLessEqual(diff / n ** 2, 0.5)
Esempio n. 7
0
    def test_approximation(self):
        for s in [298, 67]:
            with util.NumpySeedContext(seed=s):
                k = kernel.KGauss(1)
                n = 50
                d = 3
                X = np.random.randn(n, d) * 3 + 5
                D = n // 3
                induce = util.subsample_rows(X, D, seed=s + 1)
                nymap = feature.NystromFeatureMap(k, induce)

                K = k.eval(X, X)
                Z = nymap.gen_features(X)

                # check approximation quality
                diff = np.linalg.norm((K - Z.dot(Z.T)), "fro")
                self.assertLessEqual(diff / n ** 2, 0.5)

                # check sizes
                self.assertEqual(Z.shape[1], D)
                self.assertEqual(Z.shape[0], n)