Re-scale data before performing bivariate KDE
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start at the existing bivariate kde constructor and BivariateKDE implementation, then compare its separate x/y bandwidth handling with the proposed rescaled_bivariate_kde approach. Validate the correlated-data example and confirm that the resulting density integrates to approximately 1, as shown by the issue.
Written by the indexing model from the issue text.
Description
If you have data that are highly correlated or have unusual shapes, the current code in BivariateKDE does not give a very good representation of the true distribution because the bandwidths and kernel densities are estimated separately in the x and y dimensions. For example, with this data set, the current code applied to this data:
using KernelDensity
using StatsPlots
x = randn(1024)
y = x .+ 0.1 .* randn(1024)
plot(kde((x,y)))
produces

A better approach is to estimate the bandwidth of the KDE as a 2x2 covariance matrix (this is, for example, what SciPy does); but a naive implementation of such a "full rank" bandwidth makes it much harder to employ the FFT-to-a-grid trick used in KernelDensity. An equivalent approach that allows to keep the FFT trick is to first de-correlate the input data by performing a rotation and scale so that is covariance is the identity, then estimate using the current KDE code, then de-rotate onto the original grid. This code performs the manipulations:
function rescaled_bivariate_kde((x, y))
z = hcat(x, y)
mu = mean(z, dims=1)
Sigma = cov(z, dims=1)
L = cholesky(Sigma).L
detL = prod(diag(L))
zr = L \ (z .- mu)'
kr = kde((zr[1,:], zr[2,:]))
k = kde((x,y))
ikr = InterpKDE(kr)
for i in 1:size(k.x, 1)
for j in 1:size(k.y, 1)
zr = L \ (permutedims([k.x[i], k.y[j]]) .- mu)'
p = pdf(ikr, zr[1,1], zr[2,1])
k.density[i,j] = p / detL
end
end
k
end
The result on the above data set is
plot(rescaled_bivariate_kde((x,y)))

which is a much better representation of the actual density. Note: I have checked that the proper Jacobian factors are included:
k = rescaled_bivariate_kde((x,y))
sum(k.density .* step(k.x) .* step(k.y)) # 1.0 to good accuracy
I propose that rescaled_bivariate_kde replace the existing kde constructor for bivariate data.
- Dominant language
- Julia
- Stars
- 213
- Forks
- 45
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from JuliaStats/KernelDensity.jl
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
JuliaStats/KernelDensity.jl#139 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 50/100
JuliaStats/KernelDensity.jl#129 ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
JuliaStats/KernelDensity.jl#128 ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
JuliaStats/KernelDensity.jl#126 · 7 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
JuliaStats/KernelDensity.jl#125 · 5 comments ·
All issues in JuliaStats/KernelDensity.jl
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
JuliaParallel/Hwloc.jl#93 ·
-
Affine transformations of `JohnsonSU` could return a `JohnsonSU`Possibly taken @devmotion claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
JuliaStats/Distributions.jl#2117 ·
Maintainers usually reply within 2 days
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
TomerAberbach/profiler-md#970 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
grame-cncm/faust#1409 ·
Maintainers usually reply within 1 day