def setUp(self): np.random.seed(2) n_points = 100 normal_noise = np.random.normal(0, 1.0, n_points) points = np.linspace(0, 10, n_points) points = points.reshape([n_points, 1]) kernel = Matern52.define_kernel_from_array(1, np.array([2.0])) self.sigma2 = ParameterEntity(SIGMA2_NAME, np.array([1.0]), None) kernel = ScaledKernel(1, kernel, self.sigma2) function = SampleFunctions.sample_from_gp(points, kernel) function = function[0, :] evaluations = function + normal_noise + 10.0 self.training_data_gp = { "evaluations": list(evaluations), "points": points, "var_noise": [] } bounds = None self.gp_gaussian = GPFittingGaussian([MATERN52_NAME], self.training_data_gp, [1], bounds, max_steps_out=1000) self.gp_gaussian_2 = GPFittingGaussian([SCALED_KERNEL, MATERN52_NAME], self.training_data_gp, [1], bounds, max_steps_out=1000)
def test_cross_validation_mle_parameters(self): type_kernel = [MATERN52_NAME] np.random.seed(5) n_points = 10 normal_noise = np.random.normal(0, 0.01, n_points) points = np.linspace(0, 100, n_points) points = points.reshape([n_points, 1]) kernel = Matern52.define_kernel_from_array(1, np.array([100.0])) function = SampleFunctions.sample_from_gp(points, kernel) function = function[0, :] evaluations = function + normal_noise training_data = { "evaluations": evaluations, "points": points, "var_noise": None} dimensions = [1] problem_name = 'a' result = \ ValidationGPModel.cross_validation_mle_parameters(type_kernel, training_data, dimensions, problem_name, start=np.array([0.01**2, 0.0, 100.0])) compare = 'results/diagnostic_kernel/a/validation_kernel_histogram_a_' + MATERN52_NAME + \ '_same_correlation_False_10_None.png' assert result['filename_histogram'] == compare assert np.all(result['y_eval'] == evaluations) assert result['n_data'] == n_points assert result['filename_plot'] == 'results/diagnostic_kernel/a/' \ 'validation_kernel_mean_vs_observations_a_' + \ MATERN52_NAME + '_same_correlation_False_10_None' + '.png' assert result['success_proportion'] >= 0.9 noise = np.random.normal(0, 0.000001, n_points) evaluations_noisy = evaluations + noise training_data_2 = { "evaluations": evaluations_noisy, "points": points, "var_noise": np.array(n_points * [0.000001**2])} result_2 = \ ValidationGPModel.cross_validation_mle_parameters(type_kernel, training_data_2, dimensions, problem_name, start=np.array([0.01**2, 0.0, 100.0])) compare = 'results/diagnostic_kernel/a/validation_kernel_histogram_a_' + MATERN52_NAME + \ '_same_correlation_False_10_None.png' assert result_2['filename_histogram'] == compare assert np.all(result_2['y_eval'] == evaluations_noisy) assert result_2['n_data'] == n_points compare = 'results/diagnostic_kernel/a/validation_kernel_mean_vs_observations_a_' + \ MATERN52_NAME + '_same_correlation_False_10_None.png' assert result_2['filename_plot'] == compare assert result_2['success_proportion'] >= 0.9
def setUp(self): np.random.seed(5) n_points = 100 points = np.linspace(0, 100, n_points) points = points.reshape([n_points, 1]) tasks = np.random.randint(2, size=(n_points, 1)) add = [-10, 10] kernel = Matern52.define_kernel_from_array(1, np.array([100.0, 1.0])) function = SampleFunctions.sample_from_gp(points, kernel) self.function = function for i in xrange(n_points): function[0, i] += add[tasks[i, 0]] points = np.concatenate((points, tasks), axis=1) self.points = points self.evaluations = function[0, :] function = function[0, :] training_data = { 'evaluations': list(function), 'points': points, "var_noise": [], } gaussian_p = GPFittingGaussian( [PRODUCT_KERNELS_SEPARABLE, MATERN52_NAME, TASKS_KERNEL_NAME], training_data, [2, 1, 2], bounds_domain=[[0, 100], [0, 1]], type_bounds=[0, 1]) gaussian_p = gaussian_p.fit_gp_regression(random_seed=1314938) quadrature = BayesianQuadrature(gaussian_p, [0], UNIFORM_FINITE, parameters_distribution={TASKS: 2}, model_only_x=True) self.mt = MultiTasks(quadrature, quadrature.parameters_distribution.get(TASKS))
def test_cross_validation_mle_parameters_2(self): type_kernel = [MATERN52_NAME] np.random.seed(5) n_points = 10 normal_noise = np.random.normal(0, 0.01, n_points) points = np.linspace(0, 100, n_points) points = points.reshape([n_points, 1]) kernel = Matern52.define_kernel_from_array(1, np.array([100.0])) function = SampleFunctions.sample_from_gp(points, kernel) function = function[0, :] evaluations = function + normal_noise training_data = { "evaluations": evaluations, "points": points, "var_noise": None} dimensions = [1] problem_name = 'a' result = \ ValidationGPModel.cross_validation_mle_parameters(type_kernel, training_data, dimensions, problem_name, start=np.array([-1])) assert result['success_proportion'] == -1
def test_sample_new_observations(self): np.random.seed(5) n_points = 10 normal_noise = np.random.normal(0, 0.5, n_points) points = np.linspace(0, 500, n_points) points = points.reshape([n_points, 1]) kernel = Matern52.define_kernel_from_array(1, np.array([100.0, 1.0])) function = SampleFunctions.sample_from_gp(points, kernel) function = function[0, :] evaluations = function + normal_noise training_data_gp = { "evaluations": list(evaluations[1:]), "points": points[1:, :], "var_noise": []} gp = GPFittingGaussian([MATERN52_NAME], training_data_gp, [1], kernel_values=[100.0, 1.0], mean_value=[0.0], var_noise_value=[0.5**2]) n_samples = 100 samples = gp.sample_new_observations(np.array([[30.0]]), n_samples, random_seed=1) new_point = np.array([[30.0]]) z = gp.compute_posterior_parameters(new_point) mean = z['mean'] cov = z['cov'] npt.assert_almost_equal(mean, np.mean(samples), decimal=1) npt.assert_almost_equal(cov, np.var(samples), decimal=1)
def test_compute_posterior_parameters(self): np.random.seed(5) n_points = 10 normal_noise = np.random.normal(0, 0.5, n_points) points = np.linspace(0, 500, n_points) points = points.reshape([n_points, 1]) kernel = Matern52.define_kernel_from_array(1, np.array([100.0, 1.0])) function = SampleFunctions.sample_from_gp(points, kernel) function = function[0, :] evaluations = function + normal_noise training_data_gp = { "evaluations": list(evaluations[1:]), "points": points[1:, :], "var_noise": []} gp = GPFittingGaussian([MATERN52_NAME], training_data_gp, [1], kernel_values=[100.0, 1.0], mean_value=[0.0], var_noise_value=[0.5**2]) new_point = np.array([points[0], points[1]]) z = gp.compute_posterior_parameters(new_point) mean = z['mean'] cov = z['cov'] assert mean[1] - 2.0 * np.sqrt(cov[1, 1]) <= function[1] assert function[1] <= mean[1] + 2.0 * np.sqrt(cov[1, 1]) assert mean[0] - 2.0 * np.sqrt(cov[0, 0]) <= function[0] assert function[0] <= mean[0] + 2.0 * np.sqrt(cov[0, 0]) # Values obtained from GPy npt.assert_almost_equal(mean, np.array([0.30891226, 0.60256237])) npt.assert_almost_equal(cov, np.array([[0.48844879, 0.16799927], [0.16799927, 0.16536313]]))
def test_train(self): np.random.seed(5) n_points = 10 normal_noise = np.random.normal(0, 0.5, n_points) points = np.linspace(0, 500, n_points) points = points.reshape([n_points, 1]) kernel = Matern52.define_kernel_from_array(1, np.array([100.0, 1.0])) function = SampleFunctions.sample_from_gp(points, kernel) function = function[0, :] evaluations = function + normal_noise training_data_gp = { "evaluations": list(evaluations), "points": points, "var_noise": []} new_gp = GPFittingGaussian.train([MATERN52_NAME], [1], True, training_data_gp, None, random_seed=1314938) gp_gaussian = GPFittingGaussian([MATERN52_NAME], training_data_gp, [1]) gp_2 = gp_gaussian.fit_gp_regression(random_seed=1314938) npt.assert_almost_equal(new_gp.var_noise.value[0], gp_2.var_noise.value[0], decimal=6) npt.assert_almost_equal(new_gp.mean.value[0], gp_2.mean.value[0], decimal=6) npt.assert_almost_equal(new_gp.kernel_values, gp_2.kernel_values) gp_gaussian = GPFittingGaussian([MATERN52_NAME], training_data_gp, [1]) new_gp_2 = GPFittingGaussian.train([MATERN52_NAME], [1], False, training_data_gp, None) npt.assert_almost_equal(new_gp_2.var_noise.value[0], gp_gaussian.var_noise.value[0]) npt.assert_almost_equal(new_gp_2.mean.value[0], gp_gaussian.mean.value[0], decimal=6) npt.assert_almost_equal(new_gp_2.kernel_values, gp_gaussian.kernel_values)
def test_mle_parameters(self): # Results compared with the ones given by GPy np.random.seed(1) add = -45.946926660233636 llh = self.gp_gaussian.log_likelihood(1.0, 0.0, np.array([100.0, 1.0])) npt.assert_almost_equal(llh + add, -59.8285565516, decimal=6) opt = self.gp_gaussian.mle_parameters(start=np.array([1.0, 0.0, 14.0, 0.9])) assert opt['optimal_value'] + add >= -67.1494227694 compare = self.gp_gaussian.log_likelihood(9, 10.0, np.array([100.2, 1.1])) assert self.gp_gaussian_central.log_likelihood(9, 0.0, np.array([100.2, 1.1])) == compare np.random.seed(5) n_points = 10 normal_noise = np.random.normal(0, 0.5, n_points) points = np.linspace(0, 500, n_points) points = points.reshape([n_points, 1]) kernel = Matern52.define_kernel_from_array(1, np.array([100.0, 1.0])) function = SampleFunctions.sample_from_gp(points, kernel) function = function[0, :] evaluations = function + normal_noise training_data_gp = { "evaluations": list(evaluations), "points": points, "var_noise": []} gp_gaussian = GPFittingGaussian([MATERN52_NAME], training_data_gp, [1]) opt_3 = gp_gaussian.mle_parameters(random_seed=1314938) np.random.seed(1314938) start = gp_gaussian.sample_parameters_posterior(1)[0, :] opt_4 = gp_gaussian.mle_parameters(start) npt.assert_almost_equal(opt_3['optimal_value'], opt_4['optimal_value']) npt.assert_almost_equal(opt_3['solution'], opt_4['solution'], decimal=4)
from stratified_bayesian_optimization.util.json_file import JSONFile from stratified_bayesian_optimization.kernels.matern52 import Matern52 from stratified_bayesian_optimization.lib.sample_functions import SampleFunctions decimals = 10 random_seed = 5 np.random.seed(random_seed) n_points = 1000 points = np.linspace(0, 100, n_points) points = np.round(points, decimals=decimals) points = points.reshape([n_points, 1]) tasks = np.array([[0, 1]]) add = [10, -10] kernel = Matern52.define_kernel_from_array(1, np.array([100.0, 1.0])) function = SampleFunctions.sample_from_gp(points, kernel) function = function[0, :] final_function = {} for task in range(2): final_function[task] = [] for i in xrange(n_points): point = np.concatenate((points[i, :], np.array([task]))) final_function[task].append(function[i] + add[task]) filename = path.join('problems', 'test_simulated_gp', 'simulated_function_with_%d_%d' % (n_points, random_seed)) JSONFile.write({'function': final_function, 'points': points}, filename)
def setUp(self): type_kernel = [SCALED_KERNEL, MATERN52_NAME] self.training_data = { "evaluations": [42.2851784656, 72.3121248508, 1.0113231069, 30.9309246906, 15.5288331909], "points": [ [42.2851784656], [72.3121248508], [1.0113231069], [30.9309246906], [15.5288331909]], "var_noise": []} dimensions = [1] self.gp = GPFittingGaussian(type_kernel, self.training_data, dimensions, bounds_domain=[[0, 100]]) self.training_data_3 = { "evaluations": [42.2851784656, 72.3121248508, 1.0113231069, 30.9309246906, 15.5288331909], "points": [ [42.2851784656], [72.3121248508], [1.0113231069], [30.9309246906], [15.5288331909]], "var_noise": [0.5, 0.8, 0.7, 0.9, 1.0]} self.gp_3 = GPFittingGaussian(type_kernel, self.training_data_3, dimensions, bounds_domain=[[0, 100]]) self.training_data_simple = { "evaluations": [5], "points": [[5]], "var_noise": []} dimensions = [1] self.simple_gp = GPFittingGaussian(type_kernel, self.training_data_simple, dimensions, bounds_domain=[[0, 100]]) self.training_data_complex = { "evaluations": [1.0], "points": [[42.2851784656, 0]], "var_noise": [0.5]} self.complex_gp = GPFittingGaussian( [PRODUCT_KERNELS_SEPARABLE, MATERN52_NAME, TASKS_KERNEL_NAME], self.training_data_complex, [2, 1, 1], bounds_domain=[[0, 100], [0]]) self.training_data_complex_2 = { "evaluations": [1.0, 2.0, 3.0], "points": [[42.2851784656, 0], [10.532, 0], [9.123123, 1]], "var_noise": [0.5, 0.2, 0.1]} self.complex_gp_2 = GPFittingGaussian( [PRODUCT_KERNELS_SEPARABLE, MATERN52_NAME, TASKS_KERNEL_NAME], self.training_data_complex_2, [3, 1, 2], bounds_domain=[[0, 100], [0, 1]]) self.new_point = np.array([[80.0]]) self.evaluation = np.array([80.0]) self.training_data_noisy = { "evaluations": [41.0101845096], "points": [[42.2851784656]], "var_noise": [0.0181073779]} self.gp_noisy = GPFittingGaussian(type_kernel, self.training_data_noisy, dimensions, bounds_domain=[[0, 100]]) np.random.seed(2) n_points = 50 normal_noise = np.random.normal(0, 0.5, n_points) points = np.linspace(0, 500, n_points) points = points.reshape([n_points, 1]) kernel = Matern52.define_kernel_from_array(1, np.array([100.0, 1.0])) function = SampleFunctions.sample_from_gp(points, kernel) function = function[0, :] evaluations = function + normal_noise self.training_data_gp = { "evaluations": list(evaluations), "points": points, "var_noise": []} self.gp_gaussian = GPFittingGaussian([SCALED_KERNEL, MATERN52_NAME], self.training_data_gp, [1]) self.gp_gaussian_2 = GPFittingGaussian([MATERN52_NAME], self.training_data_gp, [1], bounds_domain=[[0, 100]]) self.training_data_gp_2 = { "evaluations": list(evaluations - 10.0), "points": points, "var_noise": []} self.gp_gaussian_central = GPFittingGaussian([SCALED_KERNEL, MATERN52_NAME], self.training_data_gp_2, [1], bounds_domain=[[0, 100]])