Fix KernelDensity for non-whitened data (issue #25623) #31496
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reference Issues/PRs
Fixes #25623: #25623
Summary
This PR updates
KernelDensity
to correctly handle datasets whose covariance matrix is not the identity matrix.Problem
Currently,
KernelDensity
assumes that the input data is already whitened (i.e., has identity covariance) when applying scalar bandwidths. As issue #25623 describes, density estimates are incorrect with non-whitened data.Solution
This PR fixes the issue by:
Special care is taken to:
I have tested this in 1D and 2D and the results match those of
scipy.stats.gaussian_kde()
. I haven't added any additional unit tests yet though.Any other comments?
It seems there's also a separate pull request for this issue by @Charlie-XIAO, which I didn't notice before working on this patch. That PR is here: #27971.
It would be nice to get this fixed!
KernelDensity()
gives wildly incorrect results right now ...