Skip to content

🐞 fix(model): fit GaussianKDE on the original data and honour the constructor dataset - #3921

Open
sohailshk wants to merge 2 commits into
open-edge-platform:mainfrom
sohailshk:fix/gaussian-kde-fit
Open

sohailshk wants to merge 2 commits into
open-edge-platform:mainfrom
sohailshk:fix/gaussian-kde-fit

Conversation

@sohailshk

Copy link
Copy Markdown

📝 Description

Two bugs in anomalib.models.components.stats.GaussianKDE:

  1. GaussianKDE.cov does tensor -= mean[:, None] on dataset.T, which is a view of the dataset. So fit centers the caller's tensor in place and stores centered training points, while the queries passed to forward are not centered. With the test data (200 samples of a 3-d Gaussian with mean (4, -2, 1), queries = the first five samples + 0.1), the densities are [1.3e-07, 9.0e-07, 2.5e-15, 7.1e-05, 3.1e-09] on main and [0.0250, 0.0307, 0.0107, 0.0274, 0.0148] from scipy.stats.gaussian_kde (same Scott's-rule bandwidth). With this fix they match scipy within a relative 1.5e-7. Data that is only slightly off-center is affected too: for 100 standard-normal 2-d samples (sample mean (-0.018, -0.027)) the densities at the first five samples are up to 3.6% off on main.
  2. GaussianKDE(dataset=x), the one-step usage in the class docstring, calls fit before the buffers are registered. fit assigns plain attributes, so the following register_buffer raises KeyError: "attribute 'bw_transform' already exists".

The fix makes cov non-mutating and fits after the buffers are registered. forward is unchanged, so saved models give the same scores as before.

Effect on DFKDE: with the default feature_scaling_method="scale", the KDE input is zero-mean PCA output, so scores do not change (max log-density difference 1.9e-6 on synthetic embeddings). With feature_scaling_method="norm", each row is rescaled after PCA, so the input mean is no longer zero and scores change slightly (max log-density difference 0.095 on the same embeddings).

Tests: the density equals a reference KDE (Scott's rule) for off-center data, fit leaves its input unchanged, and the constructor dataset gives the same model as fit. All three fail on main.

  • 🛠️ Fixes: no open issue

✨ Changes

  • 🐞 Bug fix (non-breaking change which fixes an issue)

✅ Checklist

  • 📚 I have made the necessary updates to the documentation (if applicable). (not applicable)
  • 🧪 I have written tests that support my changes and prove that my fix is effective or my feature works (if applicable).
  • 🏷️ My PR title follows conventional commit format.

Checks run: pytest tests/unit/models/components/stats (3 passed; all 3 fail on main), ruff check and ruff format --check (0.12.0), mypy (1.16.1) on kde.py. Not run: the full suite, DFKDE integration tests.

…ructor dataset

GaussianKDE.cov centered the fitted tensor in place, so densities were evaluated against centered training data but uncentered queries, and the constructor fitted before registering the buffers, which raised a KeyError.

Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com>
Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com>
@sohailshk
sohailshk force-pushed the fix/gaussian-kde-fit branch from 7e62d07 to 53e1028 Compare November 13, 2026 16:39

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant