def test_forward_difference(self): result = FiniteDifferences.forward_difference(self.f, self.x, self.h) base_eval = \ Matern52.evaluate_cov_defined_by_params(self.x, np.array([[2.0, 0.0], [0.0, 2.0]]), 2) for i in result: new_x = deepcopy(self.x) new_x[i] += self.h[0] new_eval = \ Matern52.evaluate_cov_defined_by_params( new_x, np.array([[2.0, 0.0], [0.0, 2.0]]), 2) for j in range(2): for h in range(2): assert result[i][j, h] == (new_eval[j, h] - base_eval[j, h]) / self.h[0]
def test_evaluate_cov_defined_by_params(self): result = Matern52.evaluate_cov_defined_by_params( np.array([1, 3, 5]), np.array([[4, 5]]), 2) kernel = Matern52.define_kernel_from_array(2, np.array([1, 3, 5])) assert result == kernel.cov(np.array([[4, 5]]))
def test_second_order_central(self): self.x = np.array([1.5, 1.5, 1.5]) h = np.array([0.1]) result = FiniteDifferences.second_order_central(self.f, self.x, h) base_eval = \ Matern52.evaluate_cov_defined_by_params(self.x, np.array([[2.0, 0.0], [0.0, 2.0]]), 2) for i in xrange(3): val = result[(i, i)] new_x = deepcopy(self.x) new_x[i] += h[0] new_eval = \ Matern52.evaluate_cov_defined_by_params( new_x, np.array([[2.0, 0.0], [0.0, 2.0]]), 2) new_x = deepcopy(self.x) new_x[i] -= h[0] new_eval_2 = \ Matern52.evaluate_cov_defined_by_params( new_x, np.array([[2.0, 0.0], [0.0, 2.0]]), 2) assert np.all(val == (new_eval + new_eval_2 - 2.0 * base_eval) / (h[0] ** 2)) for i in xrange(3): for j in xrange(3): if j == i: continue val = result[(i, j)] new_x = deepcopy(self.x) new_x[i] += h[0] new_x[j] += h[0] new_eval = \ Matern52.evaluate_cov_defined_by_params( new_x, np.array([[2.0, 0.0], [0.0, 2.0]]), 2) val_2 = new_eval new_x = deepcopy(self.x) new_x[i] -= h[0] new_x[j] -= h[0] new_eval_2 = \ Matern52.evaluate_cov_defined_by_params( new_x, np.array([[2.0, 0.0], [0.0, 2.0]]), 2) val_2 += new_eval_2 new_x = deepcopy(self.x) new_x[i] += h[0] new_x[j] -= h[0] new_eval = \ Matern52.evaluate_cov_defined_by_params( new_x, np.array([[2.0, 0.0], [0.0, 2.0]]), 2) val_2 -= new_eval new_x = deepcopy(self.x) new_x[i] -= h[0] new_x[j] += h[0] new_eval_2 = \ Matern52.evaluate_cov_defined_by_params( new_x, np.array([[2.0, 0.0], [0.0, 2.0]]), 2) val_2 -= new_eval_2 val_2 /= (4.0 * h * h) assert np.all(val_2 == val)
def f(x): return Matern52.evaluate_cov_defined_by_params(x, np.array([[2.0, 0.0], [0.0, 2.0]]), 2)