Ejemplo n.º 1
0
    def __init__(self, kernel_type, projection=False, **params):

        self.kernel_type = kernel_type
        self.projection = projection
        self.params_keys = set(params.keys())
        self.__check_args_coherence()

        # Sampling
        self.sampling_mode = 'GS'  # Gram-Schmidt
        self.list_of_samples = []

        # when using .sample_k_dpp_*
        self.size_k_dpp = 0
        self.E_poly = None  # evaluation of the

        # Attributes relative to K correlation kernel:
        # K, K_eig_vals, K_eig_vecs, A_zono
        self.K = is_symmetric(params.get('K', None))
        if self.projection:
            self.K = is_projection(self.K)

        e_vals, e_vecs = params.get('K_eig_dec', [None, None])
        if self.projection:
            self.K_eig_vals = is_equal_to_O_or_1(e_vals)
        else:
            self.K_eig_vals = is_in_01(e_vals)
        self.eig_vecs = is_orthonormal_columns(e_vecs)

        self.A_zono = is_full_row_rank(params.get('A_zono', None))

        # Attributes relative to L likelihood kernel:
        # L, L_eig_vals, L_eig_vecs, L_gram_factor, L_dual, L_dual_eig_vals, L_dual_eig_vecs
        self.L = is_symmetric(params.get('L', None))
        if self.projection:
            self.L = is_projection(self.L)

        e_vals, e_vecs = params.get('L_eig_dec', [None, None])
        if self.projection:
            self.L_eig_vals = is_equal_to_O_or_1(e_vals)
        else:
            self.L_eig_vals = is_geq_0(e_vals)
        if self.eig_vecs is None:  # K_eig_vecs = L_eig_vecs
            self.eig_vecs = is_orthonormal_columns(e_vecs)

        # L' "dual" likelihood kernel, L' = Phi Phi.T, Phi = L_gram_factor
        self.L_gram_factor = params.get('L_gram_factor', None)
        self.L_dual = None
        self.L_dual_eig_vals = None
        self.L_dual_eig_vecs = None

        if self.L_gram_factor is not None:
            Phi = self.L_gram_factor
            d, N = Phi.shape
            if d < N:
                self.L_dual = Phi.dot(Phi.T)
                print('L_dual = Phi Phi.T was computed: Phi (dxN) with d<N')
            else:
                if self.L is None:
                    self.L = Phi.T.dot(Phi)
                    print('L = Phi.T Phi was computed: Phi (dxN) with d>=N')
Ejemplo n.º 2
0
    def test_kernel_evaluations(self):
        N = 100
        dims = np.arange(2, 5)

        for d in dims:
            with self.subTest(dimension=d):

                jacobi_params = 0.5 - np.random.rand(d, 2)
                jacobi_params[0, :] = -0.5

                dpp = MultivariateJacobiOPE(N, jacobi_params)

                X = np.random.rand(20, d)
                Y = np.random.rand(20, d)

                K_XX = is_symmetric(dpp.K(X, X))
                K_xx = np.diag(K_XX)
                K_xy = np.ravel([dpp.K(x, y) for x, y in zip(X, Y)])

                checks = ((dpp.K(X), K_XX),
                          (dpp.K(X, X, eval_pointwise=True), K_xx),
                          (dpp.K(X, Y, eval_pointwise=True), K_xy))

                for idx, (a, b) in enumerate(checks):
                    with self.subTest(idx=idx):
                        self.assertTrue(np.allclose(a, b),
                                        'a={}, b={}'.format(a, b))
Ejemplo n.º 3
0
    def test_symmetric(self):

        N = 20
        X = rndm.randn(N, N)

        list_of_inputs = [(True, None), (False, X), (True, X.T + X),
                          (True, X.T.dot(X))]

        for idx, (flag, _input) in enumerate(list_of_inputs):
            with self.subTest(index=idx, is_symmetric=flag):

                if flag:
                    self.assertTrue(utils.is_symmetric(_input) is _input)
                else:
                    with self.assertRaises(ValueError) as context:
                        utils.is_symmetric(_input)

                    self.assertIn('M.T != M', str(context.exception))
