def test_set_parameters(self):
        length = ParameterEntity('scale_', np.array([1, 2]), None)
        sigma2 = ParameterEntity('sigma2', np.array([3]), None)

        self.matern52.set_parameters([length], sigma2=sigma2)

        assert self.matern52.hypers == {'scale_': length, 'sigma2': sigma2}
Beispiel #2
0
 def setUp(self):
     self.dimension = 2
     self.length_scale = ParameterEntity('scale', np.array([1.0, 2.0]),
                                         None)
     self.sigma2 = ParameterEntity('sigma2', np.array([3]), None)
     self.matern52 = Matern52(self.dimension, self.length_scale)
     self.matern52 = ScaledKernel(self.dimension, self.matern52,
                                  self.sigma2)
    def define_kernel_from_array(cls, dimension, params, **kernel_parameters):
        """
        :param dimension: (int) dimension of the domain of the kernel
        :param params: (np.array(k)) The first part are the parameters for length_scale.

        :return: Matern52
        """

        alpha = ParameterEntity('alpha', [params[0]], None)
        beta = ParameterEntity('beta', [params[1]], None)

        return cls(alpha, beta)
    def setUp(self):
        self.n_tasks = 1
        self.prior_2 = UniformPrior(1, [1], [100])
        self.lower_triang = ParameterEntity('lower_triang', np.array([1.0]), self.prior_2)
        self.task_kernel = TasksKernel(self.n_tasks, self.lower_triang)
        self.inputs = np.array([[0]])
        self.inputs_ = np.array([[0], [1]])

        lower_triang = ParameterEntity('lower_triang', np.array([0.0, 0.0]), None)
        self.task_same_cor = TasksKernel(2, lower_triang, same_correlation=True)

        lower_triang = ParameterEntity('lower_triang', np.array([0.0]), None)
        self.task_same_cor_1 = TasksKernel(1, lower_triang, same_correlation=True)
    def setUp(self):
        np.random.seed(2)
        n_points = 100
        normal_noise = np.random.normal(0, 1.0, n_points)
        points = np.linspace(0, 10, n_points)
        points = points.reshape([n_points, 1])
        kernel = Matern52.define_kernel_from_array(1, np.array([2.0]))
        self.sigma2 = ParameterEntity(SIGMA2_NAME, np.array([1.0]), None)
        kernel = ScaledKernel(1, kernel, self.sigma2)

        function = SampleFunctions.sample_from_gp(points, kernel)
        function = function[0, :]
        evaluations = function + normal_noise + 10.0
        self.training_data_gp = {
            "evaluations": list(evaluations),
            "points": points,
            "var_noise": []
        }
        bounds = None
        self.gp_gaussian = GPFittingGaussian([MATERN52_NAME],
                                             self.training_data_gp, [1],
                                             bounds,
                                             max_steps_out=1000)

        self.gp_gaussian_2 = GPFittingGaussian([SCALED_KERNEL, MATERN52_NAME],
                                               self.training_data_gp, [1],
                                               bounds,
                                               max_steps_out=1000)
