def test_ky_hyp_grad(params): hyps, name, kernel, cutoffs, \ kernel_m, hyps_list, hyps_mask_list = params func = get_ky_and_hyp hyp_mat, ky_mat = func(hyps, name, kernel[1], cutoffs) size = len(flare.gp_algebra._global_training_data[name]) like, like_grad = \ get_like_grad_from_mats(ky_mat, hyp_mat, name) delta = 0.001 for i in range(len(hyps)): newhyps = np.copy(hyps) newhyps[i] += delta hyp_mat_p, ky_mat_p = func(newhyps, name, kernel[1], cutoffs) like_p, like_grad_p = \ get_like_grad_from_mats(ky_mat_p, hyp_mat_p, name) newhyps[i] -= 2 * delta hyp_mat_m, ky_mat_m = func(newhyps, name, kernel[1], cutoffs) like_m, like_grad_m = \ get_like_grad_from_mats(ky_mat_m, hyp_mat_m, name) diff = np.abs(like_grad[i] - (like_p - like_m) / 2. / delta) assert (diff < 1e-3), "wrong calculation of hyp_mat"
def test_ky_hyp_grad(): hyps, training_data, training_labels, kernel, cutoffs = \ get_random_training_set(10) func = gp_algebra.get_ky_and_hyp hyp_mat, ky_mat = func(hyps, training_data, kernel[1], cutoffs) print(hyp_mat.shape, ky_mat.shape, len(training_labels), training_labels[0]) like, like_grad = \ get_like_grad_from_mats(ky_mat, hyp_mat, training_labels) delta = 0.001 for i in range(len(hyps)): newhyps = np.copy(hyps) newhyps[i] += delta hyp_mat_p, ky_mat_p = func(newhyps, training_data, kernel[1], cutoffs) like_p, like_grad_p = \ get_like_grad_from_mats(ky_mat_p, hyp_mat_p, training_labels) newhyps[i] -= 2*delta hyp_mat_m, ky_mat_m = func(newhyps, training_data, kernel[1], cutoffs) like_m, like_grad_m = \ get_like_grad_from_mats(ky_mat_m, hyp_mat_m, training_labels) diff = np.abs(like_grad[i]-(like_p-like_m)/2./delta) assert (diff < 1e-3), "wrong calculation of hyp_mat"
def test_grad(): hyps, training_data, training_labels, kernel, cutoffs, \ kernel_m, hyps_list, hyps_mask_list = \ get_random_training_set(10) # obtain reference func = gp_algebra.get_ky_and_hyp hyp_mat, ky_mat = func(hyps, training_data, kernel[1], cutoffs) like0, like_grad0 = \ get_like_grad_from_mats(ky_mat, hyp_mat, training_labels) # serial implementation func = gp_algebra.get_ky_and_hyp hyp_mat, ky_mat = func(hyps, training_data, kernel[1], cutoffs) like, like_grad = \ get_like_grad_from_mats(ky_mat, hyp_mat, training_labels) assert (like == like0), "wrong likelihood" assert np.max(np.abs(like_grad - like_grad0)) == 0, "wrong likelihood" func = gp_algebra.get_ky_and_hyp for i in range(len(hyps_list)): hyps = hyps_list[i] hyps_mask = hyps_mask_list[i] if hyps_mask is None: ker = kernel[1] else: ker = kernel_m[1] hyp_mat, ky_mat = func(hyps, training_data, ker, cutoffs, hyps_mask) like, like_grad = \ get_like_grad_from_mats(ky_mat, hyp_mat, training_labels) print(like, like0) assert (like == like0), "wrong likelihood" if (i == 0): like_grad9 = like_grad print(like_grad, like_grad0) diff = 0 if (i == 1): diff = (np.max(np.abs(like_grad - like_grad9[[0, 2, 4, 6, 8]]))) elif (i == 2): diff = (np.max(np.abs(like_grad - like_grad9[[0, 2, 4, 6]]))) elif (i == 3): diff = (np.max(np.abs(like_grad - like_grad0))) elif (i == 4): diff = (np.max(np.abs(like_grad - like_grad0))) assert (diff==0), "multi hyps implementation is wrong"\ f"in case {i}"
def test_ky_and_hyp(params, ihyps, ky_mat_ref): name, cutoffs, hyps_mask_list, _ = params hyps_mask = hyps_mask_list[ihyps] hyps = hyps_mask['hyps'] kernel = str_to_kernel_set(hyps_mask['kernels'], 'mc', hyps_mask) func = get_ky_and_hyp # serial version hypmat_ser, ky_mat_ser = func(hyps, name, kernel[1], cutoffs, hyps_mask) # parallel version hypmat_par, ky_mat_par = func(hyps, name, kernel[1], cutoffs, hyps_mask, n_cpus=2) ref = ky_mat_ref[:ky_mat_ser.shape[0], :ky_mat_ser.shape[1]] assert np.isclose(ky_mat_ser, ref, rtol=1e-5).all(), \ "serial implementation is not consistent with get_Ky_mat" assert np.isclose(ky_mat_par, ref, rtol=1e-5).all(), \ "parallel implementation is not consistent with get_Ky_mat" assert np.isclose(hypmat_ser, hypmat_par, rtol=1e-5).all(), \ "serial implementation is not consistent with parallel implementation" # analytical form hyp_mat, ky_mat = func(hyps, name, kernel[1], cutoffs, hyps_mask) _, like_grad = get_like_grad_from_mats(ky_mat, hyp_mat, name) delta = 0.001 for i in range(len(hyps)): newhyps = np.copy(hyps) newhyps[i] += delta hyp_mat_p, ky_mat_p = func(newhyps, name, kernel[1], cutoffs, hyps_mask) like_p, _ = \ get_like_grad_from_mats(ky_mat_p, hyp_mat_p, name) newhyps[i] -= 2 * delta hyp_mat_m, ky_mat_m = func(newhyps, name, kernel[1], cutoffs, hyps_mask) like_m, _ = \ get_like_grad_from_mats(ky_mat_m, hyp_mat_m, name) # numerical form numeric = (like_p - like_m) / 2. / delta assert np.isclose(like_grad[i], numeric, rtol=1e-3 ), \ f"wrong calculation of hyp_mat {i}"
def test_grad(params): hyps, name, kernel, cutoffs, \ kernel_m, hyps_list, hyps_mask_list = params # obtain reference func = get_ky_and_hyp hyp_mat, ky_mat = func(hyps, name, kernel[1], cutoffs) like0, like_grad0 = \ get_like_grad_from_mats(ky_mat, hyp_mat, name) # serial implementation func = get_ky_and_hyp hyp_mat, ky_mat = func(hyps, name, kernel[1], cutoffs) like, like_grad = \ get_like_grad_from_mats(ky_mat, hyp_mat, name) assert (like == like0), "wrong likelihood" assert np.max(np.abs(like_grad - like_grad0)) == 0, "wrong likelihood" func = get_ky_and_hyp for i in range(len(hyps_list)): hyps = hyps_list[i] hyps_mask = hyps_mask_list[i] if hyps_mask is None: ker = kernel[1] else: ker = kernel_m[1] hyp_mat, ky_mat = func(hyps, name, ker, cutoffs, hyps_mask) like, like_grad = \ get_like_grad_from_mats(ky_mat, hyp_mat, name) assert (like == like0), "wrong likelihood" if (i == 0): like_grad9 = like_grad diff = 0 if (i == 1): diff = (np.max(np.abs(like_grad - like_grad9[[0, 2, 4, 6, 8]]))) elif (i == 2): diff = (np.max(np.abs(like_grad - like_grad9[[0, 2, 4, 6]]))) elif (i == 3): diff = (np.max(np.abs(like_grad - like_grad0))) elif (i == 4): diff = (np.max(np.abs(like_grad - like_grad0))) assert (diff==0), "multi hyps implementation is wrong"\ f"in case {i}"
def set_L_alpha(self): if self.par: hyp_mat, ky_mat = \ get_ky_and_hyp_par(self.hyps, self.training_data, self.training_labels_np, self.kernel_grad, self.cutoffs) else: hyp_mat, ky_mat = \ get_ky_and_hyp(self.hyps, self.training_data, self.training_labels_np, self.kernel_grad, self.cutoffs) like, like_grad = \ get_like_grad_from_mats(ky_mat, hyp_mat, self.training_labels_np) l_mat = np.linalg.cholesky(ky_mat) l_mat_inv = np.linalg.inv(l_mat) ky_mat_inv = l_mat_inv.T @ l_mat_inv alpha = np.matmul(ky_mat_inv, self.training_labels_np) self.ky_mat = ky_mat self.l_mat = l_mat self.alpha = alpha self.ky_mat_inv = ky_mat_inv self.l_mat_inv = l_mat_inv self.like = like self.like_grad = like_grad
def set_L_alpha(self): """ Invert the covariance matrix, setting L (a lower triangular matrix s.t. L L^T = (K + sig_n^2 I) ) and alpha, the inverse covariance matrix multiplied by the vector of training labels. The forces and variances are later obtained using alpha. :return: """ if self.par: hyp_mat, ky_mat = \ get_ky_and_hyp_par(self.hyps, self.training_data, self.training_labels_np, self.kernel_grad, self.cutoffs) else: hyp_mat, ky_mat = \ get_ky_and_hyp(self.hyps, self.training_data, self.training_labels_np, self.kernel_grad, self.cutoffs) like, like_grad = \ get_like_grad_from_mats(ky_mat, hyp_mat, self.training_labels_np) l_mat = np.linalg.cholesky(ky_mat) l_mat_inv = np.linalg.inv(l_mat) ky_mat_inv = l_mat_inv.T @ l_mat_inv alpha = np.matmul(ky_mat_inv, self.training_labels_np) self.ky_mat = ky_mat self.l_mat = l_mat self.alpha = alpha self.ky_mat_inv = ky_mat_inv self.l_mat_inv = l_mat_inv self.likelihood = like self.likelihood_gradient = like_grad
def test_grad(): hyps, training_data, training_labels, kernel, cutoffs = \ get_random_training_set(10) # obtain reference func = gp_algebra.get_ky_and_hyp hyp_mat, ky_mat = func(hyps, training_data, kernel[1], cutoffs) like0, like_grad0 = \ get_like_grad_from_mats(ky_mat, hyp_mat, training_labels) # serial implementation func = gp_algebra.get_ky_and_hyp hyp_mat, ky_mat = func(hyps, training_data, kernel[1], cutoffs) like, like_grad = \ get_like_grad_from_mats(ky_mat, hyp_mat, training_labels) assert (like==like0), "wrong likelihood" assert np.max(np.abs(like_grad-like_grad0))==0, "wrong likelihood"