seatonullberg / kernel-density-estimation

Kernel density estimation in Rust.
https://crates.io/crates/kernel-density-estimation
MIT License
26 stars 6 forks source link

Add support for multivariate KDE #2

Open seatonullberg opened 1 year ago

seatonullberg commented 1 year ago

Currently only univariate distributions are supported. A complete implementation would include seamless support for multivariate distributions. The only type that should be changed is the KernelDensityEstimator struct. Currently, the data structure is as follows:

pub struct KernelDensityEstimator<B, K> {
    observations: Vec<Float>,
    bandwidth: B,
    kernel: K,
}

The observations field will need to be converted to a nalgebra::DMatrix to support a multivariate distribution. The type should be hidden behind an alias so that end-users do not need to add nalgebra as a dependency in their own projects.

pub type Matrix2D = nalgebra::DMatrix<Float>;

pub struct KernelDensityEstimator<B, K> {
    observations: Matrix2D,
    bandwidth: B,
    kernel: K,
}

To prevent needless conversions for users working with univariate data, the data structure could instead add a generic parameter T representing the type of observations. For univariate data T could be concretely represented as Vec<Float> and for multivariate data T could be concretely represented as Matrix2D. However, this would require the introduction of two new traits UnivariateKDE and MultivariateKDE to mimic overloading of the method names pdf, cdf, and sample.

pub struct KernelDensityEstimator<T, B, K> {
    observations: T,
    bandwidth: B,
    kernel: K,
}

pub trait UnivariateKDE {
    // Unimplemented.
}

pub trait MultivariateKDE {
    // Unimplemented.
}

// Univariate case.
impl<B, K> UnivariateKDE for KernelDensityEstimator<Vec<Float>, B, K>
where
    B: Bandwidth,
    K: Kernel,
{
    // Unimplemented.
}

// Multivariate case.
impl<B, K> MultivariateKDE for KernelDensityEstimator<Matrix2D, B, K>
where
    B: Bandwidth,
    K: Kernel,
{
    // Unimplemented.
}

Lastly, the traits UnivariateKDE and MultivariateKDE should be sealed to prevent end-user implementations.

humphreylee commented 9 months ago

Good piece of work. Would you mind if I ask if there is any progress on this? Thanks.

seatonullberg commented 9 months ago

Hi @humphreylee, I'm currently preoccupied with writing my dissertation, but I do intend to get back to this as soon as I am able.

emyr666 commented 2 months ago

bump! am using seaborn in python to do 2d kde contour plots but its very slow. am hoping that by using rust I can use the webasm stuff to do this fast on the client side instead of generating plots (exteremly slowly) on the server to send to a web client.