Exemplo n.º 1
0
    def test_mean_var_interface_returns_same_as_cpp(self):
        """Test that the /gp/mean_var endpoint does the same thing as the C++ interface."""
        tolerance = 1.0e-11
        for test_case in self.gp_test_environments:
            python_domain, python_gp = test_case
            python_cov, historical_data = python_gp.get_core_data_copy()

            cpp_cov = SquareExponential(python_cov.hyperparameters)
            cpp_gp = GaussianProcess(cpp_cov, historical_data)

            points_to_evaluate = python_domain.generate_uniform_random_points_in_domain(10)

            # mean and var from C++
            cpp_mean = cpp_gp.compute_mean_of_points(points_to_evaluate)
            cpp_var = cpp_gp.compute_variance_of_points(points_to_evaluate)

            # mean and var from REST
            json_payload = self._build_json_payload(python_domain, python_cov, historical_data, points_to_evaluate.tolist())
            resp = self.testapp.post(self.endpoint, json_payload)
            resp_schema = GpMeanVarResponse()
            resp_dict = resp_schema.deserialize(json.loads(resp.body))
            rest_mean = numpy.asarray(resp_dict.get('mean'))
            rest_var = numpy.asarray(resp_dict.get('var'))

            self.assert_vector_within_relative(rest_mean, cpp_mean, tolerance)
            self.assert_vector_within_relative(rest_var, cpp_var, tolerance)
Exemplo n.º 2
0
    def test_interfaces_equivalent(self):
        """Test that the /gp/mean, var, mean_var, etc. endpoints are consistent."""
        tolerance = numpy.finfo(numpy.float64).eps
        for test_case in self.gp_test_environments:
            python_domain, python_gp = test_case
            python_cov, historical_data = python_gp.get_core_data_copy()

            points_to_evaluate = python_domain.generate_uniform_random_points_in_domain(10)

            # mean and var from REST
            json_payload = self._build_json_payload(python_domain, python_cov, historical_data, points_to_evaluate.tolist())
            resp = self.testapp.post(self.endpoint, json_payload)
            resp_schema = GpMeanVarResponse()
            resp_dict = resp_schema.deserialize(json.loads(resp.body))

            truth_mean = numpy.asarray(resp_dict.get('mean'))
            truth_var = numpy.asarray(resp_dict.get('var'))

            self._compare_endpoint_mean_var_results(
                json_payload,
                GP_MEAN_ENDPOINT,
                GpMeanResponse(),
                tolerance,
                truth_mean=truth_mean,
            )

            self._compare_endpoint_mean_var_results(
                json_payload,
                GP_VAR_ENDPOINT,
                GpVarResponse(),
                tolerance,
                truth_var=truth_var,
            )

            self._compare_endpoint_mean_var_results(
                json_payload,
                GP_VAR_DIAG_ENDPOINT,
                GpVarDiagResponse(),
                tolerance,
                truth_var=numpy.diag(truth_var),
            )

            self._compare_endpoint_mean_var_results(
                json_payload,
                GP_MEAN_VAR_DIAG_ENDPOINT,
                GpMeanVarDiagResponse(),
                tolerance,
                truth_mean=truth_mean,
                truth_var=numpy.diag(truth_var),
            )