Kernel Bw#
bw
#
| FUNCTION | DESCRIPTION |
|---|---|
bw_compute |
Compute bandwidth (squared) for Gaussian kernel from pairwise distance |
Functions#
bw_compute(D: torch.Tensor, bw_method: str = 'median', bw_kwargs: dict | None = None, weights: torch.Tensor | None = None)
#
Compute bandwidth (squared) for Gaussian kernel from pairwise distance matrix.
When weights are provided the bandwidth is estimated under the reweighted
distribution. Each pair \((i, j)\) is weighted by \(w_i w_j\). Implemented only
for bandwidths estimated using quantiles.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix of shape (n, m).
TYPE:
|
bw_method
|
Method to compute bandwidth.
TYPE:
|
bw_kwargs
|
Additional parameters for specific bandwidth methods. Options are:
TYPE:
|
weights
|
Sample weights, shape (n,).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Squared bandwidth value or matrix depending on the selected method. |
Examples:
Standard bandwidth methods:
>>> import torch
>>> from spectre.kernel import bw_compute
>>> D = torch.tensor([[0.0, 1.0, 2.0], [1.0, 0.0, 1.5], [2.0, 1.5, 0.0]])
>>> bw = bw_compute(D, bw_method="median")
>>> print(bw)
tensor(1.0000)
>>> bw_matrix = bw_compute(D, bw_method="median_1d")
>>> print(bw_matrix)
tensor([[1.0000, 1.0000, 1.5000],
[1.0000, 1.0000, 1.5000],
[1.5000, 1.5000, 2.2500]])
>>> bw = bw_compute(D, bw_method="quantile", bw_kwargs={"q": 0.9})
>>> bw = bw_compute(D, bw_method="knn", bw_kwargs={"k_neighbor": 2})
Dimension-aware methods for feature selection:
>>> # Data has 50 features
>>> bw = bw_compute(
... D,
... bw_method="median_scaled",
... bw_kwargs={"d": 50, "alpha": 0.5},
... )
>>> bw = bw_compute(D, bw_method="silverman", bw_kwargs={"d": 50})
>>> bw = bw_compute(D, bw_method="scott", bw_kwargs={"d": 50, "n": 100})
Source code in spectre/kernel/bw.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
bw_median(D: torch.Tensor, weights: torch.Tensor | None = None) -> torch.Tensor
#
Calculate bandwidth using the median of all pairwise distances.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix.
TYPE:
|
weights
|
Sample weights, shape (n,).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Squared bandwidth value. |
Source code in spectre/kernel/bw.py
bw_median_1d(D: torch.Tensor, weights: torch.Tensor | None = None) -> torch.Tensor
#
Calculate bandwidth using the median per row and column.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix.
TYPE:
|
weights
|
Sample weights, shape (n,).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Bandwidth matrix as outer product of per-row and per-column medians. |
Source code in spectre/kernel/bw.py
bw_quantile(D: torch.Tensor, q: float, weights: torch.Tensor | None = None) -> torch.Tensor
#
Calculate bandwidth using the specified quantile of all pairwise distances.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix.
TYPE:
|
q
|
Quantile value in [0.0, 1.0].
TYPE:
|
weights
|
Sample weights, shape (n,).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Squared bandwidth value. |
Source code in spectre/kernel/bw.py
bw_quantile_1d(D: torch.Tensor, q: float, weights: torch.Tensor | None = None) -> torch.Tensor
#
Calculate bandwidth using the specified quantile per row and column.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix.
TYPE:
|
q
|
Quantile value in [0.0, 1.0].
TYPE:
|
weights
|
Sample weights, shape (n,).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Bandwidth matrix as outer product of per-row and per-column quantiles. |
Source code in spectre/kernel/bw.py
bw_quantile_search(D: torch.Tensor, n_quantiles: int = 100, eps: float = 0.001, weights: torch.Tensor | None = None) -> torch.Tensor
#
Calculate bandwidth using quantile search method.
Finds the optimal bandwidth by searching across quantiles of the distance distribution and selecting the one with maximum derivative.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix.
TYPE:
|
n_quantiles
|
Number of quantiles to evaluate.
TYPE:
|
eps
|
Epsilon value to exclude symmetrically from the quantiles.
TYPE:
|
weights
|
Sample weights, shape (n,).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Optimal bandwidth value. |
Source code in spectre/kernel/bw.py
bw_k_nearest_neighbors(D: torch.Tensor, k_neighbor: int, weights: torch.Tensor | None = None) -> torch.Tensor
#
Calculate bandwidth matrix for Gaussian kernel from k nearest neighbors.
Computes k-nearest neighbors along both dimensions and returns their outer product, producing a bandwidth matrix of the same shape as the input distance matrix.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix of shape (n_samples, m_samples).
TYPE:
|
k_neighbor
|
Number of nearest neighbors. Must be < min(n_samples, m_samples).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Bandwidth matrix of shape (n_samples, m_samples) computed as outer product of per-row and per-column k-nearest neighbor distances. |
Source code in spectre/kernel/bw.py
bw_bgh(D: torch.Tensor, bw_values: torch.Tensor | None = None) -> torch.Tensor
#
Calculate the bandwidth for a Gaussian kernel matrix using the BGH method [1].
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix.
TYPE:
|
bw_values
|
Bandwidth values to test.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Optimal bandwidth value. |
Source code in spectre/kernel/bw.py
bw_median_scaled(D: torch.Tensor, d: int, alpha: float = 0.5, weights: torch.Tensor | None = None) -> torch.Tensor
#
Calculate bandwidth using median of distances with dimension scaling.
Applies dimension-aware scaling to account for curse of dimensionality. Useful for comparing kernel densities across feature subsets with different dimensionalities.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix.
TYPE:
|
d
|
Feature dimensionality (number of features in original data).
TYPE:
|
alpha
|
Scaling exponent. Bandwidth is multiplied by d^alpha. Typical values: 0.2-1.0. Higher values apply stronger penalty for high dimensions.
TYPE:
|
weights
|
Sample weights, shape (n,).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Squared bandwidth value scaled by dimensionality. |
Source code in spectre/kernel/bw.py
bw_median_1d_scaled(D: torch.Tensor, d: int, alpha: float = 0.5, weights: torch.Tensor | None = None) -> torch.Tensor
#
Calculate bandwidth matrix using per-dimension median with dimension scaling.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix.
TYPE:
|
d
|
Feature dimensionality (number of features in original data).
TYPE:
|
alpha
|
Scaling exponent. Bandwidth is multiplied by d^alpha.
TYPE:
|
weights
|
Sample weights, shape (n,).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Bandwidth matrix as outer product scaled by dimensionality. |
Source code in spectre/kernel/bw.py
bw_silverman(D: torch.Tensor, d: int, n: int | float | None = None, weights: torch.Tensor | None = None) -> torch.Tensor
#
Calculate bandwidth using Silverman's rule of thumb.
Silverman's rule provides automatic bandwidth selection that accounts for both sample size and dimensionality. Based on optimal bandwidth for Gaussian densities.
When weights are provided, the spread estimate uses the weighted median
and the sample size is replaced by Kish's effective sample size: n_eff =
(sum w_i)^2 / sum(w_i^2).
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix.
TYPE:
|
d
|
Feature dimensionality (number of features in original data).
TYPE:
|
n
|
Sample size. If None, inferred from D.shape[0] (or Kish's effective
sample size when
TYPE:
|
weights
|
Sample weights, shape (n_samples,).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Squared bandwidth value from Silverman's rule. |
Source code in spectre/kernel/bw.py
bw_scott(D: torch.Tensor, d: int, n: int | float | None = None, weights: torch.Tensor | None = None) -> torch.Tensor
#
Calculate bandwidth using Scott's rule.
Scott's rule provides automatic bandwidth selection similar to Silverman's but with different constants. Often produces slightly larger bandwidths.
When weights are provided, the spread estimate uses the weighted median
and the sample size is replaced by Kish's effective sample size: n_eff =
(sum w_i)^2 / sum(w_i^2).
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix.
TYPE:
|
d
|
Feature dimensionality (number of features in original data).
TYPE:
|
n
|
Sample size. If None, inferred from D.shape[0] (or Kish's effective
sample size when
TYPE:
|
weights
|
Sample weights, shape (n_samples,).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Squared bandwidth value from Scott's rule. |