Skip to content

Commit

Permalink
Update to latest probnum version (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
kat committed Sep 27, 2023
1 parent 8c48032 commit 48bc9e2
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 91 deletions.
7 changes: 3 additions & 4 deletions src/probnum/problems/zoo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Collection of test problems for probabilistic numerical methods.
Subpackage offering implementations of standard example problems or
convenient interfaces to benchmark problems for probabilistic numerical
methods. These test problems are meant for rapid experimentation and
prototyping.
Subpackage offering implementations of standard example problems or convenient
interfaces to benchmark problems for probabilistic numerical methods. These test
problems are meant for rapid experimentation and prototyping.
"""
1 change: 0 additions & 1 deletion src/probnum/problems/zoo/quad/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test problems for numerical integration/ quadrature."""

from ._emukit_problems import circulargaussian2d, hennig1d, hennig2d, sombrero2d

from ._quadproblems_gaussian import *
from ._quadproblems_uniform import *

Expand Down
41 changes: 25 additions & 16 deletions src/probnum/problems/zoo/quad/_quadproblems_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from scipy.stats import norm

from probnum.problems import QuadratureProblem
from probnum.quad.integration_measures import GaussianMeasure
from probnum.typing import FloatLike

__all__ = [
Expand Down Expand Up @@ -65,13 +66,12 @@ def uniform_to_gaussian_quadprob(
of the 14th Monte Carlo and Quasi-Monte Carlo Methods (MCQMC) conference 2020.
arXiv:2006.07487.
"""

dim = len(quadprob.lower_bd)
lower_bd, upper_bd = quadprob.measure.domain

# Check that the original quadrature problem was defined on [0,1]^d
if np.any(quadprob.lower_bd != 0.0):
if np.any(lower_bd != 0.0):
raise ValueError("quadprob is not an integration problem over [0,1]^d")
if np.any(quadprob.upper_bd != 1.0):
if np.any(upper_bd != 1.0):
raise ValueError("quadprob is not an integration problem over [0,1]^d")

# Check that the original quadrature problem has a scalar valued solution
Expand All @@ -80,7 +80,7 @@ def uniform_to_gaussian_quadprob(

# Construct transformation of the integrand
def uniform_to_gaussian_integrand(
func: Callable[[np.ndarray], np.ndarray],
fun: Callable[[np.ndarray], np.ndarray],
mean: FloatLike = 0.0,
var: FloatLike = 1.0,
) -> Callable[[np.ndarray], np.ndarray]:
Expand All @@ -92,17 +92,14 @@ def uniform_to_gaussian_integrand(
raise TypeError("The variance should be a positive float.")

def newfunc(x):
return func(norm.cdf((x - mean) / var))
return fun(norm.cdf((x - mean) / var))

return newfunc

gaussian_measure = GaussianMeasure(mean=mean, cov=var)
return QuadratureProblem(
integrand=uniform_to_gaussian_integrand(
func=quadprob.integrand, mean=mean, var=var
),
lower_bd=np.broadcast_to(-np.Inf, dim),
upper_bd=np.broadcast_to(np.Inf, dim),
output_dim=None,
fun=uniform_to_gaussian_integrand(fun=quadprob.fun, mean=mean, var=var),
measure=gaussian_measure,
solution=quadprob.solution,
)

Expand Down Expand Up @@ -154,6 +151,18 @@ def sum_polynomials(
if b is None:
b = np.broadcast_to(1, (1, dim))

if len(a.shape) != 2:
raise ValueError(
f"Invalid shape {a.shape} for parameter `a`. "
f"Expected parameters of shape (p+1)xdim"
)

if len(b.shape) != 2:
raise ValueError(
f"Invalid shape {a.shape} for parameter `a`. "
f"Expected parameters of shape (p+1)xdim"
)

# Check that the parameters have valid values and shape
if a.shape[1] != dim:
raise ValueError(
Expand Down Expand Up @@ -187,11 +196,11 @@ def integrand(x: np.ndarray) -> np.ndarray:
delta = (np.remainder(b, 2) - 1) ** 2
doublefact = special.factorial2(b - 1)
solution = np.sum(np.prod(a * delta * (var**b) * doublefact, axis=1))
mean = np.zeros_like(dim)

gaussian_measure = GaussianMeasure(mean=mean, cov=var)
return QuadratureProblem(
integrand=integrand,
lower_bd=np.broadcast_to(-np.Inf, dim),
upper_bd=np.broadcast_to(np.Inf, dim),
output_dim=None,
fun=integrand,
measure=gaussian_measure,
solution=solution,
)
Loading

0 comments on commit 48bc9e2

Please sign in to comment.