Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify different measures, kernels and domains for n-dimensional integration using bayesquad #753

Closed
panweihit opened this issue Dec 12, 2022 · 6 comments
Labels
quad Issues related to quadrature question Further information is requested

Comments

@panweihit
Copy link

panweihit commented Dec 12, 2022

Hi,

I use the example in #512. I have two naive questions.

  1. I am trying to use a different measure and kernel inprobnum's bayesquad function. Did I make something wrong in the argument?
from probnum.quad import bayesquad
from probnum.quad.integration_measures LebesgueMeasure
from probnum.randprocs.kernels import ExpQuad

bayesquad(f, 2, domain=domain, measure=LebesgueMeasure, kernel=ExpQuad)
  1. Here the domain for the two inputs are both
domain=np.array([-3, 3]) 

Can I specify different domains, such as

domain = np.array([[-3, 3],[0, 1]])

Expect your reply. Thanks!

@panweihit panweihit added the bug Something isn't working label Dec 12, 2022
@mmahsereci
Copy link
Contributor

hey, could you let me know which version or commit of probnum you are using? The quad package changed quite a bit lately and the answer depends a bit on the version you are using. Thanks!

@mmahsereci mmahsereci added help wanted Extra attention is needed question Further information is requested quad Issues related to quadrature and removed bug Something isn't working help wanted Extra attention is needed labels Dec 13, 2022
@panweihit
Copy link
Author

Just to make sure, I just re-tried pip install git+https://github.com/probabilistic-numerics/probnum.git

It should be the latest version.

@mmahsereci
Copy link
Contributor

mmahsereci commented Dec 14, 2022

OK, that should be the latest version then, that's great. You can check it also by running

import probnum
probnum.__version__
>>> '0.1.22.dev10'

Regarding your questions:

  1. In order to use the Lebesgue measure, you can simply provide the domain. If all lower bounds and all upper bounds of the box-domain are the same, you only need to provide a tuple of two floats containing the lower and upper bound.
import numpy as np
from probnum.quad import bayesquad
from probnum.randprocs.kernels import ExpQuad

def fun(x):
    return np.sum(x **2, axis=1)

input_dim = 2
rng = np.random.default_rng()
domain = (-3, 3)  # if input_dim > 1, domain will be expanded to ([-3, -3, ...], [3, 3, ...])
kernel=ExpQuad(input_shape=(input_dim,))

bayesquad(
    fun=fun, 
    input_dim=input_dim, 
    domain=domain, 
    kernel=kernel,
    rng=rng,
)
  1. If lower and upper bounds differ between dimensions, domain needs to contain an array containing all lower bounds, and an array containing all upper bounds, i.e., ([lower_1, lower_2, ...], [upper_1, upper_2, ...]).
input_dim = 2
rng = np.random.default_rng()
domain = ([-3, 0], [3, 1])  # Domain contains bounds ([lower_1, lower_2, ...], [upper_1, upper_2, ...])
kernel=ExpQuad(input_shape=(input_dim,))

bayesquad(
    fun=fun, 
    input_dim=input_dim, 
    domain=domain, 
    kernel=kernel,
    rng=rng,
)
  1. You can also provide the Lebesgue measure directly
from probnum.quad.integration_measures import LebesgueMeasure

input_dim = 2
rng = np.random.default_rng()
domain = ([-3, 0], [3, 1]) 
measure = LebesgueMeasure(input_dim=input_dim, domain=domain)
kernel=ExpQuad(input_shape=(input_dim,))

bayesquad(
    fun=fun, 
    input_dim=input_dim, 
    measure=measure,
    kernel=kernel,
    rng=rng,
)
  1. You can swap out the Lebesgue measure for the Gaussian measure, e.g.
from probnum.quad.integration_measures import GaussianMeasure

input_dim = 2
rng = np.random.default_rng()
measure = GaussianMeasure(input_dim=input_dim, mean=0.0, cov=1.0)
kernel=ExpQuad(input_shape=(input_dim,))

bayesquad(
    fun=fun, 
    input_dim=input_dim, 
    measure=measure,
    kernel=kernel,
    rng=rng,
)

Please also note that bayesquad does not yet support hyper-parameters optimization (fitting the kernel parameters), apart from setting the global scale parameter to the maximum marginal likelihood estimate. Hence you should currently only use it if you can supply the ExpQuad kernel with some appropriate lengthscale for your problem. You can provide it like so:

input_dim = 2
rng = np.random.default_rng()
domain = ([-3, 0], [3, 1])
kernel=ExpQuad(input_shape=(input_dim,), lengthscale=0.3)  # appropriate lengthscale

bayesquad(
    fun=fun, 
    input_dim=input_dim, 
    domain=domain, 
    kernel=kernel,
    rng=rng,
)

Can you check if the code runs for you? And does that answer your questions?

@panweihit
Copy link
Author

It works very well! Thanks a lot!

I would assume that the hyper-parameters optimization feature is on the way, so this feature request is unnecessary.

@mmahsereci
Copy link
Contributor

It works very well! Thanks a lot!

Glad to hear! Thanks for the quick feedback.

I would assume that the hyper-parameters optimization feature is on the way, so this feature request is unnecessary.

Yes, it's planned (a first step is this PR #581 ). It may still take a while though and I cannot promise a date unfortunately.

@mmahsereci
Copy link
Contributor

Closing this issue as it seems resolved. @panweihit feel free to re-open, or open a new issue an case there are further questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
quad Issues related to quadrature question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants