def test_1d_1fi_cokriging(self):
        # CoKrigingSurrogate with one fidelity could be used as a KrigingSurrogate
        # Same test as for KrigingSurrogate...  well with predicted test value adjustment

        x = np.array([[0.05], [.25], [0.61], [0.95]])
        y = np.array([0.738513784857542, -0.210367746201974, -0.489015457891476, 12.3033138316612])
        krig1 = MultiFiCoKrigingSurrogate()
        krig1.train(x, y)
        new_x = np.array([0.5])

        mu, sigma = krig1.predict(x[0])
        assert_near_equal(mu, [[y[0]]], 1e-4)
        assert_near_equal(sigma, [[0.]], 1e-4)

        mu, sigma = krig1.predict(new_x)
        assert_near_equal(mu, [[-2.0279]], 1e-3)
        assert_near_equal(sigma, [[1.3408]], 1e-3)

        # Test with theta setting instead of estimation
        krig2 = MultiFiCoKrigingSurrogate(theta=0.1, normalize=False)
        krig2.train(x, y)

        mu, sigma = krig2.predict(x[0])
        assert_near_equal(mu, [[y[0]]], 1e-4)
        assert_near_equal(sigma, [[.0]], 1e-4)

        mu, sigma = krig2.predict(new_x)
        assert_near_equal(mu, [[-1.2719]], 1e-3)
        assert_near_equal(sigma, [[0.0439]], 1e-3)
    def test_1d_1fi_cokriging(self):
        # CoKrigingSurrogate with one fidelity could be used as a KrigingSurrogate
        # Same test as for KrigingSurrogate...  well with predicted test value adjustment

        x = array([[0.05], [.25], [0.61], [0.95]])
        y = array([
            0.738513784857542, -0.210367746201974, -0.489015457891476,
            12.3033138316612
        ])
        krig1 = MultiFiCoKrigingSurrogate()
        krig1.train(x, y)
        new_x = array([0.5])

        mu, sigma = krig1.predict(x[0])
        assert_rel_error(self, mu, y[0], 1e-4)
        assert_rel_error(self, sigma, 0., 1e-4)

        mu, sigma = krig1.predict(new_x)
        assert_rel_error(self, mu, -2.0279, 1e-3)
        assert_rel_error(self, sigma, 1.3408, 1e-3)

        # Test with theta setting instead of estimation
        krig2 = MultiFiCoKrigingSurrogate(theta=0.1)
        krig2.train(x, y)

        mu, sigma = krig2.predict(x[0])
        assert_rel_error(self, mu, y[0], 1e-4)
        assert_rel_error(self, sigma, .0, 1e-4)

        mu, sigma = krig2.predict(new_x)
        assert_rel_error(self, mu, -1.2719, 1e-3)
        assert_rel_error(self, sigma, 0.0439, 1e-3)
    def test_1d_1fi_cokriging(self):
        # CoKrigingSurrogate with one fidelity could be used as a KrigingSurrogate
        # Same test as for KrigingSurrogate...  well with predicted test value adjustment

        x = array([[0.05], [.25], [0.61], [0.95]])
        y = array([0.738513784857542, -0.210367746201974, -0.489015457891476, 12.3033138316612])
        krig1 = MultiFiCoKrigingSurrogate()
        krig1.train(x, y)
        new_x = array([0.5])

        mu, sigma = krig1.predict(x[0])
        assert_rel_error(self, mu, y[0], 1e-4)
        assert_rel_error(self, sigma, 0., 1e-4)

        mu, sigma = krig1.predict(new_x)
        assert_rel_error(self, mu, -2.0279, 1e-3)
        assert_rel_error(self, sigma, 1.3408, 1e-3)

        # Test with theta setting instead of estimation
        krig2 = MultiFiCoKrigingSurrogate(theta=0.1)
        krig2.train(x, y)

        mu, sigma = krig2.predict(x[0])
        assert_rel_error(self , mu, y[0], 1e-4)
        assert_rel_error(self, sigma, .0, 1e-4)

        mu, sigma = krig2.predict(new_x)
        assert_rel_error(self, mu, -1.2719, 1e-3)
        assert_rel_error(self, sigma, 0.0439, 1e-3)
    def test_2d_1fi_cokriging(self):
        # CoKrigingSurrogate with one fidelity could be used as a KrigingSurrogate
        # Same test as for KrigingSurrogate...  well with predicted test value adjustment

        def branin(x):
            y = (x[1]-(5.1/(4.*pi**2.))*x[0]**2.+5.*x[0]/pi-6.)**2.+10.*(1.-1./(8.*pi))*cos(x[0])+10.
            return y

        x = array([[-2., 0.], [-0.5, 1.5], [1., 3.], [8.5, 4.5],
                   [-3.5, 6.], [4., 7.5], [-5., 9.], [5.5, 10.5],
                   [10., 12.], [7., 13.5], [2.5, 15.]])
        y = array([branin(case) for case in x])
        krig1 = MultiFiCoKrigingSurrogate()
        krig1.train(x, y)

        mu, sigma = krig1.predict([-2., 0.])
        assert_rel_error(self, mu, branin(x[0]), 1e-5)
        assert_rel_error(self, sigma, 0., 1e-5)

        mu, sigma = krig1.predict([5., 5.])
        assert_rel_error(self, mu, 22, 1)
        assert_rel_error(self, sigma, 13, 1)

        # Test with theta setting instead of estimation
        krig2 = MultiFiCoKrigingSurrogate(theta=[0.1])
        krig1.train(x, y)

        mu, sigma = krig1.predict([-2., 0.])
        assert_rel_error(self, mu, branin(x[0]), 1e-5)
        assert_rel_error(self, sigma, 0., 1e-5)

        mu, sigma = krig1.predict([5., 5.])
        assert_rel_error(self, mu, 22, 1)
        assert_rel_error(self, sigma, 13, 1)
    def test_normalization(self):
        # This dataset is ill conditioned if not normalized.

        size = 100
        x = np.random.random((size, 1))
        y = np.random.random(size)
        krig = MultiFiCoKrigingSurrogate(normalize=True)

        krig.train(x, y)

        # Make sure we aren't singular. We will get a few warnings during
        # training.
        krig.predict(0.5)
        assert_near_equal(sigma, [[0.]], 0.02)

    def test_2d_1fi_cokriging(self):
        # CoKrigingSurrogate with one fidelity could be used as a KrigingSurrogate
        # Same test as for KrigingSurrogate...  well with predicted test value adjustment

        def branin(x):
            y = (x[1]-(5.1/(4.*np.pi**2.))*x[0]**2.+5.*x[0]/np.pi-6.)**2.+10.*(1.-1./(8.*np.pi))*np.cos(x[0])+10.
            return y

        x = np.array([[-2., 0.], [-0.5, 1.5], [1., 3.], [8.5, 4.5],
                    [-3.5, 6.], [4., 7.5], [-5., 9.], [5.5, 10.5],
                    [10., 12.], [7., 13.5], [2.5, 15.]])
        y = np.array([branin(case) for case in x])
        krig1 = MultiFiCoKrigingSurrogate()
        krig1.train(x, y)

        mu, sigma = krig1.predict([-2., 0.])
        assert_near_equal(mu, [[branin(x[0])]], 1e-5)
        assert_near_equal(sigma, [[0.]], 1e-5)

        mu, sigma = krig1.predict([5., 5.])
        assert_near_equal(mu, [[22]], 1)
        assert_near_equal(sigma, [[13]], 1)

        # Test with theta setting instead of estimation
        krig2 = MultiFiCoKrigingSurrogate(theta=[0.1])
        krig1.train(x, y)

        mu, sigma = krig1.predict([-2., 0.])
        assert_near_equal(mu, [[branin(x[0])]], 1e-5)