Menu

Deep learning for NeuroImaging in Python.

Note

This page is a reference documentation. It only explains the class signature, and not how to use it. Please refer to the gallery for the big picture.

class nidl.losses.yaware_infonce.KernelMetric(kernel='gaussian', bandwidth: str | float | list[float] | ndarray = 'scott')[source]

Bases: BaseEstimator

Interface for fast weighting matrix computation.

It computes a weighting matrix W between input samples based on Kernel Density Estimation (KDE) [R30], [R31]. Concretely, it computes the following weighting matrix between multivariate samples x_1, ..., x_n \in \mathbb{R}^{d}:

W_{i,j} = K\left( H^{-\frac{1}{2}} (x_i-x_j) \right)

with K a kernel (or “weighting function”) such that:

  • K(x) \ge 0 (positive)

  • \int K(x) dx = 1 (normalized)

  • K(x) = K(-x) (symmetric)

and H\in \mathbb{R}^{d\times d} is the bandwidth in the KDE estimation of p(X).

H is a symmetric definite-positive and it can be automatically computed based on Scott’s rule [R32] or Silverman’s rule [R33] if required. In that case, the bandwidth is computed as a scaled version of the diagonal terms in the data covariance matrix:

H \propto \mathrm{diag}(\hat{\Sigma})

Parameters:

kernel : {‘gaussian’, ‘epanechnikov’, ‘exponential’, ‘linear’, ‘cosine’}, default=’gaussian’

The kernel applied to the distance between samples.

bandwidth : {‘scott’, ‘silverman’} or float or list of float, default=”scott”

The method used to calculate the estimator bandwidth:

  • If bandwidth is ‘scott’ or ‘silverman’, H is a scaled version of the diagonal terms in the data covariance matrix.

  • If bandwidth is scalar (float or int), H is set to a diagonal matrix: H = \mathrm{diag}([bandwidth,\ldots, bandwidth]).

  • If bandwidth is a list of floats, H is a diagonal matrix with the list values on the diagonal: H = \mathrm{diag}(\text{bandwidth}).

  • If bandwidth is a 2d array, it must be of shape (n_features, n_features)

Notes

Scott’s Rule [R30] estimates the bandwidth as:

H = \hat{\Sigma} \cdot n^{-\frac{2}{d+4}}

where \hat{\Sigma} is the covariance matrix of the data, n is the number of samples, and d is the number of features (d=1 for univariate data). Here, we only consider the diagonal terms (assuming features decorrelation) for numerical stability.

Silverman’s rule of thumb [R31] for multivariate data is:

H = \hat{\Sigma} \cdot \left(\frac{n(d+2)}{4}\right)^
{-\frac{2}{d+4}}

References

[R30] (1,2,3)

Rosenblatt, M. (1956). “Remarks on some nonparametric estimates of a density function”. Annals of Mathematical Statistics.

[R31] (1,2,3)

Parzen, E. (1962). “On estimation of a probability density function and mode”. Annals of Mathematicals Statistics.

[R32] (1,2)

Scott, D. W. (1992). “Multivariate Density Estimation: Theory, Practice, and Visualization”. Wiley.

[R33] (1,2)

Silverman, B. W. (1986). “Density Estimation for Statistics and Data Analysis”. Monographs on Statistics and Applied Probability.

fit(X)[source]

Computes the bandwidth in the kernel density estimation.

Parameters:

X : array of shape (n_samples, n_features)

Input data used to estimate the bandwidth (based on covariance matrix).

Returns:

self : KernelMetric

pairwise(X)[source]
Parameters:

X : array of shape (n_samples, n_features)

Input data.

Returns:

S : array of shape (n_samples, n_samples)

Similarity matrix between input data.

scotts_factor()[source]

Compute Scott’s factor.

Returns:

s : float

Scott’s factor.

set_bandwidth(X)[source]

Compute the estimator bandwidth. Implementation from scipy.

The new bandwidth calculated after a call to set_bandwidth is used for subsequent evaluation of the estimated density.

Parameters:

X : ndarray of shape (n_samples, n_features)

Input data.

silverman_factor()[source]

Compute the Silverman factor.

Returns:

s : float

The silverman factor.

Follow us

© 2025, nidl developers