Re-scale data before performing bivariate KDE

Open
#96 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
julia
Domain
data

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

image

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)))

image

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaStats/KernelDensity.jl

All issues in JuliaStats/KernelDensity.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.