Ejemplo n.º 4
0
    def test_symmetric(self):

        self.assertIsNone(utils.is_symmetric(None))

        X = rndm.randn(20, 20)
        sym_part = 0.5 * (X + X.T)
        self.assertTrue(np.allclose(utils.is_symmetric(sym_part), sym_part))

        with self.assertRaises(ValueError) as context:
            utils.is_symmetric(X)

        self.assertTrue('M.T != M' in str(context.exception))

        Y = rndm.randn(20, 30)
        cov = Y.T.dot(Y)
        self.assertTrue(np.allclose(utils.is_symmetric(cov), cov))

        with self.assertRaises(ValueError) as context:
            utils.is_symmetric(Y)

        self.assertTrue('M.T != M' in str(context.exception))
Ejemplo n.º 5
0
    def __init__(self, kernel_type, projection=False, **params):

        self.kernel_type = kernel_type
        self.projection = projection
        self.params_keys = set(params.keys())
        self.__check_args_coherence()

        # Sampling
        self.sampling_mode = 'GS'  # Gram-Schmidt
        self.list_of_samples = []

        # when using .sample_k_dpp_*
        self.size_k_dpp = 0
        self.E_poly = None  # evaluation of the

        # Attributes relative to K correlation kernel:
        # K, K_eig_vals, K_eig_vecs, A_zono
        self.K = is_symmetric(params.get('K', None))
        if self.projection:
            self.K = is_projection(self.K)

        e_vals, e_vecs = params.get('K_eig_dec', [None, None])
        if self.projection:
            self.K_eig_vals = is_equal_to_O_or_1(e_vals)
        else:
            self.K_eig_vals = is_in_01(e_vals)
        self.eig_vecs = is_orthonormal_columns(e_vecs)

        self.A_zono = is_full_row_rank(params.get('A_zono', None))

        # Attributes relative to L likelihood kernel:
        # L, L_eig_vals, L_eig_vecs, L_gram_factor, L_dual
        self.L = is_symmetric(params.get('L', None))
        if self.projection:
            self.L = is_projection(self.L)

        e_vals, e_vecs = params.get('L_eig_dec', [None, None])
        if self.projection:
            self.L_eig_vals = is_equal_to_O_or_1(e_vals)
        else:
            self.L_eig_vals = is_geq_0(e_vals)
        if self.eig_vecs is None:  # K_eig_vecs = L_eig_vecs
            self.eig_vecs = is_orthonormal_columns(e_vecs)

        # L' "dual" likelihood kernel, L' = Phi Phi.T, Phi = L_gram_factor
        self.L_gram_factor = params.get('L_gram_factor', None)
        self.L_dual = None

        if self.L_gram_factor is not None:
            Phi = self.L_gram_factor
            d, N = Phi.shape
            if d < N:
                self.L_dual = Phi.dot(Phi.T)
                print('L_dual = Phi Phi.T was computed: Phi (dxN) with d<N')
            else:
                if self.L is None:
                    self.L = Phi.T.dot(Phi)
                    print('L = Phi.T Phi was computed: Phi (dxN) with d>=N')

        # L likelihood function representation
        # eval_L(X, Y) = L(X, Y)
        # eval_L(X) = L(X, X)
        self.eval_L, self.X_data = params.get('L_eval_X_data', [None, None])
        self.intermediate_sample_info = None

        if self.eval_L is not None:
            if not callable(self.eval_L):
                raise ValueError(
                    'eval_L should be a positive semi-definite kernel function'
                )
        if self.X_data is not None:
            if not (self.X_data.size and self.X_data.ndim == 2):
                err_print = [
                    'Wrong shape = {}'.format(self.X_data.shape),
                    'X_data should be a non empty (N x d) ndarray'
                ]
                raise ValueError('\n'.join(err_print))