Kernels module
kernels
Kernel construction and validation for Gaussian Processes.
This module handles kernel configuration, building, and validation. Provides a unified interface for working with multiple kernel types across numpy and JAX backends.
build_kernel(config)
Build sklearn kernel from configuration.
This is the single source of truth for kernel construction, ensuring consistency across the codebase.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Kernel configuration specifying kernel type, \(\sigma\), \(\ell\), and parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Kernel
|
sklearn Kernel object (without noise component). |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If kernel type is unknown or params are invalid. |
Examples:
>>> config = KernelConfig(KernelType.RBF, sigma=0.1, length_scale=0.5)
>>> kernel = build_kernel(config)
>>> K = kernel(log_k) # Evaluate covariance matrix
Source code in src/primefeat/gp/kernels.py
build_noise_covariance(n, noise_level=None, noise_cov=None)
Build noise covariance matrix.
Supports two modes:
- Diagonal noise (simple): \(\sigma_n^2 I\)
- Full posterior covariance (recommended for MCMC): \(\Sigma_{\mathrm{post}}\)
| PARAMETER | DESCRIPTION |
|---|---|
n
|
Number of data points.
TYPE:
|
noise_level
|
Diagonal noise standard deviation \(\sigma_n\) (used if noise_cov=None).
TYPE:
|
noise_cov
|
Full N×N posterior covariance matrix \(\Sigma_{\mathrm{post}}\).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Noise covariance matrix of shape (n, n). |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If neither noise_level nor noise_cov provided, or shape mismatch. |
Source code in src/primefeat/gp/kernels.py
compute_kernel_matrix(log_k, kernel_config)
Compute kernel covariance matrix (signal only, no noise).
Useful for visualizing kernel structure and comparing kernels.
| PARAMETER | DESCRIPTION |
|---|---|
log_k
|
\(\log(k)\) values, shape (n,) or (n, 1).
TYPE:
|
kernel_config
|
Kernel configuration specifying type and hyperparameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Signal covariance matrix \(K_{\mathrm{signal}}\) of shape (n, n). |
Source code in src/primefeat/gp/kernels.py
compare_kernels(log_k, configs)
Compute kernel matrices for multiple configurations.
Useful for comparing how different kernels represent correlation structure.
| PARAMETER | DESCRIPTION |
|---|---|
log_k
|
\(\log(k)\) values, shape (n,) or (n, 1).
TYPE:
|
configs
|
Dictionary mapping names to KernelConfig objects.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, ndarray]
|
Dictionary mapping names to kernel matrices. |
Examples:
>>> log_k = np.linspace(-7, -1.5, 20)
>>> configs = {
... 'RBF': KernelConfig(KernelType.RBF, 0.1, 0.5),
... 'RQ_low_alpha': KernelConfig(KernelType.RATIONAL_QUADRATIC, 0.1, 0.5, {'alpha': 0.5}),
... 'RQ_high_alpha': KernelConfig(KernelType.RATIONAL_QUADRATIC, 0.1, 0.5, {'alpha': 10.0}),
... }
>>> matrices = compare_kernels(log_k, configs)
Source code in src/primefeat/gp/kernels.py
compute_bin_resolution(nbins=None, k_start=None, k_end=None, binning=None)
Compute resolution limits imposed by finite binning.
With finite bins over a finite \(k\)-range, we cannot resolve arbitrarily small correlation lengths. This function computes the minimum resolvable length scale and warns if requested parameters are below this limit.
| PARAMETER | DESCRIPTION |
|---|---|
nbins
|
Number of bins (default: 20).
TYPE:
|
k_start
|
Start of \(k\)-range in \(\mathrm{Mpc}^{-1}\) (default: 1e-3).
TYPE:
|
k_end
|
End of \(k\)-range in \(\mathrm{Mpc}^{-1}\) (default: 0.34).
TYPE:
|
binning
|
optional BinningScheme object (supersedes nbins, k_start, k_end if provided)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
Dictionary with: - delta_log_k: Bin spacing in \(\log(k)\) space - log_k_range: Total range in \(\log(k)\) space - min_resolvable_length: Minimum length scale we can constrain (~2 bins) - max_sensible_length: Maximum useful length scale (~half range) |
Examples:
>>> res = compute_bin_resolution(20, 0.001, 0.23)
>>> print(f"Min length scale: {res['min_resolvable_length']:.3f}")
>>> print(f"Max length scale: {res['max_sensible_length']:.3f}")
Source code in src/primefeat/gp/kernels.py
validate_hyperparameters(sigma, length_scale, nbins=None, k_start=None, k_end=None, warn=True, binning=None)
Validate GP hyperparameters against bin resolution limits.
| PARAMETER | DESCRIPTION |
|---|---|
sigma
|
Signal standard deviation \(\sigma\).
TYPE:
|
length_scale
|
RBF kernel length scale \(\ell\) in \(\log(k)\) space.
TYPE:
|
nbins
|
Number of bins (default: 20).
TYPE:
|
k_start
|
Start of \(k\)-range in \(\mathrm{Mpc}^{-1}\) (default: 1e-3).
TYPE:
|
k_end
|
End of \(k\)-range in \(\mathrm{Mpc}^{-1}\) (default: 0.34).
TYPE:
|
warn
|
Whether to print warnings.
TYPE:
|
binning
|
optional BinningScheme object (supersedes nbins, k_start, k_end if provided)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if parameters are within reasonable bounds, False otherwise. |