Beispiel #1
0
def test_no_domain_or_measure_raises_error(input_dim):
    """Test that errors are correctly raised when both domain and a Gaussian measure is
    given."""
    fun = lambda x: x
    nodes = np.linspace(0, 1, 3)
    fun_evals = fun(nodes)

    with pytest.raises(ValueError):
        bayesquad(fun=fun, input_dim=input_dim)

    with pytest.raises(ValueError):
        bayesquad_from_data(nodes=nodes, fun_evals=fun_evals)
Beispiel #2
0
def test_domain_and_gaussian_measure_raises_error(measure, input_dim):
    """Test that errors are correctly raised when both domain and a Gaussian measure is
    given."""
    domain = (0, 1)
    fun = lambda x: x

    with pytest.raises(ValueError):
        bayesquad(fun=fun, input_dim=input_dim, domain=domain, measure=measure)

    nodes = np.linspace(0, 1, 3)
    fun_evals = fun(nodes)
    with pytest.raises(ValueError):
        bayesquad_from_data(nodes=nodes,
                            fun_evals=fun_evals,
                            domain=domain,
                            measure=measure)
Beispiel #3
0
def test_type_1d(f1d, kernel, measure, input_dim):
    """Test that BQ outputs normal random variables for 1D integrands."""
    integral, _ = bayesquad(fun=f1d,
                            input_dim=input_dim,
                            kernel=kernel,
                            measure=measure,
                            max_evals=10)
    assert isinstance(integral, Normal)
Beispiel #4
0
def test_integral_values_1d(f1d, kernel, measure, input_dim):
    """Test numerically that BQ computes 1D integrals correctly."""

    # numerical integral
    # pylint: disable=invalid-name
    def integrand(x):
        return f1d(x) * measure(np.atleast_2d(x))

    # pylint: disable=invalid-name
    bq_integral, _ = bayesquad(fun=f1d,
                               input_dim=input_dim,
                               kernel=kernel,
                               measure=measure,
                               max_evals=250)
    num_integral, _ = quad(integrand, measure.domain[0], measure.domain[1])
    np.testing.assert_almost_equal(bq_integral.mean, num_integral, decimal=2)
Beispiel #5
0
def test_integral_values_kernel_translate(kernel, measure, input_dim, x):
    """Test numerical integration of kernel translates."""
    kernel_embedding = KernelEmbedding(kernel, measure)
    # pylint: disable=cell-var-from-loop
    for translate_point in x:
        fun = lambda y: kernel(translate_point, y)
        bq_integral, _ = bayesquad(
            fun=fun,
            input_dim=input_dim,
            kernel=kernel,
            measure=measure,
            var_tol=1e-8,
            max_evals=1000,
            batch_size=50,
        )
        true_integral = kernel_embedding.kernel_mean(
            np.atleast_2d(translate_point))
        np.testing.assert_almost_equal(bq_integral.mean,
                                       true_integral,
                                       decimal=2)
Beispiel #6
0
def test_domain_ignored_if_lebesgue(input_dim, measure):
    domain = (0, 1)
    fun = lambda x: x

    # standard BQ
    bq_integral, _ = bayesquad(fun=fun,
                               input_dim=input_dim,
                               domain=domain,
                               measure=measure)
    assert isinstance(bq_integral, Normal)

    # fixed nodes BQ
    nodes = np.linspace(0, 1, 3).reshape((3, 1))
    fun_evals = fun(nodes)

    bq_integral, _ = bayesquad_from_data(nodes=nodes,
                                         fun_evals=fun_evals,
                                         domain=domain,
                                         measure=measure)
    assert isinstance(bq_integral, Normal)