Beispiel #6
0
    def test_compare_kernels(self):
        kernel_t = TasksKernel(self.dimension, np.array([0.0]))
        assert ScaledKernel.compare_kernels(self.matern52, kernel_t) is False

        kernel_s = Matern52(3, self.length_scale)
        assert ScaledKernel.compare_kernels(self.matern52, kernel_s) is False

        kernel_s = Matern52(2, self.length_scale)
        assert ScaledKernel.compare_kernels(self.matern52, kernel_s) is False

        sigma2 = ParameterEntity('sigma2', np.array([1]), None)
        kernel = ScaledKernel(self.dimension, kernel_s, sigma2)
        assert ScaledKernel.compare_kernels(self.matern52, kernel) is False

        kernel_s = Matern52(
            2, ParameterEntity('scale', np.array([1.0, 3.0]), None))
        kernel = ScaledKernel(self.dimension, kernel_s, self.sigma2)
        assert ScaledKernel.compare_kernels(self.matern52, kernel) is False
    def test_cross_cov(self):
        r2 = np.array([[0.0, 1.25], [1.25, 0.0]])
        r = np.sqrt(r2)

        left_term = ((1.0 + np.sqrt(5) * r + (5.0 / 3.0) * r2) *
                     np.exp(-np.sqrt(5) * r) * np.array([3]))[0, 1]
        comparisons = left_term == self.matern52.cross_cov(
            self.inputs, self.inputs)[0, 1]
        assert np.all(comparisons)

        point_1 = np.array([[2.0, 4.0]])
        point_2 = np.array([[3.0, 5.0]])

        matern52 = Matern52(
            2, ParameterEntity('scale', np.array([2.0, 3.0]), None))
        matern52 = ScaledKernel(
            2, matern52, ParameterEntity('sigma2', np.array([4.0]), None))

        assert np.all(
            matern52.cross_cov(point_1, point_2) == np.array(
                [[3.0737065834936015]]))

        inputs_1 = np.array([[2.0, 4.0], [3.0, 5.0]])
        inputs_2 = np.array([[1.5, 9.0], [-3.0, 8.0]])

        assert np.all(
            matern52.cross_cov(inputs_1, inputs_2) == np.array([[
                0.87752659905500319, 0.14684671522649542
            ], [1.0880320585678382, 0.084041575076539962]]))

        inputs_1 = np.array([[2.0, 4.0]])
        inputs_2 = np.array([[1.5, 9.0], [-3.0, 8.0]])

        assert np.all(
            matern52.cross_cov(inputs_1, inputs_2) == np.array(
                [[0.87752659905500319, 0.14684671522649542]]))

        inputs_1 = np.array([[2.0, 4.0], [3.0, 5.0]])
        inputs_2 = np.array([[1.5, 9.0]])

        npt.assert_almost_equal(
            matern52.cross_cov(inputs_1, inputs_2),
            np.array([[0.87752659905500319], [1.0880320585678382]]))
    def setUp(self):
        self.dimension = 2
        self.length_scale = ParameterEntity('scale', np.array([1.0, 2.0]),
                                            None)
        self.sigma2 = ParameterEntity('sigma2', np.array([3]), None)
        self.matern52 = Matern52(self.dimension, self.length_scale)
        self.matern52 = ScaledKernel(self.dimension, self.matern52,
                                     self.sigma2)

        self.inputs = np.array([[1, 0], [0, 1]])

        self.prior = UniformPrior(2, [1, 1], [100, 100])
        self.prior_2 = UniformPrior(1, [1], [100])
        self.matern52_ = Matern52(
            2,
            ParameterEntity(LENGTH_SCALE_NAME, np.array([2.0, 3.0]),
                            self.prior))
        self.matern52_ = ScaledKernel(
            self.dimension, self.matern52_,
            ParameterEntity('sigma2', np.array([4.0]), self.prior_2))
    def define_kernel_from_array(cls, dimension, params, **kernel_parameters):
        """
        :param dimension: (int) dimension of the domain of the kernel
        :param params: (np.array(k)) The first part are the parameters for length_scale.

        :return: Matern52
        """

        length_scale = ParameterEntity(LENGTH_SCALE_NAME, params[0:dimension],
                                       None)

        return cls(dimension, length_scale)
Beispiel #10
0
    def define_kernel_from_array(cls, dimension, params, **kwargs):
        """
        :param dimension: (int) number of tasks
        :param params: (np.array(k))
        :param kwargs: {SAME_CORRELATION: boolean}

        :return: TasksKernel
        """

        lower_triang = ParameterEntity(LOWER_TRIANG_NAME, params, None)

        same_correlation = kwargs.get(SAME_CORRELATION, False)

        return cls(dimension, lower_triang, same_correlation=same_correlation)
