Compute Kde#
kde
#
| CLASS | DESCRIPTION |
|---|---|
KernelDensity |
Kernel Density Estimation with GPU support. |
Classes#
KernelDensity(bw_method: float | torch.Tensor | str = 'scott', kernel_fn: Literal['gaussian', 'epanechnikov', 'tophat'] = 'gaussian', kernel_kwargs: dict | None = None, eps: float = torch.finfo(torch.float32).eps, dtype: torch.dtype = torch.float32, device: torch.device | str | None = None)
#
Bases: Estimator
Kernel Density Estimation with GPU support.
This class provides GPU-accelerated kernel density estimation compatible
with scipy.stats.gaussian_kde.
| PARAMETER | DESCRIPTION |
|---|---|
bw_method
|
Bandwidth parameter for the Gaussian kernel.
TYPE:
|
kernel_fn
|
Kernel function for bandwidth estimation.
TYPE:
|
kernel_kwargs
|
Additional parameters (reserved for future extensions).
TYPE:
|
eps
|
Small value to avoid division by zero.
TYPE:
|
dtype
|
Data type for tensors.
TYPE:
|
device
|
Device to run computations on. If None, automatically selects CUDA if available, otherwise CPU.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
X |
Training data of shape (n_samples, in_features).
TYPE:
|
weights |
Training sample weights of shape (n_samples,).
TYPE:
|
covariance |
Kernel covariance matrix: data_cov * factor^2.
TYPE:
|
inv_cov |
Inverse of kernel covariance matrix.
TYPE:
|
factor |
Bandwidth factor used.
TYPE:
|
neff |
Effective number of samples (accounting for weights).
TYPE:
|
eps |
Small value to avoid division by zero.
TYPE:
|
dtype |
Data type for tensors.
TYPE:
|
device |
Device to run computations on.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
fit |
Fit the Kernel Density model. |
predict |
Compute log-likelihood of samples under the model. |
score_samples |
Compute log-likelihood of each sample under the model. |
score |
Compute the total log-likelihood of the data. |
optimize_bandwidth |
Find optimal bandwidth using cross-validation likelihood. |
sample |
Generate random samples from the fitted kernel density model. |
Source code in spectre/compute/kde.py
Attributes#
data: torch.Tensor | None
property
#
Return training data.
weights: torch.Tensor | None
property
#
Return training sample weights.
covariance: torch.Tensor | None
property
#
Return kernel covariance matrix.
inv_cov: torch.Tensor | None
property
#
Return inverse kernel covariance matrix.
factor: float | None
property
#
Return bandwidth factor.
neff: float | None
property
#
Return effective number of samples.
Functions#
fit(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> KernelDensity
#
Fit the Kernel Density model.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples,). If provided, the KDE will be weighted, giving more importance to samples with higher weights.
TYPE:
|
target
|
Target values. Not used for density estimation but included for
compatibility with
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
KernelDensity
|
Returns self for method chaining. |
Source code in spectre/compute/kde.py
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
predict(X: torch.Tensor) -> torch.Tensor
#
Compute log-likelihood of samples under the model.
This method is an alias for score_samples() to maintain compatibility
with the Estimator interface.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Log-likelihood of each sample, shape (n_samples,). |
Source code in spectre/compute/kde.py
score_samples(X: torch.Tensor, batch_size: int | None = None) -> torch.Tensor
#
Compute log-likelihood of each sample under the model.
The log-likelihood is computed using the scipy-compatible formula: \(\log p(x) = \log \left( \sum_{i=1}^{n} w_i K(x, x_i) \right)\) where \(K\) is the kernel and \(w_i\) are (normalized) weights.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Query points of shape (n_samples, in_features).
TYPE:
|
batch_size
|
Number of samples to process at once. If None, processes all samples together.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Log-likelihood of each sample, shape (n_samples,). |
Source code in spectre/compute/kde.py
score(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Compute the total log-likelihood of the data.
This method computes the mean log-likelihood over all samples.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples,). If provided, computes weighted mean log-likelihood.
TYPE:
|
target
|
Not used for density estimation but included for compatibility.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Mean log-likelihood over all samples. Higher is better. |
Source code in spectre/compute/kde.py
optimize_bandwidth(X: torch.Tensor, weights: torch.Tensor | None = None, cv_folds: int = 5, bandwidth_range: tuple[float, float] = (0.1, 2.0), n_trials: int = 20, random_state: int | None = None) -> float
#
Find optimal bandwidth using cross-validation likelihood.
Uses k-fold CV to evaluate log-likelihood for different bandwidths.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples,).
TYPE:
|
cv_folds
|
Number of cross-validation folds.
TYPE:
|
bandwidth_range
|
Range of bandwidth factors to search.
TYPE:
|
n_trials
|
Number of bandwidth values to try.
TYPE:
|
random_state
|
Random seed for reproducibility.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Optimal bandwidth factor. |
Source code in spectre/compute/kde.py
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |
sample(n_samples: int = 1, random_state: int | None = None) -> torch.Tensor
#
Generate random samples from the fitted kernel density model.
Samples are generated by:
- Randomly selecting training samples (weighted if applicable)
- Adding Gaussian noise from N(0, covariance)
This matches scipy.stats.gaussian_kde.resample() behavior.
| PARAMETER | DESCRIPTION |
|---|---|
n_samples
|
Number of samples to generate.
TYPE:
|
random_state
|
Random seed for reproducibility.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Generated samples of shape (n_samples, in_features). |