def test_pt_cache(self): test_x = np.array([[0.5]]) self.surrogate.predict(test_x) # Mess with internals to ensure cache is being used. self.surrogate.interpolant._KData = None mu = self.surrogate.linearize(test_x) assert_rel_error(self, mu, np.array([[2.34609214]]), 1e-6)
def test_1d_ill_conditioned(self): # Test for least squares solver utilization when ill-conditioned x = array([[case] for case in linspace(0., 1., 40)]) y = sin(x) surrogate = KrigingSurrogate() surrogate.fit(x, y) new_x = array([0.5]) mu, sigma = surrogate.predict(new_x) self.assertTrue(sigma < 1.1e-8) assert_rel_error(self, mu, sin(0.5), 1e-6)
def test_vector_derivs(self): surrogate = RSurfaceSurrogate() x = array([[a, b] for a, b in itertools.product(linspace(0, 1, 10), repeat=2) ]) y = array([[a + b, a - b] for a, b in x]) surrogate.fit(x, y) jac = surrogate.linearize(array([[0.5, 0.5]])) assert_rel_error(self, jac, array([[1, 1], [1, -1]]), 1e-5)
def test_1d_training(self): x = array([[0.0], [2.0], [3.0], [4.0], [6.0]]) y = array([[branin_1d(case)] for case in x]) surrogate = KrigingSurrogate() surrogate.fit(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9) assert_rel_error(self, sigma, 0, 1e-6)
def test_1d_predictor(self): x = array([[0.0], [2.0], [3.0], [4.0], [6.0]]) y = array([[branin_1d(case)] for case in x]) surrogate = RSurfaceSurrogate() surrogate.fit(x, y) new_x = array([pi]) mu = surrogate.predict(new_x) assert_rel_error(self, mu, 1.73114, 1e-4)
def test_vector_output(self): surrogate = RSurfaceSurrogate() x = array([[0.], [2.], [4.]]) y = array([[0., 0.], [1., 1.], [2., 0.]]) surrogate.fit(x, y) for x0, y0 in zip(x, y): mu = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9)
def test_vector_output(self): surrogate = KrigingSurrogate() y = array([[0., 0.], [1., 1.], [2., 0.]]) x = array([[0.], [2.], [4.]]) surrogate.fit(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9) assert_rel_error(self, sigma, 0, 1e-6)
def test_1d_predictor(self): x = array([[0.0], [2.0], [3.0], [4.0], [6.0]]) y = array([[branin_1d(case)] for case in x]) surrogate = KrigingSurrogate() surrogate.fit(x, y) new_x = array([pi]) mu, sigma = surrogate.predict(new_x) assert_rel_error(self, mu, 0.397887, 1e-1) assert_rel_error(self, sigma, 0.0294172, 1e-2)
def test_2d(self): x = array([[-2., 0.], [-0.5, 1.5], [1., 1.], [0., .25], [.25, 0.], [.66, .33]]) y = array([[branin(case)] for case in x]) surrogate = RSurfaceSurrogate() surrogate.fit(x, y) for x0, y0 in zip(x, y): mu = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9) mu = surrogate.predict(array([.5, .5])) assert_rel_error(self, mu, branin([.5, .5]), 1e-1)
def test_bulk_prediction(self): test_x = np.array([[1., 0.5], [0.5, 1.0], [1.0, 1.5], [1.5, 1.], [0., 1.], [.5, .5]]) a = 0.05453616 b = 0.5013363 c = 0.33860606 d = 0.13507662 expected_y = np.array([[a, b, 0.5, a], [a, b, 0.5, a], [a, b, 0.5, a], [a, b, 0.5, a], [c, d, 0.5, c], [0.37840446, 0.336283, 0.5, 0.37840446]]) mu = self.surrogate.predict(test_x) assert_rel_error(self, mu, expected_y, 1e-6)
def test_jacobian(self): test_x = np.array([[0.5, 0.5], [0.5, 1.5], [1.5, 1.5], [1.5, 0.5]]) a = -0.97153433 b = -0.97153433 c = 0.59055939 d = 0.59055939 expected_deriv = np.array([[[a, b], [c, d], [0., 0.], [a, b]], [[a, -b], [c, -d], [0., 0.], [a, -b]], [[-a, -b], [-c, -d], [0., 0.], [-a, -b]], [[-a, b], [-c, d], [0., 0.], [-a, b]]]) for x0, y0 in zip(test_x, expected_deriv): mu = self.surrogate.linearize(x0) assert_rel_error(self, mu, y0, 1e-6)
def test_jacobian(self): test_x = np.array([[1., 0.5], [0.5, 1.], [1., 1.5], [1.5, 1.] ]) expected_deriv = np.array([ [[0., -1.], [0., 1.], [0., 0.], [0., -1.]], [[-1., 0.], [1., 0.], [0., 0.], [-1., 0.]], [[0., 1.], [0., -1.], [0., 0.], [0., 1.]], [[1., 0.], [-1., 0.], [0., 0.], [1., 0.]] ]) for x0, y0 in zip(test_x, expected_deriv): mu = self.surrogate.linearize(x0) assert_rel_error(self, mu, y0, 1e-9)
def test_bulk_prediction(self): test_x = np.array([[1., 0.5], [0.5, 1.0], [1.0, 1.5], [1.5, 1.], [0., 1.], [.5, .5]]) a = ((16. / (5 * np.sqrt(5.)) + 16. / (13. * np.sqrt(13.))) / (16. / (5 * np.sqrt(5.)) + 16. / (13. * np.sqrt(13.)) + 8.)) b = 8. / (8. + 16. / (5 * np.sqrt(5.)) + 16. / (13. * np.sqrt(13.))) c = (2. + 2. / (5. * np.sqrt(5))) / (3. + 2. / (5. * np.sqrt(5))) d = 1. / (3. + 2. / (5. * np.sqrt(5))) expected_y = np.array([[a, b, 0.5, a], [a, b, 0.5, a], [a, b, 0.5, a], [a, b, 0.5, a], [c, d, 0.5, c], [0.54872067, 0.45127933, 0.5, 0.54872067]]) mu = self.surrogate.predict(test_x, n=5, dist_eff=3) assert_rel_error(self, mu, expected_y, 1e-6)
def test_bulk_prediction(self): test_x = np.array([[1., 0.5], [0.5, 1.0], [1.0, 1.5], [1.5, 1.], [0., 1.], [.5, .5] ]) expected_y = np.array([[0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5], [1., 0., 0.5, 1.], [0.5, 0.5, 0.5, 0.5] ]) mu = self.surrogate.predict(test_x) assert_rel_error(self, mu, expected_y, 1e-9)
def test_prediction(self): test_x = np.array([[1., 0.5], [0.5, 1.0], [1.0, 1.5], [1.5, 1.], [0., 1.], [.5, .5] ]) expected_y = np.array([[0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5], [1., 0., 0.5, 1.], [0.5, 0.5, 0.5, 0.5] ]) for x0, y0 in zip(test_x, expected_y): mu = self.surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9)
def test_2d(self): 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]) surrogate = KrigingSurrogate() surrogate.fit(x, y) for x0, y0 in zip(x, y): mu, sigma = surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9) assert_rel_error(self, sigma, 0, 1e-6) mu, sigma = surrogate.predict([5., 5.]) assert_rel_error(self, mu, branin([5., 5.]), 1e-1) assert_rel_error(self, sigma, 5.79, 1e-2)
def test_training(self): for x0, y0 in zip(self.x, self.y): mu = self.surrogate.predict(x0) assert_rel_error(self, mu, y0, 1e-9)