Beispiel #11
0
    def define_kernel_from_array(cls, dimension, params, *args):
        """
        :param dimension: (int) dimension of the kernel instance used in this kernel
        :param params: (np.array(k)) The first part are the parameters for the kernel, the
            second part is the parameter for sigma2.
        :param args: [str] name of the kernel instance

        :return: scaled kernel
        """

        for name in args[0]:
            kernel_ct = find_kernel_constructor(name)
            kernel = kernel_ct.define_kernel_from_array(dimension, params[0: -1])

        sigma2 = ParameterEntity(SIGMA2_NAME, params[-1:], None)

        return cls(dimension, kernel, sigma2)
    def test_set_parameters(self):
        lower_traing = ParameterEntity('lw', np.array([3.0]), None)

        self.task_kernel.set_parameters(lower_traing)

        assert self.task_kernel.hypers == {'lw': lower_traing}
        assert self.task_kernel.chol_base_cov_matrix == np.array([[np.exp(3.0)]])
        assert self.task_kernel.base_cov_matrix == np.array([[np.exp(6.0)]])

        self.task_kernel.compute_cov_matrix()
        assert self.task_kernel.chol_base_cov_matrix == np.array([[np.exp(3.0)]])
        assert self.task_kernel.base_cov_matrix == np.array([[np.exp(6.0)]])

        self.task_same_cor.compute_cov_matrix()
        assert np.all(self.task_same_cor.chol_base_cov_matrix == np.array([[2.0, 1.0], [1.0, 2.0]]))
        assert np.all(self.task_same_cor.base_cov_matrix == np.array([[2.0, 1.0], [1.0, 2.0]]))

        self.task_same_cor_1.compute_cov_matrix()
        assert self.task_same_cor_1.chol_base_cov_matrix == np.array([[1.0]])
        assert self.task_same_cor_1.base_cov_matrix == np.array([[1.0]])
Beispiel #13
0
    def define_default_kernel(cls, dimension, bounds=None, default_values=None,
                              parameters_priors=None, *args):
        """
        :param dimension: (int) dimension of the domain of the kernel instance
        :param bounds: [[float, float]], lower bound and upper bound for each entry of the domain.
            This parameter is used to compute priors in a smart way.
        :param default_values: (np.array(k)) The first part are the parameters for the instance of
            the kernel, the second part is the parameter for sigma2.
        :param parameters_priors: {
            PARAMATER_NAME: [float],
            SIGMA2_NAME: float,
        }
        :param args: [str] List with the names of the default kernel

        :return: scaled kernel
        """

        if parameters_priors is None:
            parameters_priors = {}

        default_values_kernel = None

        if default_values is not None:
            default_values_kernel = default_values[0: -1]
            default_value_sigma = default_values[-1:]
        else:
            default_value_sigma = [parameters_priors.get(SIGMA2_NAME, 1.0)]

        for name in args[0]:
            kernel_ct = find_kernel_constructor(name)
            kernel = kernel_ct.define_default_kernel(dimension, bounds, default_values_kernel,
                                                     parameters_priors)

        sigma2 = ParameterEntity(SIGMA2_NAME, default_value_sigma,
                                 LogNormalSquare(1, 1.0, np.sqrt(default_value_sigma)),
                                 bounds=[(SMALLEST_POSITIVE_NUMBER, None)])

        kernel_scaled = cls(dimension, kernel, sigma2)

        return kernel_scaled
    def test_sample_parameters(self):
        parameters = self.matern52_.hypers_as_list
        samples = []
        np.random.seed(1)
        for parameter in parameters:
            samples.append(parameter.sample_from_prior(2))
        assert np.all(
            self.matern52_.sample_parameters(2, random_seed=1) == np.array([[
                samples[0][0, 0], samples[0][0, 1], samples[1][0]
            ], [samples[0][1, 0], samples[0][1, 1], samples[1][1]]]))

        np.random.seed(1)
        matern52 = Matern52(
            2,
            ParameterEntity(LENGTH_SCALE_NAME, np.array([2.0, 3.0]),
                            self.prior))
        samples = []
        parameters1 = matern52.hypers_as_list
        for parameter in parameters1:
            samples.append(parameter.sample_from_prior(2))
        assert np.all(
            matern52.sample_parameters(2, random_seed=1) == samples[0])
Beispiel #15
0
class TestParameterEntity(unittest.TestCase):
    def setUp(self):
        self.name = 'test'
        self.value = np.array([1, 2])
        self.prior = create_autospec(UniformPrior)
        self.parameter = ParameterEntity(self.name, self.value, self.prior)

    def test_set_value(self):
        self.parameter.set_value(np.array([3, 4]))
        assert np.all(self.parameter.value) == np.all(np.array([3, 4]))

    def test_log_prior(self):
        expect(self.prior).logprob.once().and_return(0.0)
        assert 0.0 == self.parameter.log_prior()

    def test_sample_from_prior(self):
        expect(self.prior).sample.twice().and_return(0.0)
        assert 0.0 == self.parameter.sample_from_prior(1)
        assert 0.0 == self.parameter.sample_from_prior(1, 1)

    def test_get_bounds(self):
        assert self.parameter.get_bounds(2) == 2 * [
            (SMALLEST_NUMBER, LARGEST_NUMBER)
        ]
Beispiel #16
0
 def setUp(self):
     self.name = 'test'
     self.value = np.array([1, 2])
     self.prior = create_autospec(UniformPrior)
     self.parameter = ParameterEntity(self.name, self.value, self.prior)
class TestTasksKernel(unittest.TestCase):

    def setUp(self):
        self.n_tasks = 1
        self.prior_2 = UniformPrior(1, [1], [100])
        self.lower_triang = ParameterEntity('lower_triang', np.array([1.0]), self.prior_2)
        self.task_kernel = TasksKernel(self.n_tasks, self.lower_triang)
        self.inputs = np.array([[0]])
        self.inputs_ = np.array([[0], [1]])

        lower_triang = ParameterEntity('lower_triang', np.array([0.0, 0.0]), None)
        self.task_same_cor = TasksKernel(2, lower_triang, same_correlation=True)

        lower_triang = ParameterEntity('lower_triang', np.array([0.0]), None)
        self.task_same_cor_1 = TasksKernel(1, lower_triang, same_correlation=True)

    def test_hypers(self):
        assert self.task_kernel.dimension_parameters == 1
        assert {'lower_triang': self.lower_triang} == self.task_kernel.hypers

    def test_name_parameters_as_list(self):
        assert self.task_kernel.name_parameters_as_list == \
               [('lower_triang', [(0, None)])]

    def test_define_kernel_from_array(self):
        kernel = TasksKernel.define_kernel_from_array(5, np.array([1, 2, 3]))
        assert kernel.n_tasks == 5
        assert np.all(kernel.lower_triang.value == np.array([1, 2, 3]))

    def test_set_parameters(self):
        lower_traing = ParameterEntity('lw', np.array([3.0]), None)

        self.task_kernel.set_parameters(lower_traing)

        assert self.task_kernel.hypers == {'lw': lower_traing}
        assert self.task_kernel.chol_base_cov_matrix == np.array([[np.exp(3.0)]])
        assert self.task_kernel.base_cov_matrix == np.array([[np.exp(6.0)]])

        self.task_kernel.compute_cov_matrix()
        assert self.task_kernel.chol_base_cov_matrix == np.array([[np.exp(3.0)]])
        assert self.task_kernel.base_cov_matrix == np.array([[np.exp(6.0)]])

        self.task_same_cor.compute_cov_matrix()
        assert np.all(self.task_same_cor.chol_base_cov_matrix == np.array([[2.0, 1.0], [1.0, 2.0]]))
        assert np.all(self.task_same_cor.base_cov_matrix == np.array([[2.0, 1.0], [1.0, 2.0]]))

        self.task_same_cor_1.compute_cov_matrix()
        assert self.task_same_cor_1.chol_base_cov_matrix == np.array([[1.0]])
        assert self.task_same_cor_1.base_cov_matrix == np.array([[1.0]])

    def test_cov(self):
        expect(self.task_kernel).cross_cov.once().and_return(0)
        assert self.task_kernel.cov(self.inputs) == 0

    def test_cross_cov(self):
        npt.assert_almost_equal(self.task_kernel.cross_cov(self.inputs, self.inputs),
                                np.array([[np.exp(2.0)]]))

    def test_gradient_respect_parameters(self):
        expect(GradientTasksKernel).gradient_respect_parameters.once().and_return(
            {0: np.array([[0]])})

        gradient = {}
        gradient['lower_triang'] = {}
        gradient['lower_triang'][0] = np.array([[0]])

        assert self.task_kernel.gradient_respect_parameters(self.inputs) == gradient


    def test_gradient_respect_parameters_finite_differences(self):
        dh = 0.00000001
        finite_diff = FiniteDifferences.forward_difference(
            lambda params: TasksKernel.evaluate_cov_defined_by_params(params, self.inputs_, 2),
            np.array([2.0, 3.0, 4.0]), np.array([dh]))

        gradient = TasksKernel.evaluate_grad_defined_by_params_respect_params(
            np.array([2.0, 3.0, 4.0]), self.inputs_, 2)

        for i in range(3):
            npt.assert_almost_equal(finite_diff[i], gradient[i],  decimal=4)

        gradient = TasksKernel.evaluate_grad_defined_by_params_respect_params(
            np.array([2.0, 3.0]), self.inputs_, 2, **{'same_correlation': True})

        finite_diff = FiniteDifferences.forward_difference(
            lambda params: TasksKernel.evaluate_cov_defined_by_params(params, self.inputs_, 2,
                                                                      **{'same_correlation': True}),
            np.array([2.0, 3.0]), np.array([dh]))

        for i in range(2):
            npt.assert_almost_equal(finite_diff[i], gradient[i],  decimal=4)

        gradient = TasksKernel.evaluate_grad_defined_by_params_respect_params(
            np.array([2.0]), self.inputs, 1, **{'same_correlation': True})

        finite_diff = FiniteDifferences.forward_difference(
            lambda params: TasksKernel.evaluate_cov_defined_by_params(params, self.inputs, 1,
                                                                      **{'same_correlation': True}),
            np.array([2.0]), np.array([dh]))

        npt.assert_almost_equal(finite_diff[0], gradient[0],  decimal=4)

    def test_grad_respect_point(self):
        assert self.task_kernel.grad_respect_point(self.inputs, self.inputs) == np.array([[0]])

    def test_gradient_respect_parameters_gradient_class(self):
        grad = GradientTasksKernel.gradient_respect_parameters(np.array([[np.exp(1.0)]]), 1)
        assert len(grad) == 1
        npt.assert_almost_equal(grad[0], np.array([[2.0 * np.exp(2.0)]]))

    def test_evaluate_cov_defined_by_params(self):
        result = TasksKernel.evaluate_cov_defined_by_params(
            np.array([1.0, 2.0, 3.0]), self.inputs_, 2)

        kernel = TasksKernel.define_kernel_from_array(2, np.array([1.0, 2.0, 3.0]))
        assert np.all(result == kernel.cov(np.array(self.inputs_)))

    def test_evaluate_grad_defined_by_params_respect_params(self):
        result = TasksKernel.evaluate_grad_defined_by_params_respect_params(
            np.array([1.0, 2.0, 3.0]), self.inputs_, 2)
        kernel = TasksKernel.define_kernel_from_array(2, np.array([1.0, 2.0, 3.0]))

        grad_kernel = kernel.gradient_respect_parameters(self.inputs_)

        for i in range(2):
            assert np.all(result[i] == grad_kernel['lower_triangular'][i])

    def test_hypers_as_list(self):
        assert self.task_kernel.hypers_as_list == [self.lower_triang]

    def test_hypers_values_as_array(self):
        assert self.task_kernel.hypers_values_as_array == np.array([1.0])

    def test_sample_parameters(self):
        np.random.seed(1)
        value = self.lower_triang.sample_from_prior(2)
        assert np.all(self.task_kernel.sample_parameters(2, 1) == value)

    def test_get_bounds_parameters(self):
        assert [(SMALLEST_NUMBER, LARGEST_NUMBER)] == self.task_kernel.get_bounds_parameters()

    def test_update_value_parameters(self):
        self.task_kernel.update_value_parameters(np.array([2]))

        assert self.task_kernel.lower_triang.value == np.array([2])
        assert self.task_kernel.chol_base_cov_matrix == np.exp(np.array([[2]]))
        npt.assert_almost_equal(self.task_kernel.base_cov_matrix, np.exp(np.array([[4]])))

    def test_define_default_kernel(self):
        kern = TasksKernel.define_default_kernel(1)
        assert kern.lower_triang.value == np.array([0])
        assert kern.name == TASKS_KERNEL_NAME
        assert kern.dimension == 1
        assert kern.dimension_parameters == 1
        assert kern.n_tasks == 1
        assert kern.base_cov_matrix is None
        assert kern.chol_base_cov_matrix is None

        kern_1 = TasksKernel.define_default_kernel(1, None, np.array([3]))
        assert kern_1.lower_triang.value == np.array([3])
        assert kern.name == TASKS_KERNEL_NAME
        assert kern.dimension == 1
        assert kern.dimension_parameters == 1
        assert kern.n_tasks == 1
        assert kern.base_cov_matrix is None
        assert kern.chol_base_cov_matrix is None

        kern_3 = TasksKernel.define_default_kernel(1, None, np.array([3]),
                                                   **{'same_correlation': True})
        assert kern_3.lower_triang.value == np.array([3])
        assert kern_3.name == TASKS_KERNEL_NAME
        assert kern_3.dimension == 1
        assert kern_3.dimension_parameters == 1
        assert kern_3.n_tasks == 1
        assert kern_3.base_cov_matrix is None
        assert kern_3.chol_base_cov_matrix is None

        kern_3 = TasksKernel.define_default_kernel(2, None, np.array([3, 4]),
                                                   **{'same_correlation': True})
        assert np.all(kern_3.lower_triang.value == np.array([3, 4]))
        assert kern_3.name == TASKS_KERNEL_NAME
        assert kern_3.dimension == 1
        assert kern_3.dimension_parameters == 2
        assert kern_3.n_tasks == 2
        assert kern_3.base_cov_matrix is None
        assert kern_3.chol_base_cov_matrix is None


    def test_compare_kernels(self):
        kernel = TasksKernel.define_kernel_from_array(1, np.ones(1))

        kernel_ = copy.deepcopy(kernel)
        kernel_.name = 'a'
        assert TasksKernel.compare_kernels(kernel, kernel_) is False

        kernel_ = copy.deepcopy(kernel)
        kernel_.dimension = 2
        assert TasksKernel.compare_kernels(kernel, kernel_) is False

        kernel_ = copy.deepcopy(kernel)
        kernel_.dimension_parameters = 5
        assert TasksKernel.compare_kernels(kernel, kernel_) is False

        kernel_ = copy.deepcopy(kernel)
        kernel_.n_tasks = 5
        assert TasksKernel.compare_kernels(kernel, kernel_) is False

        kernel_ = copy.deepcopy(kernel)
        kernel_.lower_triang.value = np.array([0])
        assert TasksKernel.compare_kernels(kernel, kernel_) is False

        kernel_ = copy.deepcopy(kernel)
        kernel_.base_cov_matrix = np.array([[1]])
        assert TasksKernel.compare_kernels(kernel, kernel_) is False

        kernel_ = copy.deepcopy(kernel)
        kernel_.chol_base_cov_matrix = np.array([[1]])
        assert TasksKernel.compare_kernels(kernel, kernel_) is False

        kernel_ = copy.deepcopy(kernel)
        kernel_.same_correlation = True
        assert TasksKernel.compare_kernels(kernel, kernel_) is False

    def test_define_prior_parameters(self):
        data = {
            'points': np.array([[0]]),
            'evaluations': np.array([1]),
            'var_noise': None,
        }

        dimension = 2

        result = TasksKernel.define_prior_parameters(data, dimension)

        assert result == {
            LOWER_TRIANG_NAME: [0.0, -9.2103403719761818, 0.0],
        }

        result = TasksKernel.define_prior_parameters(data, dimension, same_correlation=True)

        assert result == {
            LOWER_TRIANG_NAME: [-2.3025850929940455, -2.3025850929940455],
        }

        result = TasksKernel.define_prior_parameters(data, 1, same_correlation=True)

        assert result == {
            LOWER_TRIANG_NAME: [-2.3025850929940455],
        }

    def test_evaluate_cross_cov_defined_by_params(self):
        value = TasksKernel.evaluate_cross_cov_defined_by_params(np.array([1.0]), np.array([[0.0]]),
                                                                 np.array([[0.0]]), 1)
        npt.assert_almost_equal(value, np.array([[np.exp(2.0)]]))

    def test_evaluate_grad_respect_point(self):
        result = TasksKernel.evaluate_grad_respect_point(np.array([5.0]), np.array([[0]]),
                                                         np.array([[0], [0]]), 1)

        kernel = TasksKernel.define_kernel_from_array(1, np.array([5.0]))
        assert np.all(result == kernel.grad_respect_point(np.array([[0]]), np.array([[0], [0]])))