-
Notifications
You must be signed in to change notification settings - Fork 0
Expected kernel means #22
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
Draft
alpiges
wants to merge
2
commits into
main
Choose a base branch
from
meankmean
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Copyright 2025 The KED Authors. All Rights Reserved. | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
|
|
||
| import numpy as np | ||
| from scipy.special import erf | ||
|
|
||
|
|
||
| def expquad_lebesgue_var_func_1d(ell: float, lb: float, ub: float, density: float) -> np.ndarray: | ||
| """Compute the expected mean function for the exponential quadratic kernel with respect to the Lebesgue measure in 1D. | ||
|
|
||
| Args: | ||
| ell: The length scale parameter. | ||
| lb: The lower bound of the integration interval. | ||
| ub: The upper bound of the integration interval. | ||
| density: The density of the Lebesgue measure. | ||
|
|
||
| Returns: | ||
| The expected mean function value. | ||
| """ | ||
| exp_term = ell * np.sqrt(2/np.pi) * (np.exp(-0.5 * ((ub - lb) / ell) ** 2) - 1) | ||
| erf_term = (ub - lb) * (erf((ub - lb) / (ell * np.sqrt(2)))) | ||
| return np.sqrt(2 * np.pi) * ell * density**2 * (exp_term + erf_term) |
59 changes: 59 additions & 0 deletions
59
tests/kernel_embedding_dictionary/embeddings/test_expected_kmeans.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # Copyright 2025 The KED Authors. All Rights Reserved. | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| import pytest | ||
| import numpy as np | ||
|
|
||
| from kernel_embedding_dictionary._get_embedding import get_embedding | ||
|
|
||
| from scipy.integrate import quad | ||
|
|
||
|
|
||
| def get_config_expquad_lebesgue_1d_standard(): | ||
| ck = {"ndim": 1} | ||
| cm = {"ndim": 1} | ||
| return "expquad", "lebesgue", ck, cm | ||
|
|
||
|
|
||
| def get_config_expquad_lebesgue_1d_values(): | ||
| ck = {"ndim": 1, "lengthscales": [0.3]} | ||
| cm = {"ndim": 1, "bounds": [(-0.5, 2.5)], "normalize": True} # test only works for normalized measures | ||
| return "expquad", "lebesgue", ck, cm | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def config_expquad_lebesgue_1d_standard(): | ||
| kn, mn, ck, cm = get_config_expquad_lebesgue_1d_standard() | ||
| ke = get_embedding(kernel_name=kn, measure_name=mn, kernel_config=ck, measure_config=cm) | ||
|
|
||
| def ke_mean_scalar(x): | ||
| return ke.mean(np.asarray(x).reshape(1, -1)) | ||
|
|
||
| ekm, int_err = quad(ke_mean_scalar, 0, 1) | ||
| return kn, mn, ck, cm, ekm, int_err | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def config_expquad_lebesgue_1d_values(): | ||
| kn, mn, ck, cm = get_config_expquad_lebesgue_1d_values() | ||
| ke = get_embedding(kernel_name=kn, measure_name=mn, kernel_config=ck, measure_config=cm) | ||
|
|
||
| bounds = (cm["bounds"][0][0], cm["bounds"][0][1]) | ||
|
|
||
| def ke_mean_scalar(x): | ||
| return ke.mean(np.asarray(x).reshape(1, -1)) / (bounds[1] - bounds[0]) | ||
|
|
||
| ekm, int_err = quad(ke_mean_scalar, *bounds) | ||
| return kn, mn, ck, cm, ekm, int_err | ||
|
|
||
| fixture_list = [ | ||
| "config_expquad_lebesgue_1d_standard", | ||
| "config_expquad_lebesgue_1d_values", | ||
| ] | ||
|
|
||
| @pytest.mark.parametrize("fixture_name", fixture_list) | ||
| def test_expquad_lebesgue_mean_func_1d(fixture_name, request): | ||
| # Test cases for the expected mean function | ||
| kn, mn, ck, cm, num_ekm, int_err = request.getfixturevalue(fixture_name) | ||
| ke = get_embedding(kernel_name=kn, measure_name=mn, kernel_config=ck, measure_config=cm) | ||
| assert num_ekm == pytest.approx(ke.variance(), rel=int_err) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue regarding the tests is here. By passing the function handle, the function is run. This would be fine if we new that for each kernel mean embedding, we also know the integrated kernel mean. But I don't believe that's the case. I would therefore propose to delete these lines, and rename the
_get_1d_funcsdirectly to_mean_func_1dand_var_func_1d.