|
| 1 | +```@meta |
| 2 | +CurrentModule = KernelDensity |
| 3 | +``` |
| 4 | + |
| 5 | +# KernelDensity.jl |
| 6 | + |
| 7 | +Kernel density estimators for Julia. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +### Univariate |
| 12 | +The main accessor function is `kde`: |
| 13 | + |
| 14 | +```julia |
| 15 | +U = kde(data) |
| 16 | +``` |
| 17 | + |
| 18 | +will construct a `UnivariateKDE` object from the real vector `data`. The |
| 19 | +optional keyword arguments are |
| 20 | +* `boundary`: the lower and upper limits of the kde as a tuple. Due to the |
| 21 | + fourier transforms used internally, there should be sufficient spacing to |
| 22 | + prevent wrap-around at the boundaries. |
| 23 | +* `npoints`: the number of interpolation points to use. The function uses |
| 24 | + fast Fourier transforms (FFTs) internally, so for optimal efficiency this |
| 25 | + should be a power of 2 (default = 2048). |
| 26 | +* `kernel`: the distributional family from |
| 27 | + [Distributions.jl](https://github.com/JuliaStats/Distributions.jl) to use as |
| 28 | + the kernel (default = `Normal`). To add your own kernel, extend the internal |
| 29 | + `kernel_dist` function. |
| 30 | +* `bandwidth`: the bandwidth of the kernel. Default is to use Silverman's |
| 31 | + rule. |
| 32 | + |
| 33 | +The `UnivariateKDE` object `U` contains gridded coordinates (`U.x`) and the density |
| 34 | +estimate (`U.density`). These are typically sufficient for plotting. |
| 35 | +A related function |
| 36 | + |
| 37 | +``` kde_lscv(data) ``` |
| 38 | + |
| 39 | +will construct a `UnivariateKDE` object, with the bandwidth selected by |
| 40 | +least-squares cross validation. It accepts the above keyword arguments, except |
| 41 | +`bandwidth`. |
| 42 | + |
| 43 | + |
| 44 | +There are also some slightly more advanced interfaces: |
| 45 | +```julia |
| 46 | +kde(data, midpoints::R) where R<:AbstractRange |
| 47 | +``` |
| 48 | +allows specifying the internal grid to use. Optional keyword arguments are |
| 49 | +`kernel` and `bandwidth`. |
| 50 | + |
| 51 | +```julia |
| 52 | +kde(data, dist::Distribution) |
| 53 | +``` |
| 54 | +allows specifying the exact distribution to use as the kernel. Optional |
| 55 | +keyword arguments are `boundary` and `npoints`. |
| 56 | + |
| 57 | +```julia |
| 58 | +kde(data, midpoints::R, dist::Distribution) where R<:AbstractRange |
| 59 | +``` |
| 60 | +allows specifying both the distribution and grid. |
| 61 | + |
| 62 | +### Bivariate |
| 63 | + |
| 64 | +The usage mirrors that of the univariate case, except that `data` is now |
| 65 | +either a tuple of vectors |
| 66 | +```julia |
| 67 | +B = kde((xdata, ydata)) |
| 68 | +``` |
| 69 | +or a matrix with two columns |
| 70 | +```julia |
| 71 | +B = kde(datamatrix) |
| 72 | +``` |
| 73 | +Similarly, the optional arguments all now take tuple arguments: |
| 74 | +e.g. `boundary` now takes a tuple of tuples `((xlo,xhi),(ylo,yhi))`. |
| 75 | + |
| 76 | +The `BivariateKDE` object `B` contains gridded coordinates (`B.x` and `B.y`) and the bivariate density |
| 77 | +estimate (`B.density`). |
| 78 | + |
| 79 | +### Interpolation |
| 80 | + |
| 81 | +The KDE objects are stored as gridded density values, with attached |
| 82 | +coordinates. These are typically sufficient for plotting (see above), but |
| 83 | +intermediate values can be interpolated using the |
| 84 | +[Interpolations.jl](https://github.com/tlycken/Interpolations.jl) package via the `pdf` method |
| 85 | +(extended from Distributions.jl). |
| 86 | + |
| 87 | +```julia |
| 88 | +pdf(k::UnivariateKDE, x) |
| 89 | +pdf(k::BivariateKDE, x, y) |
| 90 | +``` |
| 91 | + |
| 92 | +where `x` and `y` are real numbers or arrays. |
| 93 | + |
| 94 | +If you are making multiple calls to `pdf`, it will be more efficient to |
| 95 | +construct an intermediate `InterpKDE` to store the interpolation structure: |
| 96 | + |
| 97 | +```julia |
| 98 | +ik = InterpKDE(k) |
| 99 | +pdf(ik, x) |
| 100 | +``` |
| 101 | + |
| 102 | +`InterpKDE` will pass any extra arguments to `interpolate`. |
| 103 | + |
| 104 | +## API Reference |
| 105 | + |
| 106 | +```@autodocs |
| 107 | +Modules = [KernelDensity] |
| 108 | +``` |
0 commit comments