A dictionary of kernel embeddings in Python.
Download the source code. Activate your preferred virtual env. In the root directory, run the following command.
pip3 install .
Once the library is installed, import it like so.
import numpy as np
from kernel_embedding_dictionary import get_embedding
ke = get_embedding("expquad", "lebesgue")
x = np.random.rand(3, 1) # evaluate at 3 points
kernel_means = ke.mean(x)Provide parameters to the measure and/or kernel
import numpy as np
from kernel_embedding_dictionary import get_embedding
config_measure = {
"ndim": 2,
"bounds": [(0, 1), (0.5, 2.5)],
"normalize": True
}
config_kernel = {
"ndim": 2,
"lengthscales": [1.0, 0.5],
}
ke = get_embedding("expquad", "lebesgue", config_kernel, config_measure)
x = np.random.rand(3, 2) # evaluate at 3 points
kernel_means = ke.mean(x)Inspect the embedding with the print command
print(ke)
If you would like to get your hands on some raw kernel embedding code for your own project, please feel free to inspect e.g. this module where all univariate mean embeddings are listed. The corresponding univariate kernels are here.
If you are using KED, we would appreciate a citation of our paper.
@misc{KED2015,
title={A Dictionary of Closed-Form Kernel Mean Embeddings},
author={François-Xavier Briol and Alexandra Gessner and Toni Karvonen and Maren Mahsereci},
year={2025},
eprint={2504.18830},
archivePrefix={arXiv},
primaryClass={stat.ML},
url={https://arxiv.org/abs/2504.18830},
}
All multidimensional embeddings are based on product kernels and product measures.
| kernel / embedding | lebesgue |
gaussian |
|---|---|---|
expquad |
x | x |
matern |
x | |
matern12 |
x | x |
matern32 |
x | x |
matern52 |
x | |
matern72 |
x | |
wendland0 |
x | x |
wendland2 |
x |
All kernels are product kernels of the form
If an argument is not given, a default is used or inferred. The available kernels configs are as follows.
expaquad kernel
In the config below, ndim = lengthscales =
config_kernel = {
"ndim": 2,
"lengthscales": [1.0, 2.0],
}matern kernel of order
In the config below, nu = ndim = lengthscales =
config_kernel = {
"ndim": 2,
"nu": 3.5,
"lengthscales": [1.0, 2.0],
}matern12 kernel
In the config below, ndim = lengthscales =
config_kernel = {
"ndim": 2,
"lengthscales": [1.0, 2.0],
}matern32 kernel
In the config below, ndim = lengthscales =
config_kernel = {
"ndim": 2,
"lengthscales": [1.0, 2.0],
}matern52 kernel
In the config below, ndim = lengthscales =
config_kernel = {
"ndim": 2,
"lengthscales": [1.0, 2.0],
}matern72 kernel
In the config below, ndim = lengthscales =
config_kernel = {
"ndim": 2,
"lengthscales": [1.0, 2.0],
}wendland0 with value $k(x_i, z_i) = (1 - r_i){+}$ where $r_i = \frac{|x_i - z_i|}{\ell_i}$ and $(\cdot){+} = \operatorname{max} (0, \cdot)$
In the config below, ndim = lengthscales =
config_kernel = {
"ndim": 2,
"lengthscales": [1.0, 2.0],
}wendland2 with value $k(x_i, z_i) = (1 - r_i){+}^3 (3r_i + 1)$ where $r_i = \frac{|x_i - z_i|}{\ell_i}$ and $(\cdot){+} = \operatorname{max} (0, \cdot)$
In the config below, ndim = lengthscales =
config_kernel = {
"ndim": 2,
"lengthscales": [1.0, 2.0],
}All measures are product measures of the form
If an argument is not given, a default is used or inferred. The available measure configs are as follows.
lebesgue measure with density
In the config below, ndim = bounds =
config_measure = {
"ndim": 2,
"bounds": [(0, 1), (1, 2)],
"normalize": True
}gaussian measure with density
In the config below, ndim = variances = means =
config_measure = {
"ndim": 2,
"means": [-0.5, 2.8],
"variances": [0.3, 1.2]
}If you would like to contribute an additional kernel embedding or other enhancements, please feel free to open an issue or a pull request.
It is beneficial to install the dev version of KED. For this, use your venv of choice.
E.g., install pip3 install virtualenv.
Then, go to root directory of the repo and create a venv with the following command.
python3 -m venv .venv
Active it.
source .venv/bin/activate
Install all dependencies.
pip3 install -e .[dev]
Check install with pip3 freeze. Done :)
Make the following code changes
- Add the univariate kernel function to the file
kernel_funcs_1d.py. - Create a new module under
kernel_embedding_dictionary/kernels/and implement the classesUnivariateKernelandProductKernel. Use the existing kernel modules as example. - Add the kernel to
kernel_embedding_dictionary/kernels/__init__.py.
Add the kernels to the following tests
tests/kernel_embedding_dictionary/kernels/test_kernels.pyas fixture and to the kernel list.tests/kernel_embedding_dictionary/kernels/test_kernels_uni.pyas fixture and to the kernel list.- Create a new test module under
tests/kernel_embedding_dictionary/kernels/test_<new-kernel-name>_kernel.pyusing the existing ones as example.
Make the following code changes
- Create a new module under
kernel_embedding_dictionary/measures/and implement the classesUnivariateMeasureandProductMeasure. Use the existing kernels as example. - Add the measure to
kernel_embedding_dictionary/measures/__init__.py.
Add the measure to the following tests
tests/kernel_embedding_dictionary/measures/test_measures.pyas fixture and to the measure list.- Create a new test module under
tests/kernel_embedding_dictionary/measures/test_<new-measure-name>_measure.pyusing the existing ones as example.
Make the following code changes
- Add the univariate kernel mean embedding function to the file
mean_funcs_1d.py. - Import the mean function in
embedding.pyand add the embedding to the dictmean_func_1d_dictin the methodget_1d_funcs. - Add the kernel-measure combination to
_get_embedding.py.
Add the embedding to the following tests
- Add the kernel-measure combination to
tests/test_get_embedding.py. - In order to test the kernel mean embedding values, we compare the analytic values to a Monte Carlo estimate and evaluate the mean embedding on a few datapoints. We pre-compute the numerical integral to i) get stable tests and ii) have faster running tests.
- Create a new test module (in case of a new kernel) or use the existing test module under
tests/kernel_embedding_dictionary/embeddings/test_mean_values_<kernel-name>}.py. - Compute the Monte Carlo estimates with the script
compute_mean_intervals.py. Make sure that the points on which the kernel mean is evaluated lie in the domain of the kernel and measure. - Copy the results over to the test module and use them as
mean_intervalsin the tests. Add the new combination to thefixture_list.
- Create a new test module (in case of a new kernel) or use the existing test module under
Please make sure to run isort and then black on both the kernel_embedding_dictionary and tests directory.
Pytest can be run locally (after install) with pytest.
Happy coding!