gpjax 0.10.0__tar.gz → 0.10.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gpjax-0.10.1/.cursorrules +37 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/PKG-INFO +1 -1
- gpjax-0.10.1/examples/oak_example.py +214 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/__init__.py +1 -1
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/base.py +4 -1
- gpjax-0.10.1/gpjax/kernels/nonstationary/oak.py +406 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/mean_functions.py +4 -3
- gpjax-0.10.1/tests/kernels/nonstationary/test_oak.py +208 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_mean_functions.py +25 -32
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/CODE_OF_CONDUCT.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/ISSUE_TEMPLATE/01_BUG_REPORT.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/ISSUE_TEMPLATE/02_FEATURE_REQUEST.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/ISSUE_TEMPLATE/03_CODEBASE_IMPROVEMENT.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/ISSUE_TEMPLATE/04_DOCS_IMPROVEMENT.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/ISSUE_TEMPLATE/config.yml +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/codecov.yml +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/labels.yml +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/pull_request_template.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/release-drafter.yml +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/workflows/build_docs.yml +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/workflows/integration.yml +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/workflows/pr_greeting.yml +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/workflows/ruff.yml +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/workflows/stale_prs.yml +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/workflows/test_docs.yml +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.github/workflows/tests.yml +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/.gitignore +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/CITATION.bib +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/LICENSE.txt +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/Makefile +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/README.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/CODE_OF_CONDUCT.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/GOVERNANCE.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/contributing.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/design.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/index.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/index.rst +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/installation.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/javascripts/katex.js +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/refs.bib +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/scripts/gen_examples.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/scripts/gen_pages.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/scripts/notebook_converter.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/scripts/sharp_bits_figure.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/sharp_bits.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/GP.pdf +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/GP.svg +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/bijector_figure.svg +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/css/gpjax_theme.css +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/favicon.ico +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/gpjax.mplstyle +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/gpjax_logo.pdf +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/gpjax_logo.svg +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/jaxkern/lato.ttf +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/jaxkern/logo.png +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/jaxkern/logo.svg +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/jaxkern/main.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/step_size_figure.png +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/static/step_size_figure.svg +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/stylesheets/extra.css +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/docs/stylesheets/permalinks.css +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/backend.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/barycentres/barycentre_gp.gif +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/barycentres.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/classification.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/collapsed_vi.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/constructing_new_kernels.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/data/max_tempeature_switzerland.csv +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/data/yacht_hydrodynamics.data +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/deep_kernels.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/gpjax.mplstyle +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/graph_kernels.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/intro_to_gps/decomposed_mll.png +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/intro_to_gps/generating_process.png +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/intro_to_gps.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/intro_to_kernels.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/likelihoods_guide.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/oceanmodelling.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/poisson.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/regression.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/uncollapsed_vi.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/utils.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/examples/yacht.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/citation.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/dataset.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/distributions.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/fit.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/gps.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/integrators.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/__init__.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/approximations/__init__.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/approximations/rff.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/computations/__init__.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/computations/base.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/computations/basis_functions.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/computations/constant_diagonal.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/computations/dense.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/computations/diagonal.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/computations/eigen.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/non_euclidean/__init__.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/non_euclidean/graph.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/non_euclidean/utils.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/nonstationary/__init__.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/nonstationary/arccosine.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/nonstationary/linear.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/nonstationary/polynomial.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/stationary/__init__.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/stationary/base.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/stationary/matern12.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/stationary/matern32.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/stationary/matern52.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/stationary/periodic.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/stationary/powered_exponential.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/stationary/rational_quadratic.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/stationary/rbf.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/stationary/utils.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/kernels/stationary/white.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/likelihoods.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/lower_cholesky.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/objectives.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/parameters.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/scan.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/typing.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/gpjax/variational_families.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/mkdocs.yml +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/pyproject.toml +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/static/CONTRIBUTING.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/static/paper.bib +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/static/paper.md +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/static/paper.pdf +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/__init__.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/conftest.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/integration_tests.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_citations.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_dataset.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_fit.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_gaussian_distribution.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_gps.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_integrators.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_kernels/__init__.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_kernels/test_approximations.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_kernels/test_base.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_kernels/test_computation.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_kernels/test_non_euclidean.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_kernels/test_nonstationary.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_kernels/test_stationary.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_kernels/test_utils.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_likelihoods.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_lower_cholesky.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_markdown.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_objectives.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_parameters.py +0 -0
- {gpjax-0.10.0 → gpjax-0.10.1}/tests/test_variational_families.py +0 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
You are an AI assistant specialized in Python development and machine learning. Your approach emphasizes:
|
|
2
|
+
|
|
3
|
+
Clear project structure with separate directories for source code, tests, docs, and config.
|
|
4
|
+
|
|
5
|
+
Modular design with distinct files for models, services, controllers, and utilities.
|
|
6
|
+
|
|
7
|
+
Configuration management using environment variables.
|
|
8
|
+
|
|
9
|
+
Robust error handling and logging, including context capture.
|
|
10
|
+
|
|
11
|
+
Comprehensive testing with pytest.
|
|
12
|
+
|
|
13
|
+
Detailed documentation using docstrings and README files.
|
|
14
|
+
|
|
15
|
+
Code style consistency using Ruff.
|
|
16
|
+
|
|
17
|
+
CI/CD implementation with GitHub Actions or GitLab CI.
|
|
18
|
+
|
|
19
|
+
AI-friendly coding practices:
|
|
20
|
+
|
|
21
|
+
You provide code snippets and explanations tailored to these principles, optimizing for clarity and AI-assisted development.
|
|
22
|
+
|
|
23
|
+
Follow the following rules:
|
|
24
|
+
|
|
25
|
+
For any python file, be sure to ALWAYS add typing annotations to each function or class. Be sure to include return types when necessary. Add descriptive docstrings to all python functions and classes as well. Please use pep257 convention. Update existing docstrings if need be.
|
|
26
|
+
|
|
27
|
+
Make sure you keep any comments that exist in a file.
|
|
28
|
+
|
|
29
|
+
When writing tests, make sure that you ONLY use pytest or pytest plugins, do NOT use the unittest module. All tests should have typing annotations as well. All tests should be in ./tests. Be sure to create all necessary files and folders. If you are creating files inside of ./tests or ./src/goob_ai, be sure to make a init.py file if one does not exist.
|
|
30
|
+
|
|
31
|
+
All tests should be fully annotated and should contain docstrings. Be sure to import the following if TYPE_CHECKING:
|
|
32
|
+
|
|
33
|
+
from _pytest.capture import CaptureFixture
|
|
34
|
+
from _pytest.fixtures import FixtureRequest
|
|
35
|
+
from _pytest.logging import LogCaptureFixture
|
|
36
|
+
from _pytest.monkeypatch import MonkeyPatch
|
|
37
|
+
from pytest_mock.plugin import MockerFixture
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# ---
|
|
3
|
+
# jupyter:
|
|
4
|
+
# jupytext:
|
|
5
|
+
# cell_metadata_filter: -all
|
|
6
|
+
# custom_cell_magics: kql
|
|
7
|
+
# text_representation:
|
|
8
|
+
# extension: .py
|
|
9
|
+
# format_name: percent
|
|
10
|
+
# format_version: '1.3'
|
|
11
|
+
# jupytext_version: 1.11.2
|
|
12
|
+
# kernelspec:
|
|
13
|
+
# display_name: docs
|
|
14
|
+
# language: python
|
|
15
|
+
# name: python3
|
|
16
|
+
# ---
|
|
17
|
+
|
|
18
|
+
# %% [markdown]
|
|
19
|
+
# Copyright 2022 The JaxGaussianProcesses Contributors. All Rights Reserved.
|
|
20
|
+
#
|
|
21
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
22
|
+
# you may not use this file except in compliance with the License.
|
|
23
|
+
# You may obtain a copy of the License at
|
|
24
|
+
#
|
|
25
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
26
|
+
#
|
|
27
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
28
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
29
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
30
|
+
# See the License for the specific language governing permissions and
|
|
31
|
+
# limitations under the License.
|
|
32
|
+
# ==============================================================================
|
|
33
|
+
|
|
34
|
+
# %%
|
|
35
|
+
"""Example of using the OrthogonalAdditiveKernel."""
|
|
36
|
+
|
|
37
|
+
# %%
|
|
38
|
+
import jax
|
|
39
|
+
from jax import config
|
|
40
|
+
|
|
41
|
+
config.update("jax_enable_x64", True) # Enable Float64 precision
|
|
42
|
+
|
|
43
|
+
import jax.numpy as jnp
|
|
44
|
+
import matplotlib.pyplot as plt
|
|
45
|
+
from matplotlib.colors import ListedColormap
|
|
46
|
+
import optax
|
|
47
|
+
|
|
48
|
+
import gpjax as gpx
|
|
49
|
+
from gpjax.dataset import Dataset
|
|
50
|
+
from gpjax.kernels import OrthogonalAdditiveKernel, RBF
|
|
51
|
+
from gpjax.typing import KeyArray
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# %%
|
|
55
|
+
def f(x: jnp.ndarray) -> jnp.ndarray:
|
|
56
|
+
"""Additive function with mixed dependencies:
|
|
57
|
+
f(x) = sin(π*x₁) + 2*cos(2π*x₂) + 0.5*sin(3π*x₁*x₂)
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
x: Input points array with shape (..., 2)
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
Function values at the input points
|
|
64
|
+
"""
|
|
65
|
+
return (
|
|
66
|
+
jnp.sin(jnp.pi * x[..., 0])
|
|
67
|
+
+ 2.0 * jnp.cos(2.0 * jnp.pi * x[..., 1])
|
|
68
|
+
+ 0.5 * jnp.sin(3.0 * jnp.pi * x[..., 0] * x[..., 1])
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
# %%
|
|
73
|
+
def generate_data(
|
|
74
|
+
key: KeyArray, n_train: int = 100, noise_std: float = 0.1
|
|
75
|
+
) -> tuple[Dataset, jnp.ndarray, jnp.ndarray]:
|
|
76
|
+
"""Generate synthetic training data.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
key: JAX PRNG key for random number generation
|
|
80
|
+
n_train: Number of training points to generate
|
|
81
|
+
noise_std: Standard deviation of Gaussian observation noise
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
Tuple of (training_data, X_test, meshgrid_for_plotting)
|
|
85
|
+
"""
|
|
86
|
+
key1, key2, key3 = jax.random.split(key, 3)
|
|
87
|
+
|
|
88
|
+
# Generate training data
|
|
89
|
+
X_train = jax.random.uniform(key1, (n_train, 2))
|
|
90
|
+
y_train = f(X_train) + noise_std * jax.random.normal(key2, (n_train,))
|
|
91
|
+
|
|
92
|
+
training_data = Dataset(X=X_train, y=y_train[:, None])
|
|
93
|
+
|
|
94
|
+
# Generate test points for prediction
|
|
95
|
+
n_test = 20
|
|
96
|
+
x_range = jnp.linspace(0.0, 1.0, n_test)
|
|
97
|
+
X1, X2 = jnp.meshgrid(x_range, x_range)
|
|
98
|
+
X_test = jnp.vstack([X1.flatten(), X2.flatten()]).T
|
|
99
|
+
|
|
100
|
+
return training_data, X_test, (X1, X2)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
# %%
|
|
104
|
+
def main():
|
|
105
|
+
# Set random seed for reproducibility
|
|
106
|
+
key = jax.random.PRNGKey(42)
|
|
107
|
+
|
|
108
|
+
# Generate synthetic training data
|
|
109
|
+
training_data, X_test, (X1, X2) = generate_data(key, n_train=100, noise_std=0.1)
|
|
110
|
+
|
|
111
|
+
# Create base kernel (RBF)
|
|
112
|
+
base_kernel = RBF(lengthscale=0.2)
|
|
113
|
+
|
|
114
|
+
# Create OAK kernel with second-order interactions
|
|
115
|
+
oak_kernel = OrthogonalAdditiveKernel(
|
|
116
|
+
base_kernel=base_kernel,
|
|
117
|
+
dim=2,
|
|
118
|
+
quad_deg=20,
|
|
119
|
+
second_order=True,
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
# Create a GP prior model
|
|
123
|
+
prior = gpx.gps.Prior(
|
|
124
|
+
mean_function=gpx.mean_functions.Zero(),
|
|
125
|
+
kernel=oak_kernel,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
# Create a likelihood
|
|
129
|
+
likelihood = gpx.likelihoods.Gaussian(num_datapoints=training_data.n)
|
|
130
|
+
|
|
131
|
+
# Create the posterior
|
|
132
|
+
posterior = prior * likelihood
|
|
133
|
+
|
|
134
|
+
# Create parameter optimizer
|
|
135
|
+
optimizer = optax.adam(learning_rate=0.01)
|
|
136
|
+
|
|
137
|
+
# Define objective function for training
|
|
138
|
+
def objective(model, data):
|
|
139
|
+
return -model.mll(model.params, data)
|
|
140
|
+
|
|
141
|
+
# Optimize hyperparameters
|
|
142
|
+
opt_posterior, history = gpx.fit(
|
|
143
|
+
model=posterior,
|
|
144
|
+
objective=objective,
|
|
145
|
+
train_data=training_data,
|
|
146
|
+
optim=optimizer,
|
|
147
|
+
num_iters=300,
|
|
148
|
+
key=key,
|
|
149
|
+
verbose=True,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
# Plot training curve
|
|
153
|
+
plt.figure(figsize=(10, 4))
|
|
154
|
+
plt.subplot(1, 2, 1)
|
|
155
|
+
plt.plot(history)
|
|
156
|
+
plt.title("Negative Log Marginal Likelihood")
|
|
157
|
+
plt.xlabel("Iteration")
|
|
158
|
+
plt.ylabel("NLML")
|
|
159
|
+
|
|
160
|
+
# Get posterior predictions
|
|
161
|
+
latent_dist = opt_posterior.predict(params=opt_posterior.params, x=X_test)
|
|
162
|
+
predictive_dist = opt_posterior.likelihood.condition(
|
|
163
|
+
latent_dist, opt_posterior.params
|
|
164
|
+
)
|
|
165
|
+
mu = predictive_dist.mean().reshape(X1.shape)
|
|
166
|
+
std = predictive_dist.stddev().reshape(X1.shape)
|
|
167
|
+
|
|
168
|
+
# Plot predictions
|
|
169
|
+
plt.subplot(1, 2, 2)
|
|
170
|
+
plt.contourf(X1, X2, mu, 50, cmap="viridis")
|
|
171
|
+
plt.colorbar(label="Predicted Mean")
|
|
172
|
+
plt.scatter(
|
|
173
|
+
training_data.X[:, 0],
|
|
174
|
+
training_data.X[:, 1],
|
|
175
|
+
c=training_data.y,
|
|
176
|
+
cmap=ListedColormap(["red", "blue"]),
|
|
177
|
+
alpha=0.6,
|
|
178
|
+
s=20,
|
|
179
|
+
edgecolors="k",
|
|
180
|
+
)
|
|
181
|
+
plt.title("OAK GP Predictions")
|
|
182
|
+
plt.xlabel("$x_1$")
|
|
183
|
+
plt.ylabel("$x_2$")
|
|
184
|
+
|
|
185
|
+
plt.tight_layout()
|
|
186
|
+
plt.savefig("oak_example.png", dpi=300)
|
|
187
|
+
plt.show()
|
|
188
|
+
|
|
189
|
+
# Print learned kernel parameters
|
|
190
|
+
print("\nLearned Parameters:")
|
|
191
|
+
print(f"Offset coefficient: {opt_posterior.params.kernel.offset.value}")
|
|
192
|
+
print(f"First-order coefficients: {opt_posterior.params.kernel.coeffs_1.value}")
|
|
193
|
+
|
|
194
|
+
# Analyze the importance of each dimension
|
|
195
|
+
importance_1st_order = opt_posterior.params.kernel.coeffs_1.value
|
|
196
|
+
total_importance = jnp.sum(importance_1st_order)
|
|
197
|
+
relative_importance = importance_1st_order / total_importance
|
|
198
|
+
|
|
199
|
+
print("\nRelative Importance of Input Dimensions:")
|
|
200
|
+
for i, imp in enumerate(relative_importance):
|
|
201
|
+
print(f"Dimension {i+1}: {imp:.4f}")
|
|
202
|
+
|
|
203
|
+
if opt_posterior.params.kernel.coeffs_2 is not None:
|
|
204
|
+
# Analyze second-order interactions
|
|
205
|
+
coeffs_2 = opt_posterior.params.kernel.coeffs_2
|
|
206
|
+
print("\nSecond-order Interaction Coefficient:")
|
|
207
|
+
print(f"{coeffs_2[0, 1]:.4f}")
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
# %%
|
|
211
|
+
if __name__ == "__main__":
|
|
212
|
+
main()
|
|
213
|
+
|
|
214
|
+
# %%
|
|
@@ -39,7 +39,7 @@ __license__ = "MIT"
|
|
|
39
39
|
__description__ = "Didactic Gaussian processes in JAX"
|
|
40
40
|
__url__ = "https://github.com/JaxGaussianProcesses/GPJax"
|
|
41
41
|
__contributors__ = "https://github.com/JaxGaussianProcesses/GPJax/graphs/contributors"
|
|
42
|
-
__version__ = "0.10.
|
|
42
|
+
__version__ = "0.10.1"
|
|
43
43
|
|
|
44
44
|
__all__ = [
|
|
45
45
|
"base",
|
|
@@ -32,6 +32,7 @@ from gpjax.kernels.computations import (
|
|
|
32
32
|
from gpjax.parameters import (
|
|
33
33
|
Parameter,
|
|
34
34
|
Real,
|
|
35
|
+
Static,
|
|
35
36
|
)
|
|
36
37
|
from gpjax.typing import (
|
|
37
38
|
Array,
|
|
@@ -220,7 +221,9 @@ class Constant(AbstractKernel):
|
|
|
220
221
|
def __init__(
|
|
221
222
|
self,
|
|
222
223
|
active_dims: tp.Union[list[int], slice, None] = None,
|
|
223
|
-
constant: tp.Union[
|
|
224
|
+
constant: tp.Union[
|
|
225
|
+
ScalarFloat, Parameter[ScalarFloat], Static[ScalarFloat]
|
|
226
|
+
] = jnp.array(0.0),
|
|
224
227
|
compute_engine: AbstractKernelComputation = DenseKernelComputation(),
|
|
225
228
|
):
|
|
226
229
|
if isinstance(constant, Parameter):
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
# Copyright 2022 The JaxGaussianProcesses Contributors. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
|
|
16
|
+
from typing import Optional, Callable, Union, Tuple
|
|
17
|
+
|
|
18
|
+
import beartype.typing as tp
|
|
19
|
+
from flax import nnx
|
|
20
|
+
import jax
|
|
21
|
+
import jax.numpy as jnp
|
|
22
|
+
from jaxtyping import Float, Array, Scalar
|
|
23
|
+
from gpjax.typing import ScalarFloat
|
|
24
|
+
|
|
25
|
+
from gpjax.kernels.base import AbstractKernel
|
|
26
|
+
from gpjax.kernels.computations import AbstractKernelComputation, DenseKernelComputation
|
|
27
|
+
from gpjax.parameters import PositiveReal, Parameter
|
|
28
|
+
from jax.typing import ArrayLike
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def legendre_polynomial(n: int) -> Callable:
|
|
32
|
+
"""Compute the Legendre polynomial of degree n.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
n: Degree of the Legendre polynomial.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
Function that evaluates the nth Legendre polynomial at given points.
|
|
39
|
+
"""
|
|
40
|
+
if n == 0:
|
|
41
|
+
return lambda x: jnp.ones_like(x)
|
|
42
|
+
elif n == 1:
|
|
43
|
+
return lambda x: x
|
|
44
|
+
else:
|
|
45
|
+
p_n_minus_2 = legendre_polynomial(n - 2)
|
|
46
|
+
p_n_minus_1 = legendre_polynomial(n - 1)
|
|
47
|
+
return (
|
|
48
|
+
lambda x: ((2 * n - 1) * x * p_n_minus_1(x) - (n - 1) * p_n_minus_2(x)) / n
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def legendre_polynomial_derivative(n: int) -> Callable:
|
|
53
|
+
"""Compute the derivative of the Legendre polynomial of degree n.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
n: Degree of the Legendre polynomial.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
Function that evaluates the derivative of the nth Legendre polynomial.
|
|
60
|
+
"""
|
|
61
|
+
if n == 0:
|
|
62
|
+
return lambda x: jnp.zeros_like(x)
|
|
63
|
+
else:
|
|
64
|
+
p_n_minus_1 = legendre_polynomial(n - 1)
|
|
65
|
+
return (
|
|
66
|
+
lambda x: n
|
|
67
|
+
* (p_n_minus_1(x) - x * legendre_polynomial_derivative(n - 1)(x))
|
|
68
|
+
/ (1 - x**2 + 1e-10)
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def gauss_legendre_quadrature(
|
|
73
|
+
deg: int, a: float = -1.0, b: float = 1.0
|
|
74
|
+
) -> Tuple[jnp.ndarray, jnp.ndarray]:
|
|
75
|
+
"""Generate Gauss-Legendre quadrature points and weights.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
deg: Number of quadrature points.
|
|
79
|
+
a: Lower limit of integration (default: -1.0).
|
|
80
|
+
b: Upper limit of integration (default: 1.0).
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
Tuple of (points, weights) for Gauss-Legendre quadrature.
|
|
84
|
+
"""
|
|
85
|
+
# For computational efficiency, use a simpler approach for small degrees
|
|
86
|
+
if deg <= 4:
|
|
87
|
+
# Hardcoded points and weights for small degrees
|
|
88
|
+
if deg == 1:
|
|
89
|
+
x = jnp.array([0.0])
|
|
90
|
+
w = jnp.array([2.0])
|
|
91
|
+
elif deg == 2:
|
|
92
|
+
x = jnp.array([-0.5773502691896257, 0.5773502691896257])
|
|
93
|
+
w = jnp.array([1.0, 1.0])
|
|
94
|
+
elif deg == 3:
|
|
95
|
+
x = jnp.array([-0.7745966692414834, 0.0, 0.7745966692414834])
|
|
96
|
+
w = jnp.array([0.5555555555555556, 0.8888888888888888, 0.5555555555555556])
|
|
97
|
+
elif deg == 4:
|
|
98
|
+
x = jnp.array(
|
|
99
|
+
[
|
|
100
|
+
-0.8611363115940526,
|
|
101
|
+
-0.3399810435848563,
|
|
102
|
+
0.3399810435848563,
|
|
103
|
+
0.8611363115940526,
|
|
104
|
+
]
|
|
105
|
+
)
|
|
106
|
+
w = jnp.array(
|
|
107
|
+
[
|
|
108
|
+
0.3478548451374538,
|
|
109
|
+
0.6521451548625461,
|
|
110
|
+
0.6521451548625461,
|
|
111
|
+
0.3478548451374538,
|
|
112
|
+
]
|
|
113
|
+
)
|
|
114
|
+
else:
|
|
115
|
+
# Initial guess for roots (Chebyshev nodes)
|
|
116
|
+
k = jnp.arange(1, deg + 1)
|
|
117
|
+
x0 = jnp.cos(jnp.pi * (k - 0.25) / (deg + 0.5))
|
|
118
|
+
|
|
119
|
+
# Newton iteration to find roots more accurately
|
|
120
|
+
# In practice, we would use more iterations, but for simplicity we use a fixed number
|
|
121
|
+
P_n = legendre_polynomial(deg)
|
|
122
|
+
dP_n = legendre_polynomial_derivative(deg)
|
|
123
|
+
|
|
124
|
+
# Single Newton step for demonstration (would use a loop in practice)
|
|
125
|
+
x = x0 - P_n(x0) / (dP_n(x0) + 1e-10)
|
|
126
|
+
|
|
127
|
+
# Compute weights
|
|
128
|
+
w = 2.0 / ((1.0 - x**2) * dP_n(x) ** 2 + 1e-10)
|
|
129
|
+
|
|
130
|
+
# Scale from [-1,1] to [a,b]
|
|
131
|
+
x = 0.5 * (b - a) * x + 0.5 * (b + a)
|
|
132
|
+
w = 0.5 * (b - a) * w
|
|
133
|
+
|
|
134
|
+
return x, w
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class OrthogonalAdditiveKernel(AbstractKernel):
|
|
138
|
+
"""Orthogonal Additive Kernels (OAKs) generalize additive kernels by orthogonalizing
|
|
139
|
+
the feature space to create uncorrelated kernel components.
|
|
140
|
+
|
|
141
|
+
This implementation uses a Gauss-Legendre quadrature approximation for the required
|
|
142
|
+
one-dimensional integrals involving the base kernels, allowing for arbitrary base kernels.
|
|
143
|
+
|
|
144
|
+
References:
|
|
145
|
+
- X. Lu, A. Boukouvalas, and J. Hensman. Additive Gaussian processes revisited.
|
|
146
|
+
Proceedings of the 39th International Conference on Machine Learning. Jul 2022.
|
|
147
|
+
"""
|
|
148
|
+
|
|
149
|
+
base_kernel: AbstractKernel
|
|
150
|
+
quad_deg: int
|
|
151
|
+
dim: int
|
|
152
|
+
offset: nnx.Variable
|
|
153
|
+
coeffs_1: nnx.Variable
|
|
154
|
+
coeffs_2: Optional[nnx.Variable]
|
|
155
|
+
z: jnp.ndarray
|
|
156
|
+
w: jnp.ndarray
|
|
157
|
+
name: str = "OrthogonalAdditiveKernel"
|
|
158
|
+
|
|
159
|
+
def __init__(
|
|
160
|
+
self,
|
|
161
|
+
base_kernel: AbstractKernel,
|
|
162
|
+
dim: int,
|
|
163
|
+
quad_deg: int = 32,
|
|
164
|
+
second_order: bool = False,
|
|
165
|
+
active_dims: tp.Union[list[int], slice, None] = None,
|
|
166
|
+
n_dims: tp.Union[int, None] = None,
|
|
167
|
+
offset: tp.Union[float, Parameter[ScalarFloat]] = 1.0,
|
|
168
|
+
coeffs_1: tp.Union[ArrayLike, Parameter[ArrayLike]] = None,
|
|
169
|
+
coeffs_2: tp.Union[ArrayLike, Parameter[ArrayLike]] = None,
|
|
170
|
+
compute_engine: AbstractKernelComputation = DenseKernelComputation(),
|
|
171
|
+
):
|
|
172
|
+
"""Initialise the OrthogonalAdditiveKernel.
|
|
173
|
+
|
|
174
|
+
Args:
|
|
175
|
+
base_kernel: The kernel which to orthogonalize and evaluate.
|
|
176
|
+
dim: Input dimensionality of the kernel.
|
|
177
|
+
quad_deg: Number of integration nodes for orthogonalization.
|
|
178
|
+
second_order: Toggles second order interactions. If true, both the time and
|
|
179
|
+
space complexity of evaluating the kernel are quadratic in `dim`.
|
|
180
|
+
active_dims: The indices of the input dimensions that the kernel operates on.
|
|
181
|
+
n_dims: The number of input dimensions. If not provided, it will be inferred.
|
|
182
|
+
offset: The zeroth-order coefficient.
|
|
183
|
+
coeffs_1: The first-order coefficients. Should be a 1D array of length dim.
|
|
184
|
+
coeffs_2: The second-order coefficients. Should be a 2D array of shape (dim, dim).
|
|
185
|
+
compute_engine: The computation engine to use for kernel evaluations.
|
|
186
|
+
"""
|
|
187
|
+
super().__init__(
|
|
188
|
+
active_dims=active_dims, n_dims=n_dims, compute_engine=compute_engine
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
self.base_kernel = base_kernel
|
|
192
|
+
self.quad_deg = quad_deg
|
|
193
|
+
self.dim = dim
|
|
194
|
+
|
|
195
|
+
# Integration nodes and weights for [0, 1]
|
|
196
|
+
self.z, self.w = gauss_legendre_quadrature(quad_deg, a=0.0, b=1.0)
|
|
197
|
+
|
|
198
|
+
# Create expandable axes
|
|
199
|
+
z_expanded = jnp.expand_dims(self.z, axis=-1)
|
|
200
|
+
self.z = jnp.broadcast_to(z_expanded, (quad_deg, dim))
|
|
201
|
+
self.w = jnp.expand_dims(self.w, axis=-1)
|
|
202
|
+
|
|
203
|
+
# Default coefficients if not provided
|
|
204
|
+
if isinstance(offset, Parameter):
|
|
205
|
+
self.offset = offset
|
|
206
|
+
else:
|
|
207
|
+
self.offset = PositiveReal(jnp.array(offset))
|
|
208
|
+
|
|
209
|
+
if coeffs_1 is None:
|
|
210
|
+
log_d = jnp.log(dim)
|
|
211
|
+
default_coeffs_1 = jnp.exp(-log_d) * jnp.ones(dim)
|
|
212
|
+
self.coeffs_1 = PositiveReal(default_coeffs_1)
|
|
213
|
+
elif isinstance(coeffs_1, Parameter):
|
|
214
|
+
self.coeffs_1 = coeffs_1
|
|
215
|
+
else:
|
|
216
|
+
self.coeffs_1 = PositiveReal(jnp.array(coeffs_1))
|
|
217
|
+
|
|
218
|
+
self.second_order = second_order
|
|
219
|
+
if second_order:
|
|
220
|
+
if coeffs_2 is None:
|
|
221
|
+
log_d = jnp.log(dim)
|
|
222
|
+
# Initialize with zeros for upper triangular part (excluding diagonal)
|
|
223
|
+
n_entries = dim * (dim - 1) // 2
|
|
224
|
+
default_coeffs_2 = jnp.exp(-2 * log_d) * jnp.ones(n_entries)
|
|
225
|
+
self.coeffs_2_raw = PositiveReal(default_coeffs_2)
|
|
226
|
+
elif isinstance(coeffs_2, Parameter):
|
|
227
|
+
self.coeffs_2_raw = coeffs_2
|
|
228
|
+
else:
|
|
229
|
+
self.coeffs_2_raw = PositiveReal(jnp.array(coeffs_2))
|
|
230
|
+
|
|
231
|
+
# Pre-compute indices for efficient triu operations
|
|
232
|
+
self.triu_indices = jnp.triu_indices(dim, k=1)
|
|
233
|
+
else:
|
|
234
|
+
self.coeffs_2_raw = None
|
|
235
|
+
|
|
236
|
+
# Compute normalizer (in __call__)
|
|
237
|
+
self._normalizer = None
|
|
238
|
+
|
|
239
|
+
@property
|
|
240
|
+
def coeffs_2(self) -> Optional[jnp.ndarray]:
|
|
241
|
+
"""Returns a full matrix of second-order coefficients.
|
|
242
|
+
|
|
243
|
+
Returns:
|
|
244
|
+
A dim x dim array of second-order coefficients or None if second_order is False.
|
|
245
|
+
"""
|
|
246
|
+
if not self.second_order or self.coeffs_2_raw is None:
|
|
247
|
+
return None
|
|
248
|
+
|
|
249
|
+
# Create a full matrix from the raw coefficients
|
|
250
|
+
coeffs_2_flat = self.coeffs_2_raw.value
|
|
251
|
+
coeffs_2_full = jnp.zeros((self.dim, self.dim))
|
|
252
|
+
|
|
253
|
+
# Fill the upper triangular part
|
|
254
|
+
i, j = self.triu_indices
|
|
255
|
+
coeffs_2_full = coeffs_2_full.at[i, j].set(coeffs_2_flat)
|
|
256
|
+
|
|
257
|
+
# Make it symmetric
|
|
258
|
+
coeffs_2_full = coeffs_2_full + jnp.transpose(coeffs_2_full)
|
|
259
|
+
|
|
260
|
+
return coeffs_2_full
|
|
261
|
+
|
|
262
|
+
def normalizer(self, eps: float = 1e-6) -> jnp.ndarray:
|
|
263
|
+
"""Integrates the orthogonalized base kernels over [0, 1] x [0, 1].
|
|
264
|
+
|
|
265
|
+
Args:
|
|
266
|
+
eps: Minimum value constraint on the normalizers to avoid division by zero.
|
|
267
|
+
|
|
268
|
+
Returns:
|
|
269
|
+
A d-dim tensor of normalization constants.
|
|
270
|
+
"""
|
|
271
|
+
if self._normalizer is None or self.training:
|
|
272
|
+
# Compute K(z, z) - base kernel gram matrix on integration points
|
|
273
|
+
K_zz = self.base_kernel.cross_covariance(self.z, self.z)
|
|
274
|
+
|
|
275
|
+
# Integrate: w^T * K * w
|
|
276
|
+
self._normalizer = jnp.matmul(
|
|
277
|
+
jnp.matmul(jnp.transpose(self.w), K_zz), self.w
|
|
278
|
+
)
|
|
279
|
+
|
|
280
|
+
# Ensure positive values
|
|
281
|
+
self._normalizer = jnp.maximum(self._normalizer, eps)
|
|
282
|
+
|
|
283
|
+
return self._normalizer
|
|
284
|
+
|
|
285
|
+
def _orthogonal_base_kernels(
|
|
286
|
+
self, x1: Float[Array, "N D"], x2: Float[Array, "M D"]
|
|
287
|
+
) -> Float[Array, "D N M"]:
|
|
288
|
+
"""Evaluates the set of d orthogonalized base kernels.
|
|
289
|
+
|
|
290
|
+
Args:
|
|
291
|
+
x1: Input array of shape [N, D]
|
|
292
|
+
x2: Input array of shape [M, D]
|
|
293
|
+
|
|
294
|
+
Returns:
|
|
295
|
+
Array of shape [D, N, M] with orthogonalized kernel evaluations
|
|
296
|
+
"""
|
|
297
|
+
# Compute base kernel between inputs
|
|
298
|
+
K_x1x2 = self.base_kernel.cross_covariance(x1, x2) # [N, M]
|
|
299
|
+
|
|
300
|
+
# Compute normalizer
|
|
301
|
+
norm = jnp.sqrt(self.normalizer())
|
|
302
|
+
w_normalized = self.w / norm
|
|
303
|
+
|
|
304
|
+
# Compute base kernel between x1 and integration points z
|
|
305
|
+
K_x1z = self.base_kernel.cross_covariance(x1, self.z) # [N, quad_deg]
|
|
306
|
+
S_x1 = jnp.matmul(K_x1z, w_normalized) # [N, 1]
|
|
307
|
+
|
|
308
|
+
# Compute base kernel between x2 and integration points z
|
|
309
|
+
if x1 is x2:
|
|
310
|
+
S_x2 = S_x1
|
|
311
|
+
else:
|
|
312
|
+
K_x2z = self.base_kernel.cross_covariance(x2, self.z) # [M, quad_deg]
|
|
313
|
+
S_x2 = jnp.matmul(K_x2z, w_normalized) # [M, 1]
|
|
314
|
+
|
|
315
|
+
# Compute orthogonal kernel: K_x1x2 - S_x1 * S_x2^T
|
|
316
|
+
K_ortho = K_x1x2 - jnp.outer(S_x1, S_x2)
|
|
317
|
+
|
|
318
|
+
return K_ortho
|
|
319
|
+
|
|
320
|
+
def __call__(
|
|
321
|
+
self,
|
|
322
|
+
x: Float[Array, "N D"],
|
|
323
|
+
y: Float[Array, "M D"],
|
|
324
|
+
) -> ScalarFloat:
|
|
325
|
+
"""Evaluate the kernel at a single pair of inputs.
|
|
326
|
+
|
|
327
|
+
Args:
|
|
328
|
+
x: First input.
|
|
329
|
+
y: Second input.
|
|
330
|
+
|
|
331
|
+
Returns:
|
|
332
|
+
The kernel value at (x, y).
|
|
333
|
+
"""
|
|
334
|
+
# Slice inputs to relevant dimensions
|
|
335
|
+
x_sliced = self.slice_input(x)
|
|
336
|
+
y_sliced = self.slice_input(y)
|
|
337
|
+
|
|
338
|
+
# Get orthogonalized kernels
|
|
339
|
+
K_ortho = self._orthogonal_base_kernels(
|
|
340
|
+
jnp.expand_dims(x_sliced, 0), jnp.expand_dims(y_sliced, 0)
|
|
341
|
+
) # [1, 1]
|
|
342
|
+
|
|
343
|
+
# Apply first-order effects
|
|
344
|
+
first_order = jnp.sum(self.coeffs_1.value * K_ortho)
|
|
345
|
+
|
|
346
|
+
# Add offset
|
|
347
|
+
result = self.offset.value + first_order
|
|
348
|
+
|
|
349
|
+
# Add second-order effects if enabled
|
|
350
|
+
if self.second_order and self.coeffs_2 is not None:
|
|
351
|
+
# For a single point evaluation, we use a simpler approach
|
|
352
|
+
# Computing the tensor of second order interactions
|
|
353
|
+
second_order = 0.0
|
|
354
|
+
for i in range(self.dim):
|
|
355
|
+
for j in range(i + 1, self.dim):
|
|
356
|
+
coef = self.coeffs_2[i, j]
|
|
357
|
+
if coef > 0:
|
|
358
|
+
second_order += coef * K_ortho[i] * K_ortho[j]
|
|
359
|
+
|
|
360
|
+
result = result + second_order
|
|
361
|
+
|
|
362
|
+
return result
|
|
363
|
+
|
|
364
|
+
def cross_covariance(
|
|
365
|
+
self, x1: Float[Array, "N D"], x2: Float[Array, "M D"]
|
|
366
|
+
) -> Float[Array, "N M"]:
|
|
367
|
+
"""Compute the cross-covariance matrix of the kernel.
|
|
368
|
+
|
|
369
|
+
Args:
|
|
370
|
+
x1: First input matrix of shape [N, D].
|
|
371
|
+
x2: Second input matrix of shape [M, D].
|
|
372
|
+
|
|
373
|
+
Returns:
|
|
374
|
+
Cross-covariance matrix of shape [N, M].
|
|
375
|
+
"""
|
|
376
|
+
# Slice inputs to relevant dimensions
|
|
377
|
+
x1 = self.slice_input(x1)
|
|
378
|
+
x2 = self.slice_input(x2)
|
|
379
|
+
|
|
380
|
+
# Get orthogonalized kernels for all dimensions
|
|
381
|
+
K_ortho = self._orthogonal_base_kernels(x1, x2) # [D, N, M]
|
|
382
|
+
|
|
383
|
+
# Apply first-order effects (sum over dimensions)
|
|
384
|
+
coeffs_1 = self.coeffs_1.value
|
|
385
|
+
first_order = jnp.tensordot(coeffs_1, K_ortho, axes=([0], [0])) # [N, M]
|
|
386
|
+
|
|
387
|
+
# Add offset (broadcast to match output shape)
|
|
388
|
+
result = jnp.broadcast_to(self.offset.value, first_order.shape) + first_order
|
|
389
|
+
|
|
390
|
+
# Add second-order effects if enabled
|
|
391
|
+
if self.second_order and self.coeffs_2 is not None:
|
|
392
|
+
# Compute second-order interactions using einsum
|
|
393
|
+
coeffs_2_full = self.coeffs_2
|
|
394
|
+
second_order = jnp.einsum(
|
|
395
|
+
"ij,ink,jml->nml",
|
|
396
|
+
coeffs_2_full,
|
|
397
|
+
jnp.expand_dims(K_ortho, 1),
|
|
398
|
+
jnp.expand_dims(K_ortho, 0),
|
|
399
|
+
)
|
|
400
|
+
|
|
401
|
+
# Sum over dimensions i, j
|
|
402
|
+
second_order = jnp.sum(second_order, axis=(0, 1))
|
|
403
|
+
|
|
404
|
+
result = result + second_order
|
|
405
|
+
|
|
406
|
+
return result
|