コード例 #1
0
    def cross_cov(self, inputs_1, inputs_2):
        """

        :param inputs_1: np.array(nxd)
        :param inputs_2: np.array(mxd)
        :return: np.array(nxm)
        """
        r2 = np.abs(
            Distances.dist_square_length_scale(self.length_scale.value,
                                               inputs_1, inputs_2))
        r = np.sqrt(r2)
        cov = (1.0 + np.sqrt(5) * r +
               (5.0 / 3.0) * r2) * np.exp(-np.sqrt(5) * r)
        return cov
コード例 #2
0
    def gradient_respect_distance_cross(cls,
                                        ls,
                                        inputs_1,
                                        inputs_2,
                                        second=False):
        """

        :param ls: (ParameterEntity) length_scale
        :param inputs_1: np.array(nxd)
        :param inputs_2: np.array(mxd)
        :param second: (boolean) Computes second derivative if it's True.
        :return: np.array(nxm) or {'first': np.array(nxm), 'second': np.array(nxm)}
        """

        r2 = np.abs(
            Distances.dist_square_length_scale(ls.value, inputs_1, inputs_2))
        r = np.sqrt(r2)

        exp_r = np.exp(-np.sqrt(5) * r)

        part_1 = (1.0 + np.sqrt(5) * r +
                  (5.0 / 3.0) * r2) * exp_r * (-np.sqrt(5))
        part_2 = (exp_r * (np.sqrt(5) + (10.0 / 3.0) * r))
        derivate_respect_to_r = part_1 + part_2

        if not second:
            return derivate_respect_to_r

        part_0 = (10.0 / 3.0) * exp_r
        part_1 = part_1 * (-np.sqrt(5.0))
        part_3 = 2.0 * (
            (10.0 / 3.0) * r + np.sqrt(5.0)) * exp_r * (-np.sqrt(5.0))

        hessian_respect_to_r = part_0 + part_1 + part_3

        sol = {
            'first': derivate_respect_to_r,
            'second': hessian_respect_to_r,
        }

        return sol
コード例 #3
0
    def test_hessian_distance_length_scale_respect_point(self):
        params = np.array([1.0, 5.0])
        point = np.array([[4.5, 7.5]])
        inputs = np.array([[5.0, 6.0], [8.0, 9.0]])
        result = Distances.gradient_distance_length_scale_respect_point(
            params, point, inputs, second=True)
        result = result['second']

        dh = 0.00001
        finite_diff = FiniteDifferences.second_order_central(
            lambda x: np.sqrt(
                Distances.dist_square_length_scale(
                    params, x.reshape((1, len(x))), inputs)), point[0, :],
            np.array([dh]))

        for i in xrange(2):
            for j in xrange(2):
                print i, j
                npt.assert_almost_equal(
                    finite_diff[i, j],
                    np.array([[result[0, i, j], result[1, i, j]]]),
                    decimal=5)
コード例 #4
0
 def test_dist_square_length_scale(self):
     assert Distances.dist_square_length_scale(self.ls, self.x1) == [[0.0]]