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_integral_values_x2_gaussian(kernel, measure, input_dim):
    """Test numerical integration of x**2 in higher dimensions."""
    # pylint: disable=invalid-name
    c = np.linspace(0.1, 2.2, input_dim)
    fun = lambda x: np.sum(c * x**2, 1)
    true_integral = np.sum(c * (measure.mean**2 + np.diag(measure.cov)))
    n_gh = 8  # Be very careful about increasing this - yields huge kernel matrices
    nodes, _ = gauss_hermite_tensor(n_points=n_gh,
                                    input_dim=input_dim,
                                    mean=measure.mean,
                                    cov=measure.cov)
    fun_evals = fun(nodes)
    bq_integral, _ = bayesquad_from_data(nodes=nodes,
                                         fun_evals=fun_evals,
                                         kernel=kernel,
                                         measure=measure)
    np.testing.assert_almost_equal(bq_integral.mean, true_integral, decimal=2)
Beispiel #4
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)
Beispiel #5
0
def test_integral_values_sin_lebesgue(kernel, measure, input_dim):
    """Test numerical integration of products of sinusoids."""
    # pylint: disable=invalid-name
    c = np.linspace(0.1, 0.5, input_dim)
    (a, b) = measure.domain
    fun = lambda x: np.prod(np.sin(c * x), 1)
    true_integral = (np.prod(
        (np.cos(c * a) - np.cos(c * b)) / c) * measure.normalization_constant)
    n_gl = 8  # Be very careful about increasing this - yields huge kernel matrices
    nodes, _ = gauss_legendre_tensor(
        n_points=n_gl,
        input_dim=input_dim,
        domain=measure.domain,
        normalized=measure.normalized,
    )
    fun_evals = fun(nodes)
    bq_integral, _ = bayesquad_from_data(nodes=nodes,
                                         fun_evals=fun_evals,
                                         kernel=kernel,
                                         measure=measure)
    np.testing.assert_almost_equal(bq_integral.mean, true_integral, decimal=2)