def cov_main(str_cov, X, Xs, hyps, jitter=constants.JITTER_COV): assert isinstance(str_cov, str) assert isinstance(X, np.ndarray) assert isinstance(Xs, np.ndarray) assert isinstance(hyps, dict) assert isinstance(jitter, float) assert str_cov in constants.ALLOWED_GP_COV num_X = X.shape[0] num_Xs = Xs.shape[0] cov_ = np.zeros((num_X, num_Xs)) if num_X == num_Xs: cov_ += np.eye(num_X) * jitter if str_cov == 'se' or str_cov == 'matern32' or str_cov == 'matern52': assert len(X.shape) == 2 assert len(Xs.shape) == 2 num_d_X = X.shape[1] num_d_Xs = Xs.shape[1] assert num_d_X == num_d_Xs hyps, is_valid = utils_covariance.validate_hyps_dict( hyps, str_cov, num_d_X) # TODO: ValueError is appropriate? We can just raise AssertionError in validate_hyps_dict. I am not sure. if not is_valid: raise ValueError('cov_main: invalid hyperparameters.') fun_cov = choose_fun_cov(str_cov) for ind_X in range(0, num_X): for ind_Xs in range(0, num_Xs): cov_[ind_X, ind_Xs] += fun_cov(X[ind_X], Xs[ind_Xs], hyps['lengthscales'], hyps['signal']) elif str_cov in constants.ALLOWED_GP_COV_SET: list_str_cov = str_cov.split('_') str_cov = list_str_cov[1] assert len(X.shape) == 3 assert len(Xs.shape) == 3 num_d_X = X.shape[2] num_d_Xs = Xs.shape[2] assert num_d_X == num_d_Xs hyps, is_valid = utils_covariance.validate_hyps_dict( hyps, str_cov, num_d_X) # TODO: Please check this is_valid. if not is_valid: # pragma: no cover raise ValueError('cov_main: invalid hyperparameters.') for ind_X in range(0, num_X): for ind_Xs in range(0, num_Xs): cov_[ind_X, ind_Xs] += cov_set(str_cov, X[ind_X], Xs[ind_Xs], hyps['lengthscales'], hyps['signal']) else: raise NotImplementedError( 'cov_main: allowed str_cov, but it is not implemented.') return cov_
def cov_main(str_cov, X, Xs, hyps, same_X_Xs, jitter=constants.JITTER_COV ): """ It computes kernel matrix over `X` and `Xs`, where `hyps` is given. :param str_cov: the name of covariance function. :type str_cov: str. :param X: one inputs. Shape: (n, d). :type X: numpy.ndarray :param Xs: another inputs. Shape: (m, d). :type Xs: numpy.ndarray :param hyps: dictionary of hyperparameters for covariance function. :type hyps: dict. :param same_X_Xs: flag for checking `X` and `Xs` are same. :type same_X_Xs: bool. :param jitter: jitter for diagonal entries. :type jitter: float, optional :returns: kernel matrix over `X` and `Xs`. Shape: (n, m). :rtype: numpy.ndarray :raises: AssertionError, ValueError """ assert isinstance(str_cov, str) assert isinstance(X, np.ndarray) assert isinstance(Xs, np.ndarray) assert isinstance(hyps, dict) assert isinstance(same_X_Xs, bool) assert isinstance(jitter, float) assert str_cov in constants.ALLOWED_GP_COV num_X = X.shape[0] num_Xs = Xs.shape[0] cov_ = np.zeros((num_X, num_Xs)) if same_X_Xs: assert num_X == num_Xs cov_ += np.eye(num_X) * jitter if str_cov == 'eq' or str_cov == 'se' or str_cov == 'matern32' or str_cov == 'matern52': assert len(X.shape) == 2 assert len(Xs.shape) == 2 num_d_X = X.shape[1] num_d_Xs = Xs.shape[1] assert num_d_X == num_d_Xs hyps, is_valid = utils_covariance.validate_hyps_dict(hyps, str_cov, num_d_X) # TODO: ValueError is appropriate? We can just raise AssertionError in validate_hyps_dict. I am not sure. if not is_valid: raise ValueError('cov_main: invalid hyperparameters.') fun_cov = choose_fun_cov(str_cov) cov_ += fun_cov(X, Xs, hyps['lengthscales'], hyps['signal']) assert cov_.shape == (num_X, num_Xs) elif str_cov in constants.ALLOWED_GP_COV_SET: list_str_cov = str_cov.split('_') str_cov = list_str_cov[1] assert len(X.shape) == 3 assert len(Xs.shape) == 3 num_d_X = X.shape[2] num_d_Xs = Xs.shape[2] assert num_d_X == num_d_Xs hyps, is_valid = utils_covariance.validate_hyps_dict(hyps, str_cov, num_d_X) if not is_valid: raise ValueError('cov_main: invalid hyperparameters.') if not same_X_Xs: for ind_X in range(0, num_X): for ind_Xs in range(0, num_Xs): cov_[ind_X, ind_Xs] += cov_set(str_cov, X[ind_X], Xs[ind_Xs], hyps['lengthscales'], hyps['signal']) else: for ind_X in range(0, num_X): for ind_Xs in range(ind_X, num_Xs): cov_[ind_X, ind_Xs] += cov_set(str_cov, X[ind_X], Xs[ind_Xs], hyps['lengthscales'], hyps['signal']) if ind_X < ind_Xs: cov_[ind_Xs, ind_X] = cov_[ind_X, ind_Xs] else: raise NotImplementedError('cov_main: allowed str_cov, but it is not implemented.') return cov_
def test_validate_hyps_dict(): num_dim = 2 str_cov = 'matern32' cur_hyps = utils_covariance.get_hyps(str_cov, num_dim) cur_hyps.pop('noise') with pytest.raises(AssertionError) as error: _, is_valid = utils_covariance.validate_hyps_dict( cur_hyps, str_cov, num_dim) assert is_valid == True cur_hyps = utils_covariance.get_hyps(str_cov, num_dim) with pytest.raises(AssertionError) as error: _, is_valid = utils_covariance.validate_hyps_dict( cur_hyps, 'abc', num_dim) assert is_valid == True cur_hyps = utils_covariance.get_hyps(str_cov, num_dim) cur_hyps.pop('lengthscales') with pytest.raises(AssertionError) as error: _, is_valid = utils_covariance.validate_hyps_dict( cur_hyps, str_cov, num_dim) assert is_valid == True cur_hyps = utils_covariance.get_hyps(str_cov, num_dim) cur_hyps.pop('signal') with pytest.raises(AssertionError) as error: _, is_valid = utils_covariance.validate_hyps_dict( cur_hyps, str_cov, num_dim) assert is_valid == True cur_hyps = utils_covariance.get_hyps(str_cov, num_dim) cur_hyps['noise'] = 'abc' with pytest.raises(AssertionError) as error: _, is_valid = utils_covariance.validate_hyps_dict( cur_hyps, str_cov, num_dim) assert is_valid == True cur_hyps = utils_covariance.get_hyps(str_cov, num_dim) cur_hyps['noise'] = np.inf cur_hyps, is_valid = utils_covariance.validate_hyps_dict( cur_hyps, str_cov, num_dim) assert cur_hyps['noise'] == constants.BOUND_UPPER_GP_NOISE cur_hyps = utils_covariance.get_hyps(str_cov, num_dim) with pytest.raises(AssertionError) as error: _, is_valid = utils_covariance.validate_hyps_dict( cur_hyps, str_cov, 123) assert is_valid == True cur_hyps = utils_covariance.get_hyps(str_cov, num_dim) cur_hyps['lengthscales'] = 'abc' with pytest.raises(AssertionError) as error: _, is_valid = utils_covariance.validate_hyps_dict( cur_hyps, str_cov, num_dim) assert is_valid == True cur_hyps = utils_covariance.get_hyps(str_cov, num_dim) cur_hyps['signal'] = 'abc' with pytest.raises(AssertionError) as error: _, is_valid = utils_covariance.validate_hyps_dict( cur_hyps, str_cov, num_dim) assert is_valid == True
def get_optimized_kernel( X_train: np.ndarray, Y_train: np.ndarray, prior_mu: constants.TYPING_UNION_CALLABLE_NONE, str_cov: str, str_optimizer_method: str = constants.STR_OPTIMIZER_METHOD_GP, str_modelselection_method: str = constants.STR_MODELSELECTION_METHOD, use_ard: bool = constants.USE_ARD, fix_noise: bool = constants.FIX_GP_NOISE, debug: bool = False) -> constants.TYPING_TUPLE_TWO_ARRAYS_DICT: """ This function computes the kernel matrix optimized by optimization method specified, its inverse matrix, and the optimized hyperparameters. :param X_train: inputs. Shape: (n, d) or (n, m, d). :type X_train: numpy.ndarray :param Y_train: outputs. Shape: (n, 1). :type Y_train: numpy.ndarray :param prior_mu: prior mean function or None. :type prior_mu: callable or NoneType :param str_cov: the name of covariance function. :type str_cov: str. :param str_optimizer_method: the name of optimization method. :type str_optimizer_method: str., optional :param str_modelselection_method: the name of model selection method. :type str_modelselection_method: str., optional :param use_ard: flag for using automatic relevance determination. :type use_ard: bool., optional :param fix_noise: flag for fixing a noise. :type fix_noise: bool., optional :param debug: flag for printing log messages. :type debug: bool., optional :returns: a tuple of kernel matrix over `X_train`, kernel matrix inverse, and dictionary of hyperparameters. :rtype: tuple of (numpy.ndarray, numpy.ndarray, dict.) :raises: AssertionError, ValueError """ # TODO: check to input same fix_noise to convert_hyps and restore_hyps utils_gp.validate_common_args(X_train, Y_train, str_cov, prior_mu, debug) assert isinstance(str_optimizer_method, str) assert isinstance(str_modelselection_method, str) assert isinstance(use_ard, bool) assert isinstance(fix_noise, bool) utils_covariance.check_str_cov('get_optimized_kernel', str_cov, X_train.shape) assert str_optimizer_method in constants.ALLOWED_OPTIMIZER_METHOD_GP assert str_modelselection_method in constants.ALLOWED_MODELSELECTION_METHOD use_gradient = bool(str_optimizer_method != 'Nelder-Mead') # TODO: Now, use_gradient is fixed as False. # use_gradient = False time_start = time.time() if debug: logger.debug('str_optimizer_method: %s', str_optimizer_method) logger.debug('str_modelselection_method: %s', str_modelselection_method) logger.debug('use_gradient: %s', use_gradient) prior_mu_train = utils_gp.get_prior_mu(prior_mu, X_train) if str_cov in constants.ALLOWED_COV_BASE: num_dim = X_train.shape[1] elif str_cov in constants.ALLOWED_COV_SET: num_dim = X_train.shape[2] use_gradient = False if str_modelselection_method == 'ml': neg_log_ml_ = lambda hyps: gp_likelihood.neg_log_ml( X_train, Y_train, hyps, str_cov, prior_mu_train, use_ard=use_ard, fix_noise=fix_noise, use_gradient=use_gradient, debug=debug) elif str_modelselection_method == 'loocv': # TODO: add use_ard. neg_log_ml_ = lambda hyps: gp_likelihood.neg_log_pseudo_l_loocv( X_train, Y_train, hyps, str_cov, prior_mu_train, fix_noise=fix_noise, debug=debug) use_gradient = False else: # pragma: no cover raise ValueError( 'get_optimized_kernel: missing conditions for str_modelselection_method.' ) hyps_converted = utils_covariance.convert_hyps(str_cov, utils_covariance.get_hyps( str_cov, num_dim, use_ard=use_ard), fix_noise=fix_noise) if str_optimizer_method in ['BFGS', 'SLSQP']: result_optimized = scipy.optimize.minimize(neg_log_ml_, hyps_converted, method=str_optimizer_method, jac=use_gradient, options={'disp': False}) if debug: logger.debug('negative log marginal likelihood: %.6f', result_optimized.fun) logger.debug('scipy message: %s', result_optimized.message) result_optimized = result_optimized.x elif str_optimizer_method in ['L-BFGS-B', 'SLSQP-Bounded']: if str_optimizer_method == 'SLSQP-Bounded': str_optimizer_method = 'SLSQP' bounds = utils_covariance.get_range_hyps(str_cov, num_dim, use_ard=use_ard, fix_noise=fix_noise) result_optimized = scipy.optimize.minimize(neg_log_ml_, hyps_converted, method=str_optimizer_method, bounds=bounds, jac=use_gradient, options={'disp': False}) if debug: logger.debug('negative log marginal likelihood: %.6f', result_optimized.fun) logger.debug('scipy message: %s', result_optimized.message) result_optimized = result_optimized.x elif str_optimizer_method in ['Nelder-Mead']: result_optimized = scipy.optimize.minimize(neg_log_ml_, hyps_converted, method=str_optimizer_method, options={'disp': False}) if debug: logger.debug('negative log marginal likelihood: %.6f', result_optimized.fun) logger.debug('scipy message: %s', result_optimized.message) result_optimized = result_optimized.x else: # pragma: no cover raise ValueError( 'get_optimized_kernel: missing conditions for str_optimizer_method' ) hyps = utils_covariance.restore_hyps(str_cov, result_optimized, use_ard=use_ard, fix_noise=fix_noise) hyps = utils_covariance.validate_hyps_dict(hyps, str_cov, num_dim) cov_X_X, inv_cov_X_X, _ = covariance.get_kernel_inverse( X_train, hyps, str_cov, fix_noise=fix_noise, debug=debug) time_end = time.time() if debug: logger.debug('hyps optimized: %s', utils_logger.get_str_hyps(hyps)) logger.debug('time consumed to construct gpr: %.4f sec.', time_end - time_start) return cov_X_X, inv_cov_X_X, hyps
def get_optimized_kernel( X_train, Y_train, prior_mu, str_cov, str_optimizer_method=constants.STR_OPTIMIZER_METHOD_GP, str_modelselection_method=constants.STR_MODELSELECTION_METHOD, is_fixed_noise=constants.IS_FIXED_GP_NOISE, debug=False): # TODO: check to input same is_fixed_noise to convert_hyps and restore_hyps assert isinstance(X_train, np.ndarray) assert isinstance(Y_train, np.ndarray) assert callable(prior_mu) or prior_mu is None assert isinstance(str_cov, str) assert isinstance(str_optimizer_method, str) assert isinstance(str_modelselection_method, str) assert isinstance(is_fixed_noise, bool) assert isinstance(debug, bool) assert len(Y_train.shape) == 2 assert X_train.shape[0] == Y_train.shape[0] _check_str_cov('get_optimized_kernel', str_cov, X_train.shape) assert str_optimizer_method in constants.ALLOWED_OPTIMIZER_METHOD_GP assert str_modelselection_method in constants.ALLOWED_MODELSELECTION_METHOD time_start = time.time() if debug: print('[DEBUG] get_optimized_kernel in gp.py: str_optimizer_method {}'. format(str_optimizer_method)) print( '[DEBUG] get_optimized_kernel in gp.py: str_modelselection_method {}' .format(str_modelselection_method)) prior_mu_train = get_prior_mu(prior_mu, X_train) if str_cov in constants.ALLOWED_GP_COV_BASE: num_dim = X_train.shape[1] elif str_cov in constants.ALLOWED_GP_COV_SET: num_dim = X_train.shape[2] if str_modelselection_method == 'ml': neg_log_ml = lambda hyps: -1.0 * log_ml(X_train, Y_train, hyps, str_cov, prior_mu_train, is_fixed_noise=is_fixed_noise, debug=debug) elif str_modelselection_method == 'loocv': neg_log_ml = lambda hyps: -1.0 * log_pseudo_l_loocv(X_train, Y_train, hyps, str_cov, prior_mu_train, is_fixed_noise= is_fixed_noise, debug=debug) else: # pragma: no cover raise ValueError( 'get_optimized_kernel: missing conditions for str_modelselection_method.' ) hyps_converted = utils_covariance.convert_hyps( str_cov, utils_covariance.get_hyps(str_cov, num_dim), is_fixed_noise=is_fixed_noise, ) if str_optimizer_method == 'BFGS': result_optimized = scipy.optimize.minimize(neg_log_ml, hyps_converted, method=str_optimizer_method) result_optimized = result_optimized.x elif str_optimizer_method == 'L-BFGS-B': bounds = utils_covariance.get_range_hyps(str_cov, num_dim, is_fixed_noise=is_fixed_noise) result_optimized = scipy.optimize.minimize(neg_log_ml, hyps_converted, method=str_optimizer_method, bounds=bounds) result_optimized = result_optimized.x # TODO: Fill this conditions elif str_optimizer_method == 'DIRECT': # pragma: no cover raise NotImplementedError( 'get_optimized_kernel: allowed str_optimizer_method, but it is not implemented.' ) elif str_optimizer_method == 'CMA-ES': # pragma: no cover raise NotImplementedError( 'get_optimized_kernel: allowed str_optimizer_method, but it is not implemented.' ) # INFO: It is allowed, but a condition is missed. else: # pragma: no cover raise ValueError( 'get_optimized_kernel: missing conditions for str_optimizer_method' ) hyps = utils_covariance.restore_hyps(str_cov, result_optimized, is_fixed_noise=is_fixed_noise) hyps, _ = utils_covariance.validate_hyps_dict(hyps, str_cov, num_dim) cov_X_X, inv_cov_X_X = get_kernel_inverse(X_train, hyps, str_cov, debug=debug) time_end = time.time() if debug: print('[DEBUG] get_optimized_kernel in gp.py: optimized hyps for gpr', hyps) print('[DEBUG] get_optimized_kernel in gp.py: time consumed', time_end - time_start, 'sec.') return cov_X_X, inv_cov_X_X, hyps
def get_optimized_kernel( X_train, Y_train, prior_mu, str_cov, str_optimizer_method=constants.STR_OPTIMIZER_METHOD_GP, str_modelselection_method=constants.STR_MODELSELECTION_METHOD, is_fixed_noise=constants.IS_FIXED_GP_NOISE, debug=False): """ This function computes the kernel matrix optimized by optimization method specified, its inverse matrix, and the optimized hyperparameters. :param X_train: inputs. Shape: (n, d) or (n, m, d). :type X_train: numpy.ndarray :param Y_train: outputs. Shape: (n, 1). :type Y_train: numpy.ndarray :param prior_mu: prior mean function or None. :type prior_mu: function or NoneType :param str_cov: the name of covariance function. :type str_cov: str. :param str_optimizer_method: the name of optimization method. :type str_optimizer_method: str., optional :param str_modelselection_method: the name of model selection method. :type str_modelselection_method: str., optional :param is_fixed_noise: flag for fixing a noise. :type is_fixed_noise: bool., optional :param debug: flag for printing log messages. :type debug: bool., optional :returns: a tuple of kernel matrix over `X_train`, kernel matrix inverse, and dictionary of hyperparameters. :rtype: tuple of (numpy.ndarray, numpy.ndarray, dict.) :raises: AssertionError, ValueError """ # TODO: check to input same is_fixed_noise to convert_hyps and restore_hyps assert isinstance(X_train, np.ndarray) assert isinstance(Y_train, np.ndarray) assert callable(prior_mu) or prior_mu is None assert isinstance(str_cov, str) assert isinstance(str_optimizer_method, str) assert isinstance(str_modelselection_method, str) assert isinstance(is_fixed_noise, bool) assert isinstance(debug, bool) assert len(Y_train.shape) == 2 assert X_train.shape[0] == Y_train.shape[0] _check_str_cov('get_optimized_kernel', str_cov, X_train.shape) assert str_optimizer_method in constants.ALLOWED_OPTIMIZER_METHOD_GP assert str_modelselection_method in constants.ALLOWED_MODELSELECTION_METHOD # TODO: fix this. is_gradient = True time_start = time.time() if debug: print('[DEBUG] get_optimized_kernel in gp.py: str_optimizer_method {}'. format(str_optimizer_method)) print( '[DEBUG] get_optimized_kernel in gp.py: str_modelselection_method {}' .format(str_modelselection_method)) prior_mu_train = get_prior_mu(prior_mu, X_train) if str_cov in constants.ALLOWED_GP_COV_BASE: num_dim = X_train.shape[1] elif str_cov in constants.ALLOWED_GP_COV_SET: num_dim = X_train.shape[2] is_gradient = False if str_modelselection_method == 'ml': neg_log_ml_ = lambda hyps: neg_log_ml(X_train, Y_train, hyps, str_cov, prior_mu_train, is_fixed_noise=is_fixed_noise, is_gradient=is_gradient, debug=debug) elif str_modelselection_method == 'loocv': neg_log_ml_ = lambda hyps: neg_log_pseudo_l_loocv(X_train, Y_train, hyps, str_cov, prior_mu_train, is_fixed_noise= is_fixed_noise, debug=debug) is_gradient = False else: # pragma: no cover raise ValueError( 'get_optimized_kernel: missing conditions for str_modelselection_method.' ) hyps_converted = utils_covariance.convert_hyps( str_cov, utils_covariance.get_hyps(str_cov, num_dim), is_fixed_noise=is_fixed_noise, ) if str_optimizer_method == 'BFGS': result_optimized = scipy.optimize.minimize(neg_log_ml_, hyps_converted, method=str_optimizer_method, jac=is_gradient, options={'disp': False}) result_optimized = result_optimized.x elif str_optimizer_method == 'L-BFGS-B': bounds = utils_covariance.get_range_hyps(str_cov, num_dim, is_fixed_noise=is_fixed_noise) result_optimized = scipy.optimize.minimize(neg_log_ml_, hyps_converted, method=str_optimizer_method, bounds=bounds, jac=is_gradient, options={'disp': False}) result_optimized = result_optimized.x # TODO: Fill this conditions elif str_optimizer_method == 'DIRECT': # pragma: no cover raise NotImplementedError( 'get_optimized_kernel: allowed str_optimizer_method, but it is not implemented.' ) elif str_optimizer_method == 'CMA-ES': # pragma: no cover raise NotImplementedError( 'get_optimized_kernel: allowed str_optimizer_method, but it is not implemented.' ) else: # pragma: no cover raise ValueError( 'get_optimized_kernel: missing conditions for str_optimizer_method' ) hyps = utils_covariance.restore_hyps(str_cov, result_optimized, is_fixed_noise=is_fixed_noise) hyps, _ = utils_covariance.validate_hyps_dict(hyps, str_cov, num_dim) cov_X_X, inv_cov_X_X, grad_cov_X_X = get_kernel_inverse( X_train, hyps, str_cov, is_fixed_noise=is_fixed_noise, debug=debug) time_end = time.time() if debug: print('[DEBUG] get_optimized_kernel in gp.py: optimized hyps for gpr', hyps) print('[DEBUG] get_optimized_kernel in gp.py: time consumed', time_end - time_start, 'sec.') return cov_X_X, inv_cov_X_X, hyps
def cov_main(str_cov: str, X: np.ndarray, Xp: np.ndarray, hyps: dict, same_X_Xp: bool, jitter: float = constants.JITTER_COV) -> np.ndarray: """ It computes kernel matrix over `X` and `Xp`, where `hyps` is given. :param str_cov: the name of covariance function. :type str_cov: str. :param X: one inputs. Shape: (n, d). :type X: numpy.ndarray :param Xp: another inputs. Shape: (m, d). :type Xp: numpy.ndarray :param hyps: dictionary of hyperparameters for covariance function. :type hyps: dict. :param same_X_Xp: flag for checking `X` and `Xp` are same. :type same_X_Xp: bool. :param jitter: jitter for diagonal entries. :type jitter: float, optional :returns: kernel matrix over `X` and `Xp`. Shape: (n, m). :rtype: numpy.ndarray :raises: AssertionError, ValueError """ assert isinstance(str_cov, str) assert isinstance(X, np.ndarray) assert isinstance(Xp, np.ndarray) assert isinstance(hyps, dict) assert isinstance(same_X_Xp, bool) assert isinstance(jitter, float) assert str_cov in constants.ALLOWED_COV num_X = X.shape[0] num_Xp = Xp.shape[0] cov_X_Xp = np.zeros((num_X, num_Xp)) if same_X_Xp: assert num_X == num_Xp cov_X_Xp += np.eye(num_X) * jitter if str_cov in constants.ALLOWED_COV_BASE: assert len(X.shape) == 2 assert len(Xp.shape) == 2 dim_X = X.shape[1] dim_Xp = Xp.shape[1] assert dim_X == dim_Xp hyps = utils_covariance.validate_hyps_dict(hyps, str_cov, dim_X) fun_cov = choose_fun_cov(str_cov) cov_X_Xp += fun_cov(X, Xp, hyps['lengthscales'], hyps['signal']) assert cov_X_Xp.shape == (num_X, num_Xp) elif str_cov in constants.ALLOWED_COV_SET: list_str_cov = str_cov.split('_') str_cov = list_str_cov[1] assert len(X.shape) == 3 assert len(Xp.shape) == 3 dim_X = X.shape[2] dim_Xp = Xp.shape[2] assert dim_X == dim_Xp hyps = utils_covariance.validate_hyps_dict(hyps, str_cov, dim_X) if not same_X_Xp: for ind_X in range(0, num_X): for ind_Xp in range(0, num_Xp): cov_X_Xp[ind_X, ind_Xp] += cov_set(str_cov, X[ind_X], Xp[ind_Xp], hyps['lengthscales'], hyps['signal']) else: for ind_X in range(0, num_X): for ind_Xp in range(ind_X, num_Xp): cov_X_Xp[ind_X, ind_Xp] += cov_set(str_cov, X[ind_X], Xp[ind_Xp], hyps['lengthscales'], hyps['signal']) if ind_X < ind_Xp: cov_X_Xp[ind_Xp, ind_X] = cov_X_Xp[ind_X, ind_Xp] else: raise NotImplementedError( 'cov_main: allowed str_cov, but it is not implemented.') return cov_X_Xp
def test_validate_hyps_dict(): num_dim = 2 str_cov = 'matern32' cur_hyps = package_target.get_hyps(str_cov, num_dim) with pytest.raises(AssertionError) as error: _ = package_target.validate_hyps_dict(123, str_cov, num_dim) with pytest.raises(AssertionError) as error: _ = package_target.validate_hyps_dict(cur_hyps, 'abc', num_dim) with pytest.raises(AssertionError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, 'abc') with pytest.raises(AssertionError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim, use_gp=1) with pytest.raises(AssertionError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim, use_gp='abc') cur_hyps = package_target.get_hyps(str_cov, num_dim) cur_hyps.pop('noise') with pytest.raises(ValueError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim) cur_hyps = package_target.get_hyps(str_cov, num_dim) cur_hyps.pop('lengthscales') with pytest.raises(ValueError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim) cur_hyps = package_target.get_hyps(str_cov, num_dim) cur_hyps.pop('signal') with pytest.raises(ValueError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim) cur_hyps = package_target.get_hyps(str_cov, num_dim) cur_hyps['noise'] = 'abc' with pytest.raises(ValueError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim) cur_hyps = package_target.get_hyps(str_cov, num_dim) cur_hyps['noise'] = np.inf cur_hyps = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim) assert cur_hyps['noise'] == constants.BOUND_UPPER_GP_NOISE cur_hyps = package_target.get_hyps(str_cov, num_dim) with pytest.raises(ValueError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, 123) cur_hyps = package_target.get_hyps(str_cov, num_dim) cur_hyps['lengthscales'] = 'abc' with pytest.raises(ValueError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim) cur_hyps = package_target.get_hyps(str_cov, num_dim) cur_hyps['signal'] = 'abc' with pytest.raises(ValueError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim) cur_hyps = package_target.get_hyps(str_cov, num_dim, use_gp=False) cur_hyps['signal'] = 'abc' with pytest.raises(ValueError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim) cur_hyps = package_target.get_hyps(str_cov, num_dim, use_gp=False) cur_hyps['dof'] = 'abc' with pytest.raises(ValueError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim, use_gp=False) cur_hyps = package_target.get_hyps(str_cov, num_dim, use_gp=False) cur_hyps.pop('dof') with pytest.raises(ValueError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim, use_gp=False) cur_hyps = package_target.get_hyps(str_cov, num_dim, use_gp=False) cur_hyps['dof'] = 1.5 with pytest.raises(AssertionError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim, use_gp=False) if cur_hyps['dof'] == 2.00001: assert False with pytest.raises(AssertionError) as error: assert cur_hyps['dof'] == 1.5 cur_hyps = package_target.get_hyps(str_cov, num_dim, use_ard=False, use_gp=False) print(cur_hyps) cur_hyps['lengthscales'] = 'abc' with pytest.raises(ValueError) as error: _ = package_target.validate_hyps_dict(cur_hyps, str_cov, num_dim, use_gp=False)