Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/JuliaRobotics/KernelDensityEstimate.jl
Kernel Density Estimate with product approximation using multiscale Gibbs sampling
https://github.com/JuliaRobotics/KernelDensityEstimate.jl
bayesian gadfly inference julialang multiscale-gibbs-sampling probabilistic-programming
Last synced: 3 months ago
JSON representation
Kernel Density Estimate with product approximation using multiscale Gibbs sampling
- Host: GitHub
- URL: https://github.com/JuliaRobotics/KernelDensityEstimate.jl
- Owner: JuliaRobotics
- License: lgpl-2.1
- Created: 2016-04-07T13:00:34.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-01-22T11:40:59.000Z (10 months ago)
- Last Synced: 2024-07-21T07:04:46.110Z (4 months ago)
- Topics: bayesian, gadfly, inference, julialang, multiscale-gibbs-sampling, probabilistic-programming
- Language: Julia
- Size: 640 KB
- Stars: 23
- Watchers: 4
- Forks: 7
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-sciml - JuliaRobotics/KernelDensityEstimate.jl: Kernel Density Estimate with product approximation using multiscale Gibbs sampling
README
# KernelDensityEstimate.jl
[![CI](https://github.com/JuliaRobotics/KernelDensityEstimate.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/JuliaRobotics/KernelDensityEstimate.jl/actions/workflows/ci.yml)
[![codecov.io](https://codecov.io/github/JuliaRobotics/KernelDensityEstimate.jl/coverage.svg?branch=master)](https://codecov.io/github/JuliaRobotics/KernelDensityEstimate.jl?branch=master)Kernel Density Estimation **with product approximation using multiscale Gibbs sampling**.
All code is implemented in native Julia, including plotting. The main focus of this module is the ability to take the product between multiple KDEs, and makes this module unique from other KDE implementations. This package also supports n-dimensional KDEs. Please see examples below for details. The implementation is already fairly optimized from a symbolic standpoint and is based on work by:
Sudderth, Erik B.; Ihler, Alexander, et al. "Nonparametric belief propagation." Communications of the ACM 53.10 (2010): 95-103.
## Installation
In Julia 1.0 and above:
```julia
] add KernelDensityEstimate
```## Plotting Functions
The plotting functions for this library have been separated into [KernelDensityEstimatePlotting.jl](http://www.github.com/JuliaRobotics/KernelDensityEstimatePlotting.jl). Plotting functionality uses [Gadfly](https://github.com/GiovineItalia/Gadfly.jl). Comments welcome.
# Examples
Bring the module into the workspace
```julia
using KernelDensityEstimate
# Basic one dimensional examples
# using leave-one-out likelihood cross validation for bandwidth estimation
p100 = kde!([randn(50);10.0.+2*randn(50)])
p2 = kde!([0.0;10.0],[1.0]) # multibandwidth still to be added
p75 = resample(p2,75)# bring in the plotting functions
using KernelDensityEstimatePlotting
plot([p100;p2;p75],c=["red";"green";"blue"]) # using Gadfly under the hood
```
![alt tag](https://raw.githubusercontent.com/JuliaRobotics/KernelDensityEstimate.jl/master/test/FirstExamplePlot.png)Multidimensional example
```julia
pd2 = kde!(randn(3,100));
@time pd2 = kde!(randn(3,100)); # defaults to loocv
pm12 = marginal(pd2,[1;2]);
pm2 = marginal(pm12,[2]);
plot(pm2);
```
Multiscale Gibbs product approximation example
```julia
p = kde!(randn(2,100))
q = kde!(2.0.+randn(2,100))
dummy = kde!(rand(2,100),[1.0]);
mcmciters = 5
pGM, = prodAppxMSGibbsS(dummy, [p;q], nothing, nothing, Niter=mcmciters)
pq = kde!(pGM)
pq1 = marginal(pq,[1])
Pl1 = plot([marginal(p,[1]);marginal(q,[1]);marginal(pq,[1])],c=["red";"green";"black"])
```
Direct histogram of points from the product
```julia
using Gadfly
Pl2 = Gadfly.plot(x=pGM[1,:],y=pGM[2,:],Geom.histogram2d);
draw(PDF("product.pdf",15cm,8cm),hstack(Pl1,Pl2))
```
![alt tag](https://raw.githubusercontent.com/JuliaRobotics/KernelDensityEstimate.jl/master/test/product.png)KDE product between non-gaussian distributions
```julia
using Distributions
p = kde!(rand(Beta(1.0,0.45),300));
q = kde!(rand(Rayleigh(0.5),100).-0.5);
dummy = kde!(rand(1,100),[1.0]);
pGM, = prodAppxMSGibbsS(dummy, [p;q], nothing, nothing, Niter=5)
pq = kde!(pGM)
plot([p;q;pq],c=["red";"green";"black"])
```
![alt tag](https://raw.githubusercontent.com/JuliaRobotics/KernelDensityEstimate.jl/master/test/RayleighBetaProduct.png)Draw multidimensional distributions as marginalized 2D contour plots
```julia
axis=[[-5.0;5]';[-2.0;2.0]';[-10.0;10]';[-5.0;5]']
draw(PDF("test.pdf",30cm,20cm),
plot( kde!(randn(4,200)) ) )N=200;
pts = [2*randn(1,N).+3;
[2*randn(1,round(Int,N/2))'.+3.0;2*randn(1,round(Int,N/2))'.-3.0]';
2*randn(2,N).+3];
p, q = kde!(randn(4,100)), kde!(pts);
draw(PNG("MultidimPlot.png",15cm,10cm),
plot( [p*q;p;q],c=["red";"black";"blue"], axis=axis, dims=2:4,dimLbls=["w";"x";"y";"z"], levels=4) )
```
![alt tag](https://raw.githubusercontent.com/JuliaRobotics/KernelDensityEstimate.jl/master/test/MultidimPlot.png)
```julia
# or draw product natively
draw(PNG("MultidimPlotProd.png",10cm,7cm),
plot( p*q, axis=axis, dims=[2;4],dimLbls=["w";"x";"y";"z"]) )
```
![alt tag](https://raw.githubusercontent.com/JuliaRobotics/KernelDensityEstimate.jl/master/test/MultidimPlotProd.png)# Contributors
The original C++ kde package was written by Alex Ihler and Mike Mandel in 2003, and has be rewritten in Julia and continuously modified by Dehann Fourie since.
Thank you to contributors and users alike, comments and improvements welcome according to JuliaLang and JuliaRobotics standards.