Stats Components#

Statistical functions for anomaly detection models.

This module provides statistical methods used in anomaly detection models for density estimation and probability modeling.

Classes:
GaussianKDE: Gaussian kernel density estimation for non-parametric density

estimation.

MultiVariateGaussian: Multivariate Gaussian distribution for parametric

density modeling.

Example

>>> import torch
>>> from anomalib.models.components.stats import GaussianKDE
>>> # Create density estimator
>>> kde = GaussianKDE()
>>> # Fit and evaluate density
>>> features = torch.randn(100, 10)  # 100 samples, 10 dimensions
>>> kde.fit(features)
>>> density = kde.predict(features)
class anomalib.models.components.stats.GaussianKDE(dataset=None)#

Bases: DynamicBufferMixin

Gaussian Kernel Density Estimation.

Estimates probability density using a Gaussian kernel function. The bandwidth is selected automatically using Scott’s rule.

Parameters:

dataset (torch.Tensor | None, optional) – Dataset on which to fit the KDE model. If provided, the model will be fitted immediately. Defaults to None.

Example

>>> import torch
>>> from anomalib.models.components.stats import GaussianKDE
>>> features = torch.randn(100, 10)  # 100 samples, 10 dimensions
>>> # Initialize and fit in one step
>>> kde = GaussianKDE(dataset=features)
>>> # Or fit later
>>> kde = GaussianKDE()
>>> kde.fit(features)
>>> # Get density estimates
>>> density = kde(features)
static cov(tensor)#

Calculate the unbiased covariance matrix.

Parameters:

tensor (torch.Tensor) – Input tensor of shape (D, N) where D is the dimension and N is the number of samples.

Returns:

Covariance matrix of shape (D, D).

Return type:

torch.Tensor

Example

>>> x = torch.randn(5, 100)  # 5 dimensions, 100 samples
>>> cov_matrix = GaussianKDE.cov(x)
>>> cov_matrix.shape
torch.Size([5, 5])
fit(dataset)#

Fit the KDE model to the input dataset.

Computes the bandwidth matrix using Scott’s rule and transforms the data accordingly.

Parameters:

dataset (torch.Tensor) – Input dataset of shape (N, D) where N is the number of samples and D is the dimension.

Return type:

None

Example

>>> kde = GaussianKDE()
>>> features = torch.randn(100, 10)
>>> kde.fit(features)
forward(features)#

Compute KDE estimates for the input features.

Parameters:

features (torch.Tensor) – Feature tensor of shape (N, D) where N is the number of samples and D is the dimension.

Returns:

Density estimates for each input sample, shape (N,).

Return type:

torch.Tensor

Example

>>> kde = GaussianKDE()
>>> features = torch.randn(100, 10)
>>> kde.fit(features)
>>> estimates = kde(features)
>>> estimates.shape
torch.Size([100])
class anomalib.models.components.stats.MultiVariateGaussian#

Bases: DynamicBufferMixin, Module

Multi Variate Gaussian Distribution.

Estimates a multivariate Gaussian distribution by computing the mean and covariance matrix from input feature embeddings. The distribution parameters are stored as buffers.

Example

>>> import torch
>>> from anomalib.models.components.stats import MultiVariateGaussian
>>> mvg = MultiVariateGaussian()
>>> features = torch.randn(100, 64, 32, 32)  # B x C x H x W
>>> mean, inv_cov = mvg.fit(features)
>>> print(mean.shape)      # [64, 1024]
>>> print(inv_cov.shape)   # [1024, 64, 64]
fit(embedding)#

Fit multivariate Gaussian distribution to input embeddings.

Convenience method that calls forward() to compute distribution parameters.

Parameters:

embedding (Tensor) – Input tensor of shape (B, C, H, W) containing CNN feature embeddings.

Return type:

list[Tensor]

Returns:

List containing the mean and inverse covariance tensors.

forward(embedding)#

Calculate multivariate Gaussian distribution parameters.

Computes the mean and inverse covariance matrix from input feature embeddings. A small regularization term (0.01) is added to the diagonal of the covariance matrix for numerical stability.

Parameters:

embedding (Tensor) – Input tensor of shape (B, C, H, W) containing CNN feature embeddings.

Returns:

  • Mean tensor of shape (C, H*W)

  • Inverse covariance tensor of shape (H*W, C, C)

Return type:

